blob: c0ab3d9c8cd5da1d82ce2d07fa422934b3b53e82 [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);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000517 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000521 }
522
Eli Friedman43f51ae2011-08-26 21:21:21 +0000523 if (Subtarget->hasCmpxchg16b()) {
524 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
525 }
526
Evan Cheng3c992d22006-03-07 02:02:57 +0000527 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000528 if (!Subtarget->isTargetDarwin() &&
529 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000530 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000532 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
535 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
536 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
537 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000538 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000539 setExceptionPointerRegister(X86::RAX);
540 setExceptionSelectorRegister(X86::RDX);
541 } else {
542 setExceptionPointerRegister(X86::EAX);
543 setExceptionSelectorRegister(X86::EDX);
544 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
546 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000547
Duncan Sands4a544a72011-09-06 13:37:06 +0000548 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
549 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000550
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000552
Nate Begemanacc398c2006-01-25 18:21:52 +0000553 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::VASTART , MVT::Other, Custom);
555 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000556 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::VAARG , MVT::Other, Custom);
558 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000559 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::VAARG , MVT::Other, Expand);
561 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000562 }
Evan Chengae642192007-03-02 23:16:35 +0000563
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
565 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000566
567 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
568 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
569 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000570 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000571 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
572 MVT::i64 : MVT::i32, Custom);
573 else
574 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
575 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000576
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000577 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000579 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000580 addRegisterClass(MVT::f32, &X86::FR32RegClass);
581 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Evan Cheng223547a2006-01-31 22:28:30 +0000583 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FABS , MVT::f64, Custom);
585 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000586
587 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FNEG , MVT::f64, Custom);
589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000590
Evan Cheng68c47cb2007-01-05 07:55:56 +0000591 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
593 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000594
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000595 // Lower this to FGETSIGNx86 plus an AND.
596 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
597 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
598
Evan Chengd25e9e82006-02-02 00:28:23 +0000599 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FSIN , MVT::f64, Expand);
601 setOperationAction(ISD::FCOS , MVT::f64, Expand);
602 setOperationAction(ISD::FSIN , MVT::f32, Expand);
603 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000604
Chris Lattnera54aa942006-01-29 06:26:08 +0000605 // Expand FP immediates into loads from the stack, except for the special
606 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000607 addLegalFPImmediate(APFloat(+0.0)); // xorpd
608 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000609 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 // Use SSE for f32, x87 for f64.
611 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000612 addRegisterClass(MVT::f32, &X86::FR32RegClass);
613 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614
615 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617
618 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
625 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
627 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::FSIN , MVT::f32, Expand);
629 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
Nate Begemane1795842008-02-14 08:57:00 +0000631 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632 addLegalFPImmediate(APFloat(+0.0f)); // xorps
633 addLegalFPImmediate(APFloat(+0.0)); // FLD0
634 addLegalFPImmediate(APFloat(+1.0)); // FLD1
635 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
636 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
637
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000638 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
640 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000642 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000644 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000645 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
646 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
649 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
650 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
651 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000652
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000653 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000654 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000656 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000658 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000659 addLegalFPImmediate(APFloat(+0.0)); // FLD0
660 addLegalFPImmediate(APFloat(+1.0)); // FLD1
661 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
662 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000663 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
664 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
665 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
666 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000667 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000668
Cameron Zwarich33390842011-07-08 21:39:21 +0000669 // We don't support FMA.
670 setOperationAction(ISD::FMA, MVT::f64, Expand);
671 setOperationAction(ISD::FMA, MVT::f32, Expand);
672
Dale Johannesen59a58732007-08-05 18:49:15 +0000673 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000674 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000675 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
677 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000678 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000679 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000680 addLegalFPImmediate(TmpFlt); // FLD0
681 TmpFlt.changeSign();
682 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000683
684 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000685 APFloat TmpFlt2(+1.0);
686 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
687 &ignored);
688 addLegalFPImmediate(TmpFlt2); // FLD1
689 TmpFlt2.changeSign();
690 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
691 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000692
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000693 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
695 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000696 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000697
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000698 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
699 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
700 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
701 setOperationAction(ISD::FRINT, MVT::f80, Expand);
702 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000703 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000704 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000705
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000706 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
708 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
709 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000710
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::FLOG, MVT::f80, Expand);
712 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
713 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
714 setOperationAction(ISD::FEXP, MVT::f80, Expand);
715 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000716
Mon P Wangf007a8b2008-11-06 05:31:54 +0000717 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000718 // (for widening) or expand (for scalarization). Then we will selectively
719 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000720 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
721 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000738 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
739 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000744 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000748 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000756 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000758 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000765 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
769 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
774 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000775 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000776 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
777 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
778 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
779 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000780 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000781 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
782 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000783 setTruncStoreAction((MVT::SimpleValueType)VT,
784 (MVT::SimpleValueType)InnerVT, Expand);
785 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
786 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
787 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000788 }
789
Evan Chengc7ce29b2009-02-13 22:36:38 +0000790 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
791 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000792 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000793 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000794 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795 }
796
Dale Johannesen0488fb62010-09-30 23:57:10 +0000797 // MMX-sized vectors (other than x86mmx) are expected to be expanded
798 // into smaller operations.
799 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
800 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
801 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
802 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
803 setOperationAction(ISD::AND, MVT::v8i8, Expand);
804 setOperationAction(ISD::AND, MVT::v4i16, Expand);
805 setOperationAction(ISD::AND, MVT::v2i32, Expand);
806 setOperationAction(ISD::AND, MVT::v1i64, Expand);
807 setOperationAction(ISD::OR, MVT::v8i8, Expand);
808 setOperationAction(ISD::OR, MVT::v4i16, Expand);
809 setOperationAction(ISD::OR, MVT::v2i32, Expand);
810 setOperationAction(ISD::OR, MVT::v1i64, Expand);
811 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
812 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
813 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
814 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
815 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
816 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
817 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
818 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
819 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
820 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
821 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
822 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
823 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000824 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
825 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
826 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
827 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000828
Craig Topper1accb7e2012-01-10 06:54:16 +0000829 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000830 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000831
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
833 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
834 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
835 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
836 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
837 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000838 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
840 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
841 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
842 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
843 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000844 }
845
Craig Topper1accb7e2012-01-10 06:54:16 +0000846 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000847 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000848
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000849 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
850 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000851 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
852 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
853 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
854 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000855
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
857 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
858 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
859 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
860 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
861 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
862 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
863 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
864 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
865 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
866 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
867 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
868 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
869 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
870 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
871 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000872 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000873
Nadav Rotem354efd82011-09-18 14:57:03 +0000874 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000875 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
876 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
877 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000878
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
880 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
881 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
882 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
883 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000884
Evan Cheng2c3ae372006-04-12 21:21:57 +0000885 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000886 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000887 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000888 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000890 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000891 // Do not attempt to custom lower non-128-bit vectors
892 if (!VT.is128BitVector())
893 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000894 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
895 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
896 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000897 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
900 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
901 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
902 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
903 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
904 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000905
Nate Begemancdd1eec2008-02-12 22:51:28 +0000906 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
908 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000909 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000910
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000911 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000912 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000913 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000914
915 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000916 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000917 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000918
Craig Topper0d1f1762012-08-12 00:34:56 +0000919 setOperationAction(ISD::AND, VT, Promote);
920 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
921 setOperationAction(ISD::OR, VT, Promote);
922 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
923 setOperationAction(ISD::XOR, VT, Promote);
924 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
925 setOperationAction(ISD::LOAD, VT, Promote);
926 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
927 setOperationAction(ISD::SELECT, VT, Promote);
928 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000929 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000930
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000932
Evan Cheng2c3ae372006-04-12 21:21:57 +0000933 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
935 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
936 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
937 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000938
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
940 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000941
942 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000943 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000944
Craig Topperd0a31172012-01-10 06:37:29 +0000945 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000946 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
947 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
948 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
949 setOperationAction(ISD::FRINT, MVT::f32, Legal);
950 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
951 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
952 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
953 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
954 setOperationAction(ISD::FRINT, MVT::f64, Legal);
955 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
956
Craig Topper12fb5c62012-09-08 17:42:27 +0000957 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
958 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
959
Nate Begeman14d12ca2008-02-11 04:19:36 +0000960 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000962
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000963 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
964 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
965 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
966 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
967 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000968
Nate Begeman14d12ca2008-02-11 04:19:36 +0000969 // i8 and i16 vectors are custom , because the source register and source
970 // source memory operand types are not the same width. f32 vectors are
971 // custom since the immediate controlling the insert encodes additional
972 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
974 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
975 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
976 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000977
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
979 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
980 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
981 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000982
Pete Coopera77214a2011-11-14 19:38:42 +0000983 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000984 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000985 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000986 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
987 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000988 }
989 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000990
Craig Topper1accb7e2012-01-10 06:54:16 +0000991 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000992 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000993 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000994
Nadav Rotem43012222011-05-11 08:12:09 +0000995 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000996 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000997
Nadav Rotem43012222011-05-11 08:12:09 +0000998 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000999 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001000
1001 if (Subtarget->hasAVX2()) {
1002 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1003 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1004
1005 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1006 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1007
1008 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1009 } else {
1010 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1011 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1012
1013 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1014 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1015
1016 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1017 }
Nadav Rotem43012222011-05-11 08:12:09 +00001018 }
1019
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001020 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001021 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1022 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1023 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1024 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1025 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1026 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001027
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001029 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1030 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001031
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1033 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1034 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1035 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1036 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001037 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001039 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001040
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1042 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1043 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1044 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1045 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001046 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001047 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001048 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001049
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001050 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1051 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001052 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001053
Michael Liaob8150d82012-09-10 18:33:51 +00001054 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1055
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001056 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1057 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1058
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001059 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1060 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1061
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001062 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001063 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001064
Duncan Sands28b77e92011-09-06 19:07:46 +00001065 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1066 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1067 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1068 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001069
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001070 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1071 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1072 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1073
Craig Topperaaa643c2011-11-09 07:28:55 +00001074 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1075 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1076 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1077 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001078
Craig Topperbf404372012-08-31 15:40:30 +00001079 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001080 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1081 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1082 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1083 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1084 setOperationAction(ISD::FMA, MVT::f32, Custom);
1085 setOperationAction(ISD::FMA, MVT::f64, Custom);
1086 }
Craig Topper880ef452012-08-11 22:34:26 +00001087
Craig Topperaaa643c2011-11-09 07:28:55 +00001088 if (Subtarget->hasAVX2()) {
1089 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1090 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1091 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1092 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001093
Craig Topperaaa643c2011-11-09 07:28:55 +00001094 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1095 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1096 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1097 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001098
Craig Topperaaa643c2011-11-09 07:28:55 +00001099 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1100 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1101 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001102 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001103
1104 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001105
1106 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1107 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1108
1109 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1110 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1111
1112 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001113 } else {
1114 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1115 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1116 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1117 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1118
1119 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1120 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1121 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1122 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1123
1124 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1125 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1126 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1127 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001128
1129 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1130 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1131
1132 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1133 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1134
1135 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001136 }
Craig Topper13894fa2011-08-24 06:14:18 +00001137
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001138 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001139 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1140 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001141 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001142
1143 // Extract subvector is special because the value type
1144 // (result) is 128-bit but the source is 256-bit wide.
1145 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001146 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001147
1148 // Do not attempt to custom lower other non-256-bit vectors
1149 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001150 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001151
Craig Topper0d1f1762012-08-12 00:34:56 +00001152 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1153 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1154 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1155 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1156 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1157 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1158 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001159 }
1160
David Greene54d8eba2011-01-27 22:38:56 +00001161 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001162 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001163 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001164
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001165 // Do not attempt to promote non-256-bit vectors
1166 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001167 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001168
Craig Topper0d1f1762012-08-12 00:34:56 +00001169 setOperationAction(ISD::AND, VT, Promote);
1170 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1171 setOperationAction(ISD::OR, VT, Promote);
1172 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1173 setOperationAction(ISD::XOR, VT, Promote);
1174 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1175 setOperationAction(ISD::LOAD, VT, Promote);
1176 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1177 setOperationAction(ISD::SELECT, VT, Promote);
1178 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001179 }
David Greene9b9838d2009-06-29 16:47:10 +00001180 }
1181
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001182 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1183 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001184 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1185 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001186 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1187 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001188 }
1189
Evan Cheng6be2c582006-04-05 23:38:46 +00001190 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001191 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001192 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001193
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001194
Eli Friedman962f5492010-06-02 19:35:46 +00001195 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1196 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001197 //
Eli Friedman962f5492010-06-02 19:35:46 +00001198 // FIXME: We really should do custom legalization for addition and
1199 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1200 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001201 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1202 // Add/Sub/Mul with overflow operations are custom lowered.
1203 MVT VT = IntVTs[i];
1204 setOperationAction(ISD::SADDO, VT, Custom);
1205 setOperationAction(ISD::UADDO, VT, Custom);
1206 setOperationAction(ISD::SSUBO, VT, Custom);
1207 setOperationAction(ISD::USUBO, VT, Custom);
1208 setOperationAction(ISD::SMULO, VT, Custom);
1209 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001210 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001211
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001212 // There are no 8-bit 3-address imul/mul instructions
1213 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1214 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001215
Evan Chengd54f2d52009-03-31 19:38:51 +00001216 if (!Subtarget->is64Bit()) {
1217 // These libcalls are not available in 32-bit.
1218 setLibcallName(RTLIB::SHL_I128, 0);
1219 setLibcallName(RTLIB::SRL_I128, 0);
1220 setLibcallName(RTLIB::SRA_I128, 0);
1221 }
1222
Evan Cheng206ee9d2006-07-07 08:33:52 +00001223 // We have target-specific dag combine patterns for the following nodes:
1224 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001225 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001226 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001227 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001228 setTargetDAGCombine(ISD::SHL);
1229 setTargetDAGCombine(ISD::SRA);
1230 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001231 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001232 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001233 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001234 setTargetDAGCombine(ISD::FADD);
1235 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001236 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001237 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001238 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001239 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001240 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001241 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001242 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001243 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001244 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001245 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001246 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001247 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001248 if (Subtarget->is64Bit())
1249 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001250 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001251
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001252 computeRegisterProperties();
1253
Evan Cheng05219282011-01-06 06:52:41 +00001254 // On Darwin, -Os means optimize for size without hurting performance,
1255 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001256 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001257 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001258 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001259 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1260 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1261 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001262 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001263 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001264
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001265 // Predictable cmov don't hurt on atom because it's in-order.
1266 predictableSelectIsExpensive = !Subtarget->isAtom();
1267
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001268 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001269}
1270
Scott Michel5b8f82e2008-03-10 15:42:14 +00001271
Duncan Sands28b77e92011-09-06 19:07:46 +00001272EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1273 if (!VT.isVector()) return MVT::i8;
1274 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001275}
1276
1277
Evan Cheng29286502008-01-23 23:17:41 +00001278/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1279/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001280static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001281 if (MaxAlign == 16)
1282 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001283 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001284 if (VTy->getBitWidth() == 128)
1285 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001286 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001287 unsigned EltAlign = 0;
1288 getMaxByValAlign(ATy->getElementType(), EltAlign);
1289 if (EltAlign > MaxAlign)
1290 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001291 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001292 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1293 unsigned EltAlign = 0;
1294 getMaxByValAlign(STy->getElementType(i), EltAlign);
1295 if (EltAlign > MaxAlign)
1296 MaxAlign = EltAlign;
1297 if (MaxAlign == 16)
1298 break;
1299 }
1300 }
Evan Cheng29286502008-01-23 23:17:41 +00001301}
1302
1303/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1304/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001305/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1306/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001307unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001308 if (Subtarget->is64Bit()) {
1309 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001310 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001311 if (TyAlign > 8)
1312 return TyAlign;
1313 return 8;
1314 }
1315
Evan Cheng29286502008-01-23 23:17:41 +00001316 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001317 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001318 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001319 return Align;
1320}
Chris Lattner2b02a442007-02-25 08:29:00 +00001321
Evan Chengf0df0312008-05-15 08:39:06 +00001322/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001323/// and store operations as a result of memset, memcpy, and memmove
1324/// lowering. If DstAlign is zero that means it's safe to destination
1325/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1326/// means there isn't a need to check it against alignment requirement,
1327/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001328/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001329/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1330/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1331/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001332/// It returns EVT::Other if the type should be determined using generic
1333/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001334EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001335X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1336 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001337 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001338 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001339 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001340 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1341 // linux. This is because the stack realignment code can't handle certain
1342 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001343 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001344 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001345 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001346 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001347 (Subtarget->isUnalignedMemAccessFast() ||
1348 ((DstAlign == 0 || DstAlign >= 16) &&
1349 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001350 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001351 if (Subtarget->getStackAlignment() >= 32) {
1352 if (Subtarget->hasAVX2())
1353 return MVT::v8i32;
1354 if (Subtarget->hasAVX())
1355 return MVT::v8f32;
1356 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001357 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001358 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001359 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001360 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001361 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001362 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001363 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001364 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001365 // Do not use f64 to lower memcpy if source is string constant. It's
1366 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001367 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001368 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001369 }
Evan Chengf0df0312008-05-15 08:39:06 +00001370 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001371 return MVT::i64;
1372 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001373}
1374
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001375/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1376/// current function. The returned value is a member of the
1377/// MachineJumpTableInfo::JTEntryKind enum.
1378unsigned X86TargetLowering::getJumpTableEncoding() const {
1379 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1380 // symbol.
1381 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1382 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001383 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001384
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001385 // Otherwise, use the normal jump table encoding heuristics.
1386 return TargetLowering::getJumpTableEncoding();
1387}
1388
Chris Lattnerc64daab2010-01-26 05:02:42 +00001389const MCExpr *
1390X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1391 const MachineBasicBlock *MBB,
1392 unsigned uid,MCContext &Ctx) const{
1393 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1394 Subtarget->isPICStyleGOT());
1395 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1396 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001397 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1398 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001399}
1400
Evan Chengcc415862007-11-09 01:32:10 +00001401/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1402/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001403SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001404 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001405 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001406 // This doesn't have DebugLoc associated with it, but is not really the
1407 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001408 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001409 return Table;
1410}
1411
Chris Lattner589c6f62010-01-26 06:28:43 +00001412/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1413/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1414/// MCExpr.
1415const MCExpr *X86TargetLowering::
1416getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1417 MCContext &Ctx) const {
1418 // X86-64 uses RIP relative addressing based on the jump table label.
1419 if (Subtarget->isPICStyleRIPRel())
1420 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1421
1422 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001423 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001424}
1425
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001426// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001427std::pair<const TargetRegisterClass*, uint8_t>
1428X86TargetLowering::findRepresentativeClass(EVT VT) const{
1429 const TargetRegisterClass *RRC = 0;
1430 uint8_t Cost = 1;
1431 switch (VT.getSimpleVT().SimpleTy) {
1432 default:
1433 return TargetLowering::findRepresentativeClass(VT);
1434 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001435 RRC = Subtarget->is64Bit() ?
1436 (const TargetRegisterClass*)&X86::GR64RegClass :
1437 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001438 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001439 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001440 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001441 break;
1442 case MVT::f32: case MVT::f64:
1443 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1444 case MVT::v4f32: case MVT::v2f64:
1445 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1446 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001447 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001448 break;
1449 }
1450 return std::make_pair(RRC, Cost);
1451}
1452
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001453bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1454 unsigned &Offset) const {
1455 if (!Subtarget->isTargetLinux())
1456 return false;
1457
1458 if (Subtarget->is64Bit()) {
1459 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1460 Offset = 0x28;
1461 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1462 AddressSpace = 256;
1463 else
1464 AddressSpace = 257;
1465 } else {
1466 // %gs:0x14 on i386
1467 Offset = 0x14;
1468 AddressSpace = 256;
1469 }
1470 return true;
1471}
1472
1473
Chris Lattner2b02a442007-02-25 08:29:00 +00001474//===----------------------------------------------------------------------===//
1475// Return Value Calling Convention Implementation
1476//===----------------------------------------------------------------------===//
1477
Chris Lattner59ed56b2007-02-28 04:55:35 +00001478#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001479
Michael J. Spencerec38de22010-10-10 22:04:20 +00001480bool
Eric Christopher471e4222011-06-08 23:55:35 +00001481X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001482 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001483 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001484 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001485 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001486 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001487 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001488 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001489}
1490
Dan Gohman98ca4f22009-08-05 01:29:28 +00001491SDValue
1492X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001493 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001494 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001495 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001496 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001497 MachineFunction &MF = DAG.getMachineFunction();
1498 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001499
Chris Lattner9774c912007-02-27 05:28:59 +00001500 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001501 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502 RVLocs, *DAG.getContext());
1503 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001504
Evan Chengdcea1632010-02-04 02:40:39 +00001505 // Add the regs to the liveout set for the function.
1506 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1507 for (unsigned i = 0; i != RVLocs.size(); ++i)
1508 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1509 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001510
Dan Gohman475871a2008-07-27 21:46:04 +00001511 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001512
Dan Gohman475871a2008-07-27 21:46:04 +00001513 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001514 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1515 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001516 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1517 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001518
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001519 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001520 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1521 CCValAssign &VA = RVLocs[i];
1522 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001523 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001524 EVT ValVT = ValToCopy.getValueType();
1525
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001526 // Promote values to the appropriate types
1527 if (VA.getLocInfo() == CCValAssign::SExt)
1528 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1529 else if (VA.getLocInfo() == CCValAssign::ZExt)
1530 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1531 else if (VA.getLocInfo() == CCValAssign::AExt)
1532 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1533 else if (VA.getLocInfo() == CCValAssign::BCvt)
1534 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1535
Dale Johannesenc4510512010-09-24 19:05:48 +00001536 // If this is x86-64, and we disabled SSE, we can't return FP values,
1537 // or SSE or MMX vectors.
1538 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1539 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001540 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001541 report_fatal_error("SSE register return with SSE disabled");
1542 }
1543 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1544 // llvm-gcc has never done it right and no one has noticed, so this
1545 // should be OK for now.
1546 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001547 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001548 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Chris Lattner447ff682008-03-11 03:23:40 +00001550 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1551 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001552 if (VA.getLocReg() == X86::ST0 ||
1553 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001554 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1555 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001556 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001558 RetOps.push_back(ValToCopy);
1559 // Don't emit a copytoreg.
1560 continue;
1561 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001562
Evan Cheng242b38b2009-02-23 09:03:22 +00001563 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1564 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001565 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001566 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001567 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001569 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1570 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001571 // If we don't have SSE2 available, convert to v4f32 so the generated
1572 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001573 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001574 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001575 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001576 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001577 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001578
Dale Johannesendd64c412009-02-04 00:33:20 +00001579 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001580 Flag = Chain.getValue(1);
1581 }
Dan Gohman61a92132008-04-21 23:59:07 +00001582
1583 // The x86-64 ABI for returning structs by value requires that we copy
1584 // the sret argument into %rax for the return. We saved the argument into
1585 // a virtual register in the entry block, so now we copy the value out
1586 // and into %rax.
1587 if (Subtarget->is64Bit() &&
1588 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1589 MachineFunction &MF = DAG.getMachineFunction();
1590 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1591 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001592 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001593 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001594 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001595
Dale Johannesendd64c412009-02-04 00:33:20 +00001596 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001597 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001598
1599 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001600 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001602
Chris Lattner447ff682008-03-11 03:23:40 +00001603 RetOps[0] = Chain; // Update chain.
1604
1605 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001606 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001607 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001608
1609 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001610 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001611}
1612
Evan Chengbf010eb2012-04-10 01:51:00 +00001613bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001614 if (N->getNumValues() != 1)
1615 return false;
1616 if (!N->hasNUsesOfValue(1, 0))
1617 return false;
1618
Evan Chengbf010eb2012-04-10 01:51:00 +00001619 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001620 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001621 if (Copy->getOpcode() == ISD::CopyToReg) {
1622 // If the copy has a glue operand, we conservatively assume it isn't safe to
1623 // perform a tail call.
1624 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1625 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001626 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001627 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001628 return false;
1629
Evan Cheng1bf891a2010-12-01 22:59:46 +00001630 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001631 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001632 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001633 if (UI->getOpcode() != X86ISD::RET_FLAG)
1634 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001635 HasRet = true;
1636 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001637
Evan Chengbf010eb2012-04-10 01:51:00 +00001638 if (!HasRet)
1639 return false;
1640
1641 Chain = TCChain;
1642 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001643}
1644
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001645EVT
1646X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001647 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001648 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001649 // TODO: Is this also valid on 32-bit?
1650 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001651 ReturnMVT = MVT::i8;
1652 else
1653 ReturnMVT = MVT::i32;
1654
1655 EVT MinVT = getRegisterType(Context, ReturnMVT);
1656 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001657}
1658
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659/// LowerCallResult - Lower the result values of a call into the
1660/// appropriate copies out of appropriate physical registers.
1661///
1662SDValue
1663X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001664 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665 const SmallVectorImpl<ISD::InputArg> &Ins,
1666 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001667 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001668
Chris Lattnere32bbf62007-02-28 07:09:55 +00001669 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001670 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001671 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001672 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001673 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001675
Chris Lattner3085e152007-02-25 08:59:22 +00001676 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001677 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001678 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001679 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001680
Torok Edwin3f142c32009-02-01 18:15:56 +00001681 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001682 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001683 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001684 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001685 }
1686
Evan Cheng79fb3b42009-02-20 20:43:02 +00001687 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001688
1689 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001690 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001691 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001692 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001693 // instead.
1694 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1695 // If we prefer to use the value in xmm registers, copy it out as f80 and
1696 // use a truncate to move it from fp stack reg to xmm reg.
1697 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001698 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001699 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1700 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001701 Val = Chain.getValue(0);
1702
1703 // Round the f80 to the right size, which also moves it to the appropriate
1704 // xmm register.
1705 if (CopyVT != VA.getValVT())
1706 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1707 // This truncation won't change the value.
1708 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001709 } else {
1710 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1711 CopyVT, InFlag).getValue(1);
1712 Val = Chain.getValue(0);
1713 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001714 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001716 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001717
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001719}
1720
1721
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001722//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001723// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001724//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001725// StdCall calling convention seems to be standard for many Windows' API
1726// routines and around. It differs from C calling convention just a little:
1727// callee should clean up the stack, not caller. Symbols should be also
1728// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001729// For info on fast calling convention see Fast Calling Convention (tail call)
1730// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001731
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001733/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001734enum StructReturnType {
1735 NotStructReturn,
1736 RegStructReturn,
1737 StackStructReturn
1738};
1739static StructReturnType
1740callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001742 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001743
Rafael Espindola1cee7102012-07-25 13:41:10 +00001744 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1745 if (!Flags.isSRet())
1746 return NotStructReturn;
1747 if (Flags.isInReg())
1748 return RegStructReturn;
1749 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001750}
1751
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001752/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001753/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001754static StructReturnType
1755argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001757 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001758
Rafael Espindola1cee7102012-07-25 13:41:10 +00001759 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1760 if (!Flags.isSRet())
1761 return NotStructReturn;
1762 if (Flags.isInReg())
1763 return RegStructReturn;
1764 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001765}
1766
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001767/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1768/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001769/// the specific parameter attribute. The copy will be passed as a byval
1770/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001771static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001772CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001773 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1774 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001775 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001776
Dale Johannesendd64c412009-02-04 00:33:20 +00001777 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001778 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001779 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001780}
1781
Chris Lattner29689432010-03-11 00:22:57 +00001782/// IsTailCallConvention - Return true if the calling convention is one that
1783/// supports tail call optimization.
1784static bool IsTailCallConvention(CallingConv::ID CC) {
1785 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1786}
1787
Evan Cheng485fafc2011-03-21 01:19:09 +00001788bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001789 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001790 return false;
1791
1792 CallSite CS(CI);
1793 CallingConv::ID CalleeCC = CS.getCallingConv();
1794 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1795 return false;
1796
1797 return true;
1798}
1799
Evan Cheng0c439eb2010-01-27 00:07:07 +00001800/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1801/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001802static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1803 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001804 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001805}
1806
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807SDValue
1808X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001809 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001810 const SmallVectorImpl<ISD::InputArg> &Ins,
1811 DebugLoc dl, SelectionDAG &DAG,
1812 const CCValAssign &VA,
1813 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001814 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001815 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001816 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001817 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1818 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001819 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001820 EVT ValVT;
1821
1822 // If value is passed by pointer we have address passed instead of the value
1823 // itself.
1824 if (VA.getLocInfo() == CCValAssign::Indirect)
1825 ValVT = VA.getLocVT();
1826 else
1827 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001828
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001829 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001830 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001831 // In case of tail call optimization mark all arguments mutable. Since they
1832 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001833 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001834 unsigned Bytes = Flags.getByValSize();
1835 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1836 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001837 return DAG.getFrameIndex(FI, getPointerTy());
1838 } else {
1839 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001840 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001841 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1842 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001843 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001844 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001845 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001846}
1847
Dan Gohman475871a2008-07-27 21:46:04 +00001848SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001850 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 bool isVarArg,
1852 const SmallVectorImpl<ISD::InputArg> &Ins,
1853 DebugLoc dl,
1854 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001855 SmallVectorImpl<SDValue> &InVals)
1856 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001857 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001858 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001859
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 const Function* Fn = MF.getFunction();
1861 if (Fn->hasExternalLinkage() &&
1862 Subtarget->isTargetCygMing() &&
1863 Fn->getName() == "main")
1864 FuncInfo->setForceFramePointer(true);
1865
Evan Cheng1bc78042006-04-26 01:20:17 +00001866 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001867 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001868 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001869 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001870
Chris Lattner29689432010-03-11 00:22:57 +00001871 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1872 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001873
Chris Lattner638402b2007-02-28 07:00:42 +00001874 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001875 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001876 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001877 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001878
1879 // Allocate shadow area for Win64
1880 if (IsWin64) {
1881 CCInfo.AllocateStack(32, 8);
1882 }
1883
Duncan Sands45907662010-10-31 13:21:44 +00001884 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001885
Chris Lattnerf39f7712007-02-28 05:46:49 +00001886 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001887 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001888 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1889 CCValAssign &VA = ArgLocs[i];
1890 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1891 // places.
1892 assert(VA.getValNo() != LastVal &&
1893 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001894 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001895 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001896
Chris Lattnerf39f7712007-02-28 05:46:49 +00001897 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001898 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001899 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001901 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001903 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001905 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001907 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001908 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001909 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001910 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001911 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001912 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001913 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001914 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001915 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001916
Devang Patel68e6bee2011-02-21 23:21:26 +00001917 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001919
Chris Lattnerf39f7712007-02-28 05:46:49 +00001920 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1921 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1922 // right size.
1923 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001924 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001925 DAG.getValueType(VA.getValVT()));
1926 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001927 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001928 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001929 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001930 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001931
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001932 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001933 // Handle MMX values passed in XMM regs.
1934 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001935 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1936 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001937 } else
1938 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001939 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001940 } else {
1941 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001942 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001943 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001944
1945 // If value is passed via pointer - do a load.
1946 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001947 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001948 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001949
Dan Gohman98ca4f22009-08-05 01:29:28 +00001950 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001951 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001952
Dan Gohman61a92132008-04-21 23:59:07 +00001953 // The x86-64 ABI for returning structs by value requires that we copy
1954 // the sret argument into %rax for the return. Save the argument into
1955 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001956 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001957 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1958 unsigned Reg = FuncInfo->getSRetReturnReg();
1959 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001961 FuncInfo->setSRetReturnReg(Reg);
1962 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001963 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001965 }
1966
Chris Lattnerf39f7712007-02-28 05:46:49 +00001967 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001968 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001969 if (FuncIsMadeTailCallSafe(CallConv,
1970 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001971 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001972
Evan Cheng1bc78042006-04-26 01:20:17 +00001973 // If the function takes variable number of arguments, make a frame index for
1974 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001975 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001976 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1977 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001978 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001979 }
1980 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001981 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1982
1983 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001984 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001985 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001986 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001987 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001988 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1989 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001990 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001991 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1992 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1993 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001994 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001995 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001996
1997 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001998 // The XMM registers which might contain var arg parameters are shadowed
1999 // in their paired GPR. So we only need to save the GPR to their home
2000 // slots.
2001 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002002 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002003 } else {
2004 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2005 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002006
Chad Rosier30450e82011-12-22 22:35:21 +00002007 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2008 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002009 }
2010 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2011 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002012
Devang Patel578efa92009-06-05 21:57:13 +00002013 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002014 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002015 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002016 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2017 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002018 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002019 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002020 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002021 // Kernel mode asks for SSE to be disabled, so don't push them
2022 // on the stack.
2023 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002024
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002025 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002026 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002027 // Get to the caller-allocated home save location. Add 8 to account
2028 // for the return address.
2029 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002030 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002031 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002032 // Fixup to set vararg frame on shadow area (4 x i64).
2033 if (NumIntRegs < 4)
2034 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002035 } else {
2036 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002037 // registers, then we must store them to their spots on the stack so
2038 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002039 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2040 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2041 FuncInfo->setRegSaveFrameIndex(
2042 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002043 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002044 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002045
Gordon Henriksen86737662008-01-05 16:56:59 +00002046 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002047 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002048 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2049 getPointerTy());
2050 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002051 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002052 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2053 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002054 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002055 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002057 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002058 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002059 MachinePointerInfo::getFixedStack(
2060 FuncInfo->getRegSaveFrameIndex(), Offset),
2061 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002062 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002063 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002064 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002065
Dan Gohmanface41a2009-08-16 21:24:25 +00002066 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2067 // Now store the XMM (fp + vector) parameter registers.
2068 SmallVector<SDValue, 11> SaveXMMOps;
2069 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002070
Craig Topperc9099502012-04-20 06:31:50 +00002071 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002072 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2073 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002074
Dan Gohman1e93df62010-04-17 14:41:14 +00002075 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2076 FuncInfo->getRegSaveFrameIndex()));
2077 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2078 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002079
Dan Gohmanface41a2009-08-16 21:24:25 +00002080 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002081 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002082 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002083 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2084 SaveXMMOps.push_back(Val);
2085 }
2086 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2087 MVT::Other,
2088 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002090
2091 if (!MemOps.empty())
2092 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2093 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002094 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002096
Gordon Henriksen86737662008-01-05 16:56:59 +00002097 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002098 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2099 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002100 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002101 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002102 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002103 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002104 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002105 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002106 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002107 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002108
Gordon Henriksen86737662008-01-05 16:56:59 +00002109 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002110 // RegSaveFrameIndex is X86-64 only.
2111 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002112 if (CallConv == CallingConv::X86_FastCall ||
2113 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002114 // fastcc functions can't have varargs.
2115 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002116 }
Evan Cheng25caf632006-05-23 21:06:34 +00002117
Rafael Espindola76927d752011-08-30 19:39:58 +00002118 FuncInfo->setArgumentStackSize(StackSize);
2119
Dan Gohman98ca4f22009-08-05 01:29:28 +00002120 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002121}
2122
Dan Gohman475871a2008-07-27 21:46:04 +00002123SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002124X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2125 SDValue StackPtr, SDValue Arg,
2126 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002127 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002128 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002129 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002130 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002131 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002132 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002133 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002134
2135 return DAG.getStore(Chain, dl, Arg, PtrOff,
2136 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002137 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002138}
2139
Bill Wendling64e87322009-01-16 19:25:27 +00002140/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002141/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002142SDValue
2143X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002144 SDValue &OutRetAddr, SDValue Chain,
2145 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002146 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002147 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002148 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002149 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002150
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002151 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002152 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002153 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002154 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002155}
2156
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002157/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002158/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002159static SDValue
2160EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002161 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002162 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002163 // Store the return address to the appropriate stack slot.
2164 if (!FPDiff) return Chain;
2165 // Calculate the new stack slot for the return address.
2166 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002167 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002168 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002169 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002170 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002171 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002172 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002173 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002174 return Chain;
2175}
2176
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002178X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002179 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002180 SelectionDAG &DAG = CLI.DAG;
2181 DebugLoc &dl = CLI.DL;
2182 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2183 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2184 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2185 SDValue Chain = CLI.Chain;
2186 SDValue Callee = CLI.Callee;
2187 CallingConv::ID CallConv = CLI.CallConv;
2188 bool &isTailCall = CLI.IsTailCall;
2189 bool isVarArg = CLI.IsVarArg;
2190
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191 MachineFunction &MF = DAG.getMachineFunction();
2192 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002193 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002194 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002195 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002196 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197
Nick Lewycky22de16d2012-01-19 00:34:10 +00002198 if (MF.getTarget().Options.DisableTailCalls)
2199 isTailCall = false;
2200
Evan Cheng5f941932010-02-05 02:21:12 +00002201 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002202 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002203 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002204 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002205 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002206 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002207
2208 // Sibcalls are automatically detected tailcalls which do not require
2209 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002210 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002211 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002212
2213 if (isTailCall)
2214 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002215 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002216
Chris Lattner29689432010-03-11 00:22:57 +00002217 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2218 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002219
Chris Lattner638402b2007-02-28 07:00:42 +00002220 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002221 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002222 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002223 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002224
2225 // Allocate shadow area for Win64
2226 if (IsWin64) {
2227 CCInfo.AllocateStack(32, 8);
2228 }
2229
Duncan Sands45907662010-10-31 13:21:44 +00002230 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Chris Lattner423c5f42007-02-28 05:31:48 +00002232 // Get a count of how many bytes are to be pushed on the stack.
2233 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002234 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002235 // This is a sibcall. The memory operands are available in caller's
2236 // own caller's stack.
2237 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002238 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2239 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002240 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002241
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002243 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002244 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002245 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2247 FPDiff = NumBytesCallerPushed - NumBytes;
2248
2249 // Set the delta of movement of the returnaddr stackslot.
2250 // But only set if delta is greater than previous delta.
2251 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2252 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2253 }
2254
Evan Chengf22f9b32010-02-06 03:28:46 +00002255 if (!IsSibcall)
2256 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002257
Dan Gohman475871a2008-07-27 21:46:04 +00002258 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002259 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002260 if (isTailCall && FPDiff)
2261 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2262 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002263
Dan Gohman475871a2008-07-27 21:46:04 +00002264 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2265 SmallVector<SDValue, 8> MemOpChains;
2266 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002267
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002268 // Walk the register/memloc assignments, inserting copies/loads. In the case
2269 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002270 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2271 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002272 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002273 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002274 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002275 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002276
Chris Lattner423c5f42007-02-28 05:31:48 +00002277 // Promote the value if needed.
2278 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002279 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002280 case CCValAssign::Full: break;
2281 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002282 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002283 break;
2284 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002285 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002286 break;
2287 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002288 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002289 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002290 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002291 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2292 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002293 } else
2294 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2295 break;
2296 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002297 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002298 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002299 case CCValAssign::Indirect: {
2300 // Store the argument.
2301 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002302 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002303 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002304 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002305 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002306 Arg = SpillSlot;
2307 break;
2308 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002309 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002310
Chris Lattner423c5f42007-02-28 05:31:48 +00002311 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002312 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2313 if (isVarArg && IsWin64) {
2314 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2315 // shadow reg if callee is a varargs function.
2316 unsigned ShadowReg = 0;
2317 switch (VA.getLocReg()) {
2318 case X86::XMM0: ShadowReg = X86::RCX; break;
2319 case X86::XMM1: ShadowReg = X86::RDX; break;
2320 case X86::XMM2: ShadowReg = X86::R8; break;
2321 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002322 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002323 if (ShadowReg)
2324 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002325 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002326 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002327 assert(VA.isMemLoc());
2328 if (StackPtr.getNode() == 0)
2329 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2330 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2331 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002332 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002333 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002334
Evan Cheng32fe1032006-05-25 00:59:30 +00002335 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002337 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002338
Chris Lattner88e1fd52009-07-09 04:24:46 +00002339 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002340 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2341 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002342 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002343 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2344 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002345 } else {
2346 // If we are tail calling and generating PIC/GOT style code load the
2347 // address of the callee into ECX. The value in ecx is used as target of
2348 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2349 // for tail calls on PIC/GOT architectures. Normally we would just put the
2350 // address of GOT into ebx and then call target@PLT. But for tail calls
2351 // ebx would be restored (since ebx is callee saved) before jumping to the
2352 // target@PLT.
2353
2354 // Note: The actual moving to ECX is done further down.
2355 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2356 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2357 !G->getGlobal()->hasProtectedVisibility())
2358 Callee = LowerGlobalAddress(Callee, DAG);
2359 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002360 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002361 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002362 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002363
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002364 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002365 // From AMD64 ABI document:
2366 // For calls that may call functions that use varargs or stdargs
2367 // (prototype-less calls or calls to functions containing ellipsis (...) in
2368 // the declaration) %al is used as hidden argument to specify the number
2369 // of SSE registers used. The contents of %al do not need to match exactly
2370 // the number of registers, but must be an ubound on the number of SSE
2371 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002372
Gordon Henriksen86737662008-01-05 16:56:59 +00002373 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002374 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002375 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2376 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2377 };
2378 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002379 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002380 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002381
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002382 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2383 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002384 }
2385
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002386 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002387 if (isTailCall) {
2388 // Force all the incoming stack arguments to be loaded from the stack
2389 // before any new outgoing arguments are stored to the stack, because the
2390 // outgoing stack slots may alias the incoming argument stack slots, and
2391 // the alias isn't otherwise explicit. This is slightly more conservative
2392 // than necessary, because it means that each store effectively depends
2393 // on every argument instead of just those arguments it would clobber.
2394 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2395
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SmallVector<SDValue, 8> MemOpChains2;
2397 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002398 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002399 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002400 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2401 CCValAssign &VA = ArgLocs[i];
2402 if (VA.isRegLoc())
2403 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002404 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002405 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002406 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002407 // Create frame index.
2408 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002409 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002410 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002411 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002412
Duncan Sands276dcbd2008-03-21 09:14:45 +00002413 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002414 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002415 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002416 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002417 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002418 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002419 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002420
Dan Gohman98ca4f22009-08-05 01:29:28 +00002421 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2422 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002423 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002424 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002425 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002426 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002428 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002429 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002430 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002431 }
2432 }
2433
2434 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002435 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002436 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002437
2438 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002439 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002440 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002441 }
2442
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002443 // Build a sequence of copy-to-reg nodes chained together with token chain
2444 // and flag operands which copy the outgoing args into registers.
2445 SDValue InFlag;
2446 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2447 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2448 RegsToPass[i].second, InFlag);
2449 InFlag = Chain.getValue(1);
2450 }
2451
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002452 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2453 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2454 // In the 64-bit large code model, we have to make all calls
2455 // through a register, since the call instruction's 32-bit
2456 // pc-relative offset may not be large enough to hold the whole
2457 // address.
2458 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002459 // If the callee is a GlobalAddress node (quite common, every direct call
2460 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2461 // it.
2462
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002463 // We should use extra load for direct calls to dllimported functions in
2464 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002465 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002466 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002467 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002468 bool ExtraLoad = false;
2469 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002470
Chris Lattner48a7d022009-07-09 05:02:21 +00002471 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2472 // external symbols most go through the PLT in PIC mode. If the symbol
2473 // has hidden or protected visibility, or if it is static or local, then
2474 // we don't need to use the PLT - we can directly call it.
2475 if (Subtarget->isTargetELF() &&
2476 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002477 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002478 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002479 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002480 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002481 (!Subtarget->getTargetTriple().isMacOSX() ||
2482 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002483 // PC-relative references to external symbols should go through $stub,
2484 // unless we're building with the leopard linker or later, which
2485 // automatically synthesizes these stubs.
2486 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002487 } else if (Subtarget->isPICStyleRIPRel() &&
2488 isa<Function>(GV) &&
2489 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2490 // If the function is marked as non-lazy, generate an indirect call
2491 // which loads from the GOT directly. This avoids runtime overhead
2492 // at the cost of eager binding (and one extra byte of encoding).
2493 OpFlags = X86II::MO_GOTPCREL;
2494 WrapperKind = X86ISD::WrapperRIP;
2495 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002496 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002497
Devang Patel0d881da2010-07-06 22:08:15 +00002498 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002499 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002500
2501 // Add a wrapper if needed.
2502 if (WrapperKind != ISD::DELETED_NODE)
2503 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2504 // Add extra indirection if needed.
2505 if (ExtraLoad)
2506 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2507 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002508 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002509 }
Bill Wendling056292f2008-09-16 21:48:12 +00002510 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002511 unsigned char OpFlags = 0;
2512
Evan Cheng1bf891a2010-12-01 22:59:46 +00002513 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2514 // external symbols should go through the PLT.
2515 if (Subtarget->isTargetELF() &&
2516 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2517 OpFlags = X86II::MO_PLT;
2518 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002519 (!Subtarget->getTargetTriple().isMacOSX() ||
2520 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002521 // PC-relative references to external symbols should go through $stub,
2522 // unless we're building with the leopard linker or later, which
2523 // automatically synthesizes these stubs.
2524 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002525 }
Eric Christopherfd179292009-08-27 18:07:15 +00002526
Chris Lattner48a7d022009-07-09 05:02:21 +00002527 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2528 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002529 }
2530
Chris Lattnerd96d0722007-02-25 06:40:16 +00002531 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002532 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002533 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002534
Evan Chengf22f9b32010-02-06 03:28:46 +00002535 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002536 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2537 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002538 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002539 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002540
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002541 Ops.push_back(Chain);
2542 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002543
Dan Gohman98ca4f22009-08-05 01:29:28 +00002544 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002546
Gordon Henriksen86737662008-01-05 16:56:59 +00002547 // Add argument registers to the end of the list so that they are known live
2548 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002549 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2550 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2551 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002552
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002553 // Add a register mask operand representing the call-preserved registers.
2554 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2555 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2556 assert(Mask && "Missing call preserved mask for calling convention");
2557 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002558
Gabor Greifba36cb52008-08-28 21:40:38 +00002559 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002560 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002561
Dan Gohman98ca4f22009-08-05 01:29:28 +00002562 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002563 // We used to do:
2564 //// If this is the first return lowered for this function, add the regs
2565 //// to the liveout set for the function.
2566 // This isn't right, although it's probably harmless on x86; liveouts
2567 // should be computed from returns not tail calls. Consider a void
2568 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002569 return DAG.getNode(X86ISD::TC_RETURN, dl,
2570 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002571 }
2572
Dale Johannesenace16102009-02-03 19:33:06 +00002573 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002574 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002575
Chris Lattner2d297092006-05-23 18:50:38 +00002576 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002577 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002578 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2579 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002580 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002581 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002582 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002583 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002584 // pops the hidden struct pointer, so we have to push it back.
2585 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002586 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002587 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002588 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002589 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002590
Gordon Henriksenae636f82008-01-03 16:47:34 +00002591 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002592 if (!IsSibcall) {
2593 Chain = DAG.getCALLSEQ_END(Chain,
2594 DAG.getIntPtrConstant(NumBytes, true),
2595 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2596 true),
2597 InFlag);
2598 InFlag = Chain.getValue(1);
2599 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002600
Chris Lattner3085e152007-02-25 08:59:22 +00002601 // Handle result values, copying them out of physregs into vregs that we
2602 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002603 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2604 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002605}
2606
Evan Cheng25ab6902006-09-08 06:48:29 +00002607
2608//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002609// Fast Calling Convention (tail call) implementation
2610//===----------------------------------------------------------------------===//
2611
2612// Like std call, callee cleans arguments, convention except that ECX is
2613// reserved for storing the tail called function address. Only 2 registers are
2614// free for argument passing (inreg). Tail call optimization is performed
2615// provided:
2616// * tailcallopt is enabled
2617// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002618// On X86_64 architecture with GOT-style position independent code only local
2619// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002620// To keep the stack aligned according to platform abi the function
2621// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2622// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002623// If a tail called function callee has more arguments than the caller the
2624// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002625// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002626// original REtADDR, but before the saved framepointer or the spilled registers
2627// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2628// stack layout:
2629// arg1
2630// arg2
2631// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002632// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002633// move area ]
2634// (possible EBP)
2635// ESI
2636// EDI
2637// local1 ..
2638
2639/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2640/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002641unsigned
2642X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2643 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002644 MachineFunction &MF = DAG.getMachineFunction();
2645 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002646 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002647 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002648 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002649 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002650 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002651 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2652 // Number smaller than 12 so just add the difference.
2653 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2654 } else {
2655 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002656 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002657 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002658 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002659 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002660}
2661
Evan Cheng5f941932010-02-05 02:21:12 +00002662/// MatchingStackOffset - Return true if the given stack call argument is
2663/// already available in the same position (relatively) of the caller's
2664/// incoming argument stack.
2665static
2666bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2667 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2668 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002669 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2670 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002671 if (Arg.getOpcode() == ISD::CopyFromReg) {
2672 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002673 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002674 return false;
2675 MachineInstr *Def = MRI->getVRegDef(VR);
2676 if (!Def)
2677 return false;
2678 if (!Flags.isByVal()) {
2679 if (!TII->isLoadFromStackSlot(Def, FI))
2680 return false;
2681 } else {
2682 unsigned Opcode = Def->getOpcode();
2683 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2684 Def->getOperand(1).isFI()) {
2685 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002686 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002687 } else
2688 return false;
2689 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002690 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2691 if (Flags.isByVal())
2692 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002693 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002694 // define @foo(%struct.X* %A) {
2695 // tail call @bar(%struct.X* byval %A)
2696 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002697 return false;
2698 SDValue Ptr = Ld->getBasePtr();
2699 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2700 if (!FINode)
2701 return false;
2702 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002703 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002704 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002705 FI = FINode->getIndex();
2706 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002707 } else
2708 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002709
Evan Cheng4cae1332010-03-05 08:38:04 +00002710 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002711 if (!MFI->isFixedObjectIndex(FI))
2712 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002713 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002714}
2715
Dan Gohman98ca4f22009-08-05 01:29:28 +00002716/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2717/// for tail call optimization. Targets which want to do tail call
2718/// optimization should implement this function.
2719bool
2720X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002721 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002722 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002723 bool isCalleeStructRet,
2724 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002725 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002726 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002727 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002728 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002729 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002730 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002731 CalleeCC != CallingConv::C)
2732 return false;
2733
Evan Cheng7096ae42010-01-29 06:45:59 +00002734 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002735 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002736 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002737
2738 // If the function return type is x86_fp80 and the callee return type is not,
2739 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2740 // perform a tailcall optimization here.
2741 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2742 return false;
2743
Evan Cheng13617962010-04-30 01:12:32 +00002744 CallingConv::ID CallerCC = CallerF->getCallingConv();
2745 bool CCMatch = CallerCC == CalleeCC;
2746
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002747 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002748 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002749 return true;
2750 return false;
2751 }
2752
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002753 // Look for obvious safe cases to perform tail call optimization that do not
2754 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002755
Evan Cheng2c12cb42010-03-26 16:26:03 +00002756 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2757 // emit a special epilogue.
2758 if (RegInfo->needsStackRealignment(MF))
2759 return false;
2760
Evan Chenga375d472010-03-15 18:54:48 +00002761 // Also avoid sibcall optimization if either caller or callee uses struct
2762 // return semantics.
2763 if (isCalleeStructRet || isCallerStructRet)
2764 return false;
2765
Chad Rosier2416da32011-06-24 21:15:36 +00002766 // An stdcall caller is expected to clean up its arguments; the callee
2767 // isn't going to do that.
2768 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2769 return false;
2770
Chad Rosier871f6642011-05-18 19:59:50 +00002771 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002772 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002773 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002774
2775 // Optimizing for varargs on Win64 is unlikely to be safe without
2776 // additional testing.
2777 if (Subtarget->isTargetWin64())
2778 return false;
2779
Chad Rosier871f6642011-05-18 19:59:50 +00002780 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002781 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002782 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002783
Chad Rosier871f6642011-05-18 19:59:50 +00002784 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2785 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2786 if (!ArgLocs[i].isRegLoc())
2787 return false;
2788 }
2789
Chad Rosier30450e82011-12-22 22:35:21 +00002790 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2791 // stack. Therefore, if it's not used by the call it is not safe to optimize
2792 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002793 bool Unused = false;
2794 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2795 if (!Ins[i].Used) {
2796 Unused = true;
2797 break;
2798 }
2799 }
2800 if (Unused) {
2801 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002802 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002803 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002804 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002805 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002806 CCValAssign &VA = RVLocs[i];
2807 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2808 return false;
2809 }
2810 }
2811
Evan Cheng13617962010-04-30 01:12:32 +00002812 // If the calling conventions do not match, then we'd better make sure the
2813 // results are returned in the same way as what the caller expects.
2814 if (!CCMatch) {
2815 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002816 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002817 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002818 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2819
2820 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002821 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002822 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002823 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2824
2825 if (RVLocs1.size() != RVLocs2.size())
2826 return false;
2827 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2828 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2829 return false;
2830 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2831 return false;
2832 if (RVLocs1[i].isRegLoc()) {
2833 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2834 return false;
2835 } else {
2836 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2837 return false;
2838 }
2839 }
2840 }
2841
Evan Chenga6bff982010-01-30 01:22:00 +00002842 // If the callee takes no arguments then go on to check the results of the
2843 // call.
2844 if (!Outs.empty()) {
2845 // Check if stack adjustment is needed. For now, do not do this if any
2846 // argument is passed on the stack.
2847 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002848 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002849 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002850
2851 // Allocate shadow area for Win64
2852 if (Subtarget->isTargetWin64()) {
2853 CCInfo.AllocateStack(32, 8);
2854 }
2855
Duncan Sands45907662010-10-31 13:21:44 +00002856 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002857 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002858 MachineFunction &MF = DAG.getMachineFunction();
2859 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2860 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002861
2862 // Check if the arguments are already laid out in the right way as
2863 // the caller's fixed stack objects.
2864 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002865 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2866 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002867 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002868 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2869 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002870 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002871 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002872 if (VA.getLocInfo() == CCValAssign::Indirect)
2873 return false;
2874 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002875 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2876 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002877 return false;
2878 }
2879 }
2880 }
Evan Cheng9c044672010-05-29 01:35:22 +00002881
2882 // If the tailcall address may be in a register, then make sure it's
2883 // possible to register allocate for it. In 32-bit, the call address can
2884 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002885 // callee-saved registers are restored. These happen to be the same
2886 // registers used to pass 'inreg' arguments so watch out for those.
2887 if (!Subtarget->is64Bit() &&
2888 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002889 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002890 unsigned NumInRegs = 0;
2891 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2892 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002893 if (!VA.isRegLoc())
2894 continue;
2895 unsigned Reg = VA.getLocReg();
2896 switch (Reg) {
2897 default: break;
2898 case X86::EAX: case X86::EDX: case X86::ECX:
2899 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002900 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002901 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002902 }
2903 }
2904 }
Evan Chenga6bff982010-01-30 01:22:00 +00002905 }
Evan Chengb1712452010-01-27 06:25:16 +00002906
Evan Cheng86809cc2010-02-03 03:28:02 +00002907 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002908}
2909
Dan Gohman3df24e62008-09-03 23:12:08 +00002910FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002911X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2912 const TargetLibraryInfo *libInfo) const {
2913 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002914}
2915
2916
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002917//===----------------------------------------------------------------------===//
2918// Other Lowering Hooks
2919//===----------------------------------------------------------------------===//
2920
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002921static bool MayFoldLoad(SDValue Op) {
2922 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2923}
2924
2925static bool MayFoldIntoStore(SDValue Op) {
2926 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2927}
2928
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002929static bool isTargetShuffle(unsigned Opcode) {
2930 switch(Opcode) {
2931 default: return false;
2932 case X86ISD::PSHUFD:
2933 case X86ISD::PSHUFHW:
2934 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002935 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002936 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002937 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002938 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002939 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002940 case X86ISD::MOVLPS:
2941 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002942 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002943 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002944 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002945 case X86ISD::MOVSS:
2946 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002947 case X86ISD::UNPCKL:
2948 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002949 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002950 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002951 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002952 return true;
2953 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002954}
2955
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002956static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002957 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002958 switch(Opc) {
2959 default: llvm_unreachable("Unknown x86 shuffle node");
2960 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002961 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002962 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002963 return DAG.getNode(Opc, dl, VT, V1);
2964 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002965}
2966
2967static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002968 SDValue V1, unsigned TargetMask,
2969 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002970 switch(Opc) {
2971 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002972 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002973 case X86ISD::PSHUFHW:
2974 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002975 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002976 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002977 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2978 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002979}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002980
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002981static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002982 SDValue V1, SDValue V2, unsigned TargetMask,
2983 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002984 switch(Opc) {
2985 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002986 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002987 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002988 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002989 return DAG.getNode(Opc, dl, VT, V1, V2,
2990 DAG.getConstant(TargetMask, MVT::i8));
2991 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002992}
2993
2994static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2995 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2996 switch(Opc) {
2997 default: llvm_unreachable("Unknown x86 shuffle node");
2998 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002999 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003000 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003001 case X86ISD::MOVLPS:
3002 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003003 case X86ISD::MOVSS:
3004 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003005 case X86ISD::UNPCKL:
3006 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003007 return DAG.getNode(Opc, dl, VT, V1, V2);
3008 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003009}
3010
Dan Gohmand858e902010-04-17 15:26:15 +00003011SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003012 MachineFunction &MF = DAG.getMachineFunction();
3013 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3014 int ReturnAddrIndex = FuncInfo->getRAIndex();
3015
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003016 if (ReturnAddrIndex == 0) {
3017 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00003018 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00003019 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003020 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003021 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003022 }
3023
Evan Cheng25ab6902006-09-08 06:48:29 +00003024 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003025}
3026
3027
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003028bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3029 bool hasSymbolicDisplacement) {
3030 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003031 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003032 return false;
3033
3034 // If we don't have a symbolic displacement - we don't have any extra
3035 // restrictions.
3036 if (!hasSymbolicDisplacement)
3037 return true;
3038
3039 // FIXME: Some tweaks might be needed for medium code model.
3040 if (M != CodeModel::Small && M != CodeModel::Kernel)
3041 return false;
3042
3043 // For small code model we assume that latest object is 16MB before end of 31
3044 // bits boundary. We may also accept pretty large negative constants knowing
3045 // that all objects are in the positive half of address space.
3046 if (M == CodeModel::Small && Offset < 16*1024*1024)
3047 return true;
3048
3049 // For kernel code model we know that all object resist in the negative half
3050 // of 32bits address space. We may not accept negative offsets, since they may
3051 // be just off and we may accept pretty large positive ones.
3052 if (M == CodeModel::Kernel && Offset > 0)
3053 return true;
3054
3055 return false;
3056}
3057
Evan Chengef41ff62011-06-23 17:54:54 +00003058/// isCalleePop - Determines whether the callee is required to pop its
3059/// own arguments. Callee pop is necessary to support tail calls.
3060bool X86::isCalleePop(CallingConv::ID CallingConv,
3061 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3062 if (IsVarArg)
3063 return false;
3064
3065 switch (CallingConv) {
3066 default:
3067 return false;
3068 case CallingConv::X86_StdCall:
3069 return !is64Bit;
3070 case CallingConv::X86_FastCall:
3071 return !is64Bit;
3072 case CallingConv::X86_ThisCall:
3073 return !is64Bit;
3074 case CallingConv::Fast:
3075 return TailCallOpt;
3076 case CallingConv::GHC:
3077 return TailCallOpt;
3078 }
3079}
3080
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003081/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3082/// specific condition code, returning the condition code and the LHS/RHS of the
3083/// comparison to make.
3084static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3085 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003086 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003087 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3088 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3089 // X > -1 -> X == 0, jump !sign.
3090 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003091 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003092 }
3093 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003094 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003095 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003096 }
3097 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003098 // X < 1 -> X <= 0
3099 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003100 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003101 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003102 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003103
Evan Chengd9558e02006-01-06 00:43:03 +00003104 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003105 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003106 case ISD::SETEQ: return X86::COND_E;
3107 case ISD::SETGT: return X86::COND_G;
3108 case ISD::SETGE: return X86::COND_GE;
3109 case ISD::SETLT: return X86::COND_L;
3110 case ISD::SETLE: return X86::COND_LE;
3111 case ISD::SETNE: return X86::COND_NE;
3112 case ISD::SETULT: return X86::COND_B;
3113 case ISD::SETUGT: return X86::COND_A;
3114 case ISD::SETULE: return X86::COND_BE;
3115 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003116 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003117 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003118
Chris Lattner4c78e022008-12-23 23:42:27 +00003119 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003120
Chris Lattner4c78e022008-12-23 23:42:27 +00003121 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003122 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3123 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003124 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3125 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003126 }
3127
Chris Lattner4c78e022008-12-23 23:42:27 +00003128 switch (SetCCOpcode) {
3129 default: break;
3130 case ISD::SETOLT:
3131 case ISD::SETOLE:
3132 case ISD::SETUGT:
3133 case ISD::SETUGE:
3134 std::swap(LHS, RHS);
3135 break;
3136 }
3137
3138 // On a floating point condition, the flags are set as follows:
3139 // ZF PF CF op
3140 // 0 | 0 | 0 | X > Y
3141 // 0 | 0 | 1 | X < Y
3142 // 1 | 0 | 0 | X == Y
3143 // 1 | 1 | 1 | unordered
3144 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003145 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003146 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003147 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003148 case ISD::SETOLT: // flipped
3149 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003150 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003151 case ISD::SETOLE: // flipped
3152 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003153 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003154 case ISD::SETUGT: // flipped
3155 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003156 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003157 case ISD::SETUGE: // flipped
3158 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003159 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003160 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003161 case ISD::SETNE: return X86::COND_NE;
3162 case ISD::SETUO: return X86::COND_P;
3163 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003164 case ISD::SETOEQ:
3165 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003166 }
Evan Chengd9558e02006-01-06 00:43:03 +00003167}
3168
Evan Cheng4a460802006-01-11 00:33:36 +00003169/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3170/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003171/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003172static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003173 switch (X86CC) {
3174 default:
3175 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003176 case X86::COND_B:
3177 case X86::COND_BE:
3178 case X86::COND_E:
3179 case X86::COND_P:
3180 case X86::COND_A:
3181 case X86::COND_AE:
3182 case X86::COND_NE:
3183 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003184 return true;
3185 }
3186}
3187
Evan Chengeb2f9692009-10-27 19:56:55 +00003188/// isFPImmLegal - Returns true if the target can instruction select the
3189/// specified FP immediate natively. If false, the legalizer will
3190/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003191bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003192 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3193 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3194 return true;
3195 }
3196 return false;
3197}
3198
Nate Begeman9008ca62009-04-27 18:41:29 +00003199/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3200/// the specified range (L, H].
3201static bool isUndefOrInRange(int Val, int Low, int Hi) {
3202 return (Val < 0) || (Val >= Low && Val < Hi);
3203}
3204
3205/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3206/// specified value.
3207static bool isUndefOrEqual(int Val, int CmpVal) {
3208 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003209 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003211}
3212
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003213/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003214/// from position Pos and ending in Pos+Size, falls within the specified
3215/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003216static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003217 unsigned Pos, unsigned Size, int Low) {
3218 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003219 if (!isUndefOrEqual(Mask[i], Low))
3220 return false;
3221 return true;
3222}
3223
Nate Begeman9008ca62009-04-27 18:41:29 +00003224/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3225/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3226/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003227static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003228 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003229 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003230 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 return (Mask[0] < 2 && Mask[1] < 2);
3232 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003233}
3234
Nate Begeman9008ca62009-04-27 18:41:29 +00003235/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3236/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003237static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3238 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003239 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003240
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003242 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3243 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003244
Evan Cheng506d3df2006-03-29 23:07:14 +00003245 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003246 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003247 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003248 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003249
Craig Toppera9a568a2012-05-02 08:03:44 +00003250 if (VT == MVT::v16i16) {
3251 // Lower quadword copied in order or undef.
3252 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3253 return false;
3254
3255 // Upper quadword shuffled.
3256 for (unsigned i = 12; i != 16; ++i)
3257 if (!isUndefOrInRange(Mask[i], 12, 16))
3258 return false;
3259 }
3260
Evan Cheng506d3df2006-03-29 23:07:14 +00003261 return true;
3262}
3263
Nate Begeman9008ca62009-04-27 18:41:29 +00003264/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3265/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003266static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3267 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003268 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003269
Rafael Espindola15684b22009-04-24 12:40:33 +00003270 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003271 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3272 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003273
Rafael Espindola15684b22009-04-24 12:40:33 +00003274 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003275 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003276 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003277 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003278
Craig Toppera9a568a2012-05-02 08:03:44 +00003279 if (VT == MVT::v16i16) {
3280 // Upper quadword copied in order.
3281 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3282 return false;
3283
3284 // Lower quadword shuffled.
3285 for (unsigned i = 8; i != 12; ++i)
3286 if (!isUndefOrInRange(Mask[i], 8, 12))
3287 return false;
3288 }
3289
Rafael Espindola15684b22009-04-24 12:40:33 +00003290 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003291}
3292
Nate Begemana09008b2009-10-19 02:17:23 +00003293/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3294/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003295static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3296 const X86Subtarget *Subtarget) {
3297 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3298 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003299 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003300
Craig Topper0e2037b2012-01-20 05:53:00 +00003301 unsigned NumElts = VT.getVectorNumElements();
3302 unsigned NumLanes = VT.getSizeInBits()/128;
3303 unsigned NumLaneElts = NumElts/NumLanes;
3304
3305 // Do not handle 64-bit element shuffles with palignr.
3306 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003307 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003308
Craig Topper0e2037b2012-01-20 05:53:00 +00003309 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3310 unsigned i;
3311 for (i = 0; i != NumLaneElts; ++i) {
3312 if (Mask[i+l] >= 0)
3313 break;
3314 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003315
Craig Topper0e2037b2012-01-20 05:53:00 +00003316 // Lane is all undef, go to next lane
3317 if (i == NumLaneElts)
3318 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003319
Craig Topper0e2037b2012-01-20 05:53:00 +00003320 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003321
Craig Topper0e2037b2012-01-20 05:53:00 +00003322 // Make sure its in this lane in one of the sources
3323 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3324 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003325 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003326
3327 // If not lane 0, then we must match lane 0
3328 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3329 return false;
3330
3331 // Correct second source to be contiguous with first source
3332 if (Start >= (int)NumElts)
3333 Start -= NumElts - NumLaneElts;
3334
3335 // Make sure we're shifting in the right direction.
3336 if (Start <= (int)(i+l))
3337 return false;
3338
3339 Start -= i;
3340
3341 // Check the rest of the elements to see if they are consecutive.
3342 for (++i; i != NumLaneElts; ++i) {
3343 int Idx = Mask[i+l];
3344
3345 // Make sure its in this lane
3346 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3347 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3348 return false;
3349
3350 // If not lane 0, then we must match lane 0
3351 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3352 return false;
3353
3354 if (Idx >= (int)NumElts)
3355 Idx -= NumElts - NumLaneElts;
3356
3357 if (!isUndefOrEqual(Idx, Start+i))
3358 return false;
3359
3360 }
Nate Begemana09008b2009-10-19 02:17:23 +00003361 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003362
Nate Begemana09008b2009-10-19 02:17:23 +00003363 return true;
3364}
3365
Craig Topper1a7700a2012-01-19 08:19:12 +00003366/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3367/// the two vector operands have swapped position.
3368static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3369 unsigned NumElems) {
3370 for (unsigned i = 0; i != NumElems; ++i) {
3371 int idx = Mask[i];
3372 if (idx < 0)
3373 continue;
3374 else if (idx < (int)NumElems)
3375 Mask[i] = idx + NumElems;
3376 else
3377 Mask[i] = idx - NumElems;
3378 }
3379}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003380
Craig Topper1a7700a2012-01-19 08:19:12 +00003381/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3382/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3383/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3384/// reverse of what x86 shuffles want.
3385static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3386 bool Commuted = false) {
3387 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003388 return false;
3389
Craig Topper1a7700a2012-01-19 08:19:12 +00003390 unsigned NumElems = VT.getVectorNumElements();
3391 unsigned NumLanes = VT.getSizeInBits()/128;
3392 unsigned NumLaneElems = NumElems/NumLanes;
3393
3394 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003395 return false;
3396
3397 // VSHUFPSY divides the resulting vector into 4 chunks.
3398 // The sources are also splitted into 4 chunks, and each destination
3399 // chunk must come from a different source chunk.
3400 //
3401 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3402 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3403 //
3404 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3405 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3406 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003407 // VSHUFPDY divides the resulting vector into 4 chunks.
3408 // The sources are also splitted into 4 chunks, and each destination
3409 // chunk must come from a different source chunk.
3410 //
3411 // SRC1 => X3 X2 X1 X0
3412 // SRC2 => Y3 Y2 Y1 Y0
3413 //
3414 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3415 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003416 unsigned HalfLaneElems = NumLaneElems/2;
3417 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3418 for (unsigned i = 0; i != NumLaneElems; ++i) {
3419 int Idx = Mask[i+l];
3420 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3421 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3422 return false;
3423 // For VSHUFPSY, the mask of the second half must be the same as the
3424 // first but with the appropriate offsets. This works in the same way as
3425 // VPERMILPS works with masks.
3426 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3427 continue;
3428 if (!isUndefOrEqual(Idx, Mask[i]+l))
3429 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003430 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003431 }
3432
3433 return true;
3434}
3435
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003436/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3437/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003438static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003439 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003440 return false;
3441
Craig Topper7a9a28b2012-08-12 02:23:29 +00003442 unsigned NumElems = VT.getVectorNumElements();
3443
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003444 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003445 return false;
3446
Evan Cheng2064a2b2006-03-28 06:50:32 +00003447 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003448 return isUndefOrEqual(Mask[0], 6) &&
3449 isUndefOrEqual(Mask[1], 7) &&
3450 isUndefOrEqual(Mask[2], 2) &&
3451 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003452}
3453
Nate Begeman0b10b912009-11-07 23:17:15 +00003454/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3455/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3456/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003457static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003458 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003459 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003460
Craig Topper7a9a28b2012-08-12 02:23:29 +00003461 unsigned NumElems = VT.getVectorNumElements();
3462
Nate Begeman0b10b912009-11-07 23:17:15 +00003463 if (NumElems != 4)
3464 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003465
Craig Topperdd637ae2012-02-19 05:41:45 +00003466 return isUndefOrEqual(Mask[0], 2) &&
3467 isUndefOrEqual(Mask[1], 3) &&
3468 isUndefOrEqual(Mask[2], 2) &&
3469 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003470}
3471
Evan Cheng5ced1d82006-04-06 23:23:56 +00003472/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3473/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003474static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003475 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003476 return false;
3477
Craig Topperdd637ae2012-02-19 05:41:45 +00003478 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003479
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480 if (NumElems != 2 && NumElems != 4)
3481 return false;
3482
Chad Rosier238ae312012-04-30 17:47:15 +00003483 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003484 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003485 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003486
Chad Rosier238ae312012-04-30 17:47:15 +00003487 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003488 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003489 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003490
3491 return true;
3492}
3493
Nate Begeman0b10b912009-11-07 23:17:15 +00003494/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3495/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003496static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003497 if (!VT.is128BitVector())
3498 return false;
3499
Craig Topperdd637ae2012-02-19 05:41:45 +00003500 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003501
Craig Topper7a9a28b2012-08-12 02:23:29 +00003502 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003503 return false;
3504
Chad Rosier238ae312012-04-30 17:47:15 +00003505 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003506 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003507 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003508
Chad Rosier238ae312012-04-30 17:47:15 +00003509 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3510 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003511 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003512
3513 return true;
3514}
3515
Elena Demikhovsky15963732012-06-26 08:04:10 +00003516//
3517// Some special combinations that can be optimized.
3518//
3519static
3520SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3521 SelectionDAG &DAG) {
3522 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003523 DebugLoc dl = SVOp->getDebugLoc();
3524
3525 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3526 return SDValue();
3527
3528 ArrayRef<int> Mask = SVOp->getMask();
3529
3530 // These are the special masks that may be optimized.
3531 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3532 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3533 bool MatchEvenMask = true;
3534 bool MatchOddMask = true;
3535 for (int i=0; i<8; ++i) {
3536 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3537 MatchEvenMask = false;
3538 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3539 MatchOddMask = false;
3540 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003541
Elena Demikhovsky32510202012-09-04 12:49:02 +00003542 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003543 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003544
Elena Demikhovsky15963732012-06-26 08:04:10 +00003545 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3546
Elena Demikhovsky32510202012-09-04 12:49:02 +00003547 SDValue Op0 = SVOp->getOperand(0);
3548 SDValue Op1 = SVOp->getOperand(1);
3549
3550 if (MatchEvenMask) {
3551 // Shift the second operand right to 32 bits.
3552 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3553 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3554 } else {
3555 // Shift the first operand left to 32 bits.
3556 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3557 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3558 }
3559 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3560 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003561}
3562
Evan Cheng0038e592006-03-28 00:39:58 +00003563/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3564/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003565static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003566 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003567 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003568
3569 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3570 "Unsupported vector type for unpckh");
3571
Craig Topper6347e862011-11-21 06:57:39 +00003572 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003573 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003574 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003575
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003576 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3577 // independently on 128-bit lanes.
3578 unsigned NumLanes = VT.getSizeInBits()/128;
3579 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003580
Craig Topper94438ba2011-12-16 08:06:31 +00003581 for (unsigned l = 0; l != NumLanes; ++l) {
3582 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3583 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003584 i += 2, ++j) {
3585 int BitI = Mask[i];
3586 int BitI1 = Mask[i+1];
3587 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003588 return false;
David Greenea20244d2011-03-02 17:23:43 +00003589 if (V2IsSplat) {
3590 if (!isUndefOrEqual(BitI1, NumElts))
3591 return false;
3592 } else {
3593 if (!isUndefOrEqual(BitI1, j + NumElts))
3594 return false;
3595 }
Evan Cheng39623da2006-04-20 08:58:49 +00003596 }
Evan Cheng0038e592006-03-28 00:39:58 +00003597 }
David Greenea20244d2011-03-02 17:23:43 +00003598
Evan Cheng0038e592006-03-28 00:39:58 +00003599 return true;
3600}
3601
Evan Cheng4fcb9222006-03-28 02:43:26 +00003602/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3603/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003604static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003605 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003606 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003607
3608 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3609 "Unsupported vector type for unpckh");
3610
Craig Topper6347e862011-11-21 06:57:39 +00003611 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003612 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003613 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003614
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003615 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3616 // independently on 128-bit lanes.
3617 unsigned NumLanes = VT.getSizeInBits()/128;
3618 unsigned NumLaneElts = NumElts/NumLanes;
3619
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003620 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003621 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3622 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003623 int BitI = Mask[i];
3624 int BitI1 = Mask[i+1];
3625 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003626 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003627 if (V2IsSplat) {
3628 if (isUndefOrEqual(BitI1, NumElts))
3629 return false;
3630 } else {
3631 if (!isUndefOrEqual(BitI1, j+NumElts))
3632 return false;
3633 }
Evan Cheng39623da2006-04-20 08:58:49 +00003634 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003635 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003636 return true;
3637}
3638
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003639/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3640/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3641/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003642static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003643 bool HasAVX2) {
3644 unsigned NumElts = VT.getVectorNumElements();
3645
3646 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3647 "Unsupported vector type for unpckh");
3648
3649 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3650 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003651 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003652
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003653 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3654 // FIXME: Need a better way to get rid of this, there's no latency difference
3655 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3656 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003657 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003658 return false;
3659
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003660 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3661 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003662 unsigned NumLanes = VT.getSizeInBits()/128;
3663 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003664
Craig Topper94438ba2011-12-16 08:06:31 +00003665 for (unsigned l = 0; l != NumLanes; ++l) {
3666 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3667 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003668 i += 2, ++j) {
3669 int BitI = Mask[i];
3670 int BitI1 = Mask[i+1];
3671
3672 if (!isUndefOrEqual(BitI, j))
3673 return false;
3674 if (!isUndefOrEqual(BitI1, j))
3675 return false;
3676 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003677 }
David Greenea20244d2011-03-02 17:23:43 +00003678
Rafael Espindola15684b22009-04-24 12:40:33 +00003679 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003680}
3681
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003682/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3683/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3684/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003685static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003686 unsigned NumElts = VT.getVectorNumElements();
3687
3688 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3689 "Unsupported vector type for unpckh");
3690
3691 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3692 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003693 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003694
Craig Topper94438ba2011-12-16 08:06:31 +00003695 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3696 // independently on 128-bit lanes.
3697 unsigned NumLanes = VT.getSizeInBits()/128;
3698 unsigned NumLaneElts = NumElts/NumLanes;
3699
3700 for (unsigned l = 0; l != NumLanes; ++l) {
3701 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3702 i != (l+1)*NumLaneElts; i += 2, ++j) {
3703 int BitI = Mask[i];
3704 int BitI1 = Mask[i+1];
3705 if (!isUndefOrEqual(BitI, j))
3706 return false;
3707 if (!isUndefOrEqual(BitI1, j))
3708 return false;
3709 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003710 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003711 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003712}
3713
Evan Cheng017dcc62006-04-21 01:05:10 +00003714/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3715/// specifies a shuffle of elements that is suitable for input to MOVSS,
3716/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003717static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003718 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003719 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003720 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003721 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003722
Craig Topperc612d792012-01-02 09:17:37 +00003723 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003724
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003726 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003727
Craig Topperc612d792012-01-02 09:17:37 +00003728 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003729 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003730 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003731
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003732 return true;
3733}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003734
Craig Topper70b883b2011-11-28 10:14:51 +00003735/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003736/// as permutations between 128-bit chunks or halves. As an example: this
3737/// shuffle bellow:
3738/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3739/// The first half comes from the second half of V1 and the second half from the
3740/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003741static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003742 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003743 return false;
3744
3745 // The shuffle result is divided into half A and half B. In total the two
3746 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3747 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003748 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003749 bool MatchA = false, MatchB = false;
3750
3751 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003752 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003753 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3754 MatchA = true;
3755 break;
3756 }
3757 }
3758
3759 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003760 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003761 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3762 MatchB = true;
3763 break;
3764 }
3765 }
3766
3767 return MatchA && MatchB;
3768}
3769
Craig Topper70b883b2011-11-28 10:14:51 +00003770/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3771/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003772static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003773 EVT VT = SVOp->getValueType(0);
3774
Craig Topperc612d792012-01-02 09:17:37 +00003775 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003776
Craig Topperc612d792012-01-02 09:17:37 +00003777 unsigned FstHalf = 0, SndHalf = 0;
3778 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003779 if (SVOp->getMaskElt(i) > 0) {
3780 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3781 break;
3782 }
3783 }
Craig Topperc612d792012-01-02 09:17:37 +00003784 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003785 if (SVOp->getMaskElt(i) > 0) {
3786 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3787 break;
3788 }
3789 }
3790
3791 return (FstHalf | (SndHalf << 4));
3792}
3793
Craig Topper70b883b2011-11-28 10:14:51 +00003794/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003795/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3796/// Note that VPERMIL mask matching is different depending whether theunderlying
3797/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3798/// to the same elements of the low, but to the higher half of the source.
3799/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003800/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003801static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003802 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003803 return false;
3804
Craig Topperc612d792012-01-02 09:17:37 +00003805 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003806 // Only match 256-bit with 32/64-bit types
3807 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003808 return false;
3809
Craig Topperc612d792012-01-02 09:17:37 +00003810 unsigned NumLanes = VT.getSizeInBits()/128;
3811 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003812 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003813 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003814 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003815 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003816 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003817 continue;
3818 // VPERMILPS handling
3819 if (Mask[i] < 0)
3820 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003821 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003822 return false;
3823 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003824 }
3825
3826 return true;
3827}
3828
Craig Topper5aaffa82012-02-19 02:53:47 +00003829/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003830/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003831/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003832static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003833 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003834 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003835 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003836
3837 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003838 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003839 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003840
Nate Begeman9008ca62009-04-27 18:41:29 +00003841 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003842 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003843
Craig Topperc612d792012-01-02 09:17:37 +00003844 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003845 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3846 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3847 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003848 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003849
Evan Cheng39623da2006-04-20 08:58:49 +00003850 return true;
3851}
3852
Evan Chengd9539472006-04-14 21:59:03 +00003853/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3854/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003855/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003856static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003857 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003858 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003859 return false;
3860
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003861 unsigned NumElems = VT.getVectorNumElements();
3862
3863 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3864 (VT.getSizeInBits() == 256 && NumElems != 8))
3865 return false;
3866
3867 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003868 for (unsigned i = 0; i != NumElems; i += 2)
3869 if (!isUndefOrEqual(Mask[i], i+1) ||
3870 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003872
3873 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003874}
3875
3876/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3877/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003878/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003879static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003880 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003881 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003882 return false;
3883
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003884 unsigned NumElems = VT.getVectorNumElements();
3885
3886 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3887 (VT.getSizeInBits() == 256 && NumElems != 8))
3888 return false;
3889
3890 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003891 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003892 if (!isUndefOrEqual(Mask[i], i) ||
3893 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003895
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003896 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003897}
3898
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003899/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3900/// specifies a shuffle of elements that is suitable for input to 256-bit
3901/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003902static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003903 if (!HasAVX || !VT.is256BitVector())
3904 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003905
Craig Topper7a9a28b2012-08-12 02:23:29 +00003906 unsigned NumElts = VT.getVectorNumElements();
3907 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003908 return false;
3909
Craig Topperc612d792012-01-02 09:17:37 +00003910 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003911 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003912 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003913 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003914 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003915 return false;
3916 return true;
3917}
3918
Evan Cheng0b457f02008-09-25 20:50:48 +00003919/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003920/// specifies a shuffle of elements that is suitable for input to 128-bit
3921/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003922static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003923 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003924 return false;
3925
Craig Topperc612d792012-01-02 09:17:37 +00003926 unsigned e = VT.getVectorNumElements() / 2;
3927 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003928 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003929 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003930 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003931 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003932 return false;
3933 return true;
3934}
3935
David Greenec38a03e2011-02-03 15:50:00 +00003936/// isVEXTRACTF128Index - Return true if the specified
3937/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3938/// suitable for input to VEXTRACTF128.
3939bool X86::isVEXTRACTF128Index(SDNode *N) {
3940 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3941 return false;
3942
3943 // The index should be aligned on a 128-bit boundary.
3944 uint64_t Index =
3945 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3946
3947 unsigned VL = N->getValueType(0).getVectorNumElements();
3948 unsigned VBits = N->getValueType(0).getSizeInBits();
3949 unsigned ElSize = VBits / VL;
3950 bool Result = (Index * ElSize) % 128 == 0;
3951
3952 return Result;
3953}
3954
David Greeneccacdc12011-02-04 16:08:29 +00003955/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3956/// operand specifies a subvector insert that is suitable for input to
3957/// VINSERTF128.
3958bool X86::isVINSERTF128Index(SDNode *N) {
3959 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3960 return false;
3961
3962 // The index should be aligned on a 128-bit boundary.
3963 uint64_t Index =
3964 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3965
3966 unsigned VL = N->getValueType(0).getVectorNumElements();
3967 unsigned VBits = N->getValueType(0).getSizeInBits();
3968 unsigned ElSize = VBits / VL;
3969 bool Result = (Index * ElSize) % 128 == 0;
3970
3971 return Result;
3972}
3973
Evan Cheng63d33002006-03-22 08:01:21 +00003974/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003975/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003976/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003977static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003978 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003979
Craig Topper1a7700a2012-01-19 08:19:12 +00003980 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3981 "Unsupported vector type for PSHUF/SHUFP");
3982
3983 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3984 // independently on 128-bit lanes.
3985 unsigned NumElts = VT.getVectorNumElements();
3986 unsigned NumLanes = VT.getSizeInBits()/128;
3987 unsigned NumLaneElts = NumElts/NumLanes;
3988
3989 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3990 "Only supports 2 or 4 elements per lane");
3991
3992 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003993 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003994 for (unsigned i = 0; i != NumElts; ++i) {
3995 int Elt = N->getMaskElt(i);
3996 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003997 Elt &= NumLaneElts - 1;
3998 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003999 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004000 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004001
Evan Cheng63d33002006-03-22 08:01:21 +00004002 return Mask;
4003}
4004
Evan Cheng506d3df2006-03-29 23:07:14 +00004005/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004006/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004007static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004008 EVT VT = N->getValueType(0);
4009
4010 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4011 "Unsupported vector type for PSHUFHW");
4012
4013 unsigned NumElts = VT.getVectorNumElements();
4014
Evan Cheng506d3df2006-03-29 23:07:14 +00004015 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004016 for (unsigned l = 0; l != NumElts; l += 8) {
4017 // 8 nodes per lane, but we only care about the last 4.
4018 for (unsigned i = 0; i < 4; ++i) {
4019 int Elt = N->getMaskElt(l+i+4);
4020 if (Elt < 0) continue;
4021 Elt &= 0x3; // only 2-bits.
4022 Mask |= Elt << (i * 2);
4023 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004024 }
Craig Topper6b28d352012-05-03 07:12:59 +00004025
Evan Cheng506d3df2006-03-29 23:07:14 +00004026 return Mask;
4027}
4028
4029/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004030/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004031static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004032 EVT VT = N->getValueType(0);
4033
4034 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4035 "Unsupported vector type for PSHUFHW");
4036
4037 unsigned NumElts = VT.getVectorNumElements();
4038
Evan Cheng506d3df2006-03-29 23:07:14 +00004039 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004040 for (unsigned l = 0; l != NumElts; l += 8) {
4041 // 8 nodes per lane, but we only care about the first 4.
4042 for (unsigned i = 0; i < 4; ++i) {
4043 int Elt = N->getMaskElt(l+i);
4044 if (Elt < 0) continue;
4045 Elt &= 0x3; // only 2-bits
4046 Mask |= Elt << (i * 2);
4047 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004048 }
Craig Topper6b28d352012-05-03 07:12:59 +00004049
Evan Cheng506d3df2006-03-29 23:07:14 +00004050 return Mask;
4051}
4052
Nate Begemana09008b2009-10-19 02:17:23 +00004053/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4054/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004055static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4056 EVT VT = SVOp->getValueType(0);
4057 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004058
Craig Topper0e2037b2012-01-20 05:53:00 +00004059 unsigned NumElts = VT.getVectorNumElements();
4060 unsigned NumLanes = VT.getSizeInBits()/128;
4061 unsigned NumLaneElts = NumElts/NumLanes;
4062
4063 int Val = 0;
4064 unsigned i;
4065 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004066 Val = SVOp->getMaskElt(i);
4067 if (Val >= 0)
4068 break;
4069 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004070 if (Val >= (int)NumElts)
4071 Val -= NumElts - NumLaneElts;
4072
Eli Friedman63f8dde2011-07-25 21:36:45 +00004073 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004074 return (Val - i) * EltSize;
4075}
4076
David Greenec38a03e2011-02-03 15:50:00 +00004077/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4078/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4079/// instructions.
4080unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4081 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4082 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4083
4084 uint64_t Index =
4085 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4086
4087 EVT VecVT = N->getOperand(0).getValueType();
4088 EVT ElVT = VecVT.getVectorElementType();
4089
4090 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004091 return Index / NumElemsPerChunk;
4092}
4093
David Greeneccacdc12011-02-04 16:08:29 +00004094/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4095/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4096/// instructions.
4097unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4098 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4099 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4100
4101 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004102 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004103
4104 EVT VecVT = N->getValueType(0);
4105 EVT ElVT = VecVT.getVectorElementType();
4106
4107 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004108 return Index / NumElemsPerChunk;
4109}
4110
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004111/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4112/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4113/// Handles 256-bit.
4114static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4115 EVT VT = N->getValueType(0);
4116
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004117 unsigned NumElts = VT.getVectorNumElements();
4118
Craig Topper095c5282012-04-15 23:48:57 +00004119 assert((VT.is256BitVector() && NumElts == 4) &&
4120 "Unsupported vector type for VPERMQ/VPERMPD");
4121
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004122 unsigned Mask = 0;
4123 for (unsigned i = 0; i != NumElts; ++i) {
4124 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004125 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004126 continue;
4127 Mask |= Elt << (i*2);
4128 }
4129
4130 return Mask;
4131}
Evan Cheng37b73872009-07-30 08:33:02 +00004132/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4133/// constant +0.0.
4134bool X86::isZeroNode(SDValue Elt) {
4135 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004136 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004137 (isa<ConstantFPSDNode>(Elt) &&
4138 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4139}
4140
Nate Begeman9008ca62009-04-27 18:41:29 +00004141/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4142/// their permute mask.
4143static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4144 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004145 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004146 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004147 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004148
Nate Begeman5a5ca152009-04-29 05:20:52 +00004149 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004150 int Idx = SVOp->getMaskElt(i);
4151 if (Idx >= 0) {
4152 if (Idx < (int)NumElems)
4153 Idx += NumElems;
4154 else
4155 Idx -= NumElems;
4156 }
4157 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004158 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004159 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4160 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004161}
4162
Evan Cheng533a0aa2006-04-19 20:35:22 +00004163/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4164/// match movhlps. The lower half elements should come from upper half of
4165/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004166/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004167static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004168 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004169 return false;
4170 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004171 return false;
4172 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004173 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004174 return false;
4175 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004176 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004177 return false;
4178 return true;
4179}
4180
Evan Cheng5ced1d82006-04-06 23:23:56 +00004181/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004182/// is promoted to a vector. It also returns the LoadSDNode by reference if
4183/// required.
4184static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004185 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4186 return false;
4187 N = N->getOperand(0).getNode();
4188 if (!ISD::isNON_EXTLoad(N))
4189 return false;
4190 if (LD)
4191 *LD = cast<LoadSDNode>(N);
4192 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004193}
4194
Dan Gohman65fd6562011-11-03 21:49:52 +00004195// Test whether the given value is a vector value which will be legalized
4196// into a load.
4197static bool WillBeConstantPoolLoad(SDNode *N) {
4198 if (N->getOpcode() != ISD::BUILD_VECTOR)
4199 return false;
4200
4201 // Check for any non-constant elements.
4202 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4203 switch (N->getOperand(i).getNode()->getOpcode()) {
4204 case ISD::UNDEF:
4205 case ISD::ConstantFP:
4206 case ISD::Constant:
4207 break;
4208 default:
4209 return false;
4210 }
4211
4212 // Vectors of all-zeros and all-ones are materialized with special
4213 // instructions rather than being loaded.
4214 return !ISD::isBuildVectorAllZeros(N) &&
4215 !ISD::isBuildVectorAllOnes(N);
4216}
4217
Evan Cheng533a0aa2006-04-19 20:35:22 +00004218/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4219/// match movlp{s|d}. The lower half elements should come from lower half of
4220/// V1 (and in order), and the upper half elements should come from the upper
4221/// half of V2 (and in order). And since V1 will become the source of the
4222/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004223static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004224 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004225 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004226 return false;
4227
Evan Cheng466685d2006-10-09 20:57:25 +00004228 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004229 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004230 // Is V2 is a vector load, don't do this transformation. We will try to use
4231 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004232 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004233 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004235 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004236
Evan Cheng533a0aa2006-04-19 20:35:22 +00004237 if (NumElems != 2 && NumElems != 4)
4238 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004239 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004240 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004241 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004242 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004243 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004244 return false;
4245 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004246}
4247
Evan Cheng39623da2006-04-20 08:58:49 +00004248/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4249/// all the same.
4250static bool isSplatVector(SDNode *N) {
4251 if (N->getOpcode() != ISD::BUILD_VECTOR)
4252 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004253
Dan Gohman475871a2008-07-27 21:46:04 +00004254 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004255 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4256 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004257 return false;
4258 return true;
4259}
4260
Evan Cheng213d2cf2007-05-17 18:45:50 +00004261/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004262/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004263/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004264static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004265 SDValue V1 = N->getOperand(0);
4266 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004267 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4268 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004269 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004270 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004272 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4273 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004274 if (Opc != ISD::BUILD_VECTOR ||
4275 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004276 return false;
4277 } else if (Idx >= 0) {
4278 unsigned Opc = V1.getOpcode();
4279 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4280 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004281 if (Opc != ISD::BUILD_VECTOR ||
4282 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004283 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004284 }
4285 }
4286 return true;
4287}
4288
4289/// getZeroVector - Returns a vector of specified type with all zero elements.
4290///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004291static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004292 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004293 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004294 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004295
Dale Johannesen0488fb62010-09-30 23:57:10 +00004296 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004297 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004298 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004299 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004300 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004301 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4302 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4303 } else { // SSE1
4304 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4305 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4306 }
Craig Topper9d352402012-04-23 07:24:41 +00004307 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004308 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004309 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4310 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4311 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4312 } else {
4313 // 256-bit logic and arithmetic instructions in AVX are all
4314 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4315 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4316 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4317 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4318 }
Craig Topper9d352402012-04-23 07:24:41 +00004319 } else
4320 llvm_unreachable("Unexpected vector type");
4321
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004322 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004323}
4324
Chris Lattner8a594482007-11-25 00:24:49 +00004325/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004326/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4327/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4328/// Then bitcast to their original type, ensuring they get CSE'd.
4329static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4330 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004331 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004332 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004333
Owen Anderson825b72b2009-08-11 20:47:22 +00004334 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004335 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004336 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004337 if (HasAVX2) { // AVX2
4338 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4339 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4340 } else { // AVX
4341 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004342 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004343 }
Craig Topper9d352402012-04-23 07:24:41 +00004344 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004345 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004346 } else
4347 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004348
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004349 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004350}
4351
Evan Cheng39623da2006-04-20 08:58:49 +00004352/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4353/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004354static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004355 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004356 if (Mask[i] > (int)NumElems) {
4357 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004358 }
Evan Cheng39623da2006-04-20 08:58:49 +00004359 }
Evan Cheng39623da2006-04-20 08:58:49 +00004360}
4361
Evan Cheng017dcc62006-04-21 01:05:10 +00004362/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4363/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004364static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 SDValue V2) {
4366 unsigned NumElems = VT.getVectorNumElements();
4367 SmallVector<int, 8> Mask;
4368 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004369 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 Mask.push_back(i);
4371 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004372}
4373
Nate Begeman9008ca62009-04-27 18:41:29 +00004374/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004375static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 SDValue V2) {
4377 unsigned NumElems = VT.getVectorNumElements();
4378 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004379 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 Mask.push_back(i);
4381 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004382 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004384}
4385
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004386/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004387static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004388 SDValue V2) {
4389 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004391 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 Mask.push_back(i + Half);
4393 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004394 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004396}
4397
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004398// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399// a generic shuffle instruction because the target has no such instructions.
4400// Generate shuffles which repeat i16 and i8 several times until they can be
4401// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004402static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004403 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004404 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004405 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004406
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 while (NumElems > 4) {
4408 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004409 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004410 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004411 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 EltNo -= NumElems/2;
4413 }
4414 NumElems >>= 1;
4415 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004416 return V;
4417}
Eric Christopherfd179292009-08-27 18:07:15 +00004418
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004419/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4420static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4421 EVT VT = V.getValueType();
4422 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004423 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004424
Craig Topper9d352402012-04-23 07:24:41 +00004425 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004426 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004427 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004428 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4429 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004430 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004431 // To use VPERMILPS to splat scalars, the second half of indicies must
4432 // refer to the higher part, which is a duplication of the lower one,
4433 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004434 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4435 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004436
4437 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4438 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4439 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004440 } else
4441 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442
4443 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4444}
4445
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004446/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4448 EVT SrcVT = SV->getValueType(0);
4449 SDValue V1 = SV->getOperand(0);
4450 DebugLoc dl = SV->getDebugLoc();
4451
4452 int EltNo = SV->getSplatIndex();
4453 int NumElems = SrcVT.getVectorNumElements();
4454 unsigned Size = SrcVT.getSizeInBits();
4455
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004456 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4457 "Unknown how to promote splat for type");
4458
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004459 // Extract the 128-bit part containing the splat element and update
4460 // the splat element index when it refers to the higher register.
4461 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004462 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4463 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 EltNo -= NumElems/2;
4465 }
4466
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004467 // All i16 and i8 vector types can't be used directly by a generic shuffle
4468 // instruction because the target has no such instruction. Generate shuffles
4469 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004470 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004471 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004472 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004473 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474
4475 // Recreate the 256-bit vector and place the same 128-bit vector
4476 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004477 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004479 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004480 }
4481
4482 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004483}
4484
Evan Chengba05f722006-04-21 23:03:30 +00004485/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004486/// vector of zero or undef vector. This produces a shuffle where the low
4487/// element of V2 is swizzled into the zero/undef vector, landing at element
4488/// 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 +00004489static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004490 bool IsZero,
4491 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004492 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004493 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004494 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004495 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004496 unsigned NumElems = VT.getVectorNumElements();
4497 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004498 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004499 // If this is the insertion idx, put the low elt of V2 here.
4500 MaskVec.push_back(i == Idx ? NumElems : i);
4501 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004502}
4503
Craig Toppera1ffc682012-03-20 06:42:26 +00004504/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4505/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004506/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004507static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004508 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004509 unsigned NumElems = VT.getVectorNumElements();
4510 SDValue ImmN;
4511
Craig Topper89f4e662012-03-20 07:17:59 +00004512 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004513 switch(N->getOpcode()) {
4514 case X86ISD::SHUFP:
4515 ImmN = N->getOperand(N->getNumOperands()-1);
4516 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4517 break;
4518 case X86ISD::UNPCKH:
4519 DecodeUNPCKHMask(VT, Mask);
4520 break;
4521 case X86ISD::UNPCKL:
4522 DecodeUNPCKLMask(VT, Mask);
4523 break;
4524 case X86ISD::MOVHLPS:
4525 DecodeMOVHLPSMask(NumElems, Mask);
4526 break;
4527 case X86ISD::MOVLHPS:
4528 DecodeMOVLHPSMask(NumElems, Mask);
4529 break;
4530 case X86ISD::PSHUFD:
4531 case X86ISD::VPERMILP:
4532 ImmN = N->getOperand(N->getNumOperands()-1);
4533 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004534 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004535 break;
4536 case X86ISD::PSHUFHW:
4537 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004538 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004539 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004540 break;
4541 case X86ISD::PSHUFLW:
4542 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004543 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004544 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004545 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004546 case X86ISD::VPERMI:
4547 ImmN = N->getOperand(N->getNumOperands()-1);
4548 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4549 IsUnary = true;
4550 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004551 case X86ISD::MOVSS:
4552 case X86ISD::MOVSD: {
4553 // The index 0 always comes from the first element of the second source,
4554 // this is why MOVSS and MOVSD are used in the first place. The other
4555 // elements come from the other positions of the first source vector
4556 Mask.push_back(NumElems);
4557 for (unsigned i = 1; i != NumElems; ++i) {
4558 Mask.push_back(i);
4559 }
4560 break;
4561 }
4562 case X86ISD::VPERM2X128:
4563 ImmN = N->getOperand(N->getNumOperands()-1);
4564 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004565 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004566 break;
4567 case X86ISD::MOVDDUP:
4568 case X86ISD::MOVLHPD:
4569 case X86ISD::MOVLPD:
4570 case X86ISD::MOVLPS:
4571 case X86ISD::MOVSHDUP:
4572 case X86ISD::MOVSLDUP:
4573 case X86ISD::PALIGN:
4574 // Not yet implemented
4575 return false;
4576 default: llvm_unreachable("unknown target shuffle node");
4577 }
4578
4579 return true;
4580}
4581
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004582/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4583/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004584static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004585 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004586 if (Depth == 6)
4587 return SDValue(); // Limit search depth.
4588
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004589 SDValue V = SDValue(N, 0);
4590 EVT VT = V.getValueType();
4591 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004592
4593 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4594 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004595 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004596
Craig Topper3d092db2012-03-21 02:14:01 +00004597 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004598 return DAG.getUNDEF(VT.getVectorElementType());
4599
Craig Topperd156dc12012-02-06 07:17:51 +00004600 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004601 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4602 : SV->getOperand(1);
4603 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004604 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004605
4606 // Recurse into target specific vector shuffles to find scalars.
4607 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004608 MVT ShufVT = V.getValueType().getSimpleVT();
4609 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004610 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004611 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004612 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004613
Craig Topperd978c542012-05-06 19:46:21 +00004614 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004615 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004616
Craig Topper3d092db2012-03-21 02:14:01 +00004617 int Elt = ShuffleMask[Index];
4618 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004619 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004620
Craig Topper3d092db2012-03-21 02:14:01 +00004621 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004622 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004623 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004624 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004625 }
4626
4627 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004628 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 V = V.getOperand(0);
4630 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004631 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004632
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004633 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634 return SDValue();
4635 }
4636
4637 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4638 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004639 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004640
4641 if (V.getOpcode() == ISD::BUILD_VECTOR)
4642 return V.getOperand(Index);
4643
4644 return SDValue();
4645}
4646
4647/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4648/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004649/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004650static
Craig Topper3d092db2012-03-21 02:14:01 +00004651unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004652 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004653 unsigned i;
4654 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004655 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004656 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657 if (!(Elt.getNode() &&
4658 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4659 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660 }
4661
4662 return i;
4663}
4664
Craig Topper3d092db2012-03-21 02:14:01 +00004665/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4666/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004667/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4668static
Craig Topper3d092db2012-03-21 02:14:01 +00004669bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4670 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4671 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 bool SeenV1 = false;
4673 bool SeenV2 = false;
4674
Craig Topper3d092db2012-03-21 02:14:01 +00004675 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676 int Idx = SVOp->getMaskElt(i);
4677 // Ignore undef indicies
4678 if (Idx < 0)
4679 continue;
4680
Craig Topper3d092db2012-03-21 02:14:01 +00004681 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682 SeenV1 = true;
4683 else
4684 SeenV2 = true;
4685
4686 // Only accept consecutive elements from the same vector
4687 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4688 return false;
4689 }
4690
4691 OpNum = SeenV1 ? 0 : 1;
4692 return true;
4693}
4694
4695/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4696/// logical left shift of a vector.
4697static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4698 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4699 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4700 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4701 false /* check zeros from right */, DAG);
4702 unsigned OpSrc;
4703
4704 if (!NumZeros)
4705 return false;
4706
4707 // Considering the elements in the mask that are not consecutive zeros,
4708 // check if they consecutively come from only one of the source vectors.
4709 //
4710 // V1 = {X, A, B, C} 0
4711 // \ \ \ /
4712 // vector_shuffle V1, V2 <1, 2, 3, X>
4713 //
4714 if (!isShuffleMaskConsecutive(SVOp,
4715 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004716 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004717 NumZeros, // Where to start looking in the src vector
4718 NumElems, // Number of elements in vector
4719 OpSrc)) // Which source operand ?
4720 return false;
4721
4722 isLeft = false;
4723 ShAmt = NumZeros;
4724 ShVal = SVOp->getOperand(OpSrc);
4725 return true;
4726}
4727
4728/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4729/// logical left shift of a vector.
4730static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4731 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4732 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4733 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4734 true /* check zeros from left */, DAG);
4735 unsigned OpSrc;
4736
4737 if (!NumZeros)
4738 return false;
4739
4740 // Considering the elements in the mask that are not consecutive zeros,
4741 // check if they consecutively come from only one of the source vectors.
4742 //
4743 // 0 { A, B, X, X } = V2
4744 // / \ / /
4745 // vector_shuffle V1, V2 <X, X, 4, 5>
4746 //
4747 if (!isShuffleMaskConsecutive(SVOp,
4748 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004749 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004750 0, // Where to start looking in the src vector
4751 NumElems, // Number of elements in vector
4752 OpSrc)) // Which source operand ?
4753 return false;
4754
4755 isLeft = true;
4756 ShAmt = NumZeros;
4757 ShVal = SVOp->getOperand(OpSrc);
4758 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004759}
4760
4761/// isVectorShift - Returns true if the shuffle can be implemented as a
4762/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004763static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004764 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004765 // Although the logic below support any bitwidth size, there are no
4766 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004767 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004768 return false;
4769
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004770 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4771 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4772 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004773
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004774 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004775}
4776
Evan Chengc78d3b42006-04-24 18:01:45 +00004777/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4778///
Dan Gohman475871a2008-07-27 21:46:04 +00004779static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004780 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004781 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004782 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004783 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004784 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004785 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004786
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004787 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004788 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004789 bool First = true;
4790 for (unsigned i = 0; i < 16; ++i) {
4791 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4792 if (ThisIsNonZero && First) {
4793 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004794 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004795 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004796 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004797 First = false;
4798 }
4799
4800 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004801 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004802 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4803 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004804 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004805 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004806 }
4807 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4809 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4810 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004813 } else
4814 ThisElt = LastElt;
4815
Gabor Greifba36cb52008-08-28 21:40:38 +00004816 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004818 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004819 }
4820 }
4821
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004822 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004823}
4824
Bill Wendlinga348c562007-03-22 18:42:45 +00004825/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004826///
Dan Gohman475871a2008-07-27 21:46:04 +00004827static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004828 unsigned NumNonZero, unsigned NumZero,
4829 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004830 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004831 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004833 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004834
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004835 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004836 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004837 bool First = true;
4838 for (unsigned i = 0; i < 8; ++i) {
4839 bool isNonZero = (NonZeros & (1 << i)) != 0;
4840 if (isNonZero) {
4841 if (First) {
4842 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004843 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004844 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004846 First = false;
4847 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004848 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004849 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004850 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004851 }
4852 }
4853
4854 return V;
4855}
4856
Evan Chengf26ffe92008-05-29 08:22:04 +00004857/// getVShift - Return a vector logical shift node.
4858///
Owen Andersone50ed302009-08-10 22:56:29 +00004859static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004860 unsigned NumBits, SelectionDAG &DAG,
4861 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004862 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004863 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004864 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004865 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4866 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004867 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004868 DAG.getConstant(NumBits,
4869 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004870}
4871
Dan Gohman475871a2008-07-27 21:46:04 +00004872SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004873X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004874 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004875
Evan Chengc3630942009-12-09 21:00:30 +00004876 // Check if the scalar load can be widened into a vector load. And if
4877 // the address is "base + cst" see if the cst can be "absorbed" into
4878 // the shuffle mask.
4879 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4880 SDValue Ptr = LD->getBasePtr();
4881 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4882 return SDValue();
4883 EVT PVT = LD->getValueType(0);
4884 if (PVT != MVT::i32 && PVT != MVT::f32)
4885 return SDValue();
4886
4887 int FI = -1;
4888 int64_t Offset = 0;
4889 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4890 FI = FINode->getIndex();
4891 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004892 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004893 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4894 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4895 Offset = Ptr.getConstantOperandVal(1);
4896 Ptr = Ptr.getOperand(0);
4897 } else {
4898 return SDValue();
4899 }
4900
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004901 // FIXME: 256-bit vector instructions don't require a strict alignment,
4902 // improve this code to support it better.
4903 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004904 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004905 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004906 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004907 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004908 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004909 // Can't change the alignment. FIXME: It's possible to compute
4910 // the exact stack offset and reference FI + adjust offset instead.
4911 // If someone *really* cares about this. That's the way to implement it.
4912 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004913 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004915 }
4916 }
4917
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004919 // Ptr + (Offset & ~15).
4920 if (Offset < 0)
4921 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004922 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004923 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004925 if (StartOffset)
4926 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4927 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4928
4929 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004930 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004932 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4933 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004934 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004935 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004936
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004938 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004939 Mask.push_back(EltNo);
4940
Craig Toppercc3000632012-01-30 07:50:31 +00004941 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004942 }
4943
4944 return SDValue();
4945}
4946
Michael J. Spencerec38de22010-10-10 22:04:20 +00004947/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4948/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004949/// load which has the same value as a build_vector whose operands are 'elts'.
4950///
4951/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004952///
Nate Begeman1449f292010-03-24 22:19:06 +00004953/// FIXME: we'd also like to handle the case where the last elements are zero
4954/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4955/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004956static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004957 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004958 EVT EltVT = VT.getVectorElementType();
4959 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004960
Nate Begemanfdea31a2010-03-24 20:49:50 +00004961 LoadSDNode *LDBase = NULL;
4962 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004963
Nate Begeman1449f292010-03-24 22:19:06 +00004964 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004965 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004966 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004967 for (unsigned i = 0; i < NumElems; ++i) {
4968 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004969
Nate Begemanfdea31a2010-03-24 20:49:50 +00004970 if (!Elt.getNode() ||
4971 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4972 return SDValue();
4973 if (!LDBase) {
4974 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4975 return SDValue();
4976 LDBase = cast<LoadSDNode>(Elt.getNode());
4977 LastLoadedElt = i;
4978 continue;
4979 }
4980 if (Elt.getOpcode() == ISD::UNDEF)
4981 continue;
4982
4983 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4984 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4985 return SDValue();
4986 LastLoadedElt = i;
4987 }
Nate Begeman1449f292010-03-24 22:19:06 +00004988
4989 // If we have found an entire vector of loads and undefs, then return a large
4990 // load of the entire vector width starting at the base pointer. If we found
4991 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004992 if (LastLoadedElt == NumElems - 1) {
4993 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004994 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004995 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004996 LDBase->isVolatile(), LDBase->isNonTemporal(),
4997 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004998 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004999 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005000 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005001 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005002 }
5003 if (NumElems == 4 && LastLoadedElt == 1 &&
5004 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005005 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5006 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005007 SDValue ResNode =
5008 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5009 LDBase->getPointerInfo(),
5010 LDBase->getAlignment(),
5011 false/*isVolatile*/, true/*ReadMem*/,
5012 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005013
5014 // Make sure the newly-created LOAD is in the same position as LDBase in
5015 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5016 // update uses of LDBase's output chain to use the TokenFactor.
5017 if (LDBase->hasAnyUseOfValue(1)) {
5018 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5019 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5020 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5021 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5022 SDValue(ResNode.getNode(), 1));
5023 }
5024
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005025 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005026 }
5027 return SDValue();
5028}
5029
Nadav Rotem9d68b062012-04-08 12:54:54 +00005030/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5031/// to generate a splat value for the following cases:
5032/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005033/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005034/// a scalar load, or a constant.
5035/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005036/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005037SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005038X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005039 if (!Subtarget->hasAVX())
5040 return SDValue();
5041
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005042 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005043 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005044
Craig Topper5da8a802012-05-04 05:49:51 +00005045 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5046 "Unsupported vector type for broadcast.");
5047
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005048 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005049 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005050
Nadav Rotem9d68b062012-04-08 12:54:54 +00005051 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005052 default:
5053 // Unknown pattern found.
5054 return SDValue();
5055
5056 case ISD::BUILD_VECTOR: {
5057 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005058 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005059 return SDValue();
5060
Nadav Rotem9d68b062012-04-08 12:54:54 +00005061 Ld = Op.getOperand(0);
5062 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5063 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005064
5065 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005066 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005067 // Constants may have multiple users.
5068 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005069 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005070 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005071 }
5072
5073 case ISD::VECTOR_SHUFFLE: {
5074 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5075
5076 // Shuffles must have a splat mask where the first element is
5077 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005078 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005079 return SDValue();
5080
5081 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005082 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005083 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5084
5085 if (!Subtarget->hasAVX2())
5086 return SDValue();
5087
5088 // Use the register form of the broadcast instruction available on AVX2.
5089 if (VT.is256BitVector())
5090 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5091 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5092 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005093
5094 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005095 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005096 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005097
5098 // The scalar_to_vector node and the suspected
5099 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005100 // Constants may have multiple users.
5101 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005102 return SDValue();
5103 break;
5104 }
5105 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005106
Craig Topper7a9a28b2012-08-12 02:23:29 +00005107 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005108
5109 // Handle the broadcasting a single constant scalar from the constant pool
5110 // into a vector. On Sandybridge it is still better to load a constant vector
5111 // from the constant pool and not to broadcast it from a scalar.
5112 if (ConstSplatVal && Subtarget->hasAVX2()) {
5113 EVT CVT = Ld.getValueType();
5114 assert(!CVT.isVector() && "Must not broadcast a vector type");
5115 unsigned ScalarSize = CVT.getSizeInBits();
5116
Craig Topper5da8a802012-05-04 05:49:51 +00005117 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005118 const Constant *C = 0;
5119 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5120 C = CI->getConstantIntValue();
5121 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5122 C = CF->getConstantFPValue();
5123
5124 assert(C && "Invalid constant type");
5125
Nadav Rotem154819d2012-04-09 07:45:58 +00005126 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005127 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005128 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005129 MachinePointerInfo::getConstantPool(),
5130 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005131
Nadav Rotem9d68b062012-04-08 12:54:54 +00005132 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5133 }
5134 }
5135
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005136 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005137 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5138
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005139 // Handle AVX2 in-register broadcasts.
5140 if (!IsLoad && Subtarget->hasAVX2() &&
5141 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5142 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5143
5144 // The scalar source must be a normal load.
5145 if (!IsLoad)
5146 return SDValue();
5147
Craig Topper5da8a802012-05-04 05:49:51 +00005148 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005149 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005150
Craig Toppera9376332012-01-10 08:23:59 +00005151 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005152 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005153 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005154 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005155 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005156 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005157
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005158 // Unsupported broadcast.
5159 return SDValue();
5160}
5161
Michael Liao7091b242012-08-14 21:24:47 +00005162// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5163// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5164// constraint of matching input/output vector elements.
5165SDValue
5166X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5167 DebugLoc DL = Op.getDebugLoc();
5168 SDNode *N = Op.getNode();
5169 EVT VT = Op.getValueType();
5170 unsigned NumElts = Op.getNumOperands();
5171
5172 // Check supported types and sub-targets.
5173 //
5174 // Only v2f32 -> v2f64 needs special handling.
5175 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5176 return SDValue();
5177
5178 SDValue VecIn;
5179 EVT VecInVT;
5180 SmallVector<int, 8> Mask;
5181 EVT SrcVT = MVT::Other;
5182
5183 // Check the patterns could be translated into X86vfpext.
5184 for (unsigned i = 0; i < NumElts; ++i) {
5185 SDValue In = N->getOperand(i);
5186 unsigned Opcode = In.getOpcode();
5187
5188 // Skip if the element is undefined.
5189 if (Opcode == ISD::UNDEF) {
5190 Mask.push_back(-1);
5191 continue;
5192 }
5193
5194 // Quit if one of the elements is not defined from 'fpext'.
5195 if (Opcode != ISD::FP_EXTEND)
5196 return SDValue();
5197
5198 // Check how the source of 'fpext' is defined.
5199 SDValue L2In = In.getOperand(0);
5200 EVT L2InVT = L2In.getValueType();
5201
5202 // Check the original type
5203 if (SrcVT == MVT::Other)
5204 SrcVT = L2InVT;
5205 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5206 return SDValue();
5207
5208 // Check whether the value being 'fpext'ed is extracted from the same
5209 // source.
5210 Opcode = L2In.getOpcode();
5211
5212 // Quit if it's not extracted with a constant index.
5213 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5214 !isa<ConstantSDNode>(L2In.getOperand(1)))
5215 return SDValue();
5216
5217 SDValue ExtractedFromVec = L2In.getOperand(0);
5218
5219 if (VecIn.getNode() == 0) {
5220 VecIn = ExtractedFromVec;
5221 VecInVT = ExtractedFromVec.getValueType();
5222 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5223 return SDValue();
5224
5225 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5226 }
5227
Michael Liao24438b82012-08-20 17:59:18 +00005228 // Quit if all operands of BUILD_VECTOR are undefined.
5229 if (!VecIn.getNode())
5230 return SDValue();
5231
Michael Liao7091b242012-08-14 21:24:47 +00005232 // Fill the remaining mask as undef.
5233 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5234 Mask.push_back(-1);
5235
5236 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5237 DAG.getVectorShuffle(VecInVT, DL,
5238 VecIn, DAG.getUNDEF(VecInVT),
5239 &Mask[0]));
5240}
5241
Evan Chengc3630942009-12-09 21:00:30 +00005242SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005243X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005244 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005245
David Greenef125a292011-02-08 19:04:41 +00005246 EVT VT = Op.getValueType();
5247 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005248 unsigned NumElems = Op.getNumOperands();
5249
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005250 // Vectors containing all zeros can be matched by pxor and xorps later
5251 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5252 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5253 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005254 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005255 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005256
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005257 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005258 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005259
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005260 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005261 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5262 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005263 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005264 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005265 return Op;
5266
Craig Topper07a27622012-01-22 03:07:48 +00005267 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005268 }
5269
Nadav Rotem154819d2012-04-09 07:45:58 +00005270 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005271 if (Broadcast.getNode())
5272 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005273
Michael Liao7091b242012-08-14 21:24:47 +00005274 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5275 if (FpExt.getNode())
5276 return FpExt;
5277
Owen Andersone50ed302009-08-10 22:56:29 +00005278 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005279
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280 unsigned NumZero = 0;
5281 unsigned NumNonZero = 0;
5282 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005283 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005284 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005285 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005286 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005287 if (Elt.getOpcode() == ISD::UNDEF)
5288 continue;
5289 Values.insert(Elt);
5290 if (Elt.getOpcode() != ISD::Constant &&
5291 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005292 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005293 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005294 NumZero++;
5295 else {
5296 NonZeros |= (1 << i);
5297 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005298 }
5299 }
5300
Chris Lattner97a2a562010-08-26 05:24:29 +00005301 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5302 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005303 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005304
Chris Lattner67f453a2008-03-09 05:42:06 +00005305 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005306 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005307 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005308 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005309
Chris Lattner62098042008-03-09 01:05:04 +00005310 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5311 // the value are obviously zero, truncate the value to i32 and do the
5312 // insertion that way. Only do this if the value is non-constant or if the
5313 // value is a constant being inserted into element 0. It is cheaper to do
5314 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005315 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005316 (!IsAllConstants || Idx == 0)) {
5317 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005318 // Handle SSE only.
5319 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5320 EVT VecVT = MVT::v4i32;
5321 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005322
Chris Lattner62098042008-03-09 01:05:04 +00005323 // Truncate the value (which may itself be a constant) to i32, and
5324 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005325 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005326 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005327 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005328
Chris Lattner62098042008-03-09 01:05:04 +00005329 // Now we have our 32-bit value zero extended in the low element of
5330 // a vector. If Idx != 0, swizzle it into place.
5331 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005332 SmallVector<int, 4> Mask;
5333 Mask.push_back(Idx);
5334 for (unsigned i = 1; i != VecElts; ++i)
5335 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005336 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005337 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005338 }
Craig Topper07a27622012-01-22 03:07:48 +00005339 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005340 }
5341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005342
Chris Lattner19f79692008-03-08 22:59:52 +00005343 // If we have a constant or non-constant insertion into the low element of
5344 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5345 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005346 // depending on what the source datatype is.
5347 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005348 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005349 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005350
5351 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005352 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005353 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005354 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005355 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5356 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005357 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005358 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005359 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5360 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005361 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005362 }
5363
5364 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005366 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005367 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005368 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005369 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005370 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005371 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005372 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005373 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005374 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005375 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005376 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005377
5378 // Is it a vector logical left shift?
5379 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005380 X86::isZeroNode(Op.getOperand(0)) &&
5381 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005382 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005383 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005384 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005385 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005386 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005388
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005389 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005390 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391
Chris Lattner19f79692008-03-08 22:59:52 +00005392 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5393 // is a non-constant being inserted into an element other than the low one,
5394 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5395 // movd/movss) to move this into the low element, then shuffle it into
5396 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005397 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005398 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005399
Evan Cheng0db9fe62006-04-25 20:13:52 +00005400 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005401 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005402 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005403 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005404 MaskVec.push_back(i == Idx ? 0 : 1);
5405 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005406 }
5407 }
5408
Chris Lattner67f453a2008-03-09 05:42:06 +00005409 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005410 if (Values.size() == 1) {
5411 if (EVTBits == 32) {
5412 // Instead of a shuffle like this:
5413 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5414 // Check if it's possible to issue this instead.
5415 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5416 unsigned Idx = CountTrailingZeros_32(NonZeros);
5417 SDValue Item = Op.getOperand(Idx);
5418 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5419 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5420 }
Dan Gohman475871a2008-07-27 21:46:04 +00005421 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005422 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005423
Dan Gohmana3941172007-07-24 22:55:08 +00005424 // A vector full of immediates; various special cases are already
5425 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005426 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005427 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005428
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005429 // For AVX-length vectors, build the individual 128-bit pieces and use
5430 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005431 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005432 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005433 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005434 V.push_back(Op.getOperand(i));
5435
5436 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5437
5438 // Build both the lower and upper subvector.
5439 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5440 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5441 NumElems/2);
5442
5443 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005444 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005445 }
5446
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005447 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005448 if (EVTBits == 64) {
5449 if (NumNonZero == 1) {
5450 // One half is zero or undef.
5451 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005452 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005453 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005454 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005455 }
Dan Gohman475871a2008-07-27 21:46:04 +00005456 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005457 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005458
5459 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005460 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005461 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005462 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005463 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005464 }
5465
Bill Wendling826f36f2007-03-28 00:57:11 +00005466 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005467 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005468 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005469 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005470 }
5471
5472 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005473 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005474 if (NumElems == 4 && NumZero > 0) {
5475 for (unsigned i = 0; i < 4; ++i) {
5476 bool isZero = !(NonZeros & (1 << i));
5477 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005478 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 else
Dale Johannesenace16102009-02-03 19:33:06 +00005480 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 }
5482
5483 for (unsigned i = 0; i < 2; ++i) {
5484 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5485 default: break;
5486 case 0:
5487 V[i] = V[i*2]; // Must be a zero vector.
5488 break;
5489 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005490 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005491 break;
5492 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005493 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005494 break;
5495 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005496 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005497 break;
5498 }
5499 }
5500
Benjamin Kramer9c683542012-01-30 15:16:21 +00005501 bool Reverse1 = (NonZeros & 0x3) == 2;
5502 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5503 int MaskVec[] = {
5504 Reverse1 ? 1 : 0,
5505 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005506 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5507 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005508 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005509 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005510 }
5511
Craig Topper7a9a28b2012-08-12 02:23:29 +00005512 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005513 // Check for a build vector of consecutive loads.
5514 for (unsigned i = 0; i < NumElems; ++i)
5515 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005516
Nate Begemanfdea31a2010-03-24 20:49:50 +00005517 // Check for elements which are consecutive loads.
5518 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5519 if (LD.getNode())
5520 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005521
5522 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005523 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005524 SDValue Result;
5525 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5526 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5527 else
5528 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005529
Chris Lattner24faf612010-08-28 17:59:08 +00005530 for (unsigned i = 1; i < NumElems; ++i) {
5531 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5532 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005533 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005534 }
5535 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005536 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005537
Chris Lattner6e80e442010-08-28 17:15:43 +00005538 // Otherwise, expand into a number of unpckl*, start by extending each of
5539 // our (non-undef) elements to the full vector width with the element in the
5540 // bottom slot of the vector (which generates no code for SSE).
5541 for (unsigned i = 0; i < NumElems; ++i) {
5542 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5543 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5544 else
5545 V[i] = DAG.getUNDEF(VT);
5546 }
5547
5548 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005549 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5550 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5551 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005552 unsigned EltStride = NumElems >> 1;
5553 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005554 for (unsigned i = 0; i < EltStride; ++i) {
5555 // If V[i+EltStride] is undef and this is the first round of mixing,
5556 // then it is safe to just drop this shuffle: V[i] is already in the
5557 // right place, the one element (since it's the first round) being
5558 // inserted as undef can be dropped. This isn't safe for successive
5559 // rounds because they will permute elements within both vectors.
5560 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5561 EltStride == NumElems/2)
5562 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005563
Chris Lattner6e80e442010-08-28 17:15:43 +00005564 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005565 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005566 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005567 }
5568 return V[0];
5569 }
Dan Gohman475871a2008-07-27 21:46:04 +00005570 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005571}
5572
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005573// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5574// to create 256-bit vectors from two other 128-bit ones.
5575static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5576 DebugLoc dl = Op.getDebugLoc();
5577 EVT ResVT = Op.getValueType();
5578
Craig Topper7a9a28b2012-08-12 02:23:29 +00005579 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005580
5581 SDValue V1 = Op.getOperand(0);
5582 SDValue V2 = Op.getOperand(1);
5583 unsigned NumElems = ResVT.getVectorNumElements();
5584
Craig Topper4c7972d2012-04-22 18:15:59 +00005585 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005586}
5587
Craig Topper55b24052012-09-11 06:15:32 +00005588static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005589 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005590
5591 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5592 // from two other 128-bit ones.
5593 return LowerAVXCONCAT_VECTORS(Op, DAG);
5594}
5595
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005596// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005597static SDValue
5598LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5599 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005600 SDValue V1 = SVOp->getOperand(0);
5601 SDValue V2 = SVOp->getOperand(1);
5602 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005603 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005604 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005605
Nadav Roteme6113782012-04-11 06:40:27 +00005606 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005607 return SDValue();
5608
Craig Topper1842ba02012-04-23 06:38:28 +00005609 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005610 MVT OpTy;
5611
Craig Topper708e44f2012-04-23 07:36:33 +00005612 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005613 default: return SDValue();
5614 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005615 ISDNo = X86ISD::BLENDPW;
5616 OpTy = MVT::v8i16;
5617 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005618 case MVT::v4i32:
5619 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005620 ISDNo = X86ISD::BLENDPS;
5621 OpTy = MVT::v4f32;
5622 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005623 case MVT::v2i64:
5624 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005625 ISDNo = X86ISD::BLENDPD;
5626 OpTy = MVT::v2f64;
5627 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005628 case MVT::v8i32:
5629 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005630 if (!Subtarget->hasAVX())
5631 return SDValue();
5632 ISDNo = X86ISD::BLENDPS;
5633 OpTy = MVT::v8f32;
5634 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005635 case MVT::v4i64:
5636 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005637 if (!Subtarget->hasAVX())
5638 return SDValue();
5639 ISDNo = X86ISD::BLENDPD;
5640 OpTy = MVT::v4f64;
5641 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005642 }
5643 assert(ISDNo && "Invalid Op Number");
5644
5645 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005646
Craig Topper1842ba02012-04-23 06:38:28 +00005647 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005648 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005649 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005650 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005651 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005652 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005653 else
5654 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005655 }
5656
Nadav Roteme6113782012-04-11 06:40:27 +00005657 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5658 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5659 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5660 DAG.getConstant(MaskVals, MVT::i32));
5661 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005662}
5663
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664// v8i16 shuffles - Prefer shuffles in the following order:
5665// 1. [all] pshuflw, pshufhw, optional move
5666// 2. [ssse3] 1 x pshufb
5667// 3. [ssse3] 2 x pshufb + 1 x por
5668// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005669static SDValue
5670LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5671 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005672 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005673 SDValue V1 = SVOp->getOperand(0);
5674 SDValue V2 = SVOp->getOperand(1);
5675 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005677
Nate Begemanb9a47b82009-02-23 08:49:38 +00005678 // Determine if more than 1 of the words in each of the low and high quadwords
5679 // of the result come from the same quadword of one of the two inputs. Undef
5680 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005681 unsigned LoQuad[] = { 0, 0, 0, 0 };
5682 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005683 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005685 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005686 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687 MaskVals.push_back(EltIdx);
5688 if (EltIdx < 0) {
5689 ++Quad[0];
5690 ++Quad[1];
5691 ++Quad[2];
5692 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005693 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 }
5695 ++Quad[EltIdx / 4];
5696 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005697 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005698
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005700 unsigned MaxQuad = 1;
5701 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 if (LoQuad[i] > MaxQuad) {
5703 BestLoQuad = i;
5704 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005705 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005706 }
5707
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005709 MaxQuad = 1;
5710 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 if (HiQuad[i] > MaxQuad) {
5712 BestHiQuad = i;
5713 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005714 }
5715 }
5716
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005718 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005719 // single pshufb instruction is necessary. If There are more than 2 input
5720 // quads, disable the next transformation since it does not help SSSE3.
5721 bool V1Used = InputQuads[0] || InputQuads[1];
5722 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005723 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005725 BestLoQuad = InputQuads[0] ? 0 : 1;
5726 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 }
5728 if (InputQuads.count() > 2) {
5729 BestLoQuad = -1;
5730 BestHiQuad = -1;
5731 }
5732 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005733
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5735 // the shuffle mask. If a quad is scored as -1, that means that it contains
5736 // words from all 4 input quadwords.
5737 SDValue NewV;
5738 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005739 int MaskV[] = {
5740 BestLoQuad < 0 ? 0 : BestLoQuad,
5741 BestHiQuad < 0 ? 1 : BestHiQuad
5742 };
Eric Christopherfd179292009-08-27 18:07:15 +00005743 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005744 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5745 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5746 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005747
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5749 // source words for the shuffle, to aid later transformations.
5750 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005751 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005752 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005754 if (idx != (int)i)
5755 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005757 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 AllWordsInNewV = false;
5759 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005760 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005761
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5763 if (AllWordsInNewV) {
5764 for (int i = 0; i != 8; ++i) {
5765 int idx = MaskVals[i];
5766 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005767 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005768 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 if ((idx != i) && idx < 4)
5770 pshufhw = false;
5771 if ((idx != i) && idx > 3)
5772 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005773 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 V1 = NewV;
5775 V2Used = false;
5776 BestLoQuad = 0;
5777 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005778 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005779
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5781 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005782 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005783 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5784 unsigned TargetMask = 0;
5785 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005787 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5788 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5789 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005790 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005791 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005792 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005793 }
Eric Christopherfd179292009-08-27 18:07:15 +00005794
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 // If we have SSSE3, and all words of the result are from 1 input vector,
5796 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5797 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005798 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005800
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005802 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 // mask, and elements that come from V1 in the V2 mask, so that the two
5804 // results can be OR'd together.
5805 bool TwoInputs = V1Used && V2Used;
5806 for (unsigned i = 0; i != 8; ++i) {
5807 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005808 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5809 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5810 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5811 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005814 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005815 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005818 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005819
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 // Calculate the shuffle mask for the second input, shuffle it, and
5821 // OR it with the first shuffled input.
5822 pshufbMask.clear();
5823 for (unsigned i = 0; i != 8; ++i) {
5824 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005825 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5826 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5827 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5828 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005830 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005831 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005832 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 MVT::v16i8, &pshufbMask[0], 16));
5834 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005835 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 }
5837
5838 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5839 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005840 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005842 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 for (int i = 0; i != 4; ++i) {
5844 int idx = MaskVals[i];
5845 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 InOrder.set(i);
5847 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005848 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005850 }
5851 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005852 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005853 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005854
Craig Topperdd637ae2012-02-19 05:41:45 +00005855 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5856 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005857 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005858 NewV.getOperand(0),
5859 getShufflePSHUFLWImmediate(SVOp), DAG);
5860 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 }
Eric Christopherfd179292009-08-27 18:07:15 +00005862
Nate Begemanb9a47b82009-02-23 08:49:38 +00005863 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5864 // and update MaskVals with the new element order.
5865 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005866 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005867 for (unsigned i = 4; i != 8; ++i) {
5868 int idx = MaskVals[i];
5869 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 InOrder.set(i);
5871 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005872 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 }
5875 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005876 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005877 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005878
Craig Topperdd637ae2012-02-19 05:41:45 +00005879 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5880 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005881 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005882 NewV.getOperand(0),
5883 getShufflePSHUFHWImmediate(SVOp), DAG);
5884 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 }
Eric Christopherfd179292009-08-27 18:07:15 +00005886
Nate Begemanb9a47b82009-02-23 08:49:38 +00005887 // In case BestHi & BestLo were both -1, which means each quadword has a word
5888 // from each of the four input quadwords, calculate the InOrder bitvector now
5889 // before falling through to the insert/extract cleanup.
5890 if (BestLoQuad == -1 && BestHiQuad == -1) {
5891 NewV = V1;
5892 for (int i = 0; i != 8; ++i)
5893 if (MaskVals[i] < 0 || MaskVals[i] == i)
5894 InOrder.set(i);
5895 }
Eric Christopherfd179292009-08-27 18:07:15 +00005896
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 // The other elements are put in the right place using pextrw and pinsrw.
5898 for (unsigned i = 0; i != 8; ++i) {
5899 if (InOrder[i])
5900 continue;
5901 int EltIdx = MaskVals[i];
5902 if (EltIdx < 0)
5903 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005904 SDValue ExtOp = (EltIdx < 8) ?
5905 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5906 DAG.getIntPtrConstant(EltIdx)) :
5907 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005909 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005910 DAG.getIntPtrConstant(i));
5911 }
5912 return NewV;
5913}
5914
5915// v16i8 shuffles - Prefer shuffles in the following order:
5916// 1. [ssse3] 1 x pshufb
5917// 2. [ssse3] 2 x pshufb + 1 x por
5918// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5919static
Nate Begeman9008ca62009-04-27 18:41:29 +00005920SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005921 SelectionDAG &DAG,
5922 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005923 SDValue V1 = SVOp->getOperand(0);
5924 SDValue V2 = SVOp->getOperand(1);
5925 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005926 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005927
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005929 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005933 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005934 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005935
Nate Begemanb9a47b82009-02-23 08:49:38 +00005936 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005937 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 //
5939 // Otherwise, we have elements from both input vectors, and must zero out
5940 // elements that come from V2 in the first mask, and V1 in the second mask
5941 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 for (unsigned i = 0; i != 16; ++i) {
5943 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005944 if (EltIdx < 0 || EltIdx >= 16)
5945 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005946 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005947 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005948 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005949 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005950 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005951
5952 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5953 // the 2nd operand if it's undefined or zero.
5954 if (V2.getOpcode() == ISD::UNDEF ||
5955 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005957
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 // Calculate the shuffle mask for the second input, shuffle it, and
5959 // OR it with the first shuffled input.
5960 pshufbMask.clear();
5961 for (unsigned i = 0; i != 16; ++i) {
5962 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005963 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005964 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005965 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005966 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005967 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005968 MVT::v16i8, &pshufbMask[0], 16));
5969 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 }
Eric Christopherfd179292009-08-27 18:07:15 +00005971
Nate Begemanb9a47b82009-02-23 08:49:38 +00005972 // No SSSE3 - Calculate in place words and then fix all out of place words
5973 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5974 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005975 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5976 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005977 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 for (int i = 0; i != 8; ++i) {
5979 int Elt0 = MaskVals[i*2];
5980 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005981
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 // This word of the result is all undef, skip it.
5983 if (Elt0 < 0 && Elt1 < 0)
5984 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005985
Nate Begemanb9a47b82009-02-23 08:49:38 +00005986 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005987 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005988 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005989
Nate Begemanb9a47b82009-02-23 08:49:38 +00005990 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5991 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5992 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005993
5994 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5995 // using a single extract together, load it and store it.
5996 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005997 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005998 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005999 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006000 DAG.getIntPtrConstant(i));
6001 continue;
6002 }
6003
Nate Begemanb9a47b82009-02-23 08:49:38 +00006004 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006005 // source byte is not also odd, shift the extracted word left 8 bits
6006 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006007 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006009 DAG.getIntPtrConstant(Elt1 / 2));
6010 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006012 DAG.getConstant(8,
6013 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006014 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006015 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6016 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006017 }
6018 // If Elt0 is defined, extract it from the appropriate source. If the
6019 // source byte is not also even, shift the extracted word right 8 bits. If
6020 // Elt1 was also defined, OR the extracted values together before
6021 // inserting them in the result.
6022 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6025 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006026 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006027 DAG.getConstant(8,
6028 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006029 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006030 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6031 DAG.getConstant(0x00FF, MVT::i16));
6032 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006033 : InsElt0;
6034 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006036 DAG.getIntPtrConstant(i));
6037 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006038 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006039}
6040
Elena Demikhovsky41789462012-09-06 12:42:01 +00006041// v32i8 shuffles - Translate to VPSHUFB if possible.
6042static
6043SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006044 const X86Subtarget *Subtarget,
6045 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006046 EVT VT = SVOp->getValueType(0);
6047 SDValue V1 = SVOp->getOperand(0);
6048 SDValue V2 = SVOp->getOperand(1);
6049 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006050 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006051
6052 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006053 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6054 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006055
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006056 // VPSHUFB may be generated if
6057 // (1) one of input vector is undefined or zeroinitializer.
6058 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6059 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006060 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006061 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006062 return SDValue();
6063
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006064 if (V1IsAllZero && !V2IsAllZero) {
6065 CommuteVectorShuffleMask(MaskVals, 32);
6066 V1 = V2;
6067 }
6068 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006069 for (unsigned i = 0; i != 32; i++) {
6070 int EltIdx = MaskVals[i];
6071 if (EltIdx < 0 || EltIdx >= 32)
6072 EltIdx = 0x80;
6073 else {
6074 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6075 // Cross lane is not allowed.
6076 return SDValue();
6077 EltIdx &= 0xf;
6078 }
6079 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6080 }
6081 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6082 DAG.getNode(ISD::BUILD_VECTOR, dl,
6083 MVT::v32i8, &pshufbMask[0], 32));
6084}
6085
Evan Cheng7a831ce2007-12-15 03:00:47 +00006086/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006087/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006088/// done when every pair / quad of shuffle mask elements point to elements in
6089/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006090/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006091static
Nate Begeman9008ca62009-04-27 18:41:29 +00006092SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006093 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006094 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006095 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006096 MVT NewVT;
6097 unsigned Scale;
6098 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006099 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006100 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6101 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6102 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6103 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6104 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6105 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006106 }
6107
Nate Begeman9008ca62009-04-27 18:41:29 +00006108 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006109 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006111 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006112 int EltIdx = SVOp->getMaskElt(i+j);
6113 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006114 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006115 if (StartIdx < 0)
6116 StartIdx = (EltIdx / Scale);
6117 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006118 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006119 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006120 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006121 }
6122
Craig Topper11ac1f82012-05-04 04:08:44 +00006123 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6124 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006125 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006126}
6127
Evan Chengd880b972008-05-09 21:53:03 +00006128/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006129///
Owen Andersone50ed302009-08-10 22:56:29 +00006130static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 SDValue SrcOp, SelectionDAG &DAG,
6132 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006133 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006134 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006135 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006136 LD = dyn_cast<LoadSDNode>(SrcOp);
6137 if (!LD) {
6138 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6139 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006140 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006141 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006142 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006143 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006144 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006145 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006146 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006147 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006148 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6149 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6150 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006151 SrcOp.getOperand(0)
6152 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006153 }
6154 }
6155 }
6156
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006157 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006158 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006159 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006160 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006161}
6162
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006163/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6164/// which could not be matched by any known target speficic shuffle
6165static SDValue
6166LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006167
6168 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6169 if (NewOp.getNode())
6170 return NewOp;
6171
Craig Topper8f35c132012-01-20 09:29:03 +00006172 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006173
Craig Topper8f35c132012-01-20 09:29:03 +00006174 unsigned NumElems = VT.getVectorNumElements();
6175 unsigned NumLaneElems = NumElems / 2;
6176
Craig Topper8f35c132012-01-20 09:29:03 +00006177 DebugLoc dl = SVOp->getDebugLoc();
6178 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006179 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006180 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006181
Craig Topper9a2b6e12012-04-06 07:45:23 +00006182 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006183 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006184 // Build a shuffle mask for the output, discovering on the fly which
6185 // input vectors to use as shuffle operands (recorded in InputUsed).
6186 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006187 // out with UseBuildVector set.
6188 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006189 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006190 unsigned LaneStart = l * NumLaneElems;
6191 for (unsigned i = 0; i != NumLaneElems; ++i) {
6192 // The mask element. This indexes into the input.
6193 int Idx = SVOp->getMaskElt(i+LaneStart);
6194 if (Idx < 0) {
6195 // the mask element does not index into any input vector.
6196 Mask.push_back(-1);
6197 continue;
6198 }
Craig Topper8f35c132012-01-20 09:29:03 +00006199
Craig Topper9a2b6e12012-04-06 07:45:23 +00006200 // The input vector this mask element indexes into.
6201 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006202
Craig Topper9a2b6e12012-04-06 07:45:23 +00006203 // Turn the index into an offset from the start of the input vector.
6204 Idx -= Input * NumLaneElems;
6205
6206 // Find or create a shuffle vector operand to hold this input.
6207 unsigned OpNo;
6208 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6209 if (InputUsed[OpNo] == Input)
6210 // This input vector is already an operand.
6211 break;
6212 if (InputUsed[OpNo] < 0) {
6213 // Create a new operand for this input vector.
6214 InputUsed[OpNo] = Input;
6215 break;
6216 }
6217 }
6218
6219 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006220 // More than two input vectors used! Give up on trying to create a
6221 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6222 UseBuildVector = true;
6223 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006224 }
6225
6226 // Add the mask index for the new shuffle vector.
6227 Mask.push_back(Idx + OpNo * NumLaneElems);
6228 }
6229
Craig Topper8ae97ba2012-05-21 06:40:16 +00006230 if (UseBuildVector) {
6231 SmallVector<SDValue, 16> SVOps;
6232 for (unsigned i = 0; i != NumLaneElems; ++i) {
6233 // The mask element. This indexes into the input.
6234 int Idx = SVOp->getMaskElt(i+LaneStart);
6235 if (Idx < 0) {
6236 SVOps.push_back(DAG.getUNDEF(EltVT));
6237 continue;
6238 }
6239
6240 // The input vector this mask element indexes into.
6241 int Input = Idx / NumElems;
6242
6243 // Turn the index into an offset from the start of the input vector.
6244 Idx -= Input * NumElems;
6245
6246 // Extract the vector element by hand.
6247 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6248 SVOp->getOperand(Input),
6249 DAG.getIntPtrConstant(Idx)));
6250 }
6251
6252 // Construct the output using a BUILD_VECTOR.
6253 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6254 SVOps.size());
6255 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006256 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006257 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006258 } else {
6259 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006260 (InputUsed[0] % 2) * NumLaneElems,
6261 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006262 // If only one input was used, use an undefined vector for the other.
6263 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6264 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006265 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006266 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006267 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006268 }
6269
6270 Mask.clear();
6271 }
Craig Topper8f35c132012-01-20 09:29:03 +00006272
6273 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006274 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006275}
6276
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006277/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6278/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006279static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006280LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006281 SDValue V1 = SVOp->getOperand(0);
6282 SDValue V2 = SVOp->getOperand(1);
6283 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006284 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006285
Craig Topper7a9a28b2012-08-12 02:23:29 +00006286 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006287
Benjamin Kramer9c683542012-01-30 15:16:21 +00006288 std::pair<int, int> Locs[4];
6289 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006290 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006291
Evan Chengace3c172008-07-22 21:13:36 +00006292 unsigned NumHi = 0;
6293 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006294 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006295 int Idx = PermMask[i];
6296 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006297 Locs[i] = std::make_pair(-1, -1);
6298 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006299 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6300 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006301 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006302 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006303 NumLo++;
6304 } else {
6305 Locs[i] = std::make_pair(1, NumHi);
6306 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006307 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006308 NumHi++;
6309 }
6310 }
6311 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006312
Evan Chengace3c172008-07-22 21:13:36 +00006313 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006314 // If no more than two elements come from either vector. This can be
6315 // implemented with two shuffles. First shuffle gather the elements.
6316 // The second shuffle, which takes the first shuffle as both of its
6317 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006318 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006319
Benjamin Kramer9c683542012-01-30 15:16:21 +00006320 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006321
Benjamin Kramer9c683542012-01-30 15:16:21 +00006322 for (unsigned i = 0; i != 4; ++i)
6323 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006324 unsigned Idx = (i < 2) ? 0 : 4;
6325 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006326 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006327 }
Evan Chengace3c172008-07-22 21:13:36 +00006328
Nate Begeman9008ca62009-04-27 18:41:29 +00006329 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006330 }
6331
6332 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006333 // Otherwise, we must have three elements from one vector, call it X, and
6334 // one element from the other, call it Y. First, use a shufps to build an
6335 // intermediate vector with the one element from Y and the element from X
6336 // that will be in the same half in the final destination (the indexes don't
6337 // matter). Then, use a shufps to build the final vector, taking the half
6338 // containing the element from Y from the intermediate, and the other half
6339 // from X.
6340 if (NumHi == 3) {
6341 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006342 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006343 std::swap(V1, V2);
6344 }
6345
6346 // Find the element from V2.
6347 unsigned HiIndex;
6348 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006349 int Val = PermMask[HiIndex];
6350 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006351 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006352 if (Val >= 4)
6353 break;
6354 }
6355
Nate Begeman9008ca62009-04-27 18:41:29 +00006356 Mask1[0] = PermMask[HiIndex];
6357 Mask1[1] = -1;
6358 Mask1[2] = PermMask[HiIndex^1];
6359 Mask1[3] = -1;
6360 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006361
6362 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006363 Mask1[0] = PermMask[0];
6364 Mask1[1] = PermMask[1];
6365 Mask1[2] = HiIndex & 1 ? 6 : 4;
6366 Mask1[3] = HiIndex & 1 ? 4 : 6;
6367 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006368 }
Craig Topper69947b92012-04-23 06:57:04 +00006369
6370 Mask1[0] = HiIndex & 1 ? 2 : 0;
6371 Mask1[1] = HiIndex & 1 ? 0 : 2;
6372 Mask1[2] = PermMask[2];
6373 Mask1[3] = PermMask[3];
6374 if (Mask1[2] >= 0)
6375 Mask1[2] += 4;
6376 if (Mask1[3] >= 0)
6377 Mask1[3] += 4;
6378 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006379 }
6380
6381 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006382 int LoMask[] = { -1, -1, -1, -1 };
6383 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006384
Benjamin Kramer9c683542012-01-30 15:16:21 +00006385 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006386 unsigned MaskIdx = 0;
6387 unsigned LoIdx = 0;
6388 unsigned HiIdx = 2;
6389 for (unsigned i = 0; i != 4; ++i) {
6390 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006391 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006392 MaskIdx = 1;
6393 LoIdx = 0;
6394 HiIdx = 2;
6395 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006396 int Idx = PermMask[i];
6397 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006398 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006399 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006400 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006401 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006402 LoIdx++;
6403 } else {
6404 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006405 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006406 HiIdx++;
6407 }
6408 }
6409
Nate Begeman9008ca62009-04-27 18:41:29 +00006410 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6411 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006412 int MaskOps[] = { -1, -1, -1, -1 };
6413 for (unsigned i = 0; i != 4; ++i)
6414 if (Locs[i].first != -1)
6415 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006416 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006417}
6418
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006419static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006420 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006421 V = V.getOperand(0);
6422 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6423 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006424 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6425 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6426 // BUILD_VECTOR (load), undef
6427 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006428 if (MayFoldLoad(V))
6429 return true;
6430 return false;
6431}
6432
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006433// FIXME: the version above should always be used. Since there's
6434// a bug where several vector shuffles can't be folded because the
6435// DAG is not updated during lowering and a node claims to have two
6436// uses while it only has one, use this version, and let isel match
6437// another instruction if the load really happens to have more than
6438// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006439// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006440static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006441 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006442 V = V.getOperand(0);
6443 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6444 V = V.getOperand(0);
6445 if (ISD::isNormalLoad(V.getNode()))
6446 return true;
6447 return false;
6448}
6449
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006450static
Evan Cheng835580f2010-10-07 20:50:20 +00006451SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6452 EVT VT = Op.getValueType();
6453
6454 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006455 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6456 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006457 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6458 V1, DAG));
6459}
6460
6461static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006462SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006463 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006464 SDValue V1 = Op.getOperand(0);
6465 SDValue V2 = Op.getOperand(1);
6466 EVT VT = Op.getValueType();
6467
6468 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6469
Craig Topper1accb7e2012-01-10 06:54:16 +00006470 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006471 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6472
Evan Cheng0899f5c2011-08-31 02:05:24 +00006473 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6474 return DAG.getNode(ISD::BITCAST, dl, VT,
6475 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6476 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6477 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006478}
6479
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006480static
6481SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6482 SDValue V1 = Op.getOperand(0);
6483 SDValue V2 = Op.getOperand(1);
6484 EVT VT = Op.getValueType();
6485
6486 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6487 "unsupported shuffle type");
6488
6489 if (V2.getOpcode() == ISD::UNDEF)
6490 V2 = V1;
6491
6492 // v4i32 or v4f32
6493 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6494}
6495
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006496static
Craig Topper1accb7e2012-01-10 06:54:16 +00006497SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006498 SDValue V1 = Op.getOperand(0);
6499 SDValue V2 = Op.getOperand(1);
6500 EVT VT = Op.getValueType();
6501 unsigned NumElems = VT.getVectorNumElements();
6502
6503 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6504 // operand of these instructions is only memory, so check if there's a
6505 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6506 // same masks.
6507 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006508
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006509 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006510 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006511 CanFoldLoad = true;
6512
6513 // When V1 is a load, it can be folded later into a store in isel, example:
6514 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6515 // turns into:
6516 // (MOVLPSmr addr:$src1, VR128:$src2)
6517 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006518 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006519 CanFoldLoad = true;
6520
Dan Gohman65fd6562011-11-03 21:49:52 +00006521 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006522 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006523 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006524 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6525
6526 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006527 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006528 if (SVOp->getMaskElt(1) != -1)
6529 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006530 }
6531
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006532 // movl and movlp will both match v2i64, but v2i64 is never matched by
6533 // movl earlier because we make it strict to avoid messing with the movlp load
6534 // folding logic (see the code above getMOVLP call). Match it here then,
6535 // this is horrible, but will stay like this until we move all shuffle
6536 // matching to x86 specific nodes. Note that for the 1st condition all
6537 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006538 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006539 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6540 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006541 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006542 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006543 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006544 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006545
6546 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6547
6548 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006549 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006550 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006551}
6552
Nadav Rotem154819d2012-04-09 07:45:58 +00006553SDValue
6554X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006555 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6556 EVT VT = Op.getValueType();
6557 DebugLoc dl = Op.getDebugLoc();
6558 SDValue V1 = Op.getOperand(0);
6559 SDValue V2 = Op.getOperand(1);
6560
6561 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006562 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006563
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006564 // Handle splat operations
6565 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006566 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006567 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006568
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006569 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006570 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006571 if (Broadcast.getNode())
6572 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006573
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006574 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006575 if ((Size == 128 && NumElem <= 4) ||
6576 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006577 return SDValue();
6578
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006579 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006580 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006581 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006582
6583 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6584 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006585 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6586 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006587 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6588 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006589 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006590 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006591 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006592 // FIXME: Figure out a cleaner way to do this.
6593 // Try to make use of movq to zero out the top part.
6594 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6595 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6596 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006597 EVT NewVT = NewOp.getValueType();
6598 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6599 NewVT, true, false))
6600 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006601 DAG, Subtarget, dl);
6602 }
6603 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6604 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006605 if (NewOp.getNode()) {
6606 EVT NewVT = NewOp.getValueType();
6607 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6608 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6609 DAG, Subtarget, dl);
6610 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006611 }
6612 }
6613 return SDValue();
6614}
6615
Dan Gohman475871a2008-07-27 21:46:04 +00006616SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006617X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006618 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006619 SDValue V1 = Op.getOperand(0);
6620 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006621 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006622 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006623 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006624 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006625 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006626 bool V1IsSplat = false;
6627 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006628 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006629 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006630 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006631 MachineFunction &MF = DAG.getMachineFunction();
6632 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006633
Craig Topper3426a3e2011-11-14 06:46:21 +00006634 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006635
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006636 if (V1IsUndef && V2IsUndef)
6637 return DAG.getUNDEF(VT);
6638
6639 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006640
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006641 // Vector shuffle lowering takes 3 steps:
6642 //
6643 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6644 // narrowing and commutation of operands should be handled.
6645 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6646 // shuffle nodes.
6647 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6648 // so the shuffle can be broken into other shuffles and the legalizer can
6649 // try the lowering again.
6650 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006651 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006652 // be matched during isel, all of them must be converted to a target specific
6653 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006654
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006655 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6656 // narrowing and commutation of operands should be handled. The actual code
6657 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006658 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006659 if (NewOp.getNode())
6660 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006661
Craig Topper5aaffa82012-02-19 02:53:47 +00006662 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6663
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006664 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6665 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006666 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006667 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006668 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006669 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006670
Craig Topperdd637ae2012-02-19 05:41:45 +00006671 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006672 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006673 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006674
Craig Topperdd637ae2012-02-19 05:41:45 +00006675 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006676 return getMOVHighToLow(Op, dl, DAG);
6677
6678 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006679 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006680 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006681 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006682
Craig Topper5aaffa82012-02-19 02:53:47 +00006683 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006684 // The actual implementation will match the mask in the if above and then
6685 // during isel it can match several different instructions, not only pshufd
6686 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006687 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6688 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006689
Craig Topper5aaffa82012-02-19 02:53:47 +00006690 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006691
Craig Topperdbd98a42012-02-07 06:28:42 +00006692 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6693 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6694
Craig Topper1accb7e2012-01-10 06:54:16 +00006695 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006696 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6697
Craig Topperb3982da2011-12-31 23:50:21 +00006698 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006699 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006700 }
Eric Christopherfd179292009-08-27 18:07:15 +00006701
Evan Chengf26ffe92008-05-29 08:22:04 +00006702 // Check if this can be converted into a logical shift.
6703 bool isLeft = false;
6704 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006705 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006706 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006707 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006708 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006709 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006710 EVT EltVT = VT.getVectorElementType();
6711 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006712 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006713 }
Eric Christopherfd179292009-08-27 18:07:15 +00006714
Craig Topper5aaffa82012-02-19 02:53:47 +00006715 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006716 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006717 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006718 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006719 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006720 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6721
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006722 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006723 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6724 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006725 }
Eric Christopherfd179292009-08-27 18:07:15 +00006726
Nate Begeman9008ca62009-04-27 18:41:29 +00006727 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006728 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006729 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006730
Craig Topperdd637ae2012-02-19 05:41:45 +00006731 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006732 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006733
Craig Topperdd637ae2012-02-19 05:41:45 +00006734 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006735 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006736
Craig Topperdd637ae2012-02-19 05:41:45 +00006737 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006738 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006739
Craig Topperdd637ae2012-02-19 05:41:45 +00006740 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006741 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006742
Craig Topperdd637ae2012-02-19 05:41:45 +00006743 if (ShouldXformToMOVHLPS(M, VT) ||
6744 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006745 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006746
Evan Chengf26ffe92008-05-29 08:22:04 +00006747 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006748 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006749 EVT EltVT = VT.getVectorElementType();
6750 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006751 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006752 }
Eric Christopherfd179292009-08-27 18:07:15 +00006753
Evan Cheng9eca5e82006-10-25 21:49:50 +00006754 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006755 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6756 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006757 V1IsSplat = isSplatVector(V1.getNode());
6758 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006759
Chris Lattner8a594482007-11-25 00:24:49 +00006760 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006761 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6762 CommuteVectorShuffleMask(M, NumElems);
6763 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006764 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006765 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006766 }
6767
Craig Topperbeabc6c2011-12-05 06:56:46 +00006768 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006769 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006770 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006771 return V1;
6772 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6773 // the instruction selector will not match, so get a canonical MOVL with
6774 // swapped operands to undo the commute.
6775 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006776 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006777
Craig Topperbeabc6c2011-12-05 06:56:46 +00006778 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006779 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006780
Craig Topperbeabc6c2011-12-05 06:56:46 +00006781 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006782 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006783
Evan Cheng9bbbb982006-10-25 20:48:19 +00006784 if (V2IsSplat) {
6785 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006786 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006787 // new vector_shuffle with the corrected mask.p
6788 SmallVector<int, 8> NewMask(M.begin(), M.end());
6789 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006790 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006791 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006792 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006793 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006794 }
6795
Evan Cheng9eca5e82006-10-25 21:49:50 +00006796 if (Commuted) {
6797 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006798 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006799 CommuteVectorShuffleMask(M, NumElems);
6800 std::swap(V1, V2);
6801 std::swap(V1IsSplat, V2IsSplat);
6802 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006803
Craig Topper39a9e482012-02-11 06:24:48 +00006804 if (isUNPCKLMask(M, VT, HasAVX2))
6805 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006806
Craig Topper39a9e482012-02-11 06:24:48 +00006807 if (isUNPCKHMask(M, VT, HasAVX2))
6808 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006809 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006810
Nate Begeman9008ca62009-04-27 18:41:29 +00006811 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006812 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006813 return CommuteVectorShuffle(SVOp, DAG);
6814
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006815 // The checks below are all present in isShuffleMaskLegal, but they are
6816 // inlined here right now to enable us to directly emit target specific
6817 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006818
Craig Topper0e2037b2012-01-20 05:53:00 +00006819 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006820 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006821 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006822 DAG);
6823
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006824 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6825 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006826 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006827 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006828 }
6829
Craig Toppera9a568a2012-05-02 08:03:44 +00006830 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006831 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006832 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006833 DAG);
6834
Craig Toppera9a568a2012-05-02 08:03:44 +00006835 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006836 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006837 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006838 DAG);
6839
Craig Topper1a7700a2012-01-19 08:19:12 +00006840 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006841 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006842 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006843
Craig Topper94438ba2011-12-16 08:06:31 +00006844 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006845 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006846 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006847 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006848
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006849 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006850 // Generate target specific nodes for 128 or 256-bit shuffles only
6851 // supported in the AVX instruction set.
6852 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006853
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006854 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006855 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006856 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6857
Craig Topper70b883b2011-11-28 10:14:51 +00006858 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006859 if (isVPERMILPMask(M, VT, HasAVX)) {
6860 if (HasAVX2 && VT == MVT::v8i32)
6861 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006862 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006863 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006864 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006865 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006866
Craig Topper70b883b2011-11-28 10:14:51 +00006867 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006868 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006869 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006870 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006871
Craig Topper1842ba02012-04-23 06:38:28 +00006872 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006873 if (BlendOp.getNode())
6874 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006875
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006876 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006877 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006878 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006879 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006880 }
Craig Topper92040742012-04-16 06:43:40 +00006881 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6882 &permclMask[0], 8);
6883 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006884 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006885 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006886 }
Craig Topper095c5282012-04-15 23:48:57 +00006887
Craig Topper8325c112012-04-16 00:41:45 +00006888 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6889 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006890 getShuffleCLImmediate(SVOp), DAG);
6891
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006892
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006893 //===--------------------------------------------------------------------===//
6894 // Since no target specific shuffle was selected for this generic one,
6895 // lower it into other known shuffles. FIXME: this isn't true yet, but
6896 // this is the plan.
6897 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006898
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006899 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6900 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00006901 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006902 if (NewOp.getNode())
6903 return NewOp;
6904 }
6905
6906 if (VT == MVT::v16i8) {
6907 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6908 if (NewOp.getNode())
6909 return NewOp;
6910 }
6911
Elena Demikhovsky41789462012-09-06 12:42:01 +00006912 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00006913 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00006914 if (NewOp.getNode())
6915 return NewOp;
6916 }
6917
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006918 // Handle all 128-bit wide vectors with 4 elements, and match them with
6919 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006920 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006921 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6922
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006923 // Handle general 256-bit shuffles
6924 if (VT.is256BitVector())
6925 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6926
Dan Gohman475871a2008-07-27 21:46:04 +00006927 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006928}
6929
Dan Gohman475871a2008-07-27 21:46:04 +00006930SDValue
6931X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006932 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006933 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006934 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006935
Craig Topper7a9a28b2012-08-12 02:23:29 +00006936 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006937 return SDValue();
6938
Duncan Sands83ec4b62008-06-06 12:08:01 +00006939 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006941 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006943 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006944 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006945 }
6946
6947 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006948 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6949 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6950 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6952 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006953 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006954 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006955 Op.getOperand(0)),
6956 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006958 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006959 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006960 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006961 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006962 }
6963
6964 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006965 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6966 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006967 // result has a single use which is a store or a bitcast to i32. And in
6968 // the case of a store, it's not worth it if the index is a constant 0,
6969 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006970 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006971 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006972 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006973 if ((User->getOpcode() != ISD::STORE ||
6974 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6975 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006976 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006977 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006978 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006979 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006980 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006981 Op.getOperand(0)),
6982 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006983 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006984 }
6985
6986 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006987 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006988 if (isa<ConstantSDNode>(Op.getOperand(1)))
6989 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006990 }
Dan Gohman475871a2008-07-27 21:46:04 +00006991 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006992}
6993
6994
Dan Gohman475871a2008-07-27 21:46:04 +00006995SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006996X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6997 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006998 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006999 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007000
David Greene74a579d2011-02-10 16:57:36 +00007001 SDValue Vec = Op.getOperand(0);
7002 EVT VecVT = Vec.getValueType();
7003
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007004 // If this is a 256-bit vector result, first extract the 128-bit vector and
7005 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007006 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007007 DebugLoc dl = Op.getNode()->getDebugLoc();
7008 unsigned NumElems = VecVT.getVectorNumElements();
7009 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007010 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7011
7012 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007013 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007014
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007015 if (IdxVal >= NumElems/2)
7016 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007017 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007018 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007019 }
7020
Craig Topper7a9a28b2012-08-12 02:23:29 +00007021 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007022
Craig Topperd0a31172012-01-10 06:37:29 +00007023 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007024 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007025 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007026 return Res;
7027 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007028
Owen Andersone50ed302009-08-10 22:56:29 +00007029 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007030 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007031 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007032 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007033 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007034 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007035 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007036 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7037 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007038 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007040 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007041 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007042 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007043 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007044 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007045 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007046 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007047 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007048 }
7049
7050 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007051 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007052 if (Idx == 0)
7053 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007054
Evan Cheng0db9fe62006-04-25 20:13:52 +00007055 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007056 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007057 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007058 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007059 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007060 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007061 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007062 }
7063
7064 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007065 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7066 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7067 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007068 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007069 if (Idx == 0)
7070 return Op;
7071
7072 // UNPCKHPD the element to the lowest double word, then movsd.
7073 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7074 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007075 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007076 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007077 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007078 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007079 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007080 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007081 }
7082
Dan Gohman475871a2008-07-27 21:46:04 +00007083 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007084}
7085
Dan Gohman475871a2008-07-27 21:46:04 +00007086SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007087X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7088 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007089 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007090 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007091 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007092
Dan Gohman475871a2008-07-27 21:46:04 +00007093 SDValue N0 = Op.getOperand(0);
7094 SDValue N1 = Op.getOperand(1);
7095 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007096
Craig Topper7a9a28b2012-08-12 02:23:29 +00007097 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007098 return SDValue();
7099
Dan Gohman8a55ce42009-09-23 21:02:20 +00007100 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007101 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007102 unsigned Opc;
7103 if (VT == MVT::v8i16)
7104 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007105 else if (VT == MVT::v16i8)
7106 Opc = X86ISD::PINSRB;
7107 else
7108 Opc = X86ISD::PINSRB;
7109
Nate Begeman14d12ca2008-02-11 04:19:36 +00007110 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7111 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 if (N1.getValueType() != MVT::i32)
7113 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7114 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007115 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007116 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007117 }
7118
7119 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007120 // Bits [7:6] of the constant are the source select. This will always be
7121 // zero here. The DAG Combiner may combine an extract_elt index into these
7122 // bits. For example (insert (extract, 3), 2) could be matched by putting
7123 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007124 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007125 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007126 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007127 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007128 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007129 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007131 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007132 }
7133
7134 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007135 // PINSR* works with constant index.
7136 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007137 }
Dan Gohman475871a2008-07-27 21:46:04 +00007138 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007139}
7140
Dan Gohman475871a2008-07-27 21:46:04 +00007141SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007142X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007143 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007144 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007145
David Greene6b381262011-02-09 15:32:06 +00007146 DebugLoc dl = Op.getDebugLoc();
7147 SDValue N0 = Op.getOperand(0);
7148 SDValue N1 = Op.getOperand(1);
7149 SDValue N2 = Op.getOperand(2);
7150
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007151 // If this is a 256-bit vector result, first extract the 128-bit vector,
7152 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007153 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007154 if (!isa<ConstantSDNode>(N2))
7155 return SDValue();
7156
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007157 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007158 unsigned NumElems = VT.getVectorNumElements();
7159 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007160 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007161
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007162 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007163 bool Upper = IdxVal >= NumElems/2;
7164 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7165 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007166
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007167 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007168 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007169 }
7170
Craig Topperd0a31172012-01-10 06:37:29 +00007171 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007172 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7173
Dan Gohman8a55ce42009-09-23 21:02:20 +00007174 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007175 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007176
Dan Gohman8a55ce42009-09-23 21:02:20 +00007177 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007178 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7179 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 if (N1.getValueType() != MVT::i32)
7181 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7182 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007183 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007184 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007185 }
Dan Gohman475871a2008-07-27 21:46:04 +00007186 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007187}
7188
Craig Topper55b24052012-09-11 06:15:32 +00007189static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007190 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007191 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007192 EVT OpVT = Op.getValueType();
7193
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007194 // If this is a 256-bit vector result, first insert into a 128-bit
7195 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007196 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007197 // Insert into a 128-bit vector.
7198 EVT VT128 = EVT::getVectorVT(*Context,
7199 OpVT.getVectorElementType(),
7200 OpVT.getVectorNumElements() / 2);
7201
7202 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7203
7204 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007205 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007206 }
7207
Craig Topperd77d2fe2012-04-29 20:22:05 +00007208 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007209 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007210 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007211
Owen Anderson825b72b2009-08-11 20:47:22 +00007212 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007213 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007214 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007215 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007216}
7217
David Greene91585092011-01-26 15:38:49 +00007218// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7219// a simple subregister reference or explicit instructions to grab
7220// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007221static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7222 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007223 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007224 DebugLoc dl = Op.getNode()->getDebugLoc();
7225 SDValue Vec = Op.getNode()->getOperand(0);
7226 SDValue Idx = Op.getNode()->getOperand(1);
7227
Craig Topper7a9a28b2012-08-12 02:23:29 +00007228 if (Op.getNode()->getValueType(0).is128BitVector() &&
7229 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007230 isa<ConstantSDNode>(Idx)) {
7231 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7232 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007233 }
David Greene91585092011-01-26 15:38:49 +00007234 }
7235 return SDValue();
7236}
7237
David Greenecfe33c42011-01-26 19:13:22 +00007238// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7239// simple superregister reference or explicit instructions to insert
7240// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007241static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7242 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007243 if (Subtarget->hasAVX()) {
7244 DebugLoc dl = Op.getNode()->getDebugLoc();
7245 SDValue Vec = Op.getNode()->getOperand(0);
7246 SDValue SubVec = Op.getNode()->getOperand(1);
7247 SDValue Idx = Op.getNode()->getOperand(2);
7248
Craig Topper7a9a28b2012-08-12 02:23:29 +00007249 if (Op.getNode()->getValueType(0).is256BitVector() &&
7250 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007251 isa<ConstantSDNode>(Idx)) {
7252 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7253 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007254 }
7255 }
7256 return SDValue();
7257}
7258
Bill Wendling056292f2008-09-16 21:48:12 +00007259// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7260// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7261// one of the above mentioned nodes. It has to be wrapped because otherwise
7262// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7263// be used to form addressing mode. These wrapped nodes will be selected
7264// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007265SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007266X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007267 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007268
Chris Lattner41621a22009-06-26 19:22:52 +00007269 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7270 // global base reg.
7271 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007272 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007273 CodeModel::Model M = getTargetMachine().getCodeModel();
7274
Chris Lattner4f066492009-07-11 20:29:19 +00007275 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007276 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007277 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007278 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007279 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007280 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007281 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007282
Evan Cheng1606e8e2009-03-13 07:51:59 +00007283 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007284 CP->getAlignment(),
7285 CP->getOffset(), OpFlag);
7286 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007287 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007288 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007289 if (OpFlag) {
7290 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007291 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007292 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007293 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007294 }
7295
7296 return Result;
7297}
7298
Dan Gohmand858e902010-04-17 15:26:15 +00007299SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007300 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007301
Chris Lattner18c59872009-06-27 04:16:01 +00007302 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7303 // global base reg.
7304 unsigned char OpFlag = 0;
7305 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007306 CodeModel::Model M = getTargetMachine().getCodeModel();
7307
Chris Lattner4f066492009-07-11 20:29:19 +00007308 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007309 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007310 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007311 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007312 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007313 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007314 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007315
Chris Lattner18c59872009-06-27 04:16:01 +00007316 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7317 OpFlag);
7318 DebugLoc DL = JT->getDebugLoc();
7319 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007320
Chris Lattner18c59872009-06-27 04:16:01 +00007321 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007322 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007323 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7324 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007325 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007326 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007327
Chris Lattner18c59872009-06-27 04:16:01 +00007328 return Result;
7329}
7330
7331SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007332X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007333 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007334
Chris Lattner18c59872009-06-27 04:16:01 +00007335 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7336 // global base reg.
7337 unsigned char OpFlag = 0;
7338 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007339 CodeModel::Model M = getTargetMachine().getCodeModel();
7340
Chris Lattner4f066492009-07-11 20:29:19 +00007341 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007342 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7343 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7344 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007345 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007346 } else if (Subtarget->isPICStyleGOT()) {
7347 OpFlag = X86II::MO_GOT;
7348 } else if (Subtarget->isPICStyleStubPIC()) {
7349 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7350 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7351 OpFlag = X86II::MO_DARWIN_NONLAZY;
7352 }
Eric Christopherfd179292009-08-27 18:07:15 +00007353
Chris Lattner18c59872009-06-27 04:16:01 +00007354 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007355
Chris Lattner18c59872009-06-27 04:16:01 +00007356 DebugLoc DL = Op.getDebugLoc();
7357 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007358
7359
Chris Lattner18c59872009-06-27 04:16:01 +00007360 // With PIC, the address is actually $g + Offset.
7361 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007362 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007363 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7364 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007365 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007366 Result);
7367 }
Eric Christopherfd179292009-08-27 18:07:15 +00007368
Eli Friedman586272d2011-08-11 01:48:05 +00007369 // For symbols that require a load from a stub to get the address, emit the
7370 // load.
7371 if (isGlobalStubReference(OpFlag))
7372 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007373 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007374
Chris Lattner18c59872009-06-27 04:16:01 +00007375 return Result;
7376}
7377
Dan Gohman475871a2008-07-27 21:46:04 +00007378SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007379X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007380 // Create the TargetBlockAddressAddress node.
7381 unsigned char OpFlags =
7382 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007383 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007384 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007385 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007386 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007387 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7388 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007389
Dan Gohmanf705adb2009-10-30 01:28:02 +00007390 if (Subtarget->isPICStyleRIPRel() &&
7391 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007392 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7393 else
7394 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007395
Dan Gohman29cbade2009-11-20 23:18:13 +00007396 // With PIC, the address is actually $g + Offset.
7397 if (isGlobalRelativeToPICBase(OpFlags)) {
7398 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7399 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7400 Result);
7401 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007402
7403 return Result;
7404}
7405
7406SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007407X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007408 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007409 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007410 // Create the TargetGlobalAddress node, folding in the constant
7411 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007412 unsigned char OpFlags =
7413 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007414 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007415 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007416 if (OpFlags == X86II::MO_NO_FLAG &&
7417 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007418 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007419 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007420 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007421 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007422 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007423 }
Eric Christopherfd179292009-08-27 18:07:15 +00007424
Chris Lattner4f066492009-07-11 20:29:19 +00007425 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007426 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007427 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7428 else
7429 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007430
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007431 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007432 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007433 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7434 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007435 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007437
Chris Lattner36c25012009-07-10 07:34:39 +00007438 // For globals that require a load from a stub to get the address, emit the
7439 // load.
7440 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007441 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007442 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007443
Dan Gohman6520e202008-10-18 02:06:02 +00007444 // If there was a non-zero offset that we didn't fold, create an explicit
7445 // addition for it.
7446 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007447 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007448 DAG.getConstant(Offset, getPointerTy()));
7449
Evan Cheng0db9fe62006-04-25 20:13:52 +00007450 return Result;
7451}
7452
Evan Chengda43bcf2008-09-24 00:05:32 +00007453SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007454X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007455 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007456 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007457 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007458}
7459
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007460static SDValue
7461GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007462 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007463 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007464 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007465 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007466 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007467 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007468 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007469 GA->getOffset(),
7470 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007471
7472 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7473 : X86ISD::TLSADDR;
7474
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007475 if (InFlag) {
7476 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007477 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007478 } else {
7479 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007480 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007481 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007482
7483 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007484 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007485
Rafael Espindola15f1b662009-04-24 12:59:40 +00007486 SDValue Flag = Chain.getValue(1);
7487 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007488}
7489
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007490// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007491static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007492LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007493 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007494 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007495 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7496 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007497 DAG.getNode(X86ISD::GlobalBaseReg,
7498 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007499 InFlag = Chain.getValue(1);
7500
Chris Lattnerb903bed2009-06-26 21:20:29 +00007501 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007502}
7503
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007504// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007505static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007506LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007507 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007508 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7509 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007510}
7511
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007512static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7513 SelectionDAG &DAG,
7514 const EVT PtrVT,
7515 bool is64Bit) {
7516 DebugLoc dl = GA->getDebugLoc();
7517
7518 // Get the start address of the TLS block for this module.
7519 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7520 .getInfo<X86MachineFunctionInfo>();
7521 MFI->incNumLocalDynamicTLSAccesses();
7522
7523 SDValue Base;
7524 if (is64Bit) {
7525 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7526 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7527 } else {
7528 SDValue InFlag;
7529 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7530 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7531 InFlag = Chain.getValue(1);
7532 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7533 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7534 }
7535
7536 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7537 // of Base.
7538
7539 // Build x@dtpoff.
7540 unsigned char OperandFlags = X86II::MO_DTPOFF;
7541 unsigned WrapperKind = X86ISD::Wrapper;
7542 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7543 GA->getValueType(0),
7544 GA->getOffset(), OperandFlags);
7545 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7546
7547 // Add x@dtpoff with the base.
7548 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7549}
7550
Hans Wennborg228756c2012-05-11 10:11:01 +00007551// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007552static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007553 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007554 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007555 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007556
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007557 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7558 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7559 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007560
Michael J. Spencerec38de22010-10-10 22:04:20 +00007561 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007562 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007563 MachinePointerInfo(Ptr),
7564 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007565
Chris Lattnerb903bed2009-06-26 21:20:29 +00007566 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007567 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7568 // initialexec.
7569 unsigned WrapperKind = X86ISD::Wrapper;
7570 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007571 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007572 } else if (model == TLSModel::InitialExec) {
7573 if (is64Bit) {
7574 OperandFlags = X86II::MO_GOTTPOFF;
7575 WrapperKind = X86ISD::WrapperRIP;
7576 } else {
7577 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7578 }
Chris Lattner18c59872009-06-27 04:16:01 +00007579 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007580 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007581 }
Eric Christopherfd179292009-08-27 18:07:15 +00007582
Hans Wennborg228756c2012-05-11 10:11:01 +00007583 // emit "addl x@ntpoff,%eax" (local exec)
7584 // or "addl x@indntpoff,%eax" (initial exec)
7585 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007586 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007587 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007588 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007589 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007590
Hans Wennborg228756c2012-05-11 10:11:01 +00007591 if (model == TLSModel::InitialExec) {
7592 if (isPIC && !is64Bit) {
7593 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7594 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7595 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007596 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007597
7598 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7599 MachinePointerInfo::getGOT(), false, false, false,
7600 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007601 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007602
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007603 // The address of the thread local variable is the add of the thread
7604 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007605 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007606}
7607
Dan Gohman475871a2008-07-27 21:46:04 +00007608SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007609X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007610
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007611 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007612 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007613
Eric Christopher30ef0e52010-06-03 04:07:48 +00007614 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007615 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007616
Eric Christopher30ef0e52010-06-03 04:07:48 +00007617 switch (model) {
7618 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007619 if (Subtarget->is64Bit())
7620 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7621 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007622 case TLSModel::LocalDynamic:
7623 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7624 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007625 case TLSModel::InitialExec:
7626 case TLSModel::LocalExec:
7627 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007628 Subtarget->is64Bit(),
7629 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007630 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007631 llvm_unreachable("Unknown TLS model.");
7632 }
7633
7634 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007635 // Darwin only has one model of TLS. Lower to that.
7636 unsigned char OpFlag = 0;
7637 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7638 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007639
Eric Christopher30ef0e52010-06-03 04:07:48 +00007640 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7641 // global base reg.
7642 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7643 !Subtarget->is64Bit();
7644 if (PIC32)
7645 OpFlag = X86II::MO_TLVP_PIC_BASE;
7646 else
7647 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007648 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007649 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007650 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007651 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007652 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007653
Eric Christopher30ef0e52010-06-03 04:07:48 +00007654 // With PIC32, the address is actually $g + Offset.
7655 if (PIC32)
7656 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7657 DAG.getNode(X86ISD::GlobalBaseReg,
7658 DebugLoc(), getPointerTy()),
7659 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007660
Eric Christopher30ef0e52010-06-03 04:07:48 +00007661 // Lowering the machine isd will make sure everything is in the right
7662 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007663 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007664 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007665 SDValue Args[] = { Chain, Offset };
7666 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007667
Eric Christopher30ef0e52010-06-03 04:07:48 +00007668 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7669 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7670 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007671
Eric Christopher30ef0e52010-06-03 04:07:48 +00007672 // And our return value (tls address) is in the standard call return value
7673 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007674 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007675 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7676 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007677 }
7678
7679 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007680 // Just use the implicit TLS architecture
7681 // Need to generate someting similar to:
7682 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7683 // ; from TEB
7684 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7685 // mov rcx, qword [rdx+rcx*8]
7686 // mov eax, .tls$:tlsvar
7687 // [rax+rcx] contains the address
7688 // Windows 64bit: gs:0x58
7689 // Windows 32bit: fs:__tls_array
7690
7691 // If GV is an alias then use the aliasee for determining
7692 // thread-localness.
7693 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7694 GV = GA->resolveAliasedGlobal(false);
7695 DebugLoc dl = GA->getDebugLoc();
7696 SDValue Chain = DAG.getEntryNode();
7697
7698 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7699 // %gs:0x58 (64-bit).
7700 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7701 ? Type::getInt8PtrTy(*DAG.getContext(),
7702 256)
7703 : Type::getInt32PtrTy(*DAG.getContext(),
7704 257));
7705
7706 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7707 Subtarget->is64Bit()
7708 ? DAG.getIntPtrConstant(0x58)
7709 : DAG.getExternalSymbol("_tls_array",
7710 getPointerTy()),
7711 MachinePointerInfo(Ptr),
7712 false, false, false, 0);
7713
7714 // Load the _tls_index variable
7715 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7716 if (Subtarget->is64Bit())
7717 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7718 IDX, MachinePointerInfo(), MVT::i32,
7719 false, false, 0);
7720 else
7721 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7722 false, false, false, 0);
7723
7724 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007725 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007726 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7727
7728 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7729 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7730 false, false, false, 0);
7731
7732 // Get the offset of start of .tls section
7733 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7734 GA->getValueType(0),
7735 GA->getOffset(), X86II::MO_SECREL);
7736 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7737
7738 // The address of the thread local variable is the add of the thread
7739 // pointer with the offset of the variable.
7740 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007741 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007742
David Blaikie4d6ccb52012-01-20 21:51:11 +00007743 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007744}
7745
Evan Cheng0db9fe62006-04-25 20:13:52 +00007746
Chad Rosierb90d2a92012-01-03 23:19:12 +00007747/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7748/// and take a 2 x i32 value to shift plus a shift amount.
7749SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007750 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007751 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007752 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007753 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007754 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007755 SDValue ShOpLo = Op.getOperand(0);
7756 SDValue ShOpHi = Op.getOperand(1);
7757 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007758 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007759 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007760 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007761
Dan Gohman475871a2008-07-27 21:46:04 +00007762 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007763 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007764 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7765 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007766 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007767 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7768 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007769 }
Evan Chenge3413162006-01-09 18:33:28 +00007770
Owen Anderson825b72b2009-08-11 20:47:22 +00007771 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7772 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007773 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007774 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007775
Dan Gohman475871a2008-07-27 21:46:04 +00007776 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007777 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007778 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7779 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007780
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007781 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007782 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7783 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007784 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007785 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7786 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007787 }
7788
Dan Gohman475871a2008-07-27 21:46:04 +00007789 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007790 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007791}
Evan Chenga3195e82006-01-12 22:54:21 +00007792
Dan Gohmand858e902010-04-17 15:26:15 +00007793SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7794 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007795 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007796
Dale Johannesen0488fb62010-09-30 23:57:10 +00007797 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007798 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007799
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007801 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007802
Eli Friedman36df4992009-05-27 00:47:34 +00007803 // These are really Legal; return the operand so the caller accepts it as
7804 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007805 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007806 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007807 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007808 Subtarget->is64Bit()) {
7809 return Op;
7810 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007811
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007812 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007813 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007814 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007815 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007816 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007817 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007818 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007819 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007820 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007821 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7822}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007823
Owen Andersone50ed302009-08-10 22:56:29 +00007824SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007825 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007826 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007827 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007828 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007829 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007830 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007831 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007832 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007833 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007834 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007835
Chris Lattner492a43e2010-09-22 01:28:21 +00007836 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007837
Stuart Hastings84be9582011-06-02 15:57:11 +00007838 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7839 MachineMemOperand *MMO;
7840 if (FI) {
7841 int SSFI = FI->getIndex();
7842 MMO =
7843 DAG.getMachineFunction()
7844 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7845 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7846 } else {
7847 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7848 StackSlot = StackSlot.getOperand(1);
7849 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007850 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007851 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7852 X86ISD::FILD, DL,
7853 Tys, Ops, array_lengthof(Ops),
7854 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007855
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007856 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007857 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007858 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007859
7860 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7861 // shouldn't be necessary except that RFP cannot be live across
7862 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007863 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007864 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7865 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007866 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007867 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007868 SDValue Ops[] = {
7869 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7870 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007871 MachineMemOperand *MMO =
7872 DAG.getMachineFunction()
7873 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007874 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007875
Chris Lattner492a43e2010-09-22 01:28:21 +00007876 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7877 Ops, array_lengthof(Ops),
7878 Op.getValueType(), MMO);
7879 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007880 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007881 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007882 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007883
Evan Cheng0db9fe62006-04-25 20:13:52 +00007884 return Result;
7885}
7886
Bill Wendling8b8a6362009-01-17 03:56:04 +00007887// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007888SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7889 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007890 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007891 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007892 movq %rax, %xmm0
7893 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7894 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7895 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007896 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007897 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007898 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007899 addpd %xmm1, %xmm0
7900 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007901 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007902
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007903 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007904 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007905
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007906 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007907 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7908 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007909 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007910
Chris Lattner97484792012-01-25 09:56:22 +00007911 SmallVector<Constant*,2> CV1;
7912 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007913 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007914 CV1.push_back(
7915 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7916 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007917 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007918
Bill Wendling397ae212012-01-05 02:13:20 +00007919 // Load the 64-bit value into an XMM register.
7920 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7921 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007923 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007924 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007925 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7926 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7927 CLod0);
7928
Owen Anderson825b72b2009-08-11 20:47:22 +00007929 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007930 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007931 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007932 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007933 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007934 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007935
Craig Topperd0a31172012-01-10 06:37:29 +00007936 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007937 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7938 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7939 } else {
7940 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7941 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7942 S2F, 0x4E, DAG);
7943 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7944 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7945 Sub);
7946 }
7947
7948 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007949 DAG.getIntPtrConstant(0));
7950}
7951
Bill Wendling8b8a6362009-01-17 03:56:04 +00007952// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007953SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7954 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007955 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007956 // FP constant to bias correct the final result.
7957 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007958 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007959
7960 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007962 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007963
Eli Friedmanf3704762011-08-29 21:15:46 +00007964 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007965 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007966
Owen Anderson825b72b2009-08-11 20:47:22 +00007967 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007968 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007969 DAG.getIntPtrConstant(0));
7970
7971 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007973 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007974 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007976 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007977 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007978 MVT::v2f64, Bias)));
7979 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007980 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007981 DAG.getIntPtrConstant(0));
7982
7983 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007984 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007985
7986 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007987 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007988
Craig Topper69947b92012-04-23 06:57:04 +00007989 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007990 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007991 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007992 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007993 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007994
7995 // Handle final rounding.
7996 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007997}
7998
Dan Gohmand858e902010-04-17 15:26:15 +00007999SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8000 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008001 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008002 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008003
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008004 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008005 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8006 // the optimization here.
8007 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008008 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008009
Owen Andersone50ed302009-08-10 22:56:29 +00008010 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008011 EVT DstVT = Op.getValueType();
8012 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008013 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008014 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008015 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008016 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008017 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008018
8019 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008020 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008021 if (SrcVT == MVT::i32) {
8022 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8023 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8024 getPointerTy(), StackSlot, WordOff);
8025 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008026 StackSlot, MachinePointerInfo(),
8027 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008028 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008029 OffsetSlot, MachinePointerInfo(),
8030 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008031 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8032 return Fild;
8033 }
8034
8035 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8036 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008037 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008038 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008039 // For i64 source, we need to add the appropriate power of 2 if the input
8040 // was negative. This is the same as the optimization in
8041 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8042 // we must be careful to do the computation in x87 extended precision, not
8043 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008044 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8045 MachineMemOperand *MMO =
8046 DAG.getMachineFunction()
8047 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8048 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008049
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008050 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8051 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008052 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8053 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008054
8055 APInt FF(32, 0x5F800000ULL);
8056
8057 // Check whether the sign bit is set.
8058 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8059 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8060 ISD::SETLT);
8061
8062 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8063 SDValue FudgePtr = DAG.getConstantPool(
8064 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8065 getPointerTy());
8066
8067 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8068 SDValue Zero = DAG.getIntPtrConstant(0);
8069 SDValue Four = DAG.getIntPtrConstant(4);
8070 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8071 Zero, Four);
8072 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8073
8074 // Load the value out, extending it from f32 to f80.
8075 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008076 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008077 FudgePtr, MachinePointerInfo::getConstantPool(),
8078 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008079 // Extend everything to 80 bits to force it to be done on x87.
8080 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8081 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008082}
8083
Dan Gohman475871a2008-07-27 21:46:04 +00008084std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008085FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008086 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008087
Owen Andersone50ed302009-08-10 22:56:29 +00008088 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008089
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008090 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008091 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8092 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008093 }
8094
Owen Anderson825b72b2009-08-11 20:47:22 +00008095 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8096 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008097 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008098
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008099 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008100 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008101 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008102 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008103 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008104 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008105 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008106 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008107
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008108 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8109 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008110 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008111 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008112 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008113 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008114
Evan Cheng0db9fe62006-04-25 20:13:52 +00008115 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008116 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8117 Opc = X86ISD::WIN_FTOL;
8118 else
8119 switch (DstTy.getSimpleVT().SimpleTy) {
8120 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8121 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8122 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8123 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8124 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008125
Dan Gohman475871a2008-07-27 21:46:04 +00008126 SDValue Chain = DAG.getEntryNode();
8127 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008128 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008129 // FIXME This causes a redundant load/store if the SSE-class value is already
8130 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008131 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008132 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008133 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008134 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008135 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008136 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008137 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008138 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008139 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008140
Chris Lattner492a43e2010-09-22 01:28:21 +00008141 MachineMemOperand *MMO =
8142 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8143 MachineMemOperand::MOLoad, MemSize, MemSize);
8144 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8145 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008146 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008147 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008148 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8149 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008150
Chris Lattner07290932010-09-22 01:05:16 +00008151 MachineMemOperand *MMO =
8152 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8153 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008154
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008155 if (Opc != X86ISD::WIN_FTOL) {
8156 // Build the FP_TO_INT*_IN_MEM
8157 SDValue Ops[] = { Chain, Value, StackSlot };
8158 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8159 Ops, 3, DstTy, MMO);
8160 return std::make_pair(FIST, StackSlot);
8161 } else {
8162 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8163 DAG.getVTList(MVT::Other, MVT::Glue),
8164 Chain, Value);
8165 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8166 MVT::i32, ftol.getValue(1));
8167 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8168 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008169 SDValue Ops[] = { eax, edx };
8170 SDValue pair = IsReplace
8171 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8172 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008173 return std::make_pair(pair, SDValue());
8174 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008175}
8176
Dan Gohmand858e902010-04-17 15:26:15 +00008177SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8178 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008179 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008180 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008181
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008182 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8183 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008184 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008185 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8186 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008187
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008188 if (StackSlot.getNode())
8189 // Load the result.
8190 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8191 FIST, StackSlot, MachinePointerInfo(),
8192 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008193
8194 // The node is the result.
8195 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008196}
8197
Dan Gohmand858e902010-04-17 15:26:15 +00008198SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8199 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008200 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8201 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008202 SDValue FIST = Vals.first, StackSlot = Vals.second;
8203 assert(FIST.getNode() && "Unexpected failure");
8204
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008205 if (StackSlot.getNode())
8206 // Load the result.
8207 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8208 FIST, StackSlot, MachinePointerInfo(),
8209 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008210
8211 // The node is the result.
8212 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008213}
8214
Craig Topper43620672012-09-08 07:31:51 +00008215SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008216 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008217 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008218 EVT VT = Op.getValueType();
8219 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008220 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8221 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008222 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008223 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008224 }
Craig Topper43620672012-09-08 07:31:51 +00008225 Constant *C;
8226 if (EltVT == MVT::f64)
8227 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8228 else
8229 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8230 C = ConstantVector::getSplat(NumElts, C);
8231 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8232 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008233 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008234 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008235 false, false, false, Alignment);
8236 if (VT.isVector()) {
8237 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8238 return DAG.getNode(ISD::BITCAST, dl, VT,
8239 DAG.getNode(ISD::AND, dl, ANDVT,
8240 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8241 Op.getOperand(0)),
8242 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8243 }
Dale Johannesenace16102009-02-03 19:33:06 +00008244 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008245}
8246
Dan Gohmand858e902010-04-17 15:26:15 +00008247SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008248 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008249 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008250 EVT VT = Op.getValueType();
8251 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008252 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8253 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008254 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008255 NumElts = VT.getVectorNumElements();
8256 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008257 Constant *C;
8258 if (EltVT == MVT::f64)
8259 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8260 else
8261 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8262 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008263 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8264 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008265 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008266 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008267 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008268 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008269 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008270 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008271 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008272 DAG.getNode(ISD::BITCAST, dl, XORVT,
8273 Op.getOperand(0)),
8274 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008275 }
Craig Topper69947b92012-04-23 06:57:04 +00008276
8277 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008278}
8279
Dan Gohmand858e902010-04-17 15:26:15 +00008280SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008281 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008282 SDValue Op0 = Op.getOperand(0);
8283 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008284 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008285 EVT VT = Op.getValueType();
8286 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008287
8288 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008289 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008290 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008291 SrcVT = VT;
8292 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008293 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008294 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008295 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008296 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008297 }
8298
8299 // At this point the operands and the result should have the same
8300 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008301
Evan Cheng68c47cb2007-01-05 07:55:56 +00008302 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008303 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008304 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008305 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8306 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008307 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008308 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8309 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8310 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8311 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008312 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008313 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008314 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008315 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008316 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008317 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008318 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008319
8320 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008321 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008322 // Op0 is MVT::f32, Op1 is MVT::f64.
8323 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8324 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8325 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008326 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008327 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008328 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008329 }
8330
Evan Cheng73d6cf12007-01-05 21:37:56 +00008331 // Clear first operand sign bit.
8332 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008333 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008334 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8335 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008336 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008337 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8338 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8339 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8340 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008341 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008342 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008343 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008344 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008345 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008346 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008347 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008348
8349 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008350 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008351}
8352
Craig Topper55b24052012-09-11 06:15:32 +00008353static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008354 SDValue N0 = Op.getOperand(0);
8355 DebugLoc dl = Op.getDebugLoc();
8356 EVT VT = Op.getValueType();
8357
8358 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8359 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8360 DAG.getConstant(1, VT));
8361 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8362}
8363
Michael Liaof966e4e2012-09-13 20:24:54 +00008364// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8365//
8366SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8367 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8368
8369 if (!Subtarget->hasSSE41())
8370 return SDValue();
8371
8372 if (!Op->hasOneUse())
8373 return SDValue();
8374
8375 SDNode *N = Op.getNode();
8376 DebugLoc DL = N->getDebugLoc();
8377
8378 SmallVector<SDValue, 8> Opnds;
8379 DenseMap<SDValue, unsigned> VecInMap;
8380 EVT VT = MVT::Other;
8381
8382 // Recognize a special case where a vector is casted into wide integer to
8383 // test all 0s.
8384 Opnds.push_back(N->getOperand(0));
8385 Opnds.push_back(N->getOperand(1));
8386
8387 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8388 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8389 // BFS traverse all OR'd operands.
8390 if (I->getOpcode() == ISD::OR) {
8391 Opnds.push_back(I->getOperand(0));
8392 Opnds.push_back(I->getOperand(1));
8393 // Re-evaluate the number of nodes to be traversed.
8394 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8395 continue;
8396 }
8397
8398 // Quit if a non-EXTRACT_VECTOR_ELT
8399 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8400 return SDValue();
8401
8402 // Quit if without a constant index.
8403 SDValue Idx = I->getOperand(1);
8404 if (!isa<ConstantSDNode>(Idx))
8405 return SDValue();
8406
8407 SDValue ExtractedFromVec = I->getOperand(0);
8408 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8409 if (M == VecInMap.end()) {
8410 VT = ExtractedFromVec.getValueType();
8411 // Quit if not 128/256-bit vector.
8412 if (!VT.is128BitVector() && !VT.is256BitVector())
8413 return SDValue();
8414 // Quit if not the same type.
8415 if (VecInMap.begin() != VecInMap.end() &&
8416 VT != VecInMap.begin()->first.getValueType())
8417 return SDValue();
8418 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8419 }
8420 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8421 }
8422
8423 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008424 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008425
8426 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8427 SmallVector<SDValue, 8> VecIns;
8428
8429 for (DenseMap<SDValue, unsigned>::const_iterator
8430 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8431 // Quit if not all elements are used.
8432 if (I->second != FullMask)
8433 return SDValue();
8434 VecIns.push_back(I->first);
8435 }
8436
8437 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8438
8439 // Cast all vectors into TestVT for PTEST.
8440 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8441 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8442
8443 // If more than one full vectors are evaluated, OR them first before PTEST.
8444 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8445 // Each iteration will OR 2 nodes and append the result until there is only
8446 // 1 node left, i.e. the final OR'd value of all vectors.
8447 SDValue LHS = VecIns[Slot];
8448 SDValue RHS = VecIns[Slot + 1];
8449 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8450 }
8451
8452 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8453 VecIns.back(), VecIns.back());
8454}
8455
Dan Gohman076aee32009-03-04 19:44:21 +00008456/// Emit nodes that will be selected as "test Op0,Op0", or something
8457/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008458SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008459 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008460 DebugLoc dl = Op.getDebugLoc();
8461
Dan Gohman31125812009-03-07 01:58:32 +00008462 // CF and OF aren't always set the way we want. Determine which
8463 // of these we need.
8464 bool NeedCF = false;
8465 bool NeedOF = false;
8466 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008467 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008468 case X86::COND_A: case X86::COND_AE:
8469 case X86::COND_B: case X86::COND_BE:
8470 NeedCF = true;
8471 break;
8472 case X86::COND_G: case X86::COND_GE:
8473 case X86::COND_L: case X86::COND_LE:
8474 case X86::COND_O: case X86::COND_NO:
8475 NeedOF = true;
8476 break;
Dan Gohman31125812009-03-07 01:58:32 +00008477 }
8478
Dan Gohman076aee32009-03-04 19:44:21 +00008479 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008480 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8481 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008482 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8483 // Emit a CMP with 0, which is the TEST pattern.
8484 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8485 DAG.getConstant(0, Op.getValueType()));
8486
8487 unsigned Opcode = 0;
8488 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008489
8490 // Truncate operations may prevent the merge of the SETCC instruction
8491 // and the arithmetic intruction before it. Attempt to truncate the operands
8492 // of the arithmetic instruction and use a reduced bit-width instruction.
8493 bool NeedTruncation = false;
8494 SDValue ArithOp = Op;
8495 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8496 SDValue Arith = Op->getOperand(0);
8497 // Both the trunc and the arithmetic op need to have one user each.
8498 if (Arith->hasOneUse())
8499 switch (Arith.getOpcode()) {
8500 default: break;
8501 case ISD::ADD:
8502 case ISD::SUB:
8503 case ISD::AND:
8504 case ISD::OR:
8505 case ISD::XOR: {
8506 NeedTruncation = true;
8507 ArithOp = Arith;
8508 }
8509 }
8510 }
8511
8512 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8513 // which may be the result of a CAST. We use the variable 'Op', which is the
8514 // non-casted variable when we check for possible users.
8515 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008516 case ISD::ADD:
8517 // Due to an isel shortcoming, be conservative if this add is likely to be
8518 // selected as part of a load-modify-store instruction. When the root node
8519 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8520 // uses of other nodes in the match, such as the ADD in this case. This
8521 // leads to the ADD being left around and reselected, with the result being
8522 // two adds in the output. Alas, even if none our users are stores, that
8523 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8524 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8525 // climbing the DAG back to the root, and it doesn't seem to be worth the
8526 // effort.
8527 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008528 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8529 if (UI->getOpcode() != ISD::CopyToReg &&
8530 UI->getOpcode() != ISD::SETCC &&
8531 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008532 goto default_case;
8533
8534 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008535 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008536 // An add of one will be selected as an INC.
8537 if (C->getAPIntValue() == 1) {
8538 Opcode = X86ISD::INC;
8539 NumOperands = 1;
8540 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008541 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008542
8543 // An add of negative one (subtract of one) will be selected as a DEC.
8544 if (C->getAPIntValue().isAllOnesValue()) {
8545 Opcode = X86ISD::DEC;
8546 NumOperands = 1;
8547 break;
8548 }
Dan Gohman076aee32009-03-04 19:44:21 +00008549 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008550
8551 // Otherwise use a regular EFLAGS-setting add.
8552 Opcode = X86ISD::ADD;
8553 NumOperands = 2;
8554 break;
8555 case ISD::AND: {
8556 // If the primary and result isn't used, don't bother using X86ISD::AND,
8557 // because a TEST instruction will be better.
8558 bool NonFlagUse = false;
8559 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8560 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8561 SDNode *User = *UI;
8562 unsigned UOpNo = UI.getOperandNo();
8563 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8564 // Look pass truncate.
8565 UOpNo = User->use_begin().getOperandNo();
8566 User = *User->use_begin();
8567 }
8568
8569 if (User->getOpcode() != ISD::BRCOND &&
8570 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008571 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008572 NonFlagUse = true;
8573 break;
8574 }
Dan Gohman076aee32009-03-04 19:44:21 +00008575 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008576
8577 if (!NonFlagUse)
8578 break;
8579 }
8580 // FALL THROUGH
8581 case ISD::SUB:
8582 case ISD::OR:
8583 case ISD::XOR:
8584 // Due to the ISEL shortcoming noted above, be conservative if this op is
8585 // likely to be selected as part of a load-modify-store instruction.
8586 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8587 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8588 if (UI->getOpcode() == ISD::STORE)
8589 goto default_case;
8590
8591 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008592 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008593 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008594 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008595 case ISD::XOR: Opcode = X86ISD::XOR; break;
8596 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008597 case ISD::OR: {
8598 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8599 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8600 if (EFLAGS.getNode())
8601 return EFLAGS;
8602 }
8603 Opcode = X86ISD::OR;
8604 break;
8605 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008606 }
8607
8608 NumOperands = 2;
8609 break;
8610 case X86ISD::ADD:
8611 case X86ISD::SUB:
8612 case X86ISD::INC:
8613 case X86ISD::DEC:
8614 case X86ISD::OR:
8615 case X86ISD::XOR:
8616 case X86ISD::AND:
8617 return SDValue(Op.getNode(), 1);
8618 default:
8619 default_case:
8620 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008621 }
8622
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008623 // If we found that truncation is beneficial, perform the truncation and
8624 // update 'Op'.
8625 if (NeedTruncation) {
8626 EVT VT = Op.getValueType();
8627 SDValue WideVal = Op->getOperand(0);
8628 EVT WideVT = WideVal.getValueType();
8629 unsigned ConvertedOp = 0;
8630 // Use a target machine opcode to prevent further DAGCombine
8631 // optimizations that may separate the arithmetic operations
8632 // from the setcc node.
8633 switch (WideVal.getOpcode()) {
8634 default: break;
8635 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8636 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8637 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8638 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8639 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8640 }
8641
8642 if (ConvertedOp) {
8643 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8644 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8645 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8646 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8647 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8648 }
8649 }
8650 }
8651
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008652 if (Opcode == 0)
8653 // Emit a CMP with 0, which is the TEST pattern.
8654 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8655 DAG.getConstant(0, Op.getValueType()));
8656
8657 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8658 SmallVector<SDValue, 4> Ops;
8659 for (unsigned i = 0; i != NumOperands; ++i)
8660 Ops.push_back(Op.getOperand(i));
8661
8662 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8663 DAG.ReplaceAllUsesWith(Op, New);
8664 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008665}
8666
8667/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8668/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008669SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008670 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008671 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8672 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008673 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008674
8675 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008676 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8677 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8678 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8679 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8680 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8681 Op0, Op1);
8682 return SDValue(Sub.getNode(), 1);
8683 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008684 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008685}
8686
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008687/// Convert a comparison if required by the subtarget.
8688SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8689 SelectionDAG &DAG) const {
8690 // If the subtarget does not support the FUCOMI instruction, floating-point
8691 // comparisons have to be converted.
8692 if (Subtarget->hasCMov() ||
8693 Cmp.getOpcode() != X86ISD::CMP ||
8694 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8695 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8696 return Cmp;
8697
8698 // The instruction selector will select an FUCOM instruction instead of
8699 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8700 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8701 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8702 DebugLoc dl = Cmp.getDebugLoc();
8703 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8704 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8705 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8706 DAG.getConstant(8, MVT::i8));
8707 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8708 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8709}
8710
Evan Chengd40d03e2010-01-06 19:38:29 +00008711/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8712/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008713SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8714 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008715 SDValue Op0 = And.getOperand(0);
8716 SDValue Op1 = And.getOperand(1);
8717 if (Op0.getOpcode() == ISD::TRUNCATE)
8718 Op0 = Op0.getOperand(0);
8719 if (Op1.getOpcode() == ISD::TRUNCATE)
8720 Op1 = Op1.getOperand(0);
8721
Evan Chengd40d03e2010-01-06 19:38:29 +00008722 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008723 if (Op1.getOpcode() == ISD::SHL)
8724 std::swap(Op0, Op1);
8725 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008726 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8727 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008728 // If we looked past a truncate, check that it's only truncating away
8729 // known zeros.
8730 unsigned BitWidth = Op0.getValueSizeInBits();
8731 unsigned AndBitWidth = And.getValueSizeInBits();
8732 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008733 APInt Zeros, Ones;
8734 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008735 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8736 return SDValue();
8737 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008738 LHS = Op1;
8739 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008740 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008741 } else if (Op1.getOpcode() == ISD::Constant) {
8742 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008743 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008744 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008745
8746 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008747 LHS = AndLHS.getOperand(0);
8748 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008749 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008750
8751 // Use BT if the immediate can't be encoded in a TEST instruction.
8752 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8753 LHS = AndLHS;
8754 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8755 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008756 }
Evan Cheng0488db92007-09-25 01:57:46 +00008757
Evan Chengd40d03e2010-01-06 19:38:29 +00008758 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008759 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008760 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008761 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008762 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008763 // Also promote i16 to i32 for performance / code size reason.
8764 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008765 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008766 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008767
Evan Chengd40d03e2010-01-06 19:38:29 +00008768 // If the operand types disagree, extend the shift amount to match. Since
8769 // BT ignores high bits (like shifts) we can use anyextend.
8770 if (LHS.getValueType() != RHS.getValueType())
8771 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008772
Evan Chengd40d03e2010-01-06 19:38:29 +00008773 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8774 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8775 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8776 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008777 }
8778
Evan Cheng54de3ea2010-01-05 06:52:31 +00008779 return SDValue();
8780}
8781
Dan Gohmand858e902010-04-17 15:26:15 +00008782SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008783
8784 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8785
Evan Cheng54de3ea2010-01-05 06:52:31 +00008786 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8787 SDValue Op0 = Op.getOperand(0);
8788 SDValue Op1 = Op.getOperand(1);
8789 DebugLoc dl = Op.getDebugLoc();
8790 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8791
8792 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008793 // Lower (X & (1 << N)) == 0 to BT(X, N).
8794 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8795 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008796 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008797 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008798 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008799 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8800 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8801 if (NewSetCC.getNode())
8802 return NewSetCC;
8803 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008804
Chris Lattner481eebc2010-12-19 21:23:48 +00008805 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8806 // these.
8807 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008808 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008809 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8810 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008811
Chris Lattner481eebc2010-12-19 21:23:48 +00008812 // If the input is a setcc, then reuse the input setcc or use a new one with
8813 // the inverted condition.
8814 if (Op0.getOpcode() == X86ISD::SETCC) {
8815 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8816 bool Invert = (CC == ISD::SETNE) ^
8817 cast<ConstantSDNode>(Op1)->isNullValue();
8818 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008819
Evan Cheng2c755ba2010-02-27 07:36:59 +00008820 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008821 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8822 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8823 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008824 }
8825
Evan Chenge5b51ac2010-04-17 06:13:15 +00008826 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008827 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008828 if (X86CC == X86::COND_INVALID)
8829 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008830
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008831 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008832 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008833 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008834 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008835}
8836
Craig Topper89af15e2011-09-18 08:03:58 +00008837// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008838// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008839static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008840 EVT VT = Op.getValueType();
8841
Craig Topper7a9a28b2012-08-12 02:23:29 +00008842 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008843 "Unsupported value type for operation");
8844
Craig Topper66ddd152012-04-27 22:54:43 +00008845 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008846 DebugLoc dl = Op.getDebugLoc();
8847 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008848
8849 // Extract the LHS vectors
8850 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008851 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8852 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008853
8854 // Extract the RHS vectors
8855 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008856 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8857 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008858
8859 // Issue the operation on the smaller types and concatenate the result back
8860 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8861 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8862 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8863 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8864 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8865}
8866
8867
Dan Gohmand858e902010-04-17 15:26:15 +00008868SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008869 SDValue Cond;
8870 SDValue Op0 = Op.getOperand(0);
8871 SDValue Op1 = Op.getOperand(1);
8872 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008873 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008874 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8875 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008876 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008877
8878 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008879#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008880 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008881 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8882#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008883
Craig Topper523908d2012-08-13 02:34:03 +00008884 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008885 bool Swap = false;
8886
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008887 // SSE Condition code mapping:
8888 // 0 - EQ
8889 // 1 - LT
8890 // 2 - LE
8891 // 3 - UNORD
8892 // 4 - NEQ
8893 // 5 - NLT
8894 // 6 - NLE
8895 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008896 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008897 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008898 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008899 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008900 case ISD::SETOGT:
8901 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008902 case ISD::SETLT:
8903 case ISD::SETOLT: SSECC = 1; break;
8904 case ISD::SETOGE:
8905 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008906 case ISD::SETLE:
8907 case ISD::SETOLE: SSECC = 2; break;
8908 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008909 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008910 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008911 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008912 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008913 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008914 case ISD::SETUGT: SSECC = 6; break;
8915 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008916 case ISD::SETUEQ:
8917 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008918 }
8919 if (Swap)
8920 std::swap(Op0, Op1);
8921
Nate Begemanfb8ead02008-07-25 19:05:58 +00008922 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008923 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008924 unsigned CC0, CC1;
8925 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008926 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008927 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8928 } else {
8929 assert(SetCCOpcode == ISD::SETONE);
8930 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008931 }
Craig Topper523908d2012-08-13 02:34:03 +00008932
8933 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8934 DAG.getConstant(CC0, MVT::i8));
8935 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8936 DAG.getConstant(CC1, MVT::i8));
8937 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008938 }
8939 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008940 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8941 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008943
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008944 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008945 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008946 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008947
Nate Begeman30a0de92008-07-17 16:51:19 +00008948 // We are handling one of the integer comparisons here. Since SSE only has
8949 // GT and EQ comparisons for integer, swapping operands and multiple
8950 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008951 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008952 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008953
Nate Begeman30a0de92008-07-17 16:51:19 +00008954 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008955 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008956 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008957 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008958 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008959 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008960 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008961 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008962 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008963 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008964 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008965 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008966 }
8967 if (Swap)
8968 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008969
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008970 // Check that the operation in question is available (most are plain SSE2,
8971 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008972 if (VT == MVT::v2i64) {
8973 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8974 return SDValue();
8975 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8976 return SDValue();
8977 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008978
Nate Begeman30a0de92008-07-17 16:51:19 +00008979 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8980 // bits of the inputs before performing those operations.
8981 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008982 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008983 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8984 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008985 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008986 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8987 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008988 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8989 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008991
Dale Johannesenace16102009-02-03 19:33:06 +00008992 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008993
8994 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008995 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008996 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008997
Nate Begeman30a0de92008-07-17 16:51:19 +00008998 return Result;
8999}
Evan Cheng0488db92007-09-25 01:57:46 +00009000
Evan Cheng370e5342008-12-03 08:38:43 +00009001// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009002static bool isX86LogicalCmp(SDValue Op) {
9003 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009004 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9005 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009006 return true;
9007 if (Op.getResNo() == 1 &&
9008 (Opc == X86ISD::ADD ||
9009 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009010 Opc == X86ISD::ADC ||
9011 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009012 Opc == X86ISD::SMUL ||
9013 Opc == X86ISD::UMUL ||
9014 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009015 Opc == X86ISD::DEC ||
9016 Opc == X86ISD::OR ||
9017 Opc == X86ISD::XOR ||
9018 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009019 return true;
9020
Chris Lattner9637d5b2010-12-05 07:49:54 +00009021 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9022 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009023
Dan Gohman076aee32009-03-04 19:44:21 +00009024 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009025}
9026
Chris Lattnera2b56002010-12-05 01:23:24 +00009027static bool isZero(SDValue V) {
9028 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9029 return C && C->isNullValue();
9030}
9031
Chris Lattner96908b12010-12-05 02:00:51 +00009032static bool isAllOnes(SDValue V) {
9033 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9034 return C && C->isAllOnesValue();
9035}
9036
Evan Chengb64dd5f2012-08-07 22:21:00 +00009037static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9038 if (V.getOpcode() != ISD::TRUNCATE)
9039 return false;
9040
9041 SDValue VOp0 = V.getOperand(0);
9042 unsigned InBits = VOp0.getValueSizeInBits();
9043 unsigned Bits = V.getValueSizeInBits();
9044 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9045}
9046
Dan Gohmand858e902010-04-17 15:26:15 +00009047SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009048 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009049 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009050 SDValue Op1 = Op.getOperand(1);
9051 SDValue Op2 = Op.getOperand(2);
9052 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009053 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009054
Dan Gohman1a492952009-10-20 16:22:37 +00009055 if (Cond.getOpcode() == ISD::SETCC) {
9056 SDValue NewCond = LowerSETCC(Cond, DAG);
9057 if (NewCond.getNode())
9058 Cond = NewCond;
9059 }
Evan Cheng734503b2006-09-11 02:19:56 +00009060
Chris Lattnera2b56002010-12-05 01:23:24 +00009061 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009062 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009063 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009064 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009065 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009066 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9067 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009068 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009069
Chris Lattnera2b56002010-12-05 01:23:24 +00009070 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009071
9072 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009073 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9074 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009075
9076 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009077 // Apply further optimizations for special cases
9078 // (select (x != 0), -1, 0) -> neg & sbb
9079 // (select (x == 0), 0, -1) -> neg & sbb
9080 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009081 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009082 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9083 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009084 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9085 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009086 CmpOp0);
9087 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9088 DAG.getConstant(X86::COND_B, MVT::i8),
9089 SDValue(Neg.getNode(), 1));
9090 return Res;
9091 }
9092
Chris Lattnera2b56002010-12-05 01:23:24 +00009093 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9094 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009095 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009096
Chris Lattner96908b12010-12-05 02:00:51 +00009097 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009098 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9099 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009100
Chris Lattner96908b12010-12-05 02:00:51 +00009101 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9102 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009103
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009104 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009105 if (N2C == 0 || !N2C->isNullValue())
9106 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9107 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009108 }
9109 }
9110
Chris Lattnera2b56002010-12-05 01:23:24 +00009111 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009112 if (Cond.getOpcode() == ISD::AND &&
9113 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9114 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009115 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009116 Cond = Cond.getOperand(0);
9117 }
9118
Evan Cheng3f41d662007-10-08 22:16:29 +00009119 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9120 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009121 unsigned CondOpcode = Cond.getOpcode();
9122 if (CondOpcode == X86ISD::SETCC ||
9123 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009124 CC = Cond.getOperand(0);
9125
Dan Gohman475871a2008-07-27 21:46:04 +00009126 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009127 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009128 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009129
Evan Cheng3f41d662007-10-08 22:16:29 +00009130 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009131 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009132 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009133 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009134
Chris Lattnerd1980a52009-03-12 06:52:53 +00009135 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9136 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009137 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009138 addTest = false;
9139 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009140 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9141 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9142 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9143 Cond.getOperand(0).getValueType() != MVT::i8)) {
9144 SDValue LHS = Cond.getOperand(0);
9145 SDValue RHS = Cond.getOperand(1);
9146 unsigned X86Opcode;
9147 unsigned X86Cond;
9148 SDVTList VTs;
9149 switch (CondOpcode) {
9150 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9151 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9152 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9153 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9154 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9155 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9156 default: llvm_unreachable("unexpected overflowing operator");
9157 }
9158 if (CondOpcode == ISD::UMULO)
9159 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9160 MVT::i32);
9161 else
9162 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9163
9164 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9165
9166 if (CondOpcode == ISD::UMULO)
9167 Cond = X86Op.getValue(2);
9168 else
9169 Cond = X86Op.getValue(1);
9170
9171 CC = DAG.getConstant(X86Cond, MVT::i8);
9172 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009173 }
9174
9175 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009176 // Look pass the truncate if the high bits are known zero.
9177 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9178 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009179
9180 // We know the result of AND is compared against zero. Try to match
9181 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009182 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009183 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009184 if (NewSetCC.getNode()) {
9185 CC = NewSetCC.getOperand(0);
9186 Cond = NewSetCC.getOperand(1);
9187 addTest = false;
9188 }
9189 }
9190 }
9191
9192 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009193 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009194 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009195 }
9196
Benjamin Kramere915ff32010-12-22 23:09:28 +00009197 // a < b ? -1 : 0 -> RES = ~setcc_carry
9198 // a < b ? 0 : -1 -> RES = setcc_carry
9199 // a >= b ? -1 : 0 -> RES = setcc_carry
9200 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009201 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009202 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009203 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9204
9205 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9206 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9207 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9208 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9209 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9210 return DAG.getNOT(DL, Res, Res.getValueType());
9211 return Res;
9212 }
9213 }
9214
Evan Cheng0488db92007-09-25 01:57:46 +00009215 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9216 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009217 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009218 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009219 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009220}
9221
Evan Cheng370e5342008-12-03 08:38:43 +00009222// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9223// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9224// from the AND / OR.
9225static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9226 Opc = Op.getOpcode();
9227 if (Opc != ISD::OR && Opc != ISD::AND)
9228 return false;
9229 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9230 Op.getOperand(0).hasOneUse() &&
9231 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9232 Op.getOperand(1).hasOneUse());
9233}
9234
Evan Cheng961d6d42009-02-02 08:19:07 +00009235// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9236// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009237static bool isXor1OfSetCC(SDValue Op) {
9238 if (Op.getOpcode() != ISD::XOR)
9239 return false;
9240 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9241 if (N1C && N1C->getAPIntValue() == 1) {
9242 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9243 Op.getOperand(0).hasOneUse();
9244 }
9245 return false;
9246}
9247
Dan Gohmand858e902010-04-17 15:26:15 +00009248SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009249 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009250 SDValue Chain = Op.getOperand(0);
9251 SDValue Cond = Op.getOperand(1);
9252 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009253 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009254 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009255 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009256
Dan Gohman1a492952009-10-20 16:22:37 +00009257 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009258 // Check for setcc([su]{add,sub,mul}o == 0).
9259 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9260 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9261 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9262 Cond.getOperand(0).getResNo() == 1 &&
9263 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9264 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9265 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9266 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9267 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9268 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9269 Inverted = true;
9270 Cond = Cond.getOperand(0);
9271 } else {
9272 SDValue NewCond = LowerSETCC(Cond, DAG);
9273 if (NewCond.getNode())
9274 Cond = NewCond;
9275 }
Dan Gohman1a492952009-10-20 16:22:37 +00009276 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009277#if 0
9278 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009279 else if (Cond.getOpcode() == X86ISD::ADD ||
9280 Cond.getOpcode() == X86ISD::SUB ||
9281 Cond.getOpcode() == X86ISD::SMUL ||
9282 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009283 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009284#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009285
Evan Chengad9c0a32009-12-15 00:53:42 +00009286 // Look pass (and (setcc_carry (cmp ...)), 1).
9287 if (Cond.getOpcode() == ISD::AND &&
9288 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9289 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009290 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009291 Cond = Cond.getOperand(0);
9292 }
9293
Evan Cheng3f41d662007-10-08 22:16:29 +00009294 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9295 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009296 unsigned CondOpcode = Cond.getOpcode();
9297 if (CondOpcode == X86ISD::SETCC ||
9298 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009299 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009300
Dan Gohman475871a2008-07-27 21:46:04 +00009301 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009302 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009303 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009304 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009305 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009306 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009307 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009308 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009309 default: break;
9310 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009311 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009312 // These can only come from an arithmetic instruction with overflow,
9313 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009314 Cond = Cond.getNode()->getOperand(1);
9315 addTest = false;
9316 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009317 }
Evan Cheng0488db92007-09-25 01:57:46 +00009318 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009319 }
9320 CondOpcode = Cond.getOpcode();
9321 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9322 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9323 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9324 Cond.getOperand(0).getValueType() != MVT::i8)) {
9325 SDValue LHS = Cond.getOperand(0);
9326 SDValue RHS = Cond.getOperand(1);
9327 unsigned X86Opcode;
9328 unsigned X86Cond;
9329 SDVTList VTs;
9330 switch (CondOpcode) {
9331 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9332 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9333 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9334 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9335 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9336 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9337 default: llvm_unreachable("unexpected overflowing operator");
9338 }
9339 if (Inverted)
9340 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9341 if (CondOpcode == ISD::UMULO)
9342 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9343 MVT::i32);
9344 else
9345 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9346
9347 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9348
9349 if (CondOpcode == ISD::UMULO)
9350 Cond = X86Op.getValue(2);
9351 else
9352 Cond = X86Op.getValue(1);
9353
9354 CC = DAG.getConstant(X86Cond, MVT::i8);
9355 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009356 } else {
9357 unsigned CondOpc;
9358 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9359 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009360 if (CondOpc == ISD::OR) {
9361 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9362 // two branches instead of an explicit OR instruction with a
9363 // separate test.
9364 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009365 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009366 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009367 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009368 Chain, Dest, CC, Cmp);
9369 CC = Cond.getOperand(1).getOperand(0);
9370 Cond = Cmp;
9371 addTest = false;
9372 }
9373 } else { // ISD::AND
9374 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9375 // two branches instead of an explicit AND instruction with a
9376 // separate test. However, we only do this if this block doesn't
9377 // have a fall-through edge, because this requires an explicit
9378 // jmp when the condition is false.
9379 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009380 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009381 Op.getNode()->hasOneUse()) {
9382 X86::CondCode CCode =
9383 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9384 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009385 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009386 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009387 // Look for an unconditional branch following this conditional branch.
9388 // We need this because we need to reverse the successors in order
9389 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009390 if (User->getOpcode() == ISD::BR) {
9391 SDValue FalseBB = User->getOperand(1);
9392 SDNode *NewBR =
9393 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009394 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009395 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009396 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009397
Dale Johannesene4d209d2009-02-03 20:21:25 +00009398 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009399 Chain, Dest, CC, Cmp);
9400 X86::CondCode CCode =
9401 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9402 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009403 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009404 Cond = Cmp;
9405 addTest = false;
9406 }
9407 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009408 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009409 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9410 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9411 // It should be transformed during dag combiner except when the condition
9412 // is set by a arithmetics with overflow node.
9413 X86::CondCode CCode =
9414 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9415 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009416 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009417 Cond = Cond.getOperand(0).getOperand(1);
9418 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009419 } else if (Cond.getOpcode() == ISD::SETCC &&
9420 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9421 // For FCMP_OEQ, we can emit
9422 // two branches instead of an explicit AND instruction with a
9423 // separate test. However, we only do this if this block doesn't
9424 // have a fall-through edge, because this requires an explicit
9425 // jmp when the condition is false.
9426 if (Op.getNode()->hasOneUse()) {
9427 SDNode *User = *Op.getNode()->use_begin();
9428 // Look for an unconditional branch following this conditional branch.
9429 // We need this because we need to reverse the successors in order
9430 // to implement FCMP_OEQ.
9431 if (User->getOpcode() == ISD::BR) {
9432 SDValue FalseBB = User->getOperand(1);
9433 SDNode *NewBR =
9434 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9435 assert(NewBR == User);
9436 (void)NewBR;
9437 Dest = FalseBB;
9438
9439 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9440 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009441 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009442 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9443 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9444 Chain, Dest, CC, Cmp);
9445 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9446 Cond = Cmp;
9447 addTest = false;
9448 }
9449 }
9450 } else if (Cond.getOpcode() == ISD::SETCC &&
9451 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9452 // For FCMP_UNE, we can emit
9453 // two branches instead of an explicit AND instruction with a
9454 // separate test. However, we only do this if this block doesn't
9455 // have a fall-through edge, because this requires an explicit
9456 // jmp when the condition is false.
9457 if (Op.getNode()->hasOneUse()) {
9458 SDNode *User = *Op.getNode()->use_begin();
9459 // Look for an unconditional branch following this conditional branch.
9460 // We need this because we need to reverse the successors in order
9461 // to implement FCMP_UNE.
9462 if (User->getOpcode() == ISD::BR) {
9463 SDValue FalseBB = User->getOperand(1);
9464 SDNode *NewBR =
9465 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9466 assert(NewBR == User);
9467 (void)NewBR;
9468
9469 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9470 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009471 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009472 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9473 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9474 Chain, Dest, CC, Cmp);
9475 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9476 Cond = Cmp;
9477 addTest = false;
9478 Dest = FalseBB;
9479 }
9480 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009481 }
Evan Cheng0488db92007-09-25 01:57:46 +00009482 }
9483
9484 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009485 // Look pass the truncate if the high bits are known zero.
9486 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9487 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009488
9489 // We know the result of AND is compared against zero. Try to match
9490 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009491 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009492 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9493 if (NewSetCC.getNode()) {
9494 CC = NewSetCC.getOperand(0);
9495 Cond = NewSetCC.getOperand(1);
9496 addTest = false;
9497 }
9498 }
9499 }
9500
9501 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009502 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009503 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009504 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009505 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009506 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009507 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009508}
9509
Anton Korobeynikove060b532007-04-17 19:34:00 +00009510
9511// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9512// Calls to _alloca is needed to probe the stack when allocating more than 4k
9513// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9514// that the guard pages used by the OS virtual memory manager are allocated in
9515// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009516SDValue
9517X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009518 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009519 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009520 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009521 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009522 "are being used");
9523 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009524 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009525
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009526 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009527 SDValue Chain = Op.getOperand(0);
9528 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009529 // FIXME: Ensure alignment here
9530
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009531 bool Is64Bit = Subtarget->is64Bit();
9532 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009533
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009534 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009535 MachineFunction &MF = DAG.getMachineFunction();
9536 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009537
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009538 if (Is64Bit) {
9539 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009540 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009541 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009542
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009543 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009544 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009545 if (I->hasNestAttr())
9546 report_fatal_error("Cannot use segmented stacks with functions that "
9547 "have nested arguments.");
9548 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009549
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009550 const TargetRegisterClass *AddrRegClass =
9551 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9552 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9553 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9554 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9555 DAG.getRegister(Vreg, SPTy));
9556 SDValue Ops1[2] = { Value, Chain };
9557 return DAG.getMergeValues(Ops1, 2, dl);
9558 } else {
9559 SDValue Flag;
9560 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009561
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009562 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9563 Flag = Chain.getValue(1);
9564 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009565
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009566 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9567 Flag = Chain.getValue(1);
9568
9569 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9570
9571 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9572 return DAG.getMergeValues(Ops1, 2, dl);
9573 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009574}
9575
Dan Gohmand858e902010-04-17 15:26:15 +00009576SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009577 MachineFunction &MF = DAG.getMachineFunction();
9578 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9579
Dan Gohman69de1932008-02-06 22:27:42 +00009580 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009581 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009582
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009583 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009584 // vastart just stores the address of the VarArgsFrameIndex slot into the
9585 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009586 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9587 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009588 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9589 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009590 }
9591
9592 // __va_list_tag:
9593 // gp_offset (0 - 6 * 8)
9594 // fp_offset (48 - 48 + 8 * 16)
9595 // overflow_arg_area (point to parameters coming in memory).
9596 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009597 SmallVector<SDValue, 8> MemOps;
9598 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009599 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009600 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009601 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9602 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009603 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009604 MemOps.push_back(Store);
9605
9606 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009607 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009608 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009609 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009610 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9611 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009612 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009613 MemOps.push_back(Store);
9614
9615 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009616 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009617 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009618 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9619 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009620 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9621 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009622 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009623 MemOps.push_back(Store);
9624
9625 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009626 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009627 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009628 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9629 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009630 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9631 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009632 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009633 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009634 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009635}
9636
Dan Gohmand858e902010-04-17 15:26:15 +00009637SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009638 assert(Subtarget->is64Bit() &&
9639 "LowerVAARG only handles 64-bit va_arg!");
9640 assert((Subtarget->isTargetLinux() ||
9641 Subtarget->isTargetDarwin()) &&
9642 "Unhandled target in LowerVAARG");
9643 assert(Op.getNode()->getNumOperands() == 4);
9644 SDValue Chain = Op.getOperand(0);
9645 SDValue SrcPtr = Op.getOperand(1);
9646 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9647 unsigned Align = Op.getConstantOperandVal(3);
9648 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009649
Dan Gohman320afb82010-10-12 18:00:49 +00009650 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009651 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009652 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9653 uint8_t ArgMode;
9654
9655 // Decide which area this value should be read from.
9656 // TODO: Implement the AMD64 ABI in its entirety. This simple
9657 // selection mechanism works only for the basic types.
9658 if (ArgVT == MVT::f80) {
9659 llvm_unreachable("va_arg for f80 not yet implemented");
9660 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9661 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9662 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9663 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9664 } else {
9665 llvm_unreachable("Unhandled argument type in LowerVAARG");
9666 }
9667
9668 if (ArgMode == 2) {
9669 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009670 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009671 !(DAG.getMachineFunction()
9672 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009673 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009674 }
9675
9676 // Insert VAARG_64 node into the DAG
9677 // VAARG_64 returns two values: Variable Argument Address, Chain
9678 SmallVector<SDValue, 11> InstOps;
9679 InstOps.push_back(Chain);
9680 InstOps.push_back(SrcPtr);
9681 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9682 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9683 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9684 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9685 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9686 VTs, &InstOps[0], InstOps.size(),
9687 MVT::i64,
9688 MachinePointerInfo(SV),
9689 /*Align=*/0,
9690 /*Volatile=*/false,
9691 /*ReadMem=*/true,
9692 /*WriteMem=*/true);
9693 Chain = VAARG.getValue(1);
9694
9695 // Load the next argument and return it
9696 return DAG.getLoad(ArgVT, dl,
9697 Chain,
9698 VAARG,
9699 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009700 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009701}
9702
Craig Topper55b24052012-09-11 06:15:32 +00009703static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9704 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009705 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009706 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009707 SDValue Chain = Op.getOperand(0);
9708 SDValue DstPtr = Op.getOperand(1);
9709 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009710 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9711 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009712 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009713
Chris Lattnere72f2022010-09-21 05:40:29 +00009714 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009715 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009716 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009717 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009718}
9719
Craig Topper80e46362012-01-23 06:16:53 +00009720// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9721// may or may not be a constant. Takes immediate version of shift as input.
9722static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9723 SDValue SrcOp, SDValue ShAmt,
9724 SelectionDAG &DAG) {
9725 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9726
9727 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009728 // Constant may be a TargetConstant. Use a regular constant.
9729 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009730 switch (Opc) {
9731 default: llvm_unreachable("Unknown target vector shift node");
9732 case X86ISD::VSHLI:
9733 case X86ISD::VSRLI:
9734 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009735 return DAG.getNode(Opc, dl, VT, SrcOp,
9736 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009737 }
9738 }
9739
9740 // Change opcode to non-immediate version
9741 switch (Opc) {
9742 default: llvm_unreachable("Unknown target vector shift node");
9743 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9744 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9745 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9746 }
9747
9748 // Need to build a vector containing shift amount
9749 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9750 SDValue ShOps[4];
9751 ShOps[0] = ShAmt;
9752 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009753 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009754 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009755
9756 // The return type has to be a 128-bit type with the same element
9757 // type as the input type.
9758 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9759 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9760
9761 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009762 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9763}
9764
Craig Topper55b24052012-09-11 06:15:32 +00009765static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009766 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009767 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009768 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009769 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009770 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009771 case Intrinsic::x86_sse_comieq_ss:
9772 case Intrinsic::x86_sse_comilt_ss:
9773 case Intrinsic::x86_sse_comile_ss:
9774 case Intrinsic::x86_sse_comigt_ss:
9775 case Intrinsic::x86_sse_comige_ss:
9776 case Intrinsic::x86_sse_comineq_ss:
9777 case Intrinsic::x86_sse_ucomieq_ss:
9778 case Intrinsic::x86_sse_ucomilt_ss:
9779 case Intrinsic::x86_sse_ucomile_ss:
9780 case Intrinsic::x86_sse_ucomigt_ss:
9781 case Intrinsic::x86_sse_ucomige_ss:
9782 case Intrinsic::x86_sse_ucomineq_ss:
9783 case Intrinsic::x86_sse2_comieq_sd:
9784 case Intrinsic::x86_sse2_comilt_sd:
9785 case Intrinsic::x86_sse2_comile_sd:
9786 case Intrinsic::x86_sse2_comigt_sd:
9787 case Intrinsic::x86_sse2_comige_sd:
9788 case Intrinsic::x86_sse2_comineq_sd:
9789 case Intrinsic::x86_sse2_ucomieq_sd:
9790 case Intrinsic::x86_sse2_ucomilt_sd:
9791 case Intrinsic::x86_sse2_ucomile_sd:
9792 case Intrinsic::x86_sse2_ucomigt_sd:
9793 case Intrinsic::x86_sse2_ucomige_sd:
9794 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009795 unsigned Opc;
9796 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009797 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009798 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009799 case Intrinsic::x86_sse_comieq_ss:
9800 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009801 Opc = X86ISD::COMI;
9802 CC = ISD::SETEQ;
9803 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009804 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009805 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009806 Opc = X86ISD::COMI;
9807 CC = ISD::SETLT;
9808 break;
9809 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009810 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009811 Opc = X86ISD::COMI;
9812 CC = ISD::SETLE;
9813 break;
9814 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009815 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009816 Opc = X86ISD::COMI;
9817 CC = ISD::SETGT;
9818 break;
9819 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009820 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009821 Opc = X86ISD::COMI;
9822 CC = ISD::SETGE;
9823 break;
9824 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009825 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009826 Opc = X86ISD::COMI;
9827 CC = ISD::SETNE;
9828 break;
9829 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009830 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009831 Opc = X86ISD::UCOMI;
9832 CC = ISD::SETEQ;
9833 break;
9834 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009835 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009836 Opc = X86ISD::UCOMI;
9837 CC = ISD::SETLT;
9838 break;
9839 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009840 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009841 Opc = X86ISD::UCOMI;
9842 CC = ISD::SETLE;
9843 break;
9844 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009845 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009846 Opc = X86ISD::UCOMI;
9847 CC = ISD::SETGT;
9848 break;
9849 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009850 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009851 Opc = X86ISD::UCOMI;
9852 CC = ISD::SETGE;
9853 break;
9854 case Intrinsic::x86_sse_ucomineq_ss:
9855 case Intrinsic::x86_sse2_ucomineq_sd:
9856 Opc = X86ISD::UCOMI;
9857 CC = ISD::SETNE;
9858 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009859 }
Evan Cheng734503b2006-09-11 02:19:56 +00009860
Dan Gohman475871a2008-07-27 21:46:04 +00009861 SDValue LHS = Op.getOperand(1);
9862 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009863 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009864 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009865 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9866 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9867 DAG.getConstant(X86CC, MVT::i8), Cond);
9868 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009869 }
Craig Topper6d688152012-08-14 07:43:25 +00009870
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009871 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009872 case Intrinsic::x86_sse2_pmulu_dq:
9873 case Intrinsic::x86_avx2_pmulu_dq:
9874 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9875 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009876
9877 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009878 case Intrinsic::x86_sse3_hadd_ps:
9879 case Intrinsic::x86_sse3_hadd_pd:
9880 case Intrinsic::x86_avx_hadd_ps_256:
9881 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009882 case Intrinsic::x86_sse3_hsub_ps:
9883 case Intrinsic::x86_sse3_hsub_pd:
9884 case Intrinsic::x86_avx_hsub_ps_256:
9885 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009886 case Intrinsic::x86_ssse3_phadd_w_128:
9887 case Intrinsic::x86_ssse3_phadd_d_128:
9888 case Intrinsic::x86_avx2_phadd_w:
9889 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009890 case Intrinsic::x86_ssse3_phsub_w_128:
9891 case Intrinsic::x86_ssse3_phsub_d_128:
9892 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009893 case Intrinsic::x86_avx2_phsub_d: {
9894 unsigned Opcode;
9895 switch (IntNo) {
9896 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9897 case Intrinsic::x86_sse3_hadd_ps:
9898 case Intrinsic::x86_sse3_hadd_pd:
9899 case Intrinsic::x86_avx_hadd_ps_256:
9900 case Intrinsic::x86_avx_hadd_pd_256:
9901 Opcode = X86ISD::FHADD;
9902 break;
9903 case Intrinsic::x86_sse3_hsub_ps:
9904 case Intrinsic::x86_sse3_hsub_pd:
9905 case Intrinsic::x86_avx_hsub_ps_256:
9906 case Intrinsic::x86_avx_hsub_pd_256:
9907 Opcode = X86ISD::FHSUB;
9908 break;
9909 case Intrinsic::x86_ssse3_phadd_w_128:
9910 case Intrinsic::x86_ssse3_phadd_d_128:
9911 case Intrinsic::x86_avx2_phadd_w:
9912 case Intrinsic::x86_avx2_phadd_d:
9913 Opcode = X86ISD::HADD;
9914 break;
9915 case Intrinsic::x86_ssse3_phsub_w_128:
9916 case Intrinsic::x86_ssse3_phsub_d_128:
9917 case Intrinsic::x86_avx2_phsub_w:
9918 case Intrinsic::x86_avx2_phsub_d:
9919 Opcode = X86ISD::HSUB;
9920 break;
9921 }
9922 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009923 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009924 }
9925
9926 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009927 case Intrinsic::x86_avx2_psllv_d:
9928 case Intrinsic::x86_avx2_psllv_q:
9929 case Intrinsic::x86_avx2_psllv_d_256:
9930 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009931 case Intrinsic::x86_avx2_psrlv_d:
9932 case Intrinsic::x86_avx2_psrlv_q:
9933 case Intrinsic::x86_avx2_psrlv_d_256:
9934 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009935 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009936 case Intrinsic::x86_avx2_psrav_d_256: {
9937 unsigned Opcode;
9938 switch (IntNo) {
9939 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9940 case Intrinsic::x86_avx2_psllv_d:
9941 case Intrinsic::x86_avx2_psllv_q:
9942 case Intrinsic::x86_avx2_psllv_d_256:
9943 case Intrinsic::x86_avx2_psllv_q_256:
9944 Opcode = ISD::SHL;
9945 break;
9946 case Intrinsic::x86_avx2_psrlv_d:
9947 case Intrinsic::x86_avx2_psrlv_q:
9948 case Intrinsic::x86_avx2_psrlv_d_256:
9949 case Intrinsic::x86_avx2_psrlv_q_256:
9950 Opcode = ISD::SRL;
9951 break;
9952 case Intrinsic::x86_avx2_psrav_d:
9953 case Intrinsic::x86_avx2_psrav_d_256:
9954 Opcode = ISD::SRA;
9955 break;
9956 }
9957 return DAG.getNode(Opcode, dl, Op.getValueType(),
9958 Op.getOperand(1), Op.getOperand(2));
9959 }
9960
Craig Topper969ba282012-01-25 06:43:11 +00009961 case Intrinsic::x86_ssse3_pshuf_b_128:
9962 case Intrinsic::x86_avx2_pshuf_b:
9963 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9964 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009965
Craig Topper969ba282012-01-25 06:43:11 +00009966 case Intrinsic::x86_ssse3_psign_b_128:
9967 case Intrinsic::x86_ssse3_psign_w_128:
9968 case Intrinsic::x86_ssse3_psign_d_128:
9969 case Intrinsic::x86_avx2_psign_b:
9970 case Intrinsic::x86_avx2_psign_w:
9971 case Intrinsic::x86_avx2_psign_d:
9972 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9973 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009974
Craig Toppere566cd02012-01-26 07:18:03 +00009975 case Intrinsic::x86_sse41_insertps:
9976 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9977 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009978
Craig Toppere566cd02012-01-26 07:18:03 +00009979 case Intrinsic::x86_avx_vperm2f128_ps_256:
9980 case Intrinsic::x86_avx_vperm2f128_pd_256:
9981 case Intrinsic::x86_avx_vperm2f128_si_256:
9982 case Intrinsic::x86_avx2_vperm2i128:
9983 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9984 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009985
Craig Topperffa6c402012-04-16 07:13:00 +00009986 case Intrinsic::x86_avx2_permd:
9987 case Intrinsic::x86_avx2_permps:
9988 // Operands intentionally swapped. Mask is last operand to intrinsic,
9989 // but second operand for node/intruction.
9990 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9991 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009992
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009993 // ptest and testp intrinsics. The intrinsic these come from are designed to
9994 // return an integer value, not just an instruction so lower it to the ptest
9995 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009996 case Intrinsic::x86_sse41_ptestz:
9997 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009998 case Intrinsic::x86_sse41_ptestnzc:
9999 case Intrinsic::x86_avx_ptestz_256:
10000 case Intrinsic::x86_avx_ptestc_256:
10001 case Intrinsic::x86_avx_ptestnzc_256:
10002 case Intrinsic::x86_avx_vtestz_ps:
10003 case Intrinsic::x86_avx_vtestc_ps:
10004 case Intrinsic::x86_avx_vtestnzc_ps:
10005 case Intrinsic::x86_avx_vtestz_pd:
10006 case Intrinsic::x86_avx_vtestc_pd:
10007 case Intrinsic::x86_avx_vtestnzc_pd:
10008 case Intrinsic::x86_avx_vtestz_ps_256:
10009 case Intrinsic::x86_avx_vtestc_ps_256:
10010 case Intrinsic::x86_avx_vtestnzc_ps_256:
10011 case Intrinsic::x86_avx_vtestz_pd_256:
10012 case Intrinsic::x86_avx_vtestc_pd_256:
10013 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10014 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010015 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010016 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010017 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010018 case Intrinsic::x86_avx_vtestz_ps:
10019 case Intrinsic::x86_avx_vtestz_pd:
10020 case Intrinsic::x86_avx_vtestz_ps_256:
10021 case Intrinsic::x86_avx_vtestz_pd_256:
10022 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010023 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010024 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010025 // ZF = 1
10026 X86CC = X86::COND_E;
10027 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010028 case Intrinsic::x86_avx_vtestc_ps:
10029 case Intrinsic::x86_avx_vtestc_pd:
10030 case Intrinsic::x86_avx_vtestc_ps_256:
10031 case Intrinsic::x86_avx_vtestc_pd_256:
10032 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010033 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010034 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010035 // CF = 1
10036 X86CC = X86::COND_B;
10037 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010038 case Intrinsic::x86_avx_vtestnzc_ps:
10039 case Intrinsic::x86_avx_vtestnzc_pd:
10040 case Intrinsic::x86_avx_vtestnzc_ps_256:
10041 case Intrinsic::x86_avx_vtestnzc_pd_256:
10042 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010043 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010044 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010045 // ZF and CF = 0
10046 X86CC = X86::COND_A;
10047 break;
10048 }
Eric Christopherfd179292009-08-27 18:07:15 +000010049
Eric Christopher71c67532009-07-29 00:28:05 +000010050 SDValue LHS = Op.getOperand(1);
10051 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010052 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10053 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010054 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10055 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10056 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010057 }
Evan Cheng5759f972008-05-04 09:15:50 +000010058
Craig Topper80e46362012-01-23 06:16:53 +000010059 // SSE/AVX shift intrinsics
10060 case Intrinsic::x86_sse2_psll_w:
10061 case Intrinsic::x86_sse2_psll_d:
10062 case Intrinsic::x86_sse2_psll_q:
10063 case Intrinsic::x86_avx2_psll_w:
10064 case Intrinsic::x86_avx2_psll_d:
10065 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010066 case Intrinsic::x86_sse2_psrl_w:
10067 case Intrinsic::x86_sse2_psrl_d:
10068 case Intrinsic::x86_sse2_psrl_q:
10069 case Intrinsic::x86_avx2_psrl_w:
10070 case Intrinsic::x86_avx2_psrl_d:
10071 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010072 case Intrinsic::x86_sse2_psra_w:
10073 case Intrinsic::x86_sse2_psra_d:
10074 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010075 case Intrinsic::x86_avx2_psra_d: {
10076 unsigned Opcode;
10077 switch (IntNo) {
10078 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10079 case Intrinsic::x86_sse2_psll_w:
10080 case Intrinsic::x86_sse2_psll_d:
10081 case Intrinsic::x86_sse2_psll_q:
10082 case Intrinsic::x86_avx2_psll_w:
10083 case Intrinsic::x86_avx2_psll_d:
10084 case Intrinsic::x86_avx2_psll_q:
10085 Opcode = X86ISD::VSHL;
10086 break;
10087 case Intrinsic::x86_sse2_psrl_w:
10088 case Intrinsic::x86_sse2_psrl_d:
10089 case Intrinsic::x86_sse2_psrl_q:
10090 case Intrinsic::x86_avx2_psrl_w:
10091 case Intrinsic::x86_avx2_psrl_d:
10092 case Intrinsic::x86_avx2_psrl_q:
10093 Opcode = X86ISD::VSRL;
10094 break;
10095 case Intrinsic::x86_sse2_psra_w:
10096 case Intrinsic::x86_sse2_psra_d:
10097 case Intrinsic::x86_avx2_psra_w:
10098 case Intrinsic::x86_avx2_psra_d:
10099 Opcode = X86ISD::VSRA;
10100 break;
10101 }
10102 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010103 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010104 }
10105
10106 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010107 case Intrinsic::x86_sse2_pslli_w:
10108 case Intrinsic::x86_sse2_pslli_d:
10109 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010110 case Intrinsic::x86_avx2_pslli_w:
10111 case Intrinsic::x86_avx2_pslli_d:
10112 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010113 case Intrinsic::x86_sse2_psrli_w:
10114 case Intrinsic::x86_sse2_psrli_d:
10115 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010116 case Intrinsic::x86_avx2_psrli_w:
10117 case Intrinsic::x86_avx2_psrli_d:
10118 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010119 case Intrinsic::x86_sse2_psrai_w:
10120 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010121 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010122 case Intrinsic::x86_avx2_psrai_d: {
10123 unsigned Opcode;
10124 switch (IntNo) {
10125 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10126 case Intrinsic::x86_sse2_pslli_w:
10127 case Intrinsic::x86_sse2_pslli_d:
10128 case Intrinsic::x86_sse2_pslli_q:
10129 case Intrinsic::x86_avx2_pslli_w:
10130 case Intrinsic::x86_avx2_pslli_d:
10131 case Intrinsic::x86_avx2_pslli_q:
10132 Opcode = X86ISD::VSHLI;
10133 break;
10134 case Intrinsic::x86_sse2_psrli_w:
10135 case Intrinsic::x86_sse2_psrli_d:
10136 case Intrinsic::x86_sse2_psrli_q:
10137 case Intrinsic::x86_avx2_psrli_w:
10138 case Intrinsic::x86_avx2_psrli_d:
10139 case Intrinsic::x86_avx2_psrli_q:
10140 Opcode = X86ISD::VSRLI;
10141 break;
10142 case Intrinsic::x86_sse2_psrai_w:
10143 case Intrinsic::x86_sse2_psrai_d:
10144 case Intrinsic::x86_avx2_psrai_w:
10145 case Intrinsic::x86_avx2_psrai_d:
10146 Opcode = X86ISD::VSRAI;
10147 break;
10148 }
10149 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010150 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010151 }
10152
Craig Topper4feb6472012-08-06 06:22:36 +000010153 case Intrinsic::x86_sse42_pcmpistria128:
10154 case Intrinsic::x86_sse42_pcmpestria128:
10155 case Intrinsic::x86_sse42_pcmpistric128:
10156 case Intrinsic::x86_sse42_pcmpestric128:
10157 case Intrinsic::x86_sse42_pcmpistrio128:
10158 case Intrinsic::x86_sse42_pcmpestrio128:
10159 case Intrinsic::x86_sse42_pcmpistris128:
10160 case Intrinsic::x86_sse42_pcmpestris128:
10161 case Intrinsic::x86_sse42_pcmpistriz128:
10162 case Intrinsic::x86_sse42_pcmpestriz128: {
10163 unsigned Opcode;
10164 unsigned X86CC;
10165 switch (IntNo) {
10166 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10167 case Intrinsic::x86_sse42_pcmpistria128:
10168 Opcode = X86ISD::PCMPISTRI;
10169 X86CC = X86::COND_A;
10170 break;
10171 case Intrinsic::x86_sse42_pcmpestria128:
10172 Opcode = X86ISD::PCMPESTRI;
10173 X86CC = X86::COND_A;
10174 break;
10175 case Intrinsic::x86_sse42_pcmpistric128:
10176 Opcode = X86ISD::PCMPISTRI;
10177 X86CC = X86::COND_B;
10178 break;
10179 case Intrinsic::x86_sse42_pcmpestric128:
10180 Opcode = X86ISD::PCMPESTRI;
10181 X86CC = X86::COND_B;
10182 break;
10183 case Intrinsic::x86_sse42_pcmpistrio128:
10184 Opcode = X86ISD::PCMPISTRI;
10185 X86CC = X86::COND_O;
10186 break;
10187 case Intrinsic::x86_sse42_pcmpestrio128:
10188 Opcode = X86ISD::PCMPESTRI;
10189 X86CC = X86::COND_O;
10190 break;
10191 case Intrinsic::x86_sse42_pcmpistris128:
10192 Opcode = X86ISD::PCMPISTRI;
10193 X86CC = X86::COND_S;
10194 break;
10195 case Intrinsic::x86_sse42_pcmpestris128:
10196 Opcode = X86ISD::PCMPESTRI;
10197 X86CC = X86::COND_S;
10198 break;
10199 case Intrinsic::x86_sse42_pcmpistriz128:
10200 Opcode = X86ISD::PCMPISTRI;
10201 X86CC = X86::COND_E;
10202 break;
10203 case Intrinsic::x86_sse42_pcmpestriz128:
10204 Opcode = X86ISD::PCMPESTRI;
10205 X86CC = X86::COND_E;
10206 break;
10207 }
10208 SmallVector<SDValue, 5> NewOps;
10209 NewOps.append(Op->op_begin()+1, Op->op_end());
10210 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10211 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10212 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10213 DAG.getConstant(X86CC, MVT::i8),
10214 SDValue(PCMP.getNode(), 1));
10215 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10216 }
Craig Topper6d688152012-08-14 07:43:25 +000010217
Craig Topper4feb6472012-08-06 06:22:36 +000010218 case Intrinsic::x86_sse42_pcmpistri128:
10219 case Intrinsic::x86_sse42_pcmpestri128: {
10220 unsigned Opcode;
10221 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10222 Opcode = X86ISD::PCMPISTRI;
10223 else
10224 Opcode = X86ISD::PCMPESTRI;
10225
10226 SmallVector<SDValue, 5> NewOps;
10227 NewOps.append(Op->op_begin()+1, Op->op_end());
10228 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10229 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10230 }
Craig Topper0e292372012-08-24 04:03:22 +000010231 case Intrinsic::x86_fma_vfmadd_ps:
10232 case Intrinsic::x86_fma_vfmadd_pd:
10233 case Intrinsic::x86_fma_vfmsub_ps:
10234 case Intrinsic::x86_fma_vfmsub_pd:
10235 case Intrinsic::x86_fma_vfnmadd_ps:
10236 case Intrinsic::x86_fma_vfnmadd_pd:
10237 case Intrinsic::x86_fma_vfnmsub_ps:
10238 case Intrinsic::x86_fma_vfnmsub_pd:
10239 case Intrinsic::x86_fma_vfmaddsub_ps:
10240 case Intrinsic::x86_fma_vfmaddsub_pd:
10241 case Intrinsic::x86_fma_vfmsubadd_ps:
10242 case Intrinsic::x86_fma_vfmsubadd_pd:
10243 case Intrinsic::x86_fma_vfmadd_ps_256:
10244 case Intrinsic::x86_fma_vfmadd_pd_256:
10245 case Intrinsic::x86_fma_vfmsub_ps_256:
10246 case Intrinsic::x86_fma_vfmsub_pd_256:
10247 case Intrinsic::x86_fma_vfnmadd_ps_256:
10248 case Intrinsic::x86_fma_vfnmadd_pd_256:
10249 case Intrinsic::x86_fma_vfnmsub_ps_256:
10250 case Intrinsic::x86_fma_vfnmsub_pd_256:
10251 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10252 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10253 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10254 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010255 unsigned Opc;
10256 switch (IntNo) {
10257 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10258 case Intrinsic::x86_fma_vfmadd_ps:
10259 case Intrinsic::x86_fma_vfmadd_pd:
10260 case Intrinsic::x86_fma_vfmadd_ps_256:
10261 case Intrinsic::x86_fma_vfmadd_pd_256:
10262 Opc = X86ISD::FMADD;
10263 break;
10264 case Intrinsic::x86_fma_vfmsub_ps:
10265 case Intrinsic::x86_fma_vfmsub_pd:
10266 case Intrinsic::x86_fma_vfmsub_ps_256:
10267 case Intrinsic::x86_fma_vfmsub_pd_256:
10268 Opc = X86ISD::FMSUB;
10269 break;
10270 case Intrinsic::x86_fma_vfnmadd_ps:
10271 case Intrinsic::x86_fma_vfnmadd_pd:
10272 case Intrinsic::x86_fma_vfnmadd_ps_256:
10273 case Intrinsic::x86_fma_vfnmadd_pd_256:
10274 Opc = X86ISD::FNMADD;
10275 break;
10276 case Intrinsic::x86_fma_vfnmsub_ps:
10277 case Intrinsic::x86_fma_vfnmsub_pd:
10278 case Intrinsic::x86_fma_vfnmsub_ps_256:
10279 case Intrinsic::x86_fma_vfnmsub_pd_256:
10280 Opc = X86ISD::FNMSUB;
10281 break;
10282 case Intrinsic::x86_fma_vfmaddsub_ps:
10283 case Intrinsic::x86_fma_vfmaddsub_pd:
10284 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10285 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10286 Opc = X86ISD::FMADDSUB;
10287 break;
10288 case Intrinsic::x86_fma_vfmsubadd_ps:
10289 case Intrinsic::x86_fma_vfmsubadd_pd:
10290 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10291 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10292 Opc = X86ISD::FMSUBADD;
10293 break;
10294 }
10295
10296 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10297 Op.getOperand(2), Op.getOperand(3));
10298 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010299 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010300}
Evan Cheng72261582005-12-20 06:22:03 +000010301
Craig Topper55b24052012-09-11 06:15:32 +000010302static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010303 DebugLoc dl = Op.getDebugLoc();
10304 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10305 switch (IntNo) {
10306 default: return SDValue(); // Don't custom lower most intrinsics.
10307
10308 // RDRAND intrinsics.
10309 case Intrinsic::x86_rdrand_16:
10310 case Intrinsic::x86_rdrand_32:
10311 case Intrinsic::x86_rdrand_64: {
10312 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010313 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10314 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010315
10316 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10317 // return the value from Rand, which is always 0, casted to i32.
10318 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10319 DAG.getConstant(1, Op->getValueType(1)),
10320 DAG.getConstant(X86::COND_B, MVT::i32),
10321 SDValue(Result.getNode(), 1) };
10322 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10323 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10324 Ops, 4);
10325
10326 // Return { result, isValid, chain }.
10327 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010328 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010329 }
10330 }
10331}
10332
Dan Gohmand858e902010-04-17 15:26:15 +000010333SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10334 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010335 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10336 MFI->setReturnAddressIsTaken(true);
10337
Bill Wendling64e87322009-01-16 19:25:27 +000010338 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010339 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010340
10341 if (Depth > 0) {
10342 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10343 SDValue Offset =
10344 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010345 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010346 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010347 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010348 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010349 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010350 }
10351
10352 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010353 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010354 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010355 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010356}
10357
Dan Gohmand858e902010-04-17 15:26:15 +000010358SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010359 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10360 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010361
Owen Andersone50ed302009-08-10 22:56:29 +000010362 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010363 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010364 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10365 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010366 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010367 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010368 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10369 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010370 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010371 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010372}
10373
Dan Gohman475871a2008-07-27 21:46:04 +000010374SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010375 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010376 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010377}
10378
Dan Gohmand858e902010-04-17 15:26:15 +000010379SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010380 SDValue Chain = Op.getOperand(0);
10381 SDValue Offset = Op.getOperand(1);
10382 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010383 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010384
Dan Gohmand8816272010-08-11 18:14:00 +000010385 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10386 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10387 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010388 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010389
Dan Gohmand8816272010-08-11 18:14:00 +000010390 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10391 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010392 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010393 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10394 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010395 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010396
Dale Johannesene4d209d2009-02-03 20:21:25 +000010397 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010398 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010399 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010400}
10401
Craig Topper55b24052012-09-11 06:15:32 +000010402static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010403 return Op.getOperand(0);
10404}
10405
10406SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10407 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010408 SDValue Root = Op.getOperand(0);
10409 SDValue Trmp = Op.getOperand(1); // trampoline
10410 SDValue FPtr = Op.getOperand(2); // nested function
10411 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010412 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010413
Dan Gohman69de1932008-02-06 22:27:42 +000010414 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010415
10416 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010417 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010418
10419 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010420 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10421 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010422
Evan Cheng0e6a0522011-07-18 20:57:22 +000010423 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10424 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010425
10426 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10427
10428 // Load the pointer to the nested function into R11.
10429 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010430 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010431 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010432 Addr, MachinePointerInfo(TrmpAddr),
10433 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010434
Owen Anderson825b72b2009-08-11 20:47:22 +000010435 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10436 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010437 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10438 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010439 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010440
10441 // Load the 'nest' parameter value into R10.
10442 // R10 is specified in X86CallingConv.td
10443 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010444 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10445 DAG.getConstant(10, MVT::i64));
10446 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010447 Addr, MachinePointerInfo(TrmpAddr, 10),
10448 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010449
Owen Anderson825b72b2009-08-11 20:47:22 +000010450 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10451 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010452 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10453 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010454 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010455
10456 // Jump to the nested function.
10457 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010458 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10459 DAG.getConstant(20, MVT::i64));
10460 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010461 Addr, MachinePointerInfo(TrmpAddr, 20),
10462 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010463
10464 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010465 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10466 DAG.getConstant(22, MVT::i64));
10467 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010468 MachinePointerInfo(TrmpAddr, 22),
10469 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010470
Duncan Sands4a544a72011-09-06 13:37:06 +000010471 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010472 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010473 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010474 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010475 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010476 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010477
10478 switch (CC) {
10479 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010480 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010481 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010482 case CallingConv::X86_StdCall: {
10483 // Pass 'nest' parameter in ECX.
10484 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010485 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010486
10487 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010488 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010489 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010490
Chris Lattner58d74912008-03-12 17:45:29 +000010491 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010492 unsigned InRegCount = 0;
10493 unsigned Idx = 1;
10494
10495 for (FunctionType::param_iterator I = FTy->param_begin(),
10496 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010497 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010498 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010499 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010500
10501 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010502 report_fatal_error("Nest register in use - reduce number of inreg"
10503 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010504 }
10505 }
10506 break;
10507 }
10508 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010509 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010510 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010511 // Pass 'nest' parameter in EAX.
10512 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010513 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010514 break;
10515 }
10516
Dan Gohman475871a2008-07-27 21:46:04 +000010517 SDValue OutChains[4];
10518 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010519
Owen Anderson825b72b2009-08-11 20:47:22 +000010520 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10521 DAG.getConstant(10, MVT::i32));
10522 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010523
Chris Lattnera62fe662010-02-05 19:20:30 +000010524 // This is storing the opcode for MOV32ri.
10525 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010526 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010527 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010528 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010529 Trmp, MachinePointerInfo(TrmpAddr),
10530 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010531
Owen Anderson825b72b2009-08-11 20:47:22 +000010532 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10533 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010534 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10535 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010536 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010537
Chris Lattnera62fe662010-02-05 19:20:30 +000010538 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010539 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10540 DAG.getConstant(5, MVT::i32));
10541 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010542 MachinePointerInfo(TrmpAddr, 5),
10543 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010544
Owen Anderson825b72b2009-08-11 20:47:22 +000010545 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10546 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010547 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10548 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010549 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010550
Duncan Sands4a544a72011-09-06 13:37:06 +000010551 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010552 }
10553}
10554
Dan Gohmand858e902010-04-17 15:26:15 +000010555SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10556 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010557 /*
10558 The rounding mode is in bits 11:10 of FPSR, and has the following
10559 settings:
10560 00 Round to nearest
10561 01 Round to -inf
10562 10 Round to +inf
10563 11 Round to 0
10564
10565 FLT_ROUNDS, on the other hand, expects the following:
10566 -1 Undefined
10567 0 Round to 0
10568 1 Round to nearest
10569 2 Round to +inf
10570 3 Round to -inf
10571
10572 To perform the conversion, we do:
10573 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10574 */
10575
10576 MachineFunction &MF = DAG.getMachineFunction();
10577 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010578 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010579 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010580 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010581 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010582
10583 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010584 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010585 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010586
Michael J. Spencerec38de22010-10-10 22:04:20 +000010587
Chris Lattner2156b792010-09-22 01:11:26 +000010588 MachineMemOperand *MMO =
10589 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10590 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010591
Chris Lattner2156b792010-09-22 01:11:26 +000010592 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10593 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10594 DAG.getVTList(MVT::Other),
10595 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010596
10597 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010598 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010599 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010600
10601 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010602 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010603 DAG.getNode(ISD::SRL, DL, MVT::i16,
10604 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010605 CWD, DAG.getConstant(0x800, MVT::i16)),
10606 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010607 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010608 DAG.getNode(ISD::SRL, DL, MVT::i16,
10609 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010610 CWD, DAG.getConstant(0x400, MVT::i16)),
10611 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010612
Dan Gohman475871a2008-07-27 21:46:04 +000010613 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010614 DAG.getNode(ISD::AND, DL, MVT::i16,
10615 DAG.getNode(ISD::ADD, DL, MVT::i16,
10616 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010617 DAG.getConstant(1, MVT::i16)),
10618 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010619
10620
Duncan Sands83ec4b62008-06-06 12:08:01 +000010621 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010622 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010623}
10624
Craig Topper55b24052012-09-11 06:15:32 +000010625static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010626 EVT VT = Op.getValueType();
10627 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010628 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010629 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010630
10631 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010632 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010633 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010634 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010635 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010636 }
Evan Cheng18efe262007-12-14 02:13:44 +000010637
Evan Cheng152804e2007-12-14 08:30:15 +000010638 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010639 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010640 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010641
10642 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010643 SDValue Ops[] = {
10644 Op,
10645 DAG.getConstant(NumBits+NumBits-1, OpVT),
10646 DAG.getConstant(X86::COND_E, MVT::i8),
10647 Op.getValue(1)
10648 };
10649 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010650
10651 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010652 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010653
Owen Anderson825b72b2009-08-11 20:47:22 +000010654 if (VT == MVT::i8)
10655 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010656 return Op;
10657}
10658
Craig Topper55b24052012-09-11 06:15:32 +000010659static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010660 EVT VT = Op.getValueType();
10661 EVT OpVT = VT;
10662 unsigned NumBits = VT.getSizeInBits();
10663 DebugLoc dl = Op.getDebugLoc();
10664
10665 Op = Op.getOperand(0);
10666 if (VT == MVT::i8) {
10667 // Zero extend to i32 since there is not an i8 bsr.
10668 OpVT = MVT::i32;
10669 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10670 }
10671
10672 // Issue a bsr (scan bits in reverse).
10673 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10674 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10675
10676 // And xor with NumBits-1.
10677 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10678
10679 if (VT == MVT::i8)
10680 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10681 return Op;
10682}
10683
Craig Topper55b24052012-09-11 06:15:32 +000010684static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010685 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010686 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010687 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010688 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010689
10690 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010691 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010692 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010693
10694 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010695 SDValue Ops[] = {
10696 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010697 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010698 DAG.getConstant(X86::COND_E, MVT::i8),
10699 Op.getValue(1)
10700 };
Chandler Carruth77821022011-12-24 12:12:34 +000010701 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010702}
10703
Craig Topper13894fa2011-08-24 06:14:18 +000010704// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10705// ones, and then concatenate the result back.
10706static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010707 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010708
Craig Topper7a9a28b2012-08-12 02:23:29 +000010709 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010710 "Unsupported value type for operation");
10711
Craig Topper66ddd152012-04-27 22:54:43 +000010712 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010713 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010714
10715 // Extract the LHS vectors
10716 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010717 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10718 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010719
10720 // Extract the RHS vectors
10721 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010722 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10723 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010724
10725 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10726 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10727
10728 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10729 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10730 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10731}
10732
Craig Topper55b24052012-09-11 06:15:32 +000010733static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010734 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010735 Op.getValueType().isInteger() &&
10736 "Only handle AVX 256-bit vector integer operation");
10737 return Lower256IntArith(Op, DAG);
10738}
10739
Craig Topper55b24052012-09-11 06:15:32 +000010740static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010741 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010742 Op.getValueType().isInteger() &&
10743 "Only handle AVX 256-bit vector integer operation");
10744 return Lower256IntArith(Op, DAG);
10745}
10746
Craig Topper55b24052012-09-11 06:15:32 +000010747static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10748 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010749 EVT VT = Op.getValueType();
10750
10751 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010752 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010753 return Lower256IntArith(Op, DAG);
10754
Craig Topper5b209e82012-02-05 03:14:49 +000010755 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10756 "Only know how to lower V2I64/V4I64 multiply");
10757
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010758 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010759
Craig Topper5b209e82012-02-05 03:14:49 +000010760 // Ahi = psrlqi(a, 32);
10761 // Bhi = psrlqi(b, 32);
10762 //
10763 // AloBlo = pmuludq(a, b);
10764 // AloBhi = pmuludq(a, Bhi);
10765 // AhiBlo = pmuludq(Ahi, b);
10766
10767 // AloBhi = psllqi(AloBhi, 32);
10768 // AhiBlo = psllqi(AhiBlo, 32);
10769 // return AloBlo + AloBhi + AhiBlo;
10770
Craig Topperaaa643c2011-11-09 07:28:55 +000010771 SDValue A = Op.getOperand(0);
10772 SDValue B = Op.getOperand(1);
10773
Craig Topper5b209e82012-02-05 03:14:49 +000010774 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010775
Craig Topper5b209e82012-02-05 03:14:49 +000010776 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10777 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010778
Craig Topper5b209e82012-02-05 03:14:49 +000010779 // Bit cast to 32-bit vectors for MULUDQ
10780 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10781 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10782 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10783 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10784 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010785
Craig Topper5b209e82012-02-05 03:14:49 +000010786 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10787 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10788 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010789
Craig Topper5b209e82012-02-05 03:14:49 +000010790 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10791 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010792
Dale Johannesene4d209d2009-02-03 20:21:25 +000010793 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010794 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010795}
10796
Nadav Rotem43012222011-05-11 08:12:09 +000010797SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10798
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010799 EVT VT = Op.getValueType();
10800 DebugLoc dl = Op.getDebugLoc();
10801 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010802 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010803 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010804
Craig Topper1accb7e2012-01-10 06:54:16 +000010805 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010806 return SDValue();
10807
Nadav Rotem43012222011-05-11 08:12:09 +000010808 // Optimize shl/srl/sra with constant shift amount.
10809 if (isSplatVector(Amt.getNode())) {
10810 SDValue SclrAmt = Amt->getOperand(0);
10811 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10812 uint64_t ShiftAmt = C->getZExtValue();
10813
Craig Toppered2e13d2012-01-22 19:15:14 +000010814 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10815 (Subtarget->hasAVX2() &&
10816 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10817 if (Op.getOpcode() == ISD::SHL)
10818 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10819 DAG.getConstant(ShiftAmt, MVT::i32));
10820 if (Op.getOpcode() == ISD::SRL)
10821 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10822 DAG.getConstant(ShiftAmt, MVT::i32));
10823 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10824 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10825 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010826 }
10827
Craig Toppered2e13d2012-01-22 19:15:14 +000010828 if (VT == MVT::v16i8) {
10829 if (Op.getOpcode() == ISD::SHL) {
10830 // Make a large shift.
10831 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10832 DAG.getConstant(ShiftAmt, MVT::i32));
10833 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10834 // Zero out the rightmost bits.
10835 SmallVector<SDValue, 16> V(16,
10836 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10837 MVT::i8));
10838 return DAG.getNode(ISD::AND, dl, VT, SHL,
10839 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010840 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010841 if (Op.getOpcode() == ISD::SRL) {
10842 // Make a large shift.
10843 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10844 DAG.getConstant(ShiftAmt, MVT::i32));
10845 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10846 // Zero out the leftmost bits.
10847 SmallVector<SDValue, 16> V(16,
10848 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10849 MVT::i8));
10850 return DAG.getNode(ISD::AND, dl, VT, SRL,
10851 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10852 }
10853 if (Op.getOpcode() == ISD::SRA) {
10854 if (ShiftAmt == 7) {
10855 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010856 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010857 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010858 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010859
Craig Toppered2e13d2012-01-22 19:15:14 +000010860 // R s>> a === ((R u>> a) ^ m) - m
10861 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10862 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10863 MVT::i8));
10864 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10865 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10866 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10867 return Res;
10868 }
Craig Topper731dfd02012-04-23 03:42:40 +000010869 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010870 }
Craig Topper46154eb2011-11-11 07:39:23 +000010871
Craig Topper0d86d462011-11-20 00:12:05 +000010872 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10873 if (Op.getOpcode() == ISD::SHL) {
10874 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010875 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10876 DAG.getConstant(ShiftAmt, MVT::i32));
10877 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010878 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010879 SmallVector<SDValue, 32> V(32,
10880 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10881 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010882 return DAG.getNode(ISD::AND, dl, VT, SHL,
10883 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010884 }
Craig Topper0d86d462011-11-20 00:12:05 +000010885 if (Op.getOpcode() == ISD::SRL) {
10886 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010887 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10888 DAG.getConstant(ShiftAmt, MVT::i32));
10889 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010890 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010891 SmallVector<SDValue, 32> V(32,
10892 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10893 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010894 return DAG.getNode(ISD::AND, dl, VT, SRL,
10895 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10896 }
10897 if (Op.getOpcode() == ISD::SRA) {
10898 if (ShiftAmt == 7) {
10899 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010900 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010901 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010902 }
10903
10904 // R s>> a === ((R u>> a) ^ m) - m
10905 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10906 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10907 MVT::i8));
10908 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10909 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10910 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10911 return Res;
10912 }
Craig Topper731dfd02012-04-23 03:42:40 +000010913 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010914 }
Nadav Rotem43012222011-05-11 08:12:09 +000010915 }
10916 }
10917
10918 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010919 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010920 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10921 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010922
Chris Lattner7302d802012-02-06 21:56:39 +000010923 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10924 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010925 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10926 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010927 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010928 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010929
10930 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010931 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010932 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10933 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10934 }
Nadav Rotem43012222011-05-11 08:12:09 +000010935 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010936 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010937
Nate Begeman51409212010-07-28 00:21:48 +000010938 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010939 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10940 DAG.getConstant(5, MVT::i32));
10941 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010942
Lang Hames8b99c1e2011-12-17 01:08:46 +000010943 // Turn 'a' into a mask suitable for VSELECT
10944 SDValue VSelM = DAG.getConstant(0x80, VT);
10945 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010946 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010947
Lang Hames8b99c1e2011-12-17 01:08:46 +000010948 SDValue CM1 = DAG.getConstant(0x0f, VT);
10949 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010950
Lang Hames8b99c1e2011-12-17 01:08:46 +000010951 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10952 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010953 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10954 DAG.getConstant(4, MVT::i32), DAG);
10955 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010956 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10957
Nate Begeman51409212010-07-28 00:21:48 +000010958 // a += a
10959 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010960 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010961 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010962
Lang Hames8b99c1e2011-12-17 01:08:46 +000010963 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10964 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010965 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10966 DAG.getConstant(2, MVT::i32), DAG);
10967 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010968 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10969
Nate Begeman51409212010-07-28 00:21:48 +000010970 // a += a
10971 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010972 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010973 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010974
Lang Hames8b99c1e2011-12-17 01:08:46 +000010975 // return VSELECT(r, r+r, a);
10976 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010977 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010978 return R;
10979 }
Craig Topper46154eb2011-11-11 07:39:23 +000010980
10981 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010982 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010983 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010984 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10985 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10986
10987 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010988 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10989 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010990
10991 // Recreate the shift amount vectors
10992 SDValue Amt1, Amt2;
10993 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10994 // Constant shift amount
10995 SmallVector<SDValue, 4> Amt1Csts;
10996 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010997 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010998 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010999 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011000 Amt2Csts.push_back(Amt->getOperand(i));
11001
11002 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11003 &Amt1Csts[0], NumElems/2);
11004 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11005 &Amt2Csts[0], NumElems/2);
11006 } else {
11007 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011008 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11009 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011010 }
11011
11012 // Issue new vector shifts for the smaller types
11013 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11014 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11015
11016 // Concatenate the result back
11017 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11018 }
11019
Nate Begeman51409212010-07-28 00:21:48 +000011020 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011021}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011022
Craig Topper55b24052012-09-11 06:15:32 +000011023static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011024 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11025 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011026 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11027 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011028 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011029 SDValue LHS = N->getOperand(0);
11030 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011031 unsigned BaseOp = 0;
11032 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011033 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011034 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011035 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011036 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011037 // A subtract of one will be selected as a INC. Note that INC doesn't
11038 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011039 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11040 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011041 BaseOp = X86ISD::INC;
11042 Cond = X86::COND_O;
11043 break;
11044 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011045 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011046 Cond = X86::COND_O;
11047 break;
11048 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011049 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011050 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011051 break;
11052 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011053 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11054 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11056 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011057 BaseOp = X86ISD::DEC;
11058 Cond = X86::COND_O;
11059 break;
11060 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011061 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011062 Cond = X86::COND_O;
11063 break;
11064 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011065 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011066 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011067 break;
11068 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011069 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011070 Cond = X86::COND_O;
11071 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011072 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11073 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11074 MVT::i32);
11075 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011076
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011077 SDValue SetCC =
11078 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11079 DAG.getConstant(X86::COND_O, MVT::i32),
11080 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011081
Dan Gohman6e5fda22011-07-22 18:45:15 +000011082 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011083 }
Bill Wendling74c37652008-12-09 22:08:41 +000011084 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011085
Bill Wendling61edeb52008-12-02 01:06:39 +000011086 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011087 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011088 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011089
Bill Wendling61edeb52008-12-02 01:06:39 +000011090 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011091 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11092 DAG.getConstant(Cond, MVT::i32),
11093 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011094
Dan Gohman6e5fda22011-07-22 18:45:15 +000011095 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011096}
11097
Chad Rosier30450e82011-12-22 22:35:21 +000011098SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11099 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011100 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011101 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11102 EVT VT = Op.getValueType();
11103
Craig Toppered2e13d2012-01-22 19:15:14 +000011104 if (!Subtarget->hasSSE2() || !VT.isVector())
11105 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011106
Craig Toppered2e13d2012-01-22 19:15:14 +000011107 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11108 ExtraVT.getScalarType().getSizeInBits();
11109 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11110
11111 switch (VT.getSimpleVT().SimpleTy) {
11112 default: return SDValue();
11113 case MVT::v8i32:
11114 case MVT::v16i16:
11115 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011116 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011117 if (!Subtarget->hasAVX2()) {
11118 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011119 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011120
Craig Toppered2e13d2012-01-22 19:15:14 +000011121 // Extract the LHS vectors
11122 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011123 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11124 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011125
Craig Toppered2e13d2012-01-22 19:15:14 +000011126 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11127 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011128
Craig Toppered2e13d2012-01-22 19:15:14 +000011129 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011130 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011131 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11132 ExtraNumElems/2);
11133 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011134
Craig Toppered2e13d2012-01-22 19:15:14 +000011135 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11136 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011137
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011138 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011139 }
11140 // fall through
11141 case MVT::v4i32:
11142 case MVT::v8i16: {
11143 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11144 Op.getOperand(0), ShAmt, DAG);
11145 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011146 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011147 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011148}
11149
11150
Craig Topper55b24052012-09-11 06:15:32 +000011151static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11152 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011153 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011154
Eric Christopher77ed1352011-07-08 00:04:56 +000011155 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11156 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011157 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011158 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011159 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011160 SDValue Ops[] = {
11161 DAG.getRegister(X86::ESP, MVT::i32), // Base
11162 DAG.getTargetConstant(1, MVT::i8), // Scale
11163 DAG.getRegister(0, MVT::i32), // Index
11164 DAG.getTargetConstant(0, MVT::i32), // Disp
11165 DAG.getRegister(0, MVT::i32), // Segment.
11166 Zero,
11167 Chain
11168 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011169 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011170 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11171 array_lengthof(Ops));
11172 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011173 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011174
Eric Christopher9a9d2752010-07-22 02:48:34 +000011175 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011176 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011177 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011178
Chris Lattner132929a2010-08-14 17:26:09 +000011179 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11180 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11181 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11182 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011183
Chris Lattner132929a2010-08-14 17:26:09 +000011184 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11185 if (!Op1 && !Op2 && !Op3 && Op4)
11186 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011187
Chris Lattner132929a2010-08-14 17:26:09 +000011188 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11189 if (Op1 && !Op2 && !Op3 && !Op4)
11190 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011191
11192 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011193 // (MFENCE)>;
11194 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011195}
11196
Craig Topper55b24052012-09-11 06:15:32 +000011197static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11198 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011199 DebugLoc dl = Op.getDebugLoc();
11200 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11201 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11202 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11203 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11204
11205 // The only fence that needs an instruction is a sequentially-consistent
11206 // cross-thread fence.
11207 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11208 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11209 // no-sse2). There isn't any reason to disable it if the target processor
11210 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011211 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011212 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11213
11214 SDValue Chain = Op.getOperand(0);
11215 SDValue Zero = DAG.getConstant(0, MVT::i32);
11216 SDValue Ops[] = {
11217 DAG.getRegister(X86::ESP, MVT::i32), // Base
11218 DAG.getTargetConstant(1, MVT::i8), // Scale
11219 DAG.getRegister(0, MVT::i32), // Index
11220 DAG.getTargetConstant(0, MVT::i32), // Disp
11221 DAG.getRegister(0, MVT::i32), // Segment.
11222 Zero,
11223 Chain
11224 };
11225 SDNode *Res =
11226 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11227 array_lengthof(Ops));
11228 return SDValue(Res, 0);
11229 }
11230
11231 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11232 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11233}
11234
11235
Craig Topper55b24052012-09-11 06:15:32 +000011236static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11237 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011238 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011239 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011240 unsigned Reg = 0;
11241 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011242 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011243 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011244 case MVT::i8: Reg = X86::AL; size = 1; break;
11245 case MVT::i16: Reg = X86::AX; size = 2; break;
11246 case MVT::i32: Reg = X86::EAX; size = 4; break;
11247 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011248 assert(Subtarget->is64Bit() && "Node not type legal!");
11249 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011250 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011251 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011252 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011253 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011254 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011255 Op.getOperand(1),
11256 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011257 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011258 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011259 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011260 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11261 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11262 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011263 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011264 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011265 return cpOut;
11266}
11267
Craig Topper55b24052012-09-11 06:15:32 +000011268static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11269 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011270 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011271 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011272 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011273 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011274 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011275 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11276 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011277 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011278 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11279 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011280 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011281 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011282 rdx.getValue(1)
11283 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011284 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011285}
11286
Craig Topper55b24052012-09-11 06:15:32 +000011287SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011288 EVT SrcVT = Op.getOperand(0).getValueType();
11289 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011290 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011291 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011292 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011293 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011294 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011295 // i64 <=> MMX conversions are Legal.
11296 if (SrcVT==MVT::i64 && DstVT.isVector())
11297 return Op;
11298 if (DstVT==MVT::i64 && SrcVT.isVector())
11299 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011300 // MMX <=> MMX conversions are Legal.
11301 if (SrcVT.isVector() && DstVT.isVector())
11302 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011303 // All other conversions need to be expanded.
11304 return SDValue();
11305}
Chris Lattner5b856542010-12-20 00:59:46 +000011306
Craig Topper55b24052012-09-11 06:15:32 +000011307static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011308 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011309 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011310 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011311 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011312 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011313 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011314 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011315 Node->getOperand(0),
11316 Node->getOperand(1), negOp,
11317 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011318 cast<AtomicSDNode>(Node)->getAlignment(),
11319 cast<AtomicSDNode>(Node)->getOrdering(),
11320 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011321}
11322
Eli Friedman327236c2011-08-24 20:50:09 +000011323static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11324 SDNode *Node = Op.getNode();
11325 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011326 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011327
11328 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011329 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11330 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11331 // (The only way to get a 16-byte store is cmpxchg16b)
11332 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11333 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11334 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011335 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11336 cast<AtomicSDNode>(Node)->getMemoryVT(),
11337 Node->getOperand(0),
11338 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011339 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011340 cast<AtomicSDNode>(Node)->getOrdering(),
11341 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011342 return Swap.getValue(1);
11343 }
11344 // Other atomic stores have a simple pattern.
11345 return Op;
11346}
11347
Chris Lattner5b856542010-12-20 00:59:46 +000011348static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11349 EVT VT = Op.getNode()->getValueType(0);
11350
11351 // Let legalize expand this if it isn't a legal type yet.
11352 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11353 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011354
Chris Lattner5b856542010-12-20 00:59:46 +000011355 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011356
Chris Lattner5b856542010-12-20 00:59:46 +000011357 unsigned Opc;
11358 bool ExtraOp = false;
11359 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011360 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011361 case ISD::ADDC: Opc = X86ISD::ADD; break;
11362 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11363 case ISD::SUBC: Opc = X86ISD::SUB; break;
11364 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11365 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011366
Chris Lattner5b856542010-12-20 00:59:46 +000011367 if (!ExtraOp)
11368 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11369 Op.getOperand(1));
11370 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11371 Op.getOperand(1), Op.getOperand(2));
11372}
11373
Evan Cheng0db9fe62006-04-25 20:13:52 +000011374/// LowerOperation - Provide custom lowering hooks for some operations.
11375///
Dan Gohmand858e902010-04-17 15:26:15 +000011376SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011377 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011378 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011379 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011380 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11381 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11382 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011383 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011384 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011385 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011386 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011387 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11388 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11389 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011390 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11391 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011392 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11393 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11394 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011395 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011396 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011397 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011398 case ISD::SHL_PARTS:
11399 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011400 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011401 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011402 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011403 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011404 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011405 case ISD::FABS: return LowerFABS(Op, DAG);
11406 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011407 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011408 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011409 case ISD::SETCC: return LowerSETCC(Op, DAG);
11410 case ISD::SELECT: return LowerSELECT(Op, DAG);
11411 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011412 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011413 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011414 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011415 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011416 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011417 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011418 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11419 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011420 case ISD::FRAME_TO_ARGS_OFFSET:
11421 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011422 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011423 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011424 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11425 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011426 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011427 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011428 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011429 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011430 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011431 case ISD::SRA:
11432 case ISD::SRL:
11433 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011434 case ISD::SADDO:
11435 case ISD::UADDO:
11436 case ISD::SSUBO:
11437 case ISD::USUBO:
11438 case ISD::SMULO:
11439 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011440 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011441 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011442 case ISD::ADDC:
11443 case ISD::ADDE:
11444 case ISD::SUBC:
11445 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011446 case ISD::ADD: return LowerADD(Op, DAG);
11447 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011448 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011449}
11450
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011451static void ReplaceATOMIC_LOAD(SDNode *Node,
11452 SmallVectorImpl<SDValue> &Results,
11453 SelectionDAG &DAG) {
11454 DebugLoc dl = Node->getDebugLoc();
11455 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11456
11457 // Convert wide load -> cmpxchg8b/cmpxchg16b
11458 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11459 // (The only way to get a 16-byte load is cmpxchg16b)
11460 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011461 SDValue Zero = DAG.getConstant(0, VT);
11462 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011463 Node->getOperand(0),
11464 Node->getOperand(1), Zero, Zero,
11465 cast<AtomicSDNode>(Node)->getMemOperand(),
11466 cast<AtomicSDNode>(Node)->getOrdering(),
11467 cast<AtomicSDNode>(Node)->getSynchScope());
11468 Results.push_back(Swap.getValue(0));
11469 Results.push_back(Swap.getValue(1));
11470}
11471
Craig Topperc0878702012-08-17 06:55:11 +000011472static void
Duncan Sands1607f052008-12-01 11:39:25 +000011473ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011474 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011475 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011476 assert (Node->getValueType(0) == MVT::i64 &&
11477 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011478
11479 SDValue Chain = Node->getOperand(0);
11480 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011481 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011482 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011483 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011484 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011485 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011486 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011487 SDValue Result =
11488 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11489 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011490 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011491 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011492 Results.push_back(Result.getValue(2));
11493}
11494
Duncan Sands126d9072008-07-04 11:47:58 +000011495/// ReplaceNodeResults - Replace a node with an illegal result type
11496/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011497void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11498 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011499 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011500 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011501 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011502 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011503 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011504 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011505 case ISD::ADDC:
11506 case ISD::ADDE:
11507 case ISD::SUBC:
11508 case ISD::SUBE:
11509 // We don't want to expand or promote these.
11510 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011511 case ISD::FP_TO_SINT:
11512 case ISD::FP_TO_UINT: {
11513 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11514
11515 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11516 return;
11517
Eli Friedman948e95a2009-05-23 09:59:16 +000011518 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011519 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011520 SDValue FIST = Vals.first, StackSlot = Vals.second;
11521 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011522 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011523 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011524 if (StackSlot.getNode() != 0)
11525 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11526 MachinePointerInfo(),
11527 false, false, false, 0));
11528 else
11529 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011530 }
11531 return;
11532 }
11533 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011534 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011535 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011536 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011537 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011538 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011539 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011540 eax.getValue(2));
11541 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11542 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011543 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011544 Results.push_back(edx.getValue(1));
11545 return;
11546 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011547 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011548 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011549 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011550 bool Regs64bit = T == MVT::i128;
11551 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011552 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011553 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11554 DAG.getConstant(0, HalfT));
11555 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11556 DAG.getConstant(1, HalfT));
11557 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11558 Regs64bit ? X86::RAX : X86::EAX,
11559 cpInL, SDValue());
11560 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11561 Regs64bit ? X86::RDX : X86::EDX,
11562 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011563 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011564 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11565 DAG.getConstant(0, HalfT));
11566 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11567 DAG.getConstant(1, HalfT));
11568 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11569 Regs64bit ? X86::RBX : X86::EBX,
11570 swapInL, cpInH.getValue(1));
11571 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011572 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011573 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011574 SDValue Ops[] = { swapInH.getValue(0),
11575 N->getOperand(1),
11576 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011577 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011578 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011579 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11580 X86ISD::LCMPXCHG8_DAG;
11581 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011582 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011583 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11584 Regs64bit ? X86::RAX : X86::EAX,
11585 HalfT, Result.getValue(1));
11586 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11587 Regs64bit ? X86::RDX : X86::EDX,
11588 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011589 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011590 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011591 Results.push_back(cpOutH.getValue(1));
11592 return;
11593 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011594 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011595 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011596 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011597 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011598 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011599 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011600 case ISD::ATOMIC_LOAD_MAX:
11601 case ISD::ATOMIC_LOAD_MIN:
11602 case ISD::ATOMIC_LOAD_UMAX:
11603 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011604 case ISD::ATOMIC_SWAP: {
11605 unsigned Opc;
11606 switch (N->getOpcode()) {
11607 default: llvm_unreachable("Unexpected opcode");
11608 case ISD::ATOMIC_LOAD_ADD:
11609 Opc = X86ISD::ATOMADD64_DAG;
11610 break;
11611 case ISD::ATOMIC_LOAD_AND:
11612 Opc = X86ISD::ATOMAND64_DAG;
11613 break;
11614 case ISD::ATOMIC_LOAD_NAND:
11615 Opc = X86ISD::ATOMNAND64_DAG;
11616 break;
11617 case ISD::ATOMIC_LOAD_OR:
11618 Opc = X86ISD::ATOMOR64_DAG;
11619 break;
11620 case ISD::ATOMIC_LOAD_SUB:
11621 Opc = X86ISD::ATOMSUB64_DAG;
11622 break;
11623 case ISD::ATOMIC_LOAD_XOR:
11624 Opc = X86ISD::ATOMXOR64_DAG;
11625 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011626 case ISD::ATOMIC_LOAD_MAX:
11627 Opc = X86ISD::ATOMMAX64_DAG;
11628 break;
11629 case ISD::ATOMIC_LOAD_MIN:
11630 Opc = X86ISD::ATOMMIN64_DAG;
11631 break;
11632 case ISD::ATOMIC_LOAD_UMAX:
11633 Opc = X86ISD::ATOMUMAX64_DAG;
11634 break;
11635 case ISD::ATOMIC_LOAD_UMIN:
11636 Opc = X86ISD::ATOMUMIN64_DAG;
11637 break;
Craig Topperc0878702012-08-17 06:55:11 +000011638 case ISD::ATOMIC_SWAP:
11639 Opc = X86ISD::ATOMSWAP64_DAG;
11640 break;
11641 }
11642 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011643 return;
Craig Topperc0878702012-08-17 06:55:11 +000011644 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011645 case ISD::ATOMIC_LOAD:
11646 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011647 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011648}
11649
Evan Cheng72261582005-12-20 06:22:03 +000011650const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11651 switch (Opcode) {
11652 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011653 case X86ISD::BSF: return "X86ISD::BSF";
11654 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011655 case X86ISD::SHLD: return "X86ISD::SHLD";
11656 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011657 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011658 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011659 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011660 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011661 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011662 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011663 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11664 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11665 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011666 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011667 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011668 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011669 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011670 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011671 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011672 case X86ISD::COMI: return "X86ISD::COMI";
11673 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011674 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011675 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011676 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11677 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011678 case X86ISD::CMOV: return "X86ISD::CMOV";
11679 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011680 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011681 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11682 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011683 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011684 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011685 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011686 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011687 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011688 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11689 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011690 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011691 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011692 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011693 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011694 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011695 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11696 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11697 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011698 case X86ISD::HADD: return "X86ISD::HADD";
11699 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011700 case X86ISD::FHADD: return "X86ISD::FHADD";
11701 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011702 case X86ISD::FMAX: return "X86ISD::FMAX";
11703 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011704 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11705 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011706 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11707 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011708 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011709 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011710 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011711 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011712 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011713 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011714 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011715 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11716 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011717 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11718 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11719 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11720 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11721 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11722 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011723 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011724 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011725 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011726 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011727 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11728 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011729 case X86ISD::VSHL: return "X86ISD::VSHL";
11730 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011731 case X86ISD::VSRA: return "X86ISD::VSRA";
11732 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11733 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11734 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011735 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011736 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11737 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011738 case X86ISD::ADD: return "X86ISD::ADD";
11739 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011740 case X86ISD::ADC: return "X86ISD::ADC";
11741 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011742 case X86ISD::SMUL: return "X86ISD::SMUL";
11743 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011744 case X86ISD::INC: return "X86ISD::INC";
11745 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011746 case X86ISD::OR: return "X86ISD::OR";
11747 case X86ISD::XOR: return "X86ISD::XOR";
11748 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011749 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011750 case X86ISD::BLSI: return "X86ISD::BLSI";
11751 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11752 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011753 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011754 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011755 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011756 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11757 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11758 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011759 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011760 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011761 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011762 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011763 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011764 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11765 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011766 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11767 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11768 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011769 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11770 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011771 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11772 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011773 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011774 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011775 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011776 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11777 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011778 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011779 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011780 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011781 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011782 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011783 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011784 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011785 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011786 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011787 case X86ISD::FMADD: return "X86ISD::FMADD";
11788 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11789 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11790 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11791 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11792 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011793 }
11794}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011795
Chris Lattnerc9addb72007-03-30 23:15:24 +000011796// isLegalAddressingMode - Return true if the addressing mode represented
11797// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011798bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011799 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011800 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011801 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011802 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011803
Chris Lattnerc9addb72007-03-30 23:15:24 +000011804 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011805 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011806 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011807
Chris Lattnerc9addb72007-03-30 23:15:24 +000011808 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011809 unsigned GVFlags =
11810 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011811
Chris Lattnerdfed4132009-07-10 07:38:24 +000011812 // If a reference to this global requires an extra load, we can't fold it.
11813 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011814 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011815
Chris Lattnerdfed4132009-07-10 07:38:24 +000011816 // If BaseGV requires a register for the PIC base, we cannot also have a
11817 // BaseReg specified.
11818 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011819 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011820
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011821 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011822 if ((M != CodeModel::Small || R != Reloc::Static) &&
11823 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011824 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011825 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011826
Chris Lattnerc9addb72007-03-30 23:15:24 +000011827 switch (AM.Scale) {
11828 case 0:
11829 case 1:
11830 case 2:
11831 case 4:
11832 case 8:
11833 // These scales always work.
11834 break;
11835 case 3:
11836 case 5:
11837 case 9:
11838 // These scales are formed with basereg+scalereg. Only accept if there is
11839 // no basereg yet.
11840 if (AM.HasBaseReg)
11841 return false;
11842 break;
11843 default: // Other stuff never works.
11844 return false;
11845 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011846
Chris Lattnerc9addb72007-03-30 23:15:24 +000011847 return true;
11848}
11849
11850
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011851bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011852 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011853 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011854 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11855 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011856 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011857 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011858 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011859}
11860
Evan Cheng70e10d32012-07-17 06:53:39 +000011861bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11862 return Imm == (int32_t)Imm;
11863}
11864
11865bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011866 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011867 return Imm == (int32_t)Imm;
11868}
11869
Owen Andersone50ed302009-08-10 22:56:29 +000011870bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011871 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011872 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011873 unsigned NumBits1 = VT1.getSizeInBits();
11874 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011875 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011876 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011877 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011878}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011879
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011880bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011881 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011882 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011883}
11884
Owen Andersone50ed302009-08-10 22:56:29 +000011885bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011886 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011887 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011888}
11889
Owen Andersone50ed302009-08-10 22:56:29 +000011890bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011891 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011892 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011893}
11894
Evan Cheng60c07e12006-07-05 22:17:51 +000011895/// isShuffleMaskLegal - Targets can use this to indicate that they only
11896/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11897/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11898/// are assumed to be legal.
11899bool
Eric Christopherfd179292009-08-27 18:07:15 +000011900X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011901 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011902 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011903 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011904 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011905
Nate Begemana09008b2009-10-19 02:17:23 +000011906 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011907 return (VT.getVectorNumElements() == 2 ||
11908 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11909 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011910 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011911 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011912 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11913 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011914 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011915 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11916 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011917 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11918 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011919}
11920
Dan Gohman7d8143f2008-04-09 20:09:42 +000011921bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011922X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011923 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011924 unsigned NumElts = VT.getVectorNumElements();
11925 // FIXME: This collection of masks seems suspect.
11926 if (NumElts == 2)
11927 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011928 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011929 return (isMOVLMask(Mask, VT) ||
11930 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011931 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11932 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011933 }
11934 return false;
11935}
11936
11937//===----------------------------------------------------------------------===//
11938// X86 Scheduler Hooks
11939//===----------------------------------------------------------------------===//
11940
Mon P Wang63307c32008-05-05 19:05:59 +000011941// private utility function
Scott Michelfdc40a02009-02-17 22:15:04 +000011942
Michael Liaob118a072012-09-20 03:06:15 +000011943// Get CMPXCHG opcode for the specified data type.
11944static unsigned getCmpXChgOpcode(EVT VT) {
11945 switch (VT.getSimpleVT().SimpleTy) {
11946 case MVT::i8: return X86::LCMPXCHG8;
11947 case MVT::i16: return X86::LCMPXCHG16;
11948 case MVT::i32: return X86::LCMPXCHG32;
11949 case MVT::i64: return X86::LCMPXCHG64;
11950 default:
11951 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000011952 }
Michael Liaob118a072012-09-20 03:06:15 +000011953 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000011954}
11955
Michael Liaob118a072012-09-20 03:06:15 +000011956// Get LOAD opcode for the specified data type.
11957static unsigned getLoadOpcode(EVT VT) {
11958 switch (VT.getSimpleVT().SimpleTy) {
11959 case MVT::i8: return X86::MOV8rm;
11960 case MVT::i16: return X86::MOV16rm;
11961 case MVT::i32: return X86::MOV32rm;
11962 case MVT::i64: return X86::MOV64rm;
11963 default:
11964 break;
11965 }
11966 llvm_unreachable("Invalid operand size!");
11967}
11968
11969// Get opcode of the non-atomic one from the specified atomic instruction.
11970static unsigned getNonAtomicOpcode(unsigned Opc) {
11971 switch (Opc) {
11972 case X86::ATOMAND8: return X86::AND8rr;
11973 case X86::ATOMAND16: return X86::AND16rr;
11974 case X86::ATOMAND32: return X86::AND32rr;
11975 case X86::ATOMAND64: return X86::AND64rr;
11976 case X86::ATOMOR8: return X86::OR8rr;
11977 case X86::ATOMOR16: return X86::OR16rr;
11978 case X86::ATOMOR32: return X86::OR32rr;
11979 case X86::ATOMOR64: return X86::OR64rr;
11980 case X86::ATOMXOR8: return X86::XOR8rr;
11981 case X86::ATOMXOR16: return X86::XOR16rr;
11982 case X86::ATOMXOR32: return X86::XOR32rr;
11983 case X86::ATOMXOR64: return X86::XOR64rr;
11984 }
11985 llvm_unreachable("Unhandled atomic-load-op opcode!");
11986}
11987
11988// Get opcode of the non-atomic one from the specified atomic instruction with
11989// extra opcode.
11990static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
11991 unsigned &ExtraOpc) {
11992 switch (Opc) {
11993 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
11994 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
11995 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
11996 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011997 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011998 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
11999 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12000 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012001 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012002 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12003 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12004 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012005 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012006 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12007 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12008 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012009 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012010 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12011 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12012 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12013 }
12014 llvm_unreachable("Unhandled atomic-load-op opcode!");
12015}
12016
12017// Get opcode of the non-atomic one from the specified atomic instruction for
12018// 64-bit data type on 32-bit target.
12019static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12020 switch (Opc) {
12021 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12022 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12023 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12024 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12025 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12026 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012027 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12028 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12029 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12030 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012031 }
12032 llvm_unreachable("Unhandled atomic-load-op opcode!");
12033}
12034
12035// Get opcode of the non-atomic one from the specified atomic instruction for
12036// 64-bit data type on 32-bit target with extra opcode.
12037static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12038 unsigned &HiOpc,
12039 unsigned &ExtraOpc) {
12040 switch (Opc) {
12041 case X86::ATOMNAND6432:
12042 ExtraOpc = X86::NOT32r;
12043 HiOpc = X86::AND32rr;
12044 return X86::AND32rr;
12045 }
12046 llvm_unreachable("Unhandled atomic-load-op opcode!");
12047}
12048
12049// Get pseudo CMOV opcode from the specified data type.
12050static unsigned getPseudoCMOVOpc(EVT VT) {
12051 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012052 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012053 case MVT::i16: return X86::CMOV_GR16;
12054 case MVT::i32: return X86::CMOV_GR32;
12055 default:
12056 break;
12057 }
12058 llvm_unreachable("Unknown CMOV opcode!");
12059}
12060
12061// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12062// They will be translated into a spin-loop or compare-exchange loop from
12063//
12064// ...
12065// dst = atomic-fetch-op MI.addr, MI.val
12066// ...
12067//
12068// to
12069//
12070// ...
12071// EAX = LOAD MI.addr
12072// loop:
12073// t1 = OP MI.val, EAX
12074// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12075// JNE loop
12076// sink:
12077// dst = EAX
12078// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012079MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012080X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12081 MachineBasicBlock *MBB) const {
12082 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12083 DebugLoc DL = MI->getDebugLoc();
12084
12085 MachineFunction *MF = MBB->getParent();
12086 MachineRegisterInfo &MRI = MF->getRegInfo();
12087
12088 const BasicBlock *BB = MBB->getBasicBlock();
12089 MachineFunction::iterator I = MBB;
12090 ++I;
12091
12092 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12093 "Unexpected number of operands");
12094
12095 assert(MI->hasOneMemOperand() &&
12096 "Expected atomic-load-op to have one memoperand");
12097
12098 // Memory Reference
12099 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12100 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12101
12102 unsigned DstReg, SrcReg;
12103 unsigned MemOpndSlot;
12104
12105 unsigned CurOp = 0;
12106
12107 DstReg = MI->getOperand(CurOp++).getReg();
12108 MemOpndSlot = CurOp;
12109 CurOp += X86::AddrNumOperands;
12110 SrcReg = MI->getOperand(CurOp++).getReg();
12111
12112 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
12113 EVT VT = *RC->vt_begin();
12114 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12115
12116 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12117 unsigned LOADOpc = getLoadOpcode(VT);
12118
12119 // For the atomic load-arith operator, we generate
12120 //
12121 // thisMBB:
12122 // EAX = LOAD [MI.addr]
12123 // mainMBB:
12124 // t1 = OP MI.val, EAX
12125 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12126 // JNE mainMBB
12127 // sinkMBB:
12128
12129 MachineBasicBlock *thisMBB = MBB;
12130 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12131 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12132 MF->insert(I, mainMBB);
12133 MF->insert(I, sinkMBB);
12134
12135 MachineInstrBuilder MIB;
12136
12137 // Transfer the remainder of BB and its successor edges to sinkMBB.
12138 sinkMBB->splice(sinkMBB->begin(), MBB,
12139 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12140 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12141
12142 // thisMBB:
12143 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12144 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12145 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12146 MIB.setMemRefs(MMOBegin, MMOEnd);
12147
12148 thisMBB->addSuccessor(mainMBB);
12149
12150 // mainMBB:
12151 MachineBasicBlock *origMainMBB = mainMBB;
12152 mainMBB->addLiveIn(AccPhyReg);
12153
12154 // Copy AccPhyReg as it is used more than once.
12155 unsigned AccReg = MRI.createVirtualRegister(RC);
12156 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12157 .addReg(AccPhyReg);
12158
12159 unsigned t1 = MRI.createVirtualRegister(RC);
12160 unsigned Opc = MI->getOpcode();
12161 switch (Opc) {
12162 default:
12163 llvm_unreachable("Unhandled atomic-load-op opcode!");
12164 case X86::ATOMAND8:
12165 case X86::ATOMAND16:
12166 case X86::ATOMAND32:
12167 case X86::ATOMAND64:
12168 case X86::ATOMOR8:
12169 case X86::ATOMOR16:
12170 case X86::ATOMOR32:
12171 case X86::ATOMOR64:
12172 case X86::ATOMXOR8:
12173 case X86::ATOMXOR16:
12174 case X86::ATOMXOR32:
12175 case X86::ATOMXOR64: {
12176 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12177 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12178 .addReg(AccReg);
12179 break;
12180 }
12181 case X86::ATOMNAND8:
12182 case X86::ATOMNAND16:
12183 case X86::ATOMNAND32:
12184 case X86::ATOMNAND64: {
12185 unsigned t2 = MRI.createVirtualRegister(RC);
12186 unsigned NOTOpc;
12187 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12188 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12189 .addReg(AccReg);
12190 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12191 break;
12192 }
Michael Liao08382492012-09-21 03:00:17 +000012193 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012194 case X86::ATOMMAX16:
12195 case X86::ATOMMAX32:
12196 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012197 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012198 case X86::ATOMMIN16:
12199 case X86::ATOMMIN32:
12200 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012201 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012202 case X86::ATOMUMAX16:
12203 case X86::ATOMUMAX32:
12204 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012205 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012206 case X86::ATOMUMIN16:
12207 case X86::ATOMUMIN32:
12208 case X86::ATOMUMIN64: {
12209 unsigned CMPOpc;
12210 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12211
12212 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12213 .addReg(SrcReg)
12214 .addReg(AccReg);
12215
12216 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012217 if (VT != MVT::i8) {
12218 // Native support
12219 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12220 .addReg(SrcReg)
12221 .addReg(AccReg);
12222 } else {
12223 // Promote i8 to i32 to use CMOV32
12224 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12225 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12226 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12227 unsigned t2 = MRI.createVirtualRegister(RC32);
12228
12229 unsigned Undef = MRI.createVirtualRegister(RC32);
12230 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12231
12232 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12233 .addReg(Undef)
12234 .addReg(SrcReg)
12235 .addImm(X86::sub_8bit);
12236 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12237 .addReg(Undef)
12238 .addReg(AccReg)
12239 .addImm(X86::sub_8bit);
12240
12241 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12242 .addReg(SrcReg32)
12243 .addReg(AccReg32);
12244
12245 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12246 .addReg(t2, 0, X86::sub_8bit);
12247 }
Michael Liaob118a072012-09-20 03:06:15 +000012248 } else {
12249 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012250 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012251 "Invalid atomic-load-op transformation!");
12252 unsigned SelOpc = getPseudoCMOVOpc(VT);
12253 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12254 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12255 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12256 .addReg(SrcReg).addReg(AccReg)
12257 .addImm(CC);
12258 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12259 }
12260 break;
12261 }
12262 }
12263
12264 // Copy AccPhyReg back from virtual register.
12265 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12266 .addReg(AccReg);
12267
12268 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12269 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12270 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12271 MIB.addReg(t1);
12272 MIB.setMemRefs(MMOBegin, MMOEnd);
12273
12274 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12275
12276 mainMBB->addSuccessor(origMainMBB);
12277 mainMBB->addSuccessor(sinkMBB);
12278
12279 // sinkMBB:
12280 sinkMBB->addLiveIn(AccPhyReg);
12281
12282 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12283 TII->get(TargetOpcode::COPY), DstReg)
12284 .addReg(AccPhyReg);
12285
12286 MI->eraseFromParent();
12287 return sinkMBB;
12288}
12289
12290// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12291// instructions. They will be translated into a spin-loop or compare-exchange
12292// loop from
12293//
12294// ...
12295// dst = atomic-fetch-op MI.addr, MI.val
12296// ...
12297//
12298// to
12299//
12300// ...
12301// EAX = LOAD [MI.addr + 0]
12302// EDX = LOAD [MI.addr + 4]
12303// loop:
12304// EBX = OP MI.val.lo, EAX
12305// ECX = OP MI.val.hi, EDX
12306// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12307// JNE loop
12308// sink:
12309// dst = EDX:EAX
12310// ...
12311MachineBasicBlock *
12312X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12313 MachineBasicBlock *MBB) const {
12314 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12315 DebugLoc DL = MI->getDebugLoc();
12316
12317 MachineFunction *MF = MBB->getParent();
12318 MachineRegisterInfo &MRI = MF->getRegInfo();
12319
12320 const BasicBlock *BB = MBB->getBasicBlock();
12321 MachineFunction::iterator I = MBB;
12322 ++I;
12323
12324 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12325 "Unexpected number of operands");
12326
12327 assert(MI->hasOneMemOperand() &&
12328 "Expected atomic-load-op32 to have one memoperand");
12329
12330 // Memory Reference
12331 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12332 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12333
12334 unsigned DstLoReg, DstHiReg;
12335 unsigned SrcLoReg, SrcHiReg;
12336 unsigned MemOpndSlot;
12337
12338 unsigned CurOp = 0;
12339
12340 DstLoReg = MI->getOperand(CurOp++).getReg();
12341 DstHiReg = MI->getOperand(CurOp++).getReg();
12342 MemOpndSlot = CurOp;
12343 CurOp += X86::AddrNumOperands;
12344 SrcLoReg = MI->getOperand(CurOp++).getReg();
12345 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012346
Craig Topperc9099502012-04-20 06:31:50 +000012347 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012348 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012349
Michael Liaob118a072012-09-20 03:06:15 +000012350 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12351 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012352
Michael Liaob118a072012-09-20 03:06:15 +000012353 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012354 //
Michael Liaob118a072012-09-20 03:06:15 +000012355 // thisMBB:
12356 // EAX = LOAD [MI.addr + 0]
12357 // EDX = LOAD [MI.addr + 4]
12358 // mainMBB:
12359 // EBX = OP MI.vallo, EAX
12360 // ECX = OP MI.valhi, EDX
12361 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12362 // JNE mainMBB
12363 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012364
Mon P Wang63307c32008-05-05 19:05:59 +000012365 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012366 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12367 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12368 MF->insert(I, mainMBB);
12369 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012370
Michael Liaob118a072012-09-20 03:06:15 +000012371 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012372
Michael Liaob118a072012-09-20 03:06:15 +000012373 // Transfer the remainder of BB and its successor edges to sinkMBB.
12374 sinkMBB->splice(sinkMBB->begin(), MBB,
12375 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12376 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012377
Michael Liaob118a072012-09-20 03:06:15 +000012378 // thisMBB:
12379 // Lo
12380 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12381 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12382 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12383 MIB.setMemRefs(MMOBegin, MMOEnd);
12384 // Hi
12385 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12386 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
12387 if (i == X86::AddrDisp)
12388 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
12389 else
12390 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12391 }
12392 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012393
Michael Liaob118a072012-09-20 03:06:15 +000012394 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012395
Michael Liaob118a072012-09-20 03:06:15 +000012396 // mainMBB:
12397 MachineBasicBlock *origMainMBB = mainMBB;
12398 mainMBB->addLiveIn(X86::EAX);
12399 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012400
Michael Liaob118a072012-09-20 03:06:15 +000012401 // Copy EDX:EAX as they are used more than once.
12402 unsigned LoReg = MRI.createVirtualRegister(RC);
12403 unsigned HiReg = MRI.createVirtualRegister(RC);
12404 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12405 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012406
Michael Liaob118a072012-09-20 03:06:15 +000012407 unsigned t1L = MRI.createVirtualRegister(RC);
12408 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012409
Michael Liaob118a072012-09-20 03:06:15 +000012410 unsigned Opc = MI->getOpcode();
12411 switch (Opc) {
12412 default:
12413 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12414 case X86::ATOMAND6432:
12415 case X86::ATOMOR6432:
12416 case X86::ATOMXOR6432:
12417 case X86::ATOMADD6432:
12418 case X86::ATOMSUB6432: {
12419 unsigned HiOpc;
12420 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12421 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg).addReg(LoReg);
12422 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg).addReg(HiReg);
12423 break;
12424 }
12425 case X86::ATOMNAND6432: {
12426 unsigned HiOpc, NOTOpc;
12427 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12428 unsigned t2L = MRI.createVirtualRegister(RC);
12429 unsigned t2H = MRI.createVirtualRegister(RC);
12430 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12431 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12432 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12433 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12434 break;
12435 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012436 case X86::ATOMMAX6432:
12437 case X86::ATOMMIN6432:
12438 case X86::ATOMUMAX6432:
12439 case X86::ATOMUMIN6432: {
12440 unsigned HiOpc;
12441 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12442 unsigned cL = MRI.createVirtualRegister(RC8);
12443 unsigned cH = MRI.createVirtualRegister(RC8);
12444 unsigned cL32 = MRI.createVirtualRegister(RC);
12445 unsigned cH32 = MRI.createVirtualRegister(RC);
12446 unsigned cc = MRI.createVirtualRegister(RC);
12447 // cl := cmp src_lo, lo
12448 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12449 .addReg(SrcLoReg).addReg(LoReg);
12450 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12451 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12452 // ch := cmp src_hi, hi
12453 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12454 .addReg(SrcHiReg).addReg(HiReg);
12455 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12456 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12457 // cc := if (src_hi == hi) ? cl : ch;
12458 if (Subtarget->hasCMov()) {
12459 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12460 .addReg(cH32).addReg(cL32);
12461 } else {
12462 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12463 .addReg(cH32).addReg(cL32)
12464 .addImm(X86::COND_E);
12465 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12466 }
12467 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12468 if (Subtarget->hasCMov()) {
12469 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12470 .addReg(SrcLoReg).addReg(LoReg);
12471 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12472 .addReg(SrcHiReg).addReg(HiReg);
12473 } else {
12474 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12475 .addReg(SrcLoReg).addReg(LoReg)
12476 .addImm(X86::COND_NE);
12477 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12478 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12479 .addReg(SrcHiReg).addReg(HiReg)
12480 .addImm(X86::COND_NE);
12481 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12482 }
12483 break;
12484 }
Michael Liaob118a072012-09-20 03:06:15 +000012485 case X86::ATOMSWAP6432: {
12486 unsigned HiOpc;
12487 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12488 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12489 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12490 break;
12491 }
12492 }
Mon P Wang63307c32008-05-05 19:05:59 +000012493
Michael Liaob118a072012-09-20 03:06:15 +000012494 // Copy EDX:EAX back from HiReg:LoReg
12495 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12496 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12497 // Copy ECX:EBX from t1H:t1L
12498 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12499 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012500
Michael Liaob118a072012-09-20 03:06:15 +000012501 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12502 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12503 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12504 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012505
Michael Liaob118a072012-09-20 03:06:15 +000012506 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012507
Michael Liaob118a072012-09-20 03:06:15 +000012508 mainMBB->addSuccessor(origMainMBB);
12509 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012510
Michael Liaob118a072012-09-20 03:06:15 +000012511 // sinkMBB:
12512 sinkMBB->addLiveIn(X86::EAX);
12513 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012514
Michael Liaob118a072012-09-20 03:06:15 +000012515 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12516 TII->get(TargetOpcode::COPY), DstLoReg)
12517 .addReg(X86::EAX);
12518 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12519 TII->get(TargetOpcode::COPY), DstHiReg)
12520 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012521
Michael Liaob118a072012-09-20 03:06:15 +000012522 MI->eraseFromParent();
12523 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012524}
12525
Eric Christopherf83a5de2009-08-27 18:08:16 +000012526// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012527// or XMM0_V32I8 in AVX all of this code can be replaced with that
12528// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012529MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012530X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012531 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012532 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012533 "Target must have SSE4.2 or AVX features enabled");
12534
Eric Christopherb120ab42009-08-18 22:50:32 +000012535 DebugLoc dl = MI->getDebugLoc();
12536 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012537 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012538 if (!Subtarget->hasAVX()) {
12539 if (memArg)
12540 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12541 else
12542 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12543 } else {
12544 if (memArg)
12545 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12546 else
12547 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12548 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012549
Eric Christopher41c902f2010-11-30 08:20:21 +000012550 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012551 for (unsigned i = 0; i < numArgs; ++i) {
12552 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012553 if (!(Op.isReg() && Op.isImplicit()))
12554 MIB.addOperand(Op);
12555 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012556 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012557 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012558 .addReg(X86::XMM0);
12559
Dan Gohman14152b42010-07-06 20:24:04 +000012560 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012561 return BB;
12562}
12563
12564MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012565X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012566 DebugLoc dl = MI->getDebugLoc();
12567 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012568
Eric Christopher228232b2010-11-30 07:20:12 +000012569 // Address into RAX/EAX, other two args into ECX, EDX.
12570 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12571 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12572 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12573 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012574 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012575
Eric Christopher228232b2010-11-30 07:20:12 +000012576 unsigned ValOps = X86::AddrNumOperands;
12577 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12578 .addReg(MI->getOperand(ValOps).getReg());
12579 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12580 .addReg(MI->getOperand(ValOps+1).getReg());
12581
12582 // The instruction doesn't actually take any operands though.
12583 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012584
Eric Christopher228232b2010-11-30 07:20:12 +000012585 MI->eraseFromParent(); // The pseudo is gone now.
12586 return BB;
12587}
12588
12589MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012590X86TargetLowering::EmitVAARG64WithCustomInserter(
12591 MachineInstr *MI,
12592 MachineBasicBlock *MBB) const {
12593 // Emit va_arg instruction on X86-64.
12594
12595 // Operands to this pseudo-instruction:
12596 // 0 ) Output : destination address (reg)
12597 // 1-5) Input : va_list address (addr, i64mem)
12598 // 6 ) ArgSize : Size (in bytes) of vararg type
12599 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12600 // 8 ) Align : Alignment of type
12601 // 9 ) EFLAGS (implicit-def)
12602
12603 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12604 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12605
12606 unsigned DestReg = MI->getOperand(0).getReg();
12607 MachineOperand &Base = MI->getOperand(1);
12608 MachineOperand &Scale = MI->getOperand(2);
12609 MachineOperand &Index = MI->getOperand(3);
12610 MachineOperand &Disp = MI->getOperand(4);
12611 MachineOperand &Segment = MI->getOperand(5);
12612 unsigned ArgSize = MI->getOperand(6).getImm();
12613 unsigned ArgMode = MI->getOperand(7).getImm();
12614 unsigned Align = MI->getOperand(8).getImm();
12615
12616 // Memory Reference
12617 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12618 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12619 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12620
12621 // Machine Information
12622 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12623 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12624 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12625 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12626 DebugLoc DL = MI->getDebugLoc();
12627
12628 // struct va_list {
12629 // i32 gp_offset
12630 // i32 fp_offset
12631 // i64 overflow_area (address)
12632 // i64 reg_save_area (address)
12633 // }
12634 // sizeof(va_list) = 24
12635 // alignment(va_list) = 8
12636
12637 unsigned TotalNumIntRegs = 6;
12638 unsigned TotalNumXMMRegs = 8;
12639 bool UseGPOffset = (ArgMode == 1);
12640 bool UseFPOffset = (ArgMode == 2);
12641 unsigned MaxOffset = TotalNumIntRegs * 8 +
12642 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12643
12644 /* Align ArgSize to a multiple of 8 */
12645 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12646 bool NeedsAlign = (Align > 8);
12647
12648 MachineBasicBlock *thisMBB = MBB;
12649 MachineBasicBlock *overflowMBB;
12650 MachineBasicBlock *offsetMBB;
12651 MachineBasicBlock *endMBB;
12652
12653 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12654 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12655 unsigned OffsetReg = 0;
12656
12657 if (!UseGPOffset && !UseFPOffset) {
12658 // If we only pull from the overflow region, we don't create a branch.
12659 // We don't need to alter control flow.
12660 OffsetDestReg = 0; // unused
12661 OverflowDestReg = DestReg;
12662
12663 offsetMBB = NULL;
12664 overflowMBB = thisMBB;
12665 endMBB = thisMBB;
12666 } else {
12667 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12668 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12669 // If not, pull from overflow_area. (branch to overflowMBB)
12670 //
12671 // thisMBB
12672 // | .
12673 // | .
12674 // offsetMBB overflowMBB
12675 // | .
12676 // | .
12677 // endMBB
12678
12679 // Registers for the PHI in endMBB
12680 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12681 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12682
12683 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12684 MachineFunction *MF = MBB->getParent();
12685 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12686 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12687 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12688
12689 MachineFunction::iterator MBBIter = MBB;
12690 ++MBBIter;
12691
12692 // Insert the new basic blocks
12693 MF->insert(MBBIter, offsetMBB);
12694 MF->insert(MBBIter, overflowMBB);
12695 MF->insert(MBBIter, endMBB);
12696
12697 // Transfer the remainder of MBB and its successor edges to endMBB.
12698 endMBB->splice(endMBB->begin(), thisMBB,
12699 llvm::next(MachineBasicBlock::iterator(MI)),
12700 thisMBB->end());
12701 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12702
12703 // Make offsetMBB and overflowMBB successors of thisMBB
12704 thisMBB->addSuccessor(offsetMBB);
12705 thisMBB->addSuccessor(overflowMBB);
12706
12707 // endMBB is a successor of both offsetMBB and overflowMBB
12708 offsetMBB->addSuccessor(endMBB);
12709 overflowMBB->addSuccessor(endMBB);
12710
12711 // Load the offset value into a register
12712 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12713 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12714 .addOperand(Base)
12715 .addOperand(Scale)
12716 .addOperand(Index)
12717 .addDisp(Disp, UseFPOffset ? 4 : 0)
12718 .addOperand(Segment)
12719 .setMemRefs(MMOBegin, MMOEnd);
12720
12721 // Check if there is enough room left to pull this argument.
12722 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12723 .addReg(OffsetReg)
12724 .addImm(MaxOffset + 8 - ArgSizeA8);
12725
12726 // Branch to "overflowMBB" if offset >= max
12727 // Fall through to "offsetMBB" otherwise
12728 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12729 .addMBB(overflowMBB);
12730 }
12731
12732 // In offsetMBB, emit code to use the reg_save_area.
12733 if (offsetMBB) {
12734 assert(OffsetReg != 0);
12735
12736 // Read the reg_save_area address.
12737 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12738 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12739 .addOperand(Base)
12740 .addOperand(Scale)
12741 .addOperand(Index)
12742 .addDisp(Disp, 16)
12743 .addOperand(Segment)
12744 .setMemRefs(MMOBegin, MMOEnd);
12745
12746 // Zero-extend the offset
12747 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12748 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12749 .addImm(0)
12750 .addReg(OffsetReg)
12751 .addImm(X86::sub_32bit);
12752
12753 // Add the offset to the reg_save_area to get the final address.
12754 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12755 .addReg(OffsetReg64)
12756 .addReg(RegSaveReg);
12757
12758 // Compute the offset for the next argument
12759 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12760 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12761 .addReg(OffsetReg)
12762 .addImm(UseFPOffset ? 16 : 8);
12763
12764 // Store it back into the va_list.
12765 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12766 .addOperand(Base)
12767 .addOperand(Scale)
12768 .addOperand(Index)
12769 .addDisp(Disp, UseFPOffset ? 4 : 0)
12770 .addOperand(Segment)
12771 .addReg(NextOffsetReg)
12772 .setMemRefs(MMOBegin, MMOEnd);
12773
12774 // Jump to endMBB
12775 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12776 .addMBB(endMBB);
12777 }
12778
12779 //
12780 // Emit code to use overflow area
12781 //
12782
12783 // Load the overflow_area address into a register.
12784 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12785 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12786 .addOperand(Base)
12787 .addOperand(Scale)
12788 .addOperand(Index)
12789 .addDisp(Disp, 8)
12790 .addOperand(Segment)
12791 .setMemRefs(MMOBegin, MMOEnd);
12792
12793 // If we need to align it, do so. Otherwise, just copy the address
12794 // to OverflowDestReg.
12795 if (NeedsAlign) {
12796 // Align the overflow address
12797 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12798 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12799
12800 // aligned_addr = (addr + (align-1)) & ~(align-1)
12801 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12802 .addReg(OverflowAddrReg)
12803 .addImm(Align-1);
12804
12805 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12806 .addReg(TmpReg)
12807 .addImm(~(uint64_t)(Align-1));
12808 } else {
12809 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12810 .addReg(OverflowAddrReg);
12811 }
12812
12813 // Compute the next overflow address after this argument.
12814 // (the overflow address should be kept 8-byte aligned)
12815 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12816 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12817 .addReg(OverflowDestReg)
12818 .addImm(ArgSizeA8);
12819
12820 // Store the new overflow address.
12821 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12822 .addOperand(Base)
12823 .addOperand(Scale)
12824 .addOperand(Index)
12825 .addDisp(Disp, 8)
12826 .addOperand(Segment)
12827 .addReg(NextAddrReg)
12828 .setMemRefs(MMOBegin, MMOEnd);
12829
12830 // If we branched, emit the PHI to the front of endMBB.
12831 if (offsetMBB) {
12832 BuildMI(*endMBB, endMBB->begin(), DL,
12833 TII->get(X86::PHI), DestReg)
12834 .addReg(OffsetDestReg).addMBB(offsetMBB)
12835 .addReg(OverflowDestReg).addMBB(overflowMBB);
12836 }
12837
12838 // Erase the pseudo instruction
12839 MI->eraseFromParent();
12840
12841 return endMBB;
12842}
12843
12844MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012845X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12846 MachineInstr *MI,
12847 MachineBasicBlock *MBB) const {
12848 // Emit code to save XMM registers to the stack. The ABI says that the
12849 // number of registers to save is given in %al, so it's theoretically
12850 // possible to do an indirect jump trick to avoid saving all of them,
12851 // however this code takes a simpler approach and just executes all
12852 // of the stores if %al is non-zero. It's less code, and it's probably
12853 // easier on the hardware branch predictor, and stores aren't all that
12854 // expensive anyway.
12855
12856 // Create the new basic blocks. One block contains all the XMM stores,
12857 // and one block is the final destination regardless of whether any
12858 // stores were performed.
12859 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12860 MachineFunction *F = MBB->getParent();
12861 MachineFunction::iterator MBBIter = MBB;
12862 ++MBBIter;
12863 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12864 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12865 F->insert(MBBIter, XMMSaveMBB);
12866 F->insert(MBBIter, EndMBB);
12867
Dan Gohman14152b42010-07-06 20:24:04 +000012868 // Transfer the remainder of MBB and its successor edges to EndMBB.
12869 EndMBB->splice(EndMBB->begin(), MBB,
12870 llvm::next(MachineBasicBlock::iterator(MI)),
12871 MBB->end());
12872 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12873
Dan Gohmand6708ea2009-08-15 01:38:56 +000012874 // The original block will now fall through to the XMM save block.
12875 MBB->addSuccessor(XMMSaveMBB);
12876 // The XMMSaveMBB will fall through to the end block.
12877 XMMSaveMBB->addSuccessor(EndMBB);
12878
12879 // Now add the instructions.
12880 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12881 DebugLoc DL = MI->getDebugLoc();
12882
12883 unsigned CountReg = MI->getOperand(0).getReg();
12884 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12885 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12886
12887 if (!Subtarget->isTargetWin64()) {
12888 // If %al is 0, branch around the XMM save block.
12889 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012890 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012891 MBB->addSuccessor(EndMBB);
12892 }
12893
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012894 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012895 // In the XMM save block, save all the XMM argument registers.
12896 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12897 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012898 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012899 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012900 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012901 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012902 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012903 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012904 .addFrameIndex(RegSaveFrameIndex)
12905 .addImm(/*Scale=*/1)
12906 .addReg(/*IndexReg=*/0)
12907 .addImm(/*Disp=*/Offset)
12908 .addReg(/*Segment=*/0)
12909 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012910 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012911 }
12912
Dan Gohman14152b42010-07-06 20:24:04 +000012913 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012914
12915 return EndMBB;
12916}
Mon P Wang63307c32008-05-05 19:05:59 +000012917
Lang Hames6e3f7e42012-02-03 01:13:49 +000012918// The EFLAGS operand of SelectItr might be missing a kill marker
12919// because there were multiple uses of EFLAGS, and ISel didn't know
12920// which to mark. Figure out whether SelectItr should have had a
12921// kill marker, and set it if it should. Returns the correct kill
12922// marker value.
12923static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12924 MachineBasicBlock* BB,
12925 const TargetRegisterInfo* TRI) {
12926 // Scan forward through BB for a use/def of EFLAGS.
12927 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12928 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012929 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012930 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012931 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012932 if (mi.definesRegister(X86::EFLAGS))
12933 break; // Should have kill-flag - update below.
12934 }
12935
12936 // If we hit the end of the block, check whether EFLAGS is live into a
12937 // successor.
12938 if (miI == BB->end()) {
12939 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12940 sEnd = BB->succ_end();
12941 sItr != sEnd; ++sItr) {
12942 MachineBasicBlock* succ = *sItr;
12943 if (succ->isLiveIn(X86::EFLAGS))
12944 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012945 }
12946 }
12947
Lang Hames6e3f7e42012-02-03 01:13:49 +000012948 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12949 // out. SelectMI should have a kill flag on EFLAGS.
12950 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012951 return true;
12952}
12953
Evan Cheng60c07e12006-07-05 22:17:51 +000012954MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012955X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012956 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012957 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12958 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012959
Chris Lattner52600972009-09-02 05:57:00 +000012960 // To "insert" a SELECT_CC instruction, we actually have to insert the
12961 // diamond control-flow pattern. The incoming instruction knows the
12962 // destination vreg to set, the condition code register to branch on, the
12963 // true/false values to select between, and a branch opcode to use.
12964 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12965 MachineFunction::iterator It = BB;
12966 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012967
Chris Lattner52600972009-09-02 05:57:00 +000012968 // thisMBB:
12969 // ...
12970 // TrueVal = ...
12971 // cmpTY ccX, r1, r2
12972 // bCC copy1MBB
12973 // fallthrough --> copy0MBB
12974 MachineBasicBlock *thisMBB = BB;
12975 MachineFunction *F = BB->getParent();
12976 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12977 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012978 F->insert(It, copy0MBB);
12979 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012980
Bill Wendling730c07e2010-06-25 20:48:10 +000012981 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12982 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012983 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12984 if (!MI->killsRegister(X86::EFLAGS) &&
12985 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12986 copy0MBB->addLiveIn(X86::EFLAGS);
12987 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012988 }
12989
Dan Gohman14152b42010-07-06 20:24:04 +000012990 // Transfer the remainder of BB and its successor edges to sinkMBB.
12991 sinkMBB->splice(sinkMBB->begin(), BB,
12992 llvm::next(MachineBasicBlock::iterator(MI)),
12993 BB->end());
12994 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12995
12996 // Add the true and fallthrough blocks as its successors.
12997 BB->addSuccessor(copy0MBB);
12998 BB->addSuccessor(sinkMBB);
12999
13000 // Create the conditional branch instruction.
13001 unsigned Opc =
13002 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13003 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13004
Chris Lattner52600972009-09-02 05:57:00 +000013005 // copy0MBB:
13006 // %FalseValue = ...
13007 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013008 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013009
Chris Lattner52600972009-09-02 05:57:00 +000013010 // sinkMBB:
13011 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13012 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013013 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13014 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013015 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13016 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13017
Dan Gohman14152b42010-07-06 20:24:04 +000013018 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013019 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013020}
13021
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013022MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013023X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13024 bool Is64Bit) const {
13025 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13026 DebugLoc DL = MI->getDebugLoc();
13027 MachineFunction *MF = BB->getParent();
13028 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13029
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013030 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013031
13032 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13033 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13034
13035 // BB:
13036 // ... [Till the alloca]
13037 // If stacklet is not large enough, jump to mallocMBB
13038 //
13039 // bumpMBB:
13040 // Allocate by subtracting from RSP
13041 // Jump to continueMBB
13042 //
13043 // mallocMBB:
13044 // Allocate by call to runtime
13045 //
13046 // continueMBB:
13047 // ...
13048 // [rest of original BB]
13049 //
13050
13051 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13052 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13053 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13054
13055 MachineRegisterInfo &MRI = MF->getRegInfo();
13056 const TargetRegisterClass *AddrRegClass =
13057 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13058
13059 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13060 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13061 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013062 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013063 sizeVReg = MI->getOperand(1).getReg(),
13064 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13065
13066 MachineFunction::iterator MBBIter = BB;
13067 ++MBBIter;
13068
13069 MF->insert(MBBIter, bumpMBB);
13070 MF->insert(MBBIter, mallocMBB);
13071 MF->insert(MBBIter, continueMBB);
13072
13073 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13074 (MachineBasicBlock::iterator(MI)), BB->end());
13075 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13076
13077 // Add code to the main basic block to check if the stack limit has been hit,
13078 // and if so, jump to mallocMBB otherwise to bumpMBB.
13079 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013080 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013081 .addReg(tmpSPVReg).addReg(sizeVReg);
13082 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013083 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013084 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013085 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13086
13087 // bumpMBB simply decreases the stack pointer, since we know the current
13088 // stacklet has enough space.
13089 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013090 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013091 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013092 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013093 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13094
13095 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013096 const uint32_t *RegMask =
13097 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013098 if (Is64Bit) {
13099 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13100 .addReg(sizeVReg);
13101 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013102 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013103 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013104 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013105 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013106 } else {
13107 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13108 .addImm(12);
13109 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13110 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013111 .addExternalSymbol("__morestack_allocate_stack_space")
13112 .addRegMask(RegMask)
13113 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013114 }
13115
13116 if (!Is64Bit)
13117 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13118 .addImm(16);
13119
13120 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13121 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13122 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13123
13124 // Set up the CFG correctly.
13125 BB->addSuccessor(bumpMBB);
13126 BB->addSuccessor(mallocMBB);
13127 mallocMBB->addSuccessor(continueMBB);
13128 bumpMBB->addSuccessor(continueMBB);
13129
13130 // Take care of the PHI nodes.
13131 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13132 MI->getOperand(0).getReg())
13133 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13134 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13135
13136 // Delete the original pseudo instruction.
13137 MI->eraseFromParent();
13138
13139 // And we're done.
13140 return continueMBB;
13141}
13142
13143MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013144X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013145 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013146 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13147 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013148
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013149 assert(!Subtarget->isTargetEnvMacho());
13150
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013151 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13152 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013153
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013154 if (Subtarget->isTargetWin64()) {
13155 if (Subtarget->isTargetCygMing()) {
13156 // ___chkstk(Mingw64):
13157 // Clobbers R10, R11, RAX and EFLAGS.
13158 // Updates RSP.
13159 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13160 .addExternalSymbol("___chkstk")
13161 .addReg(X86::RAX, RegState::Implicit)
13162 .addReg(X86::RSP, RegState::Implicit)
13163 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13164 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13165 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13166 } else {
13167 // __chkstk(MSVCRT): does not update stack pointer.
13168 // Clobbers R10, R11 and EFLAGS.
13169 // FIXME: RAX(allocated size) might be reused and not killed.
13170 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13171 .addExternalSymbol("__chkstk")
13172 .addReg(X86::RAX, RegState::Implicit)
13173 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13174 // RAX has the offset to subtracted from RSP.
13175 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13176 .addReg(X86::RSP)
13177 .addReg(X86::RAX);
13178 }
13179 } else {
13180 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013181 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13182
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013183 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13184 .addExternalSymbol(StackProbeSymbol)
13185 .addReg(X86::EAX, RegState::Implicit)
13186 .addReg(X86::ESP, RegState::Implicit)
13187 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13188 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13189 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13190 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013191
Dan Gohman14152b42010-07-06 20:24:04 +000013192 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013193 return BB;
13194}
Chris Lattner52600972009-09-02 05:57:00 +000013195
13196MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013197X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13198 MachineBasicBlock *BB) const {
13199 // This is pretty easy. We're taking the value that we received from
13200 // our load from the relocation, sticking it in either RDI (x86-64)
13201 // or EAX and doing an indirect call. The return value will then
13202 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013203 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013204 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013205 DebugLoc DL = MI->getDebugLoc();
13206 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013207
13208 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013209 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013210
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013211 // Get a register mask for the lowered call.
13212 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13213 // proper register mask.
13214 const uint32_t *RegMask =
13215 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013216 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013217 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13218 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013219 .addReg(X86::RIP)
13220 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013221 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013222 MI->getOperand(3).getTargetFlags())
13223 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013224 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013225 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013226 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013227 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013228 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13229 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013230 .addReg(0)
13231 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013232 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013233 MI->getOperand(3).getTargetFlags())
13234 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013235 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013236 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013237 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013238 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013239 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13240 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013241 .addReg(TII->getGlobalBaseReg(F))
13242 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013243 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013244 MI->getOperand(3).getTargetFlags())
13245 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013246 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013247 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013248 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013249 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013250
Dan Gohman14152b42010-07-06 20:24:04 +000013251 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013252 return BB;
13253}
13254
13255MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013256X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013257 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013258 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013259 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013260 case X86::TAILJMPd64:
13261 case X86::TAILJMPr64:
13262 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013263 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013264 case X86::TCRETURNdi64:
13265 case X86::TCRETURNri64:
13266 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013267 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013268 case X86::WIN_ALLOCA:
13269 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013270 case X86::SEG_ALLOCA_32:
13271 return EmitLoweredSegAlloca(MI, BB, false);
13272 case X86::SEG_ALLOCA_64:
13273 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013274 case X86::TLSCall_32:
13275 case X86::TLSCall_64:
13276 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013277 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013278 case X86::CMOV_FR32:
13279 case X86::CMOV_FR64:
13280 case X86::CMOV_V4F32:
13281 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013282 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013283 case X86::CMOV_V8F32:
13284 case X86::CMOV_V4F64:
13285 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013286 case X86::CMOV_GR16:
13287 case X86::CMOV_GR32:
13288 case X86::CMOV_RFP32:
13289 case X86::CMOV_RFP64:
13290 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013291 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013292
Dale Johannesen849f2142007-07-03 00:53:03 +000013293 case X86::FP32_TO_INT16_IN_MEM:
13294 case X86::FP32_TO_INT32_IN_MEM:
13295 case X86::FP32_TO_INT64_IN_MEM:
13296 case X86::FP64_TO_INT16_IN_MEM:
13297 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013298 case X86::FP64_TO_INT64_IN_MEM:
13299 case X86::FP80_TO_INT16_IN_MEM:
13300 case X86::FP80_TO_INT32_IN_MEM:
13301 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013302 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13303 DebugLoc DL = MI->getDebugLoc();
13304
Evan Cheng60c07e12006-07-05 22:17:51 +000013305 // Change the floating point control register to use "round towards zero"
13306 // mode when truncating to an integer value.
13307 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013308 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013309 addFrameReference(BuildMI(*BB, MI, DL,
13310 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013311
13312 // Load the old value of the high byte of the control word...
13313 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013314 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013315 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013316 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013317
13318 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013319 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013320 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013321
13322 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013323 addFrameReference(BuildMI(*BB, MI, DL,
13324 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013325
13326 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013327 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013328 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013329
13330 // Get the X86 opcode to use.
13331 unsigned Opc;
13332 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013333 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013334 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13335 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13336 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13337 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13338 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13339 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013340 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13341 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13342 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013343 }
13344
13345 X86AddressMode AM;
13346 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013347 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013348 AM.BaseType = X86AddressMode::RegBase;
13349 AM.Base.Reg = Op.getReg();
13350 } else {
13351 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013352 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013353 }
13354 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013355 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013356 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013357 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013358 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013359 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013360 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013361 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013362 AM.GV = Op.getGlobal();
13363 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013364 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013365 }
Dan Gohman14152b42010-07-06 20:24:04 +000013366 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013367 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013368
13369 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013370 addFrameReference(BuildMI(*BB, MI, DL,
13371 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013372
Dan Gohman14152b42010-07-06 20:24:04 +000013373 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013374 return BB;
13375 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013376 // String/text processing lowering.
13377 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013378 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013379 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013380 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013381 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013382 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013383 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013384 case X86::VPCMPESTRM128MEM: {
13385 unsigned NumArgs;
13386 bool MemArg;
13387 switch (MI->getOpcode()) {
13388 default: llvm_unreachable("illegal opcode!");
13389 case X86::PCMPISTRM128REG:
13390 case X86::VPCMPISTRM128REG:
13391 NumArgs = 3; MemArg = false; break;
13392 case X86::PCMPISTRM128MEM:
13393 case X86::VPCMPISTRM128MEM:
13394 NumArgs = 3; MemArg = true; break;
13395 case X86::PCMPESTRM128REG:
13396 case X86::VPCMPESTRM128REG:
13397 NumArgs = 5; MemArg = false; break;
13398 case X86::PCMPESTRM128MEM:
13399 case X86::VPCMPESTRM128MEM:
13400 NumArgs = 5; MemArg = true; break;
13401 }
13402 return EmitPCMP(MI, BB, NumArgs, MemArg);
13403 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013404
Eric Christopher228232b2010-11-30 07:20:12 +000013405 // Thread synchronization.
13406 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013407 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013408
Eric Christopherb120ab42009-08-18 22:50:32 +000013409 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013410 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013411 case X86::ATOMAND16:
13412 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013413 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013414 // Fall through
13415 case X86::ATOMOR8:
13416 case X86::ATOMOR16:
13417 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013418 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013419 // Fall through
13420 case X86::ATOMXOR16:
13421 case X86::ATOMXOR8:
13422 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013423 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013424 // Fall through
13425 case X86::ATOMNAND8:
13426 case X86::ATOMNAND16:
13427 case X86::ATOMNAND32:
13428 case X86::ATOMNAND64:
13429 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013430 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013431 case X86::ATOMMAX16:
13432 case X86::ATOMMAX32:
13433 case X86::ATOMMAX64:
13434 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013435 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013436 case X86::ATOMMIN16:
13437 case X86::ATOMMIN32:
13438 case X86::ATOMMIN64:
13439 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013440 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013441 case X86::ATOMUMAX16:
13442 case X86::ATOMUMAX32:
13443 case X86::ATOMUMAX64:
13444 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013445 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013446 case X86::ATOMUMIN16:
13447 case X86::ATOMUMIN32:
13448 case X86::ATOMUMIN64:
13449 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013450
13451 // This group does 64-bit operations on a 32-bit host.
13452 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013453 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013454 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013455 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013456 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013457 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000013458 case X86::ATOMMAX6432:
13459 case X86::ATOMMIN6432:
13460 case X86::ATOMUMAX6432:
13461 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000013462 case X86::ATOMSWAP6432:
13463 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000013464
Dan Gohmand6708ea2009-08-15 01:38:56 +000013465 case X86::VASTART_SAVE_XMM_REGS:
13466 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013467
13468 case X86::VAARG_64:
13469 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013470 }
13471}
13472
13473//===----------------------------------------------------------------------===//
13474// X86 Optimization Hooks
13475//===----------------------------------------------------------------------===//
13476
Dan Gohman475871a2008-07-27 21:46:04 +000013477void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013478 APInt &KnownZero,
13479 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013480 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013481 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013482 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013483 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013484 assert((Opc >= ISD::BUILTIN_OP_END ||
13485 Opc == ISD::INTRINSIC_WO_CHAIN ||
13486 Opc == ISD::INTRINSIC_W_CHAIN ||
13487 Opc == ISD::INTRINSIC_VOID) &&
13488 "Should use MaskedValueIsZero if you don't know whether Op"
13489 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013490
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013491 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013492 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013493 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013494 case X86ISD::ADD:
13495 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013496 case X86ISD::ADC:
13497 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013498 case X86ISD::SMUL:
13499 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013500 case X86ISD::INC:
13501 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013502 case X86ISD::OR:
13503 case X86ISD::XOR:
13504 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013505 // These nodes' second result is a boolean.
13506 if (Op.getResNo() == 0)
13507 break;
13508 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013509 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013510 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013511 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013512 case ISD::INTRINSIC_WO_CHAIN: {
13513 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13514 unsigned NumLoBits = 0;
13515 switch (IntId) {
13516 default: break;
13517 case Intrinsic::x86_sse_movmsk_ps:
13518 case Intrinsic::x86_avx_movmsk_ps_256:
13519 case Intrinsic::x86_sse2_movmsk_pd:
13520 case Intrinsic::x86_avx_movmsk_pd_256:
13521 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013522 case Intrinsic::x86_sse2_pmovmskb_128:
13523 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013524 // High bits of movmskp{s|d}, pmovmskb are known zero.
13525 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013526 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013527 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13528 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13529 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13530 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13531 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13532 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013533 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013534 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013535 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013536 break;
13537 }
13538 }
13539 break;
13540 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013541 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013542}
Chris Lattner259e97c2006-01-31 19:43:35 +000013543
Owen Andersonbc146b02010-09-21 20:42:50 +000013544unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13545 unsigned Depth) const {
13546 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13547 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13548 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013549
Owen Andersonbc146b02010-09-21 20:42:50 +000013550 // Fallback case.
13551 return 1;
13552}
13553
Evan Cheng206ee9d2006-07-07 08:33:52 +000013554/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013555/// node is a GlobalAddress + offset.
13556bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013557 const GlobalValue* &GA,
13558 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013559 if (N->getOpcode() == X86ISD::Wrapper) {
13560 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013561 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013562 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013563 return true;
13564 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013565 }
Evan Chengad4196b2008-05-12 19:56:52 +000013566 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013567}
13568
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013569/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13570/// same as extracting the high 128-bit part of 256-bit vector and then
13571/// inserting the result into the low part of a new 256-bit vector
13572static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13573 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013574 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013575
13576 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013577 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013578 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13579 SVOp->getMaskElt(j) >= 0)
13580 return false;
13581
13582 return true;
13583}
13584
13585/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13586/// same as extracting the low 128-bit part of 256-bit vector and then
13587/// inserting the result into the high part of a new 256-bit vector
13588static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13589 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013590 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013591
13592 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013593 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013594 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13595 SVOp->getMaskElt(j) >= 0)
13596 return false;
13597
13598 return true;
13599}
13600
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013601/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13602static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013603 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013604 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013605 DebugLoc dl = N->getDebugLoc();
13606 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13607 SDValue V1 = SVOp->getOperand(0);
13608 SDValue V2 = SVOp->getOperand(1);
13609 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013610 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013611
13612 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13613 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13614 //
13615 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013616 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013617 // V UNDEF BUILD_VECTOR UNDEF
13618 // \ / \ /
13619 // CONCAT_VECTOR CONCAT_VECTOR
13620 // \ /
13621 // \ /
13622 // RESULT: V + zero extended
13623 //
13624 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13625 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13626 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13627 return SDValue();
13628
13629 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13630 return SDValue();
13631
13632 // To match the shuffle mask, the first half of the mask should
13633 // be exactly the first vector, and all the rest a splat with the
13634 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013635 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013636 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13637 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13638 return SDValue();
13639
Chad Rosier3d1161e2012-01-03 21:05:52 +000013640 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13641 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013642 if (Ld->hasNUsesOfValue(1, 0)) {
13643 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13644 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13645 SDValue ResNode =
13646 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13647 Ld->getMemoryVT(),
13648 Ld->getPointerInfo(),
13649 Ld->getAlignment(),
13650 false/*isVolatile*/, true/*ReadMem*/,
13651 false/*WriteMem*/);
13652 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13653 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013654 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013655
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013656 // Emit a zeroed vector and insert the desired subvector on its
13657 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013658 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013659 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013660 return DCI.CombineTo(N, InsV);
13661 }
13662
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013663 //===--------------------------------------------------------------------===//
13664 // Combine some shuffles into subvector extracts and inserts:
13665 //
13666
13667 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13668 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013669 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13670 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013671 return DCI.CombineTo(N, InsV);
13672 }
13673
13674 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13675 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013676 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13677 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013678 return DCI.CombineTo(N, InsV);
13679 }
13680
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013681 return SDValue();
13682}
13683
13684/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013685static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013686 TargetLowering::DAGCombinerInfo &DCI,
13687 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013688 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013689 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013690
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013691 // Don't create instructions with illegal types after legalize types has run.
13692 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13693 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13694 return SDValue();
13695
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013696 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013697 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013698 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013699 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013700
13701 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013702 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013703 return SDValue();
13704
13705 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13706 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13707 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013708 SmallVector<SDValue, 16> Elts;
13709 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013710 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013711
Nate Begemanfdea31a2010-03-24 20:49:50 +000013712 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013713}
Evan Chengd880b972008-05-09 21:53:03 +000013714
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013715
Craig Topper55b24052012-09-11 06:15:32 +000013716/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013717/// a sequence of vector shuffle operations.
13718/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000013719static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
13720 TargetLowering::DAGCombinerInfo &DCI,
13721 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013722 if (!DCI.isBeforeLegalizeOps())
13723 return SDValue();
13724
Craig Topper3ef43cf2012-04-24 06:36:35 +000013725 if (!Subtarget->hasAVX())
13726 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013727
13728 EVT VT = N->getValueType(0);
13729 SDValue Op = N->getOperand(0);
13730 EVT OpVT = Op.getValueType();
13731 DebugLoc dl = N->getDebugLoc();
13732
13733 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13734
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013735 if (Subtarget->hasAVX2()) {
13736 // AVX2: v4i64 -> v4i32
13737
13738 // VPERMD
13739 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13740
13741 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13742 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13743 ShufMask);
13744
Craig Topperd63fa652012-04-22 18:51:37 +000013745 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13746 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013747 }
13748
13749 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013750 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013751 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013752
13753 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013754 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013755
13756 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13757 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13758
13759 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013760 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013761
Craig Toppercacafd42012-08-14 08:18:43 +000013762 SDValue Undef = DAG.getUNDEF(VT);
13763 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13764 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013765
13766 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013767 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013768
Elena Demikhovsky73252572012-02-01 10:33:05 +000013769 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013770 }
Craig Topperd63fa652012-04-22 18:51:37 +000013771
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013772 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13773
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013774 if (Subtarget->hasAVX2()) {
13775 // AVX2: v8i32 -> v8i16
13776
13777 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013778
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013779 // PSHUFB
13780 SmallVector<SDValue,32> pshufbMask;
13781 for (unsigned i = 0; i < 2; ++i) {
13782 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13783 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13784 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13785 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13786 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13787 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13788 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13789 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13790 for (unsigned j = 0; j < 8; ++j)
13791 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13792 }
Craig Topperd63fa652012-04-22 18:51:37 +000013793 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13794 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013795 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13796
13797 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13798
13799 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013800 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013801 &ShufMask[0]);
13802
Craig Topperd63fa652012-04-22 18:51:37 +000013803 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13804 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013805
13806 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13807 }
13808
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013809 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013810 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013811
13812 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013813 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013814
13815 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13816 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13817
13818 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013819 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13820 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013821
Craig Toppercacafd42012-08-14 08:18:43 +000013822 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13823 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13824 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013825
13826 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13827 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13828
13829 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013830 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013831
Elena Demikhovsky73252572012-02-01 10:33:05 +000013832 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013833 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013834 }
13835
13836 return SDValue();
13837}
13838
Craig Topper89f4e662012-03-20 07:17:59 +000013839/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13840/// specific shuffle of a load can be folded into a single element load.
13841/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13842/// shuffles have been customed lowered so we need to handle those here.
13843static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13844 TargetLowering::DAGCombinerInfo &DCI) {
13845 if (DCI.isBeforeLegalizeOps())
13846 return SDValue();
13847
13848 SDValue InVec = N->getOperand(0);
13849 SDValue EltNo = N->getOperand(1);
13850
13851 if (!isa<ConstantSDNode>(EltNo))
13852 return SDValue();
13853
13854 EVT VT = InVec.getValueType();
13855
13856 bool HasShuffleIntoBitcast = false;
13857 if (InVec.getOpcode() == ISD::BITCAST) {
13858 // Don't duplicate a load with other uses.
13859 if (!InVec.hasOneUse())
13860 return SDValue();
13861 EVT BCVT = InVec.getOperand(0).getValueType();
13862 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13863 return SDValue();
13864 InVec = InVec.getOperand(0);
13865 HasShuffleIntoBitcast = true;
13866 }
13867
13868 if (!isTargetShuffle(InVec.getOpcode()))
13869 return SDValue();
13870
13871 // Don't duplicate a load with other uses.
13872 if (!InVec.hasOneUse())
13873 return SDValue();
13874
13875 SmallVector<int, 16> ShuffleMask;
13876 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013877 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13878 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013879 return SDValue();
13880
13881 // Select the input vector, guarding against out of range extract vector.
13882 unsigned NumElems = VT.getVectorNumElements();
13883 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13884 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13885 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13886 : InVec.getOperand(1);
13887
13888 // If inputs to shuffle are the same for both ops, then allow 2 uses
13889 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13890
13891 if (LdNode.getOpcode() == ISD::BITCAST) {
13892 // Don't duplicate a load with other uses.
13893 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13894 return SDValue();
13895
13896 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13897 LdNode = LdNode.getOperand(0);
13898 }
13899
13900 if (!ISD::isNormalLoad(LdNode.getNode()))
13901 return SDValue();
13902
13903 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13904
13905 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13906 return SDValue();
13907
13908 if (HasShuffleIntoBitcast) {
13909 // If there's a bitcast before the shuffle, check if the load type and
13910 // alignment is valid.
13911 unsigned Align = LN0->getAlignment();
13912 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13913 unsigned NewAlign = TLI.getTargetData()->
13914 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13915
13916 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13917 return SDValue();
13918 }
13919
13920 // All checks match so transform back to vector_shuffle so that DAG combiner
13921 // can finish the job
13922 DebugLoc dl = N->getDebugLoc();
13923
13924 // Create shuffle node taking into account the case that its a unary shuffle
13925 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13926 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13927 InVec.getOperand(0), Shuffle,
13928 &ShuffleMask[0]);
13929 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13930 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13931 EltNo);
13932}
13933
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013934/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13935/// generation and convert it from being a bunch of shuffles and extracts
13936/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013937static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013938 TargetLowering::DAGCombinerInfo &DCI) {
13939 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13940 if (NewOp.getNode())
13941 return NewOp;
13942
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013943 SDValue InputVector = N->getOperand(0);
13944
13945 // Only operate on vectors of 4 elements, where the alternative shuffling
13946 // gets to be more expensive.
13947 if (InputVector.getValueType() != MVT::v4i32)
13948 return SDValue();
13949
13950 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13951 // single use which is a sign-extend or zero-extend, and all elements are
13952 // used.
13953 SmallVector<SDNode *, 4> Uses;
13954 unsigned ExtractedElements = 0;
13955 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13956 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13957 if (UI.getUse().getResNo() != InputVector.getResNo())
13958 return SDValue();
13959
13960 SDNode *Extract = *UI;
13961 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13962 return SDValue();
13963
13964 if (Extract->getValueType(0) != MVT::i32)
13965 return SDValue();
13966 if (!Extract->hasOneUse())
13967 return SDValue();
13968 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13969 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13970 return SDValue();
13971 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13972 return SDValue();
13973
13974 // Record which element was extracted.
13975 ExtractedElements |=
13976 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13977
13978 Uses.push_back(Extract);
13979 }
13980
13981 // If not all the elements were used, this may not be worthwhile.
13982 if (ExtractedElements != 15)
13983 return SDValue();
13984
13985 // Ok, we've now decided to do the transformation.
13986 DebugLoc dl = InputVector.getDebugLoc();
13987
13988 // Store the value to a temporary stack slot.
13989 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013990 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13991 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013992
13993 // Replace each use (extract) with a load of the appropriate element.
13994 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13995 UE = Uses.end(); UI != UE; ++UI) {
13996 SDNode *Extract = *UI;
13997
Nadav Rotem86694292011-05-17 08:31:57 +000013998 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013999 SDValue Idx = Extract->getOperand(1);
14000 unsigned EltSize =
14001 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14002 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014003 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014004 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14005
Nadav Rotem86694292011-05-17 08:31:57 +000014006 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014007 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014008
14009 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014010 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014011 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014012 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014013
14014 // Replace the exact with the load.
14015 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14016 }
14017
14018 // The replacement was made in place; don't return anything.
14019 return SDValue();
14020}
14021
Duncan Sands6bcd2192011-09-17 16:49:39 +000014022/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14023/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014024static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014025 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014026 const X86Subtarget *Subtarget) {
14027 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014028 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014029 // Get the LHS/RHS of the select.
14030 SDValue LHS = N->getOperand(1);
14031 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014032 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014033
Dan Gohman670e5392009-09-21 18:03:22 +000014034 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014035 // instructions match the semantics of the common C idiom x<y?x:y but not
14036 // x<=y?x:y, because of how they handle negative zero (which can be
14037 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014038 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14039 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014040 (Subtarget->hasSSE2() ||
14041 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014042 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014043
Chris Lattner47b4ce82009-03-11 05:48:52 +000014044 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014045 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014046 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14047 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014048 switch (CC) {
14049 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014050 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014051 // Converting this to a min would handle NaNs incorrectly, and swapping
14052 // the operands would cause it to handle comparisons between positive
14053 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014054 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014055 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014056 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14057 break;
14058 std::swap(LHS, RHS);
14059 }
Dan Gohman670e5392009-09-21 18:03:22 +000014060 Opcode = X86ISD::FMIN;
14061 break;
14062 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014063 // Converting this to a min would handle comparisons between positive
14064 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014065 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014066 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14067 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014068 Opcode = X86ISD::FMIN;
14069 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014070 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014071 // Converting this to a min would handle both negative zeros and NaNs
14072 // incorrectly, but we can swap the operands to fix both.
14073 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014074 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014075 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014076 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014077 Opcode = X86ISD::FMIN;
14078 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014079
Dan Gohman670e5392009-09-21 18:03:22 +000014080 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014081 // Converting this to a max would handle comparisons between positive
14082 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014083 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014084 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014085 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014086 Opcode = X86ISD::FMAX;
14087 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014088 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014089 // Converting this to a max would handle NaNs incorrectly, and swapping
14090 // the operands would cause it to handle comparisons between positive
14091 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014092 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014093 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014094 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14095 break;
14096 std::swap(LHS, RHS);
14097 }
Dan Gohman670e5392009-09-21 18:03:22 +000014098 Opcode = X86ISD::FMAX;
14099 break;
14100 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014101 // Converting this to a max would handle both negative zeros and NaNs
14102 // incorrectly, but we can swap the operands to fix both.
14103 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014104 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014105 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014106 case ISD::SETGE:
14107 Opcode = X86ISD::FMAX;
14108 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014109 }
Dan Gohman670e5392009-09-21 18:03:22 +000014110 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014111 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14112 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014113 switch (CC) {
14114 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014115 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014116 // Converting this to a min would handle comparisons between positive
14117 // and negative zero incorrectly, and swapping the operands would
14118 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014119 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014120 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014121 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014122 break;
14123 std::swap(LHS, RHS);
14124 }
Dan Gohman670e5392009-09-21 18:03:22 +000014125 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014126 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014127 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014128 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014129 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014130 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14131 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014132 Opcode = X86ISD::FMIN;
14133 break;
14134 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014135 // Converting this to a min would handle both negative zeros and NaNs
14136 // incorrectly, but we can swap the operands to fix both.
14137 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014138 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014139 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014140 case ISD::SETGE:
14141 Opcode = X86ISD::FMIN;
14142 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014143
Dan Gohman670e5392009-09-21 18:03:22 +000014144 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014145 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014146 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014147 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014148 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014149 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014150 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014151 // Converting this to a max would handle comparisons between positive
14152 // and negative zero incorrectly, and swapping the operands would
14153 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014154 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014155 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014156 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014157 break;
14158 std::swap(LHS, RHS);
14159 }
Dan Gohman670e5392009-09-21 18:03:22 +000014160 Opcode = X86ISD::FMAX;
14161 break;
14162 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014163 // Converting this to a max would handle both negative zeros and NaNs
14164 // incorrectly, but we can swap the operands to fix both.
14165 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014166 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014167 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014168 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014169 Opcode = X86ISD::FMAX;
14170 break;
14171 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014172 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014173
Chris Lattner47b4ce82009-03-11 05:48:52 +000014174 if (Opcode)
14175 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014176 }
Eric Christopherfd179292009-08-27 18:07:15 +000014177
Chris Lattnerd1980a52009-03-12 06:52:53 +000014178 // If this is a select between two integer constants, try to do some
14179 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014180 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14181 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014182 // Don't do this for crazy integer types.
14183 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14184 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014185 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014186 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014187
Chris Lattnercee56e72009-03-13 05:53:31 +000014188 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014189 // Efficiently invertible.
14190 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14191 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14192 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14193 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014194 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014195 }
Eric Christopherfd179292009-08-27 18:07:15 +000014196
Chris Lattnerd1980a52009-03-12 06:52:53 +000014197 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014198 if (FalseC->getAPIntValue() == 0 &&
14199 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014200 if (NeedsCondInvert) // Invert the condition if needed.
14201 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14202 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014203
Chris Lattnerd1980a52009-03-12 06:52:53 +000014204 // Zero extend the condition if needed.
14205 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014206
Chris Lattnercee56e72009-03-13 05:53:31 +000014207 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014208 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014209 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014210 }
Eric Christopherfd179292009-08-27 18:07:15 +000014211
Chris Lattner97a29a52009-03-13 05:22:11 +000014212 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014213 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014214 if (NeedsCondInvert) // Invert the condition if needed.
14215 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14216 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014217
Chris Lattner97a29a52009-03-13 05:22:11 +000014218 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014219 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14220 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014221 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014222 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014223 }
Eric Christopherfd179292009-08-27 18:07:15 +000014224
Chris Lattnercee56e72009-03-13 05:53:31 +000014225 // Optimize cases that will turn into an LEA instruction. This requires
14226 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014227 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014228 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014229 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014230
Chris Lattnercee56e72009-03-13 05:53:31 +000014231 bool isFastMultiplier = false;
14232 if (Diff < 10) {
14233 switch ((unsigned char)Diff) {
14234 default: break;
14235 case 1: // result = add base, cond
14236 case 2: // result = lea base( , cond*2)
14237 case 3: // result = lea base(cond, cond*2)
14238 case 4: // result = lea base( , cond*4)
14239 case 5: // result = lea base(cond, cond*4)
14240 case 8: // result = lea base( , cond*8)
14241 case 9: // result = lea base(cond, cond*8)
14242 isFastMultiplier = true;
14243 break;
14244 }
14245 }
Eric Christopherfd179292009-08-27 18:07:15 +000014246
Chris Lattnercee56e72009-03-13 05:53:31 +000014247 if (isFastMultiplier) {
14248 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14249 if (NeedsCondInvert) // Invert the condition if needed.
14250 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14251 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014252
Chris Lattnercee56e72009-03-13 05:53:31 +000014253 // Zero extend the condition if needed.
14254 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14255 Cond);
14256 // Scale the condition by the difference.
14257 if (Diff != 1)
14258 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14259 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014260
Chris Lattnercee56e72009-03-13 05:53:31 +000014261 // Add the base if non-zero.
14262 if (FalseC->getAPIntValue() != 0)
14263 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14264 SDValue(FalseC, 0));
14265 return Cond;
14266 }
Eric Christopherfd179292009-08-27 18:07:15 +000014267 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014268 }
14269 }
Eric Christopherfd179292009-08-27 18:07:15 +000014270
Evan Cheng56f582d2012-01-04 01:41:39 +000014271 // Canonicalize max and min:
14272 // (x > y) ? x : y -> (x >= y) ? x : y
14273 // (x < y) ? x : y -> (x <= y) ? x : y
14274 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14275 // the need for an extra compare
14276 // against zero. e.g.
14277 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14278 // subl %esi, %edi
14279 // testl %edi, %edi
14280 // movl $0, %eax
14281 // cmovgl %edi, %eax
14282 // =>
14283 // xorl %eax, %eax
14284 // subl %esi, $edi
14285 // cmovsl %eax, %edi
14286 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14287 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14288 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14289 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14290 switch (CC) {
14291 default: break;
14292 case ISD::SETLT:
14293 case ISD::SETGT: {
14294 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14295 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14296 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14297 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14298 }
14299 }
14300 }
14301
Nadav Rotemcc616562012-01-15 19:27:55 +000014302 // If we know that this node is legal then we know that it is going to be
14303 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14304 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14305 // to simplify previous instructions.
14306 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14307 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014308 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014309 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014310
14311 // Don't optimize vector selects that map to mask-registers.
14312 if (BitWidth == 1)
14313 return SDValue();
14314
Nadav Rotemcc616562012-01-15 19:27:55 +000014315 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14316 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14317
14318 APInt KnownZero, KnownOne;
14319 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14320 DCI.isBeforeLegalizeOps());
14321 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14322 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14323 DCI.CommitTargetLoweringOpt(TLO);
14324 }
14325
Dan Gohman475871a2008-07-27 21:46:04 +000014326 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014327}
14328
Michael Liao2a33cec2012-08-10 19:58:13 +000014329// Check whether a boolean test is testing a boolean value generated by
14330// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14331// code.
14332//
14333// Simplify the following patterns:
14334// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14335// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14336// to (Op EFLAGS Cond)
14337//
14338// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14339// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14340// to (Op EFLAGS !Cond)
14341//
14342// where Op could be BRCOND or CMOV.
14343//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014344static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014345 // Quit if not CMP and SUB with its value result used.
14346 if (Cmp.getOpcode() != X86ISD::CMP &&
14347 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14348 return SDValue();
14349
14350 // Quit if not used as a boolean value.
14351 if (CC != X86::COND_E && CC != X86::COND_NE)
14352 return SDValue();
14353
14354 // Check CMP operands. One of them should be 0 or 1 and the other should be
14355 // an SetCC or extended from it.
14356 SDValue Op1 = Cmp.getOperand(0);
14357 SDValue Op2 = Cmp.getOperand(1);
14358
14359 SDValue SetCC;
14360 const ConstantSDNode* C = 0;
14361 bool needOppositeCond = (CC == X86::COND_E);
14362
14363 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14364 SetCC = Op2;
14365 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14366 SetCC = Op1;
14367 else // Quit if all operands are not constants.
14368 return SDValue();
14369
14370 if (C->getZExtValue() == 1)
14371 needOppositeCond = !needOppositeCond;
14372 else if (C->getZExtValue() != 0)
14373 // Quit if the constant is neither 0 or 1.
14374 return SDValue();
14375
14376 // Skip 'zext' node.
14377 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14378 SetCC = SetCC.getOperand(0);
14379
Michael Liao7fdc66b2012-09-10 16:36:16 +000014380 switch (SetCC.getOpcode()) {
14381 case X86ISD::SETCC:
14382 // Set the condition code or opposite one if necessary.
14383 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14384 if (needOppositeCond)
14385 CC = X86::GetOppositeBranchCondition(CC);
14386 return SetCC.getOperand(1);
14387 case X86ISD::CMOV: {
14388 // Check whether false/true value has canonical one, i.e. 0 or 1.
14389 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14390 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14391 // Quit if true value is not a constant.
14392 if (!TVal)
14393 return SDValue();
14394 // Quit if false value is not a constant.
14395 if (!FVal) {
14396 // A special case for rdrand, where 0 is set if false cond is found.
14397 SDValue Op = SetCC.getOperand(0);
14398 if (Op.getOpcode() != X86ISD::RDRAND)
14399 return SDValue();
14400 }
14401 // Quit if false value is not the constant 0 or 1.
14402 bool FValIsFalse = true;
14403 if (FVal && FVal->getZExtValue() != 0) {
14404 if (FVal->getZExtValue() != 1)
14405 return SDValue();
14406 // If FVal is 1, opposite cond is needed.
14407 needOppositeCond = !needOppositeCond;
14408 FValIsFalse = false;
14409 }
14410 // Quit if TVal is not the constant opposite of FVal.
14411 if (FValIsFalse && TVal->getZExtValue() != 1)
14412 return SDValue();
14413 if (!FValIsFalse && TVal->getZExtValue() != 0)
14414 return SDValue();
14415 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14416 if (needOppositeCond)
14417 CC = X86::GetOppositeBranchCondition(CC);
14418 return SetCC.getOperand(3);
14419 }
14420 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014421
Michael Liao7fdc66b2012-09-10 16:36:16 +000014422 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014423}
14424
Chris Lattnerd1980a52009-03-12 06:52:53 +000014425/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14426static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014427 TargetLowering::DAGCombinerInfo &DCI,
14428 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014429 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014430
Chris Lattnerd1980a52009-03-12 06:52:53 +000014431 // If the flag operand isn't dead, don't touch this CMOV.
14432 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14433 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014434
Evan Chengb5a55d92011-05-24 01:48:22 +000014435 SDValue FalseOp = N->getOperand(0);
14436 SDValue TrueOp = N->getOperand(1);
14437 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14438 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014439
Evan Chengb5a55d92011-05-24 01:48:22 +000014440 if (CC == X86::COND_E || CC == X86::COND_NE) {
14441 switch (Cond.getOpcode()) {
14442 default: break;
14443 case X86ISD::BSR:
14444 case X86ISD::BSF:
14445 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14446 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14447 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14448 }
14449 }
14450
Michael Liao2a33cec2012-08-10 19:58:13 +000014451 SDValue Flags;
14452
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014453 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014454 if (Flags.getNode() &&
14455 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014456 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014457 SDValue Ops[] = { FalseOp, TrueOp,
14458 DAG.getConstant(CC, MVT::i8), Flags };
14459 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14460 Ops, array_lengthof(Ops));
14461 }
14462
Chris Lattnerd1980a52009-03-12 06:52:53 +000014463 // If this is a select between two integer constants, try to do some
14464 // optimizations. Note that the operands are ordered the opposite of SELECT
14465 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014466 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14467 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014468 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14469 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014470 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14471 CC = X86::GetOppositeBranchCondition(CC);
14472 std::swap(TrueC, FalseC);
14473 }
Eric Christopherfd179292009-08-27 18:07:15 +000014474
Chris Lattnerd1980a52009-03-12 06:52:53 +000014475 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014476 // This is efficient for any integer data type (including i8/i16) and
14477 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014478 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014479 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14480 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014481
Chris Lattnerd1980a52009-03-12 06:52:53 +000014482 // Zero extend the condition if needed.
14483 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014484
Chris Lattnerd1980a52009-03-12 06:52:53 +000014485 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14486 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014487 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014488 if (N->getNumValues() == 2) // Dead flag value?
14489 return DCI.CombineTo(N, Cond, SDValue());
14490 return Cond;
14491 }
Eric Christopherfd179292009-08-27 18:07:15 +000014492
Chris Lattnercee56e72009-03-13 05:53:31 +000014493 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14494 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014495 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014496 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14497 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014498
Chris Lattner97a29a52009-03-13 05:22:11 +000014499 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014500 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14501 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014502 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14503 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014504
Chris Lattner97a29a52009-03-13 05:22:11 +000014505 if (N->getNumValues() == 2) // Dead flag value?
14506 return DCI.CombineTo(N, Cond, SDValue());
14507 return Cond;
14508 }
Eric Christopherfd179292009-08-27 18:07:15 +000014509
Chris Lattnercee56e72009-03-13 05:53:31 +000014510 // Optimize cases that will turn into an LEA instruction. This requires
14511 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014512 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014513 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014514 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014515
Chris Lattnercee56e72009-03-13 05:53:31 +000014516 bool isFastMultiplier = false;
14517 if (Diff < 10) {
14518 switch ((unsigned char)Diff) {
14519 default: break;
14520 case 1: // result = add base, cond
14521 case 2: // result = lea base( , cond*2)
14522 case 3: // result = lea base(cond, cond*2)
14523 case 4: // result = lea base( , cond*4)
14524 case 5: // result = lea base(cond, cond*4)
14525 case 8: // result = lea base( , cond*8)
14526 case 9: // result = lea base(cond, cond*8)
14527 isFastMultiplier = true;
14528 break;
14529 }
14530 }
Eric Christopherfd179292009-08-27 18:07:15 +000014531
Chris Lattnercee56e72009-03-13 05:53:31 +000014532 if (isFastMultiplier) {
14533 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014534 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14535 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014536 // Zero extend the condition if needed.
14537 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14538 Cond);
14539 // Scale the condition by the difference.
14540 if (Diff != 1)
14541 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14542 DAG.getConstant(Diff, Cond.getValueType()));
14543
14544 // Add the base if non-zero.
14545 if (FalseC->getAPIntValue() != 0)
14546 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14547 SDValue(FalseC, 0));
14548 if (N->getNumValues() == 2) // Dead flag value?
14549 return DCI.CombineTo(N, Cond, SDValue());
14550 return Cond;
14551 }
Eric Christopherfd179292009-08-27 18:07:15 +000014552 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014553 }
14554 }
14555 return SDValue();
14556}
14557
14558
Evan Cheng0b0cd912009-03-28 05:57:29 +000014559/// PerformMulCombine - Optimize a single multiply with constant into two
14560/// in order to implement it with two cheaper instructions, e.g.
14561/// LEA + SHL, LEA + LEA.
14562static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14563 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014564 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14565 return SDValue();
14566
Owen Andersone50ed302009-08-10 22:56:29 +000014567 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014568 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014569 return SDValue();
14570
14571 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14572 if (!C)
14573 return SDValue();
14574 uint64_t MulAmt = C->getZExtValue();
14575 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14576 return SDValue();
14577
14578 uint64_t MulAmt1 = 0;
14579 uint64_t MulAmt2 = 0;
14580 if ((MulAmt % 9) == 0) {
14581 MulAmt1 = 9;
14582 MulAmt2 = MulAmt / 9;
14583 } else if ((MulAmt % 5) == 0) {
14584 MulAmt1 = 5;
14585 MulAmt2 = MulAmt / 5;
14586 } else if ((MulAmt % 3) == 0) {
14587 MulAmt1 = 3;
14588 MulAmt2 = MulAmt / 3;
14589 }
14590 if (MulAmt2 &&
14591 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14592 DebugLoc DL = N->getDebugLoc();
14593
14594 if (isPowerOf2_64(MulAmt2) &&
14595 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14596 // If second multiplifer is pow2, issue it first. We want the multiply by
14597 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14598 // is an add.
14599 std::swap(MulAmt1, MulAmt2);
14600
14601 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014602 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014603 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014604 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014605 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014606 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014607 DAG.getConstant(MulAmt1, VT));
14608
Eric Christopherfd179292009-08-27 18:07:15 +000014609 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014610 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014611 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014612 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014613 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014614 DAG.getConstant(MulAmt2, VT));
14615
14616 // Do not add new nodes to DAG combiner worklist.
14617 DCI.CombineTo(N, NewMul, false);
14618 }
14619 return SDValue();
14620}
14621
Evan Chengad9c0a32009-12-15 00:53:42 +000014622static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14623 SDValue N0 = N->getOperand(0);
14624 SDValue N1 = N->getOperand(1);
14625 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14626 EVT VT = N0.getValueType();
14627
14628 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14629 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014630 if (VT.isInteger() && !VT.isVector() &&
14631 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014632 N0.getOperand(1).getOpcode() == ISD::Constant) {
14633 SDValue N00 = N0.getOperand(0);
14634 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14635 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14636 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14637 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14638 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14639 APInt ShAmt = N1C->getAPIntValue();
14640 Mask = Mask.shl(ShAmt);
14641 if (Mask != 0)
14642 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14643 N00, DAG.getConstant(Mask, VT));
14644 }
14645 }
14646
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014647
14648 // Hardware support for vector shifts is sparse which makes us scalarize the
14649 // vector operations in many cases. Also, on sandybridge ADD is faster than
14650 // shl.
14651 // (shl V, 1) -> add V,V
14652 if (isSplatVector(N1.getNode())) {
14653 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14654 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14655 // We shift all of the values by one. In many cases we do not have
14656 // hardware support for this operation. This is better expressed as an ADD
14657 // of two values.
14658 if (N1C && (1 == N1C->getZExtValue())) {
14659 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14660 }
14661 }
14662
Evan Chengad9c0a32009-12-15 00:53:42 +000014663 return SDValue();
14664}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014665
Nate Begeman740ab032009-01-26 00:52:55 +000014666/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14667/// when possible.
14668static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014669 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014670 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014671 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014672 if (N->getOpcode() == ISD::SHL) {
14673 SDValue V = PerformSHLCombine(N, DAG);
14674 if (V.getNode()) return V;
14675 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014676
Nate Begeman740ab032009-01-26 00:52:55 +000014677 // On X86 with SSE2 support, we can transform this to a vector shift if
14678 // all elements are shifted by the same amount. We can't do this in legalize
14679 // because the a constant vector is typically transformed to a constant pool
14680 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014681 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014682 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014683
Craig Topper7be5dfd2011-11-12 09:58:49 +000014684 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14685 (!Subtarget->hasAVX2() ||
14686 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014687 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014688
Mon P Wang3becd092009-01-28 08:12:05 +000014689 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014690 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014691 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014692 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014693 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14694 unsigned NumElts = VT.getVectorNumElements();
14695 unsigned i = 0;
14696 for (; i != NumElts; ++i) {
14697 SDValue Arg = ShAmtOp.getOperand(i);
14698 if (Arg.getOpcode() == ISD::UNDEF) continue;
14699 BaseShAmt = Arg;
14700 break;
14701 }
Craig Topper37c26772012-01-17 04:44:50 +000014702 // Handle the case where the build_vector is all undef
14703 // FIXME: Should DAG allow this?
14704 if (i == NumElts)
14705 return SDValue();
14706
Mon P Wang3becd092009-01-28 08:12:05 +000014707 for (; i != NumElts; ++i) {
14708 SDValue Arg = ShAmtOp.getOperand(i);
14709 if (Arg.getOpcode() == ISD::UNDEF) continue;
14710 if (Arg != BaseShAmt) {
14711 return SDValue();
14712 }
14713 }
14714 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014715 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014716 SDValue InVec = ShAmtOp.getOperand(0);
14717 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14718 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14719 unsigned i = 0;
14720 for (; i != NumElts; ++i) {
14721 SDValue Arg = InVec.getOperand(i);
14722 if (Arg.getOpcode() == ISD::UNDEF) continue;
14723 BaseShAmt = Arg;
14724 break;
14725 }
14726 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14727 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014728 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014729 if (C->getZExtValue() == SplatIdx)
14730 BaseShAmt = InVec.getOperand(1);
14731 }
14732 }
Mon P Wang845b1892012-02-01 22:15:20 +000014733 if (BaseShAmt.getNode() == 0) {
14734 // Don't create instructions with illegal types after legalize
14735 // types has run.
14736 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14737 !DCI.isBeforeLegalize())
14738 return SDValue();
14739
Mon P Wangefa42202009-09-03 19:56:25 +000014740 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14741 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014742 }
Mon P Wang3becd092009-01-28 08:12:05 +000014743 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014744 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014745
Mon P Wangefa42202009-09-03 19:56:25 +000014746 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014747 if (EltVT.bitsGT(MVT::i32))
14748 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14749 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014750 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014751
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014752 // The shift amount is identical so we can do a vector shift.
14753 SDValue ValOp = N->getOperand(0);
14754 switch (N->getOpcode()) {
14755 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014756 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014757 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014758 switch (VT.getSimpleVT().SimpleTy) {
14759 default: return SDValue();
14760 case MVT::v2i64:
14761 case MVT::v4i32:
14762 case MVT::v8i16:
14763 case MVT::v4i64:
14764 case MVT::v8i32:
14765 case MVT::v16i16:
14766 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14767 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014768 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014769 switch (VT.getSimpleVT().SimpleTy) {
14770 default: return SDValue();
14771 case MVT::v4i32:
14772 case MVT::v8i16:
14773 case MVT::v8i32:
14774 case MVT::v16i16:
14775 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14776 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014777 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014778 switch (VT.getSimpleVT().SimpleTy) {
14779 default: return SDValue();
14780 case MVT::v2i64:
14781 case MVT::v4i32:
14782 case MVT::v8i16:
14783 case MVT::v4i64:
14784 case MVT::v8i32:
14785 case MVT::v16i16:
14786 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14787 }
Nate Begeman740ab032009-01-26 00:52:55 +000014788 }
Nate Begeman740ab032009-01-26 00:52:55 +000014789}
14790
Nate Begemanb65c1752010-12-17 22:55:37 +000014791
Stuart Hastings865f0932011-06-03 23:53:54 +000014792// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14793// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14794// and friends. Likewise for OR -> CMPNEQSS.
14795static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14796 TargetLowering::DAGCombinerInfo &DCI,
14797 const X86Subtarget *Subtarget) {
14798 unsigned opcode;
14799
14800 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14801 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014802 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014803 SDValue N0 = N->getOperand(0);
14804 SDValue N1 = N->getOperand(1);
14805 SDValue CMP0 = N0->getOperand(1);
14806 SDValue CMP1 = N1->getOperand(1);
14807 DebugLoc DL = N->getDebugLoc();
14808
14809 // The SETCCs should both refer to the same CMP.
14810 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14811 return SDValue();
14812
14813 SDValue CMP00 = CMP0->getOperand(0);
14814 SDValue CMP01 = CMP0->getOperand(1);
14815 EVT VT = CMP00.getValueType();
14816
14817 if (VT == MVT::f32 || VT == MVT::f64) {
14818 bool ExpectingFlags = false;
14819 // Check for any users that want flags:
14820 for (SDNode::use_iterator UI = N->use_begin(),
14821 UE = N->use_end();
14822 !ExpectingFlags && UI != UE; ++UI)
14823 switch (UI->getOpcode()) {
14824 default:
14825 case ISD::BR_CC:
14826 case ISD::BRCOND:
14827 case ISD::SELECT:
14828 ExpectingFlags = true;
14829 break;
14830 case ISD::CopyToReg:
14831 case ISD::SIGN_EXTEND:
14832 case ISD::ZERO_EXTEND:
14833 case ISD::ANY_EXTEND:
14834 break;
14835 }
14836
14837 if (!ExpectingFlags) {
14838 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14839 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14840
14841 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14842 X86::CondCode tmp = cc0;
14843 cc0 = cc1;
14844 cc1 = tmp;
14845 }
14846
14847 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14848 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14849 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14850 X86ISD::NodeType NTOperator = is64BitFP ?
14851 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14852 // FIXME: need symbolic constants for these magic numbers.
14853 // See X86ATTInstPrinter.cpp:printSSECC().
14854 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14855 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14856 DAG.getConstant(x86cc, MVT::i8));
14857 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14858 OnesOrZeroesF);
14859 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14860 DAG.getConstant(1, MVT::i32));
14861 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14862 return OneBitOfTruth;
14863 }
14864 }
14865 }
14866 }
14867 return SDValue();
14868}
14869
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014870/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14871/// so it can be folded inside ANDNP.
14872static bool CanFoldXORWithAllOnes(const SDNode *N) {
14873 EVT VT = N->getValueType(0);
14874
14875 // Match direct AllOnes for 128 and 256-bit vectors
14876 if (ISD::isBuildVectorAllOnes(N))
14877 return true;
14878
14879 // Look through a bit convert.
14880 if (N->getOpcode() == ISD::BITCAST)
14881 N = N->getOperand(0).getNode();
14882
14883 // Sometimes the operand may come from a insert_subvector building a 256-bit
14884 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014885 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014886 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14887 SDValue V1 = N->getOperand(0);
14888 SDValue V2 = N->getOperand(1);
14889
14890 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14891 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14892 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14893 ISD::isBuildVectorAllOnes(V2.getNode()))
14894 return true;
14895 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014896
14897 return false;
14898}
14899
Nate Begemanb65c1752010-12-17 22:55:37 +000014900static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14901 TargetLowering::DAGCombinerInfo &DCI,
14902 const X86Subtarget *Subtarget) {
14903 if (DCI.isBeforeLegalizeOps())
14904 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014905
Stuart Hastings865f0932011-06-03 23:53:54 +000014906 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14907 if (R.getNode())
14908 return R;
14909
Craig Topper54a11172011-10-14 07:06:56 +000014910 EVT VT = N->getValueType(0);
14911
Craig Topperb4c94572011-10-21 06:55:01 +000014912 // Create ANDN, BLSI, and BLSR instructions
14913 // BLSI is X & (-X)
14914 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014915 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14916 SDValue N0 = N->getOperand(0);
14917 SDValue N1 = N->getOperand(1);
14918 DebugLoc DL = N->getDebugLoc();
14919
14920 // Check LHS for not
14921 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14922 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14923 // Check RHS for not
14924 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14925 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14926
Craig Topperb4c94572011-10-21 06:55:01 +000014927 // Check LHS for neg
14928 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14929 isZero(N0.getOperand(0)))
14930 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14931
14932 // Check RHS for neg
14933 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14934 isZero(N1.getOperand(0)))
14935 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14936
14937 // Check LHS for X-1
14938 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14939 isAllOnes(N0.getOperand(1)))
14940 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14941
14942 // Check RHS for X-1
14943 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14944 isAllOnes(N1.getOperand(1)))
14945 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14946
Craig Topper54a11172011-10-14 07:06:56 +000014947 return SDValue();
14948 }
14949
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014950 // Want to form ANDNP nodes:
14951 // 1) In the hopes of then easily combining them with OR and AND nodes
14952 // to form PBLEND/PSIGN.
14953 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014954 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014955 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014956
Nate Begemanb65c1752010-12-17 22:55:37 +000014957 SDValue N0 = N->getOperand(0);
14958 SDValue N1 = N->getOperand(1);
14959 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014960
Nate Begemanb65c1752010-12-17 22:55:37 +000014961 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014962 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014963 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14964 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014965 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014966
14967 // Check RHS for vnot
14968 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014969 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14970 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014971 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014972
Nate Begemanb65c1752010-12-17 22:55:37 +000014973 return SDValue();
14974}
14975
Evan Cheng760d1942010-01-04 21:22:48 +000014976static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014977 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014978 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014979 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014980 return SDValue();
14981
Stuart Hastings865f0932011-06-03 23:53:54 +000014982 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14983 if (R.getNode())
14984 return R;
14985
Evan Cheng760d1942010-01-04 21:22:48 +000014986 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014987
Evan Cheng760d1942010-01-04 21:22:48 +000014988 SDValue N0 = N->getOperand(0);
14989 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014990
Nate Begemanb65c1752010-12-17 22:55:37 +000014991 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014992 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014993 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014994 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14995 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014996
Craig Topper1666cb62011-11-19 07:07:26 +000014997 // Canonicalize pandn to RHS
14998 if (N0.getOpcode() == X86ISD::ANDNP)
14999 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015000 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015001 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15002 SDValue Mask = N1.getOperand(0);
15003 SDValue X = N1.getOperand(1);
15004 SDValue Y;
15005 if (N0.getOperand(0) == Mask)
15006 Y = N0.getOperand(1);
15007 if (N0.getOperand(1) == Mask)
15008 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015009
Craig Topper1666cb62011-11-19 07:07:26 +000015010 // Check to see if the mask appeared in both the AND and ANDNP and
15011 if (!Y.getNode())
15012 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015013
Craig Topper1666cb62011-11-19 07:07:26 +000015014 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015015 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015016 if (Mask.getOpcode() == ISD::BITCAST)
15017 Mask = Mask.getOperand(0);
15018 if (X.getOpcode() == ISD::BITCAST)
15019 X = X.getOperand(0);
15020 if (Y.getOpcode() == ISD::BITCAST)
15021 Y = Y.getOperand(0);
15022
Craig Topper1666cb62011-11-19 07:07:26 +000015023 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015024
Craig Toppered2e13d2012-01-22 19:15:14 +000015025 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015026 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15027 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015028 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015029 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015030
15031 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015032 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015033 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15034 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15035 if ((SraAmt + 1) != EltBits)
15036 return SDValue();
15037
15038 DebugLoc DL = N->getDebugLoc();
15039
15040 // Now we know we at least have a plendvb with the mask val. See if
15041 // we can form a psignb/w/d.
15042 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015043 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15044 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015045 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15046 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15047 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015048 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015049 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015050 }
15051 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015052 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015053 return SDValue();
15054
15055 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15056
15057 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15058 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15059 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015060 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015061 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015062 }
15063 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015064
Craig Topper1666cb62011-11-19 07:07:26 +000015065 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15066 return SDValue();
15067
Nate Begemanb65c1752010-12-17 22:55:37 +000015068 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015069 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15070 std::swap(N0, N1);
15071 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15072 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015073 if (!N0.hasOneUse() || !N1.hasOneUse())
15074 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015075
15076 SDValue ShAmt0 = N0.getOperand(1);
15077 if (ShAmt0.getValueType() != MVT::i8)
15078 return SDValue();
15079 SDValue ShAmt1 = N1.getOperand(1);
15080 if (ShAmt1.getValueType() != MVT::i8)
15081 return SDValue();
15082 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15083 ShAmt0 = ShAmt0.getOperand(0);
15084 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15085 ShAmt1 = ShAmt1.getOperand(0);
15086
15087 DebugLoc DL = N->getDebugLoc();
15088 unsigned Opc = X86ISD::SHLD;
15089 SDValue Op0 = N0.getOperand(0);
15090 SDValue Op1 = N1.getOperand(0);
15091 if (ShAmt0.getOpcode() == ISD::SUB) {
15092 Opc = X86ISD::SHRD;
15093 std::swap(Op0, Op1);
15094 std::swap(ShAmt0, ShAmt1);
15095 }
15096
Evan Cheng8b1190a2010-04-28 01:18:01 +000015097 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015098 if (ShAmt1.getOpcode() == ISD::SUB) {
15099 SDValue Sum = ShAmt1.getOperand(0);
15100 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015101 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15102 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15103 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15104 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015105 return DAG.getNode(Opc, DL, VT,
15106 Op0, Op1,
15107 DAG.getNode(ISD::TRUNCATE, DL,
15108 MVT::i8, ShAmt0));
15109 }
15110 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15111 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15112 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015113 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015114 return DAG.getNode(Opc, DL, VT,
15115 N0.getOperand(0), N1.getOperand(0),
15116 DAG.getNode(ISD::TRUNCATE, DL,
15117 MVT::i8, ShAmt0));
15118 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015119
Evan Cheng760d1942010-01-04 21:22:48 +000015120 return SDValue();
15121}
15122
Manman Ren92363622012-06-07 22:39:10 +000015123// Generate NEG and CMOV for integer abs.
15124static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15125 EVT VT = N->getValueType(0);
15126
15127 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15128 // 8-bit integer abs to NEG and CMOV.
15129 if (VT.isInteger() && VT.getSizeInBits() == 8)
15130 return SDValue();
15131
15132 SDValue N0 = N->getOperand(0);
15133 SDValue N1 = N->getOperand(1);
15134 DebugLoc DL = N->getDebugLoc();
15135
15136 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15137 // and change it to SUB and CMOV.
15138 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15139 N0.getOpcode() == ISD::ADD &&
15140 N0.getOperand(1) == N1 &&
15141 N1.getOpcode() == ISD::SRA &&
15142 N1.getOperand(0) == N0.getOperand(0))
15143 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15144 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15145 // Generate SUB & CMOV.
15146 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15147 DAG.getConstant(0, VT), N0.getOperand(0));
15148
15149 SDValue Ops[] = { N0.getOperand(0), Neg,
15150 DAG.getConstant(X86::COND_GE, MVT::i8),
15151 SDValue(Neg.getNode(), 1) };
15152 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15153 Ops, array_lengthof(Ops));
15154 }
15155 return SDValue();
15156}
15157
Craig Topper3738ccd2011-12-27 06:27:23 +000015158// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015159static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15160 TargetLowering::DAGCombinerInfo &DCI,
15161 const X86Subtarget *Subtarget) {
15162 if (DCI.isBeforeLegalizeOps())
15163 return SDValue();
15164
Manman Ren45d53b82012-06-08 18:58:26 +000015165 if (Subtarget->hasCMov()) {
15166 SDValue RV = performIntegerAbsCombine(N, DAG);
15167 if (RV.getNode())
15168 return RV;
15169 }
Manman Ren92363622012-06-07 22:39:10 +000015170
15171 // Try forming BMI if it is available.
15172 if (!Subtarget->hasBMI())
15173 return SDValue();
15174
Craig Topperb4c94572011-10-21 06:55:01 +000015175 EVT VT = N->getValueType(0);
15176
15177 if (VT != MVT::i32 && VT != MVT::i64)
15178 return SDValue();
15179
Craig Topper3738ccd2011-12-27 06:27:23 +000015180 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15181
Craig Topperb4c94572011-10-21 06:55:01 +000015182 // Create BLSMSK instructions by finding X ^ (X-1)
15183 SDValue N0 = N->getOperand(0);
15184 SDValue N1 = N->getOperand(1);
15185 DebugLoc DL = N->getDebugLoc();
15186
15187 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15188 isAllOnes(N0.getOperand(1)))
15189 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15190
15191 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15192 isAllOnes(N1.getOperand(1)))
15193 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15194
15195 return SDValue();
15196}
15197
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015198/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15199static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015200 TargetLowering::DAGCombinerInfo &DCI,
15201 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015202 LoadSDNode *Ld = cast<LoadSDNode>(N);
15203 EVT RegVT = Ld->getValueType(0);
15204 EVT MemVT = Ld->getMemoryVT();
15205 DebugLoc dl = Ld->getDebugLoc();
15206 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15207
15208 ISD::LoadExtType Ext = Ld->getExtensionType();
15209
Nadav Rotemca6f2962011-09-18 19:00:23 +000015210 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015211 // shuffle. We need SSE4 for the shuffles.
15212 // TODO: It is possible to support ZExt by zeroing the undef values
15213 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015214 if (RegVT.isVector() && RegVT.isInteger() &&
15215 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015216 assert(MemVT != RegVT && "Cannot extend to the same type");
15217 assert(MemVT.isVector() && "Must load a vector from memory");
15218
15219 unsigned NumElems = RegVT.getVectorNumElements();
15220 unsigned RegSz = RegVT.getSizeInBits();
15221 unsigned MemSz = MemVT.getSizeInBits();
15222 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015223
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015224 // All sizes must be a power of two.
15225 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15226 return SDValue();
15227
15228 // Attempt to load the original value using scalar loads.
15229 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015230 MVT SclrLoadTy = MVT::i8;
15231 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15232 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15233 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015234 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015235 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015236 }
15237 }
15238
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015239 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15240 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15241 (64 <= MemSz))
15242 SclrLoadTy = MVT::f64;
15243
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015244 // Calculate the number of scalar loads that we need to perform
15245 // in order to load our vector from memory.
15246 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015247
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015248 // Represent our vector as a sequence of elements which are the
15249 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015250 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15251 RegSz/SclrLoadTy.getSizeInBits());
15252
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015253 // Represent the data using the same element type that is stored in
15254 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015255 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15256 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015257
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015258 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15259 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015260
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015261 // We can't shuffle using an illegal type.
15262 if (!TLI.isTypeLegal(WideVecVT))
15263 return SDValue();
15264
15265 SmallVector<SDValue, 8> Chains;
15266 SDValue Ptr = Ld->getBasePtr();
15267 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15268 TLI.getPointerTy());
15269 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15270
15271 for (unsigned i = 0; i < NumLoads; ++i) {
15272 // Perform a single load.
15273 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15274 Ptr, Ld->getPointerInfo(),
15275 Ld->isVolatile(), Ld->isNonTemporal(),
15276 Ld->isInvariant(), Ld->getAlignment());
15277 Chains.push_back(ScalarLoad.getValue(1));
15278 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15279 // another round of DAGCombining.
15280 if (i == 0)
15281 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15282 else
15283 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15284 ScalarLoad, DAG.getIntPtrConstant(i));
15285
15286 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15287 }
15288
15289 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15290 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015291
15292 // Bitcast the loaded value to a vector of the original element type, in
15293 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015294 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015295 unsigned SizeRatio = RegSz/MemSz;
15296
15297 // Redistribute the loaded elements into the different locations.
15298 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015299 for (unsigned i = 0; i != NumElems; ++i)
15300 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015301
15302 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015303 DAG.getUNDEF(WideVecVT),
15304 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015305
15306 // Bitcast to the requested type.
15307 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15308 // Replace the original load with the new sequence
15309 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015310 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015311 }
15312
15313 return SDValue();
15314}
15315
Chris Lattner149a4e52008-02-22 02:09:43 +000015316/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015317static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015318 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015319 StoreSDNode *St = cast<StoreSDNode>(N);
15320 EVT VT = St->getValue().getValueType();
15321 EVT StVT = St->getMemoryVT();
15322 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015323 SDValue StoredVal = St->getOperand(1);
15324 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15325
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015326 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015327 // On Sandy Bridge, 256-bit memory operations are executed by two
15328 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15329 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015330 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015331 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15332 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015333 SDValue Value0 = StoredVal.getOperand(0);
15334 SDValue Value1 = StoredVal.getOperand(1);
15335
15336 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15337 SDValue Ptr0 = St->getBasePtr();
15338 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15339
15340 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15341 St->getPointerInfo(), St->isVolatile(),
15342 St->isNonTemporal(), St->getAlignment());
15343 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15344 St->getPointerInfo(), St->isVolatile(),
15345 St->isNonTemporal(), St->getAlignment());
15346 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15347 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015348
15349 // Optimize trunc store (of multiple scalars) to shuffle and store.
15350 // First, pack all of the elements in one place. Next, store to memory
15351 // in fewer chunks.
15352 if (St->isTruncatingStore() && VT.isVector()) {
15353 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15354 unsigned NumElems = VT.getVectorNumElements();
15355 assert(StVT != VT && "Cannot truncate to the same type");
15356 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15357 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15358
15359 // From, To sizes and ElemCount must be pow of two
15360 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015361 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015362 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015363 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015364
Nadav Rotem614061b2011-08-10 19:30:14 +000015365 unsigned SizeRatio = FromSz / ToSz;
15366
15367 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15368
15369 // Create a type on which we perform the shuffle
15370 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15371 StVT.getScalarType(), NumElems*SizeRatio);
15372
15373 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15374
15375 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15376 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015377 for (unsigned i = 0; i != NumElems; ++i)
15378 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015379
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015380 // Can't shuffle using an illegal type.
15381 if (!TLI.isTypeLegal(WideVecVT))
15382 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015383
15384 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015385 DAG.getUNDEF(WideVecVT),
15386 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015387 // At this point all of the data is stored at the bottom of the
15388 // register. We now need to save it to mem.
15389
15390 // Find the largest store unit
15391 MVT StoreType = MVT::i8;
15392 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15393 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15394 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015395 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015396 StoreType = Tp;
15397 }
15398
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015399 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15400 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15401 (64 <= NumElems * ToSz))
15402 StoreType = MVT::f64;
15403
Nadav Rotem614061b2011-08-10 19:30:14 +000015404 // Bitcast the original vector into a vector of store-size units
15405 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015406 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015407 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15408 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15409 SmallVector<SDValue, 8> Chains;
15410 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15411 TLI.getPointerTy());
15412 SDValue Ptr = St->getBasePtr();
15413
15414 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015415 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015416 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15417 StoreType, ShuffWide,
15418 DAG.getIntPtrConstant(i));
15419 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15420 St->getPointerInfo(), St->isVolatile(),
15421 St->isNonTemporal(), St->getAlignment());
15422 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15423 Chains.push_back(Ch);
15424 }
15425
15426 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15427 Chains.size());
15428 }
15429
15430
Chris Lattner149a4e52008-02-22 02:09:43 +000015431 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15432 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015433 // A preferable solution to the general problem is to figure out the right
15434 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015435
15436 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015437 if (VT.getSizeInBits() != 64)
15438 return SDValue();
15439
Devang Patel578efa92009-06-05 21:57:13 +000015440 const Function *F = DAG.getMachineFunction().getFunction();
15441 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015442 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015443 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015444 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015445 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015446 isa<LoadSDNode>(St->getValue()) &&
15447 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15448 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015449 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015450 LoadSDNode *Ld = 0;
15451 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015452 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015453 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015454 // Must be a store of a load. We currently handle two cases: the load
15455 // is a direct child, and it's under an intervening TokenFactor. It is
15456 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015457 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015458 Ld = cast<LoadSDNode>(St->getChain());
15459 else if (St->getValue().hasOneUse() &&
15460 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015461 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015462 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015463 TokenFactorIndex = i;
15464 Ld = cast<LoadSDNode>(St->getValue());
15465 } else
15466 Ops.push_back(ChainVal->getOperand(i));
15467 }
15468 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015469
Evan Cheng536e6672009-03-12 05:59:15 +000015470 if (!Ld || !ISD::isNormalLoad(Ld))
15471 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015472
Evan Cheng536e6672009-03-12 05:59:15 +000015473 // If this is not the MMX case, i.e. we are just turning i64 load/store
15474 // into f64 load/store, avoid the transformation if there are multiple
15475 // uses of the loaded value.
15476 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15477 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015478
Evan Cheng536e6672009-03-12 05:59:15 +000015479 DebugLoc LdDL = Ld->getDebugLoc();
15480 DebugLoc StDL = N->getDebugLoc();
15481 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15482 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15483 // pair instead.
15484 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015485 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015486 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15487 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015488 Ld->isNonTemporal(), Ld->isInvariant(),
15489 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015490 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015491 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015492 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015493 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015494 Ops.size());
15495 }
Evan Cheng536e6672009-03-12 05:59:15 +000015496 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015497 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015498 St->isVolatile(), St->isNonTemporal(),
15499 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015500 }
Evan Cheng536e6672009-03-12 05:59:15 +000015501
15502 // Otherwise, lower to two pairs of 32-bit loads / stores.
15503 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015504 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15505 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015506
Owen Anderson825b72b2009-08-11 20:47:22 +000015507 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015508 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015509 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015510 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015511 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015512 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015513 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015514 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015515 MinAlign(Ld->getAlignment(), 4));
15516
15517 SDValue NewChain = LoLd.getValue(1);
15518 if (TokenFactorIndex != -1) {
15519 Ops.push_back(LoLd);
15520 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015521 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015522 Ops.size());
15523 }
15524
15525 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015526 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15527 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015528
15529 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015530 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015531 St->isVolatile(), St->isNonTemporal(),
15532 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015533 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015534 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015535 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015536 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015537 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015538 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015539 }
Dan Gohman475871a2008-07-27 21:46:04 +000015540 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015541}
15542
Duncan Sands17470be2011-09-22 20:15:48 +000015543/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15544/// and return the operands for the horizontal operation in LHS and RHS. A
15545/// horizontal operation performs the binary operation on successive elements
15546/// of its first operand, then on successive elements of its second operand,
15547/// returning the resulting values in a vector. For example, if
15548/// A = < float a0, float a1, float a2, float a3 >
15549/// and
15550/// B = < float b0, float b1, float b2, float b3 >
15551/// then the result of doing a horizontal operation on A and B is
15552/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15553/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15554/// A horizontal-op B, for some already available A and B, and if so then LHS is
15555/// set to A, RHS to B, and the routine returns 'true'.
15556/// Note that the binary operation should have the property that if one of the
15557/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015558static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015559 // Look for the following pattern: if
15560 // A = < float a0, float a1, float a2, float a3 >
15561 // B = < float b0, float b1, float b2, float b3 >
15562 // and
15563 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15564 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15565 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15566 // which is A horizontal-op B.
15567
15568 // At least one of the operands should be a vector shuffle.
15569 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15570 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15571 return false;
15572
15573 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015574
15575 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15576 "Unsupported vector type for horizontal add/sub");
15577
15578 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15579 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015580 unsigned NumElts = VT.getVectorNumElements();
15581 unsigned NumLanes = VT.getSizeInBits()/128;
15582 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015583 assert((NumLaneElts % 2 == 0) &&
15584 "Vector type should have an even number of elements in each lane");
15585 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015586
15587 // View LHS in the form
15588 // LHS = VECTOR_SHUFFLE A, B, LMask
15589 // If LHS is not a shuffle then pretend it is the shuffle
15590 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15591 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15592 // type VT.
15593 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015594 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015595 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15596 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15597 A = LHS.getOperand(0);
15598 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15599 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015600 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15601 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015602 } else {
15603 if (LHS.getOpcode() != ISD::UNDEF)
15604 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015605 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015606 LMask[i] = i;
15607 }
15608
15609 // Likewise, view RHS in the form
15610 // RHS = VECTOR_SHUFFLE C, D, RMask
15611 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015612 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015613 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15614 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15615 C = RHS.getOperand(0);
15616 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15617 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015618 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15619 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015620 } else {
15621 if (RHS.getOpcode() != ISD::UNDEF)
15622 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015623 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015624 RMask[i] = i;
15625 }
15626
15627 // Check that the shuffles are both shuffling the same vectors.
15628 if (!(A == C && B == D) && !(A == D && B == C))
15629 return false;
15630
15631 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15632 if (!A.getNode() && !B.getNode())
15633 return false;
15634
15635 // If A and B occur in reverse order in RHS, then "swap" them (which means
15636 // rewriting the mask).
15637 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015638 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015639
15640 // At this point LHS and RHS are equivalent to
15641 // LHS = VECTOR_SHUFFLE A, B, LMask
15642 // RHS = VECTOR_SHUFFLE A, B, RMask
15643 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015644 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015645 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015646
Craig Topperf8363302011-12-02 08:18:41 +000015647 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015648 if (LIdx < 0 || RIdx < 0 ||
15649 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15650 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015651 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015652
Craig Topperf8363302011-12-02 08:18:41 +000015653 // Check that successive elements are being operated on. If not, this is
15654 // not a horizontal operation.
15655 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15656 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015657 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015658 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015659 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015660 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015661 }
15662
15663 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15664 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15665 return true;
15666}
15667
15668/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15669static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15670 const X86Subtarget *Subtarget) {
15671 EVT VT = N->getValueType(0);
15672 SDValue LHS = N->getOperand(0);
15673 SDValue RHS = N->getOperand(1);
15674
15675 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015676 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015677 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015678 isHorizontalBinOp(LHS, RHS, true))
15679 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15680 return SDValue();
15681}
15682
15683/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15684static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15685 const X86Subtarget *Subtarget) {
15686 EVT VT = N->getValueType(0);
15687 SDValue LHS = N->getOperand(0);
15688 SDValue RHS = N->getOperand(1);
15689
15690 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015691 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015692 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015693 isHorizontalBinOp(LHS, RHS, false))
15694 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15695 return SDValue();
15696}
15697
Chris Lattner6cf73262008-01-25 06:14:17 +000015698/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15699/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015700static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015701 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15702 // F[X]OR(0.0, x) -> x
15703 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015704 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15705 if (C->getValueAPF().isPosZero())
15706 return N->getOperand(1);
15707 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15708 if (C->getValueAPF().isPosZero())
15709 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015710 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015711}
15712
Nadav Rotemd60cb112012-08-19 13:06:16 +000015713/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15714/// X86ISD::FMAX nodes.
15715static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15716 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15717
15718 // Only perform optimizations if UnsafeMath is used.
15719 if (!DAG.getTarget().Options.UnsafeFPMath)
15720 return SDValue();
15721
15722 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015723 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015724 unsigned NewOp = 0;
15725 switch (N->getOpcode()) {
15726 default: llvm_unreachable("unknown opcode");
15727 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15728 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15729 }
15730
15731 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15732 N->getOperand(0), N->getOperand(1));
15733}
15734
15735
Chris Lattneraf723b92008-01-25 05:46:26 +000015736/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015737static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015738 // FAND(0.0, x) -> 0.0
15739 // FAND(x, 0.0) -> 0.0
15740 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15741 if (C->getValueAPF().isPosZero())
15742 return N->getOperand(0);
15743 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15744 if (C->getValueAPF().isPosZero())
15745 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015746 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015747}
15748
Dan Gohmane5af2d32009-01-29 01:59:02 +000015749static SDValue PerformBTCombine(SDNode *N,
15750 SelectionDAG &DAG,
15751 TargetLowering::DAGCombinerInfo &DCI) {
15752 // BT ignores high bits in the bit index operand.
15753 SDValue Op1 = N->getOperand(1);
15754 if (Op1.hasOneUse()) {
15755 unsigned BitWidth = Op1.getValueSizeInBits();
15756 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15757 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015758 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15759 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015760 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015761 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15762 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15763 DCI.CommitTargetLoweringOpt(TLO);
15764 }
15765 return SDValue();
15766}
Chris Lattner83e6c992006-10-04 06:57:07 +000015767
Eli Friedman7a5e5552009-06-07 06:52:44 +000015768static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15769 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015770 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015771 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015772 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015773 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015774 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015775 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015776 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015777 }
15778 return SDValue();
15779}
15780
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015781static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15782 TargetLowering::DAGCombinerInfo &DCI,
15783 const X86Subtarget *Subtarget) {
15784 if (!DCI.isBeforeLegalizeOps())
15785 return SDValue();
15786
Craig Topper3ef43cf2012-04-24 06:36:35 +000015787 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015788 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015789
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015790 EVT VT = N->getValueType(0);
15791 SDValue Op = N->getOperand(0);
15792 EVT OpVT = Op.getValueType();
15793 DebugLoc dl = N->getDebugLoc();
15794
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015795 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15796 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015797
Craig Topper3ef43cf2012-04-24 06:36:35 +000015798 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015799 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015800
15801 // Optimize vectors in AVX mode
15802 // Sign extend v8i16 to v8i32 and
15803 // v4i32 to v4i64
15804 //
15805 // Divide input vector into two parts
15806 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15807 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15808 // concat the vectors to original VT
15809
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015810 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015811 SDValue Undef = DAG.getUNDEF(OpVT);
15812
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015813 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015814 for (unsigned i = 0; i != NumElems/2; ++i)
15815 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015816
Craig Toppercacafd42012-08-14 08:18:43 +000015817 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015818
15819 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015820 for (unsigned i = 0; i != NumElems/2; ++i)
15821 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015822
Craig Toppercacafd42012-08-14 08:18:43 +000015823 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015824
Craig Topper3ef43cf2012-04-24 06:36:35 +000015825 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015826 VT.getVectorNumElements()/2);
15827
Craig Topper3ef43cf2012-04-24 06:36:35 +000015828 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015829 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15830
15831 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15832 }
15833 return SDValue();
15834}
15835
Michael Liaof6c24ee2012-08-10 14:39:24 +000015836static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015837 const X86Subtarget* Subtarget) {
15838 DebugLoc dl = N->getDebugLoc();
15839 EVT VT = N->getValueType(0);
15840
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015841 // Let legalize expand this if it isn't a legal type yet.
15842 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15843 return SDValue();
15844
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015845 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015846 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15847 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015848 return SDValue();
15849
15850 SDValue A = N->getOperand(0);
15851 SDValue B = N->getOperand(1);
15852 SDValue C = N->getOperand(2);
15853
15854 bool NegA = (A.getOpcode() == ISD::FNEG);
15855 bool NegB = (B.getOpcode() == ISD::FNEG);
15856 bool NegC = (C.getOpcode() == ISD::FNEG);
15857
Michael Liaof6c24ee2012-08-10 14:39:24 +000015858 // Negative multiplication when NegA xor NegB
15859 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015860 if (NegA)
15861 A = A.getOperand(0);
15862 if (NegB)
15863 B = B.getOperand(0);
15864 if (NegC)
15865 C = C.getOperand(0);
15866
15867 unsigned Opcode;
15868 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015869 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015870 else
Craig Topperbf404372012-08-31 15:40:30 +000015871 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15872
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015873 return DAG.getNode(Opcode, dl, VT, A, B, C);
15874}
15875
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015876static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015877 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015878 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015879 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15880 // (and (i32 x86isd::setcc_carry), 1)
15881 // This eliminates the zext. This transformation is necessary because
15882 // ISD::SETCC is always legalized to i8.
15883 DebugLoc dl = N->getDebugLoc();
15884 SDValue N0 = N->getOperand(0);
15885 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015886 EVT OpVT = N0.getValueType();
15887
Evan Cheng2e489c42009-12-16 00:53:11 +000015888 if (N0.getOpcode() == ISD::AND &&
15889 N0.hasOneUse() &&
15890 N0.getOperand(0).hasOneUse()) {
15891 SDValue N00 = N0.getOperand(0);
15892 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15893 return SDValue();
15894 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15895 if (!C || C->getZExtValue() != 1)
15896 return SDValue();
15897 return DAG.getNode(ISD::AND, dl, VT,
15898 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15899 N00.getOperand(0), N00.getOperand(1)),
15900 DAG.getConstant(1, VT));
15901 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015902
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015903 // Optimize vectors in AVX mode:
15904 //
15905 // v8i16 -> v8i32
15906 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15907 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15908 // Concat upper and lower parts.
15909 //
15910 // v4i32 -> v4i64
15911 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15912 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15913 // Concat upper and lower parts.
15914 //
Craig Topperc16f8512012-04-25 06:39:39 +000015915 if (!DCI.isBeforeLegalizeOps())
15916 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015917
Craig Topperc16f8512012-04-25 06:39:39 +000015918 if (!Subtarget->hasAVX())
15919 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015920
Craig Topperc16f8512012-04-25 06:39:39 +000015921 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15922 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015923
Craig Topperc16f8512012-04-25 06:39:39 +000015924 if (Subtarget->hasAVX2())
15925 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015926
Craig Topperc16f8512012-04-25 06:39:39 +000015927 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15928 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15929 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015930
Craig Topperc16f8512012-04-25 06:39:39 +000015931 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15932 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015933
Craig Topperc16f8512012-04-25 06:39:39 +000015934 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15935 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15936
15937 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015938 }
15939
Evan Cheng2e489c42009-12-16 00:53:11 +000015940 return SDValue();
15941}
15942
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015943// Optimize x == -y --> x+y == 0
15944// x != -y --> x+y != 0
15945static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15946 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15947 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015948 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015949
15950 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15951 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15952 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15953 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15954 LHS.getValueType(), RHS, LHS.getOperand(1));
15955 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15956 addV, DAG.getConstant(0, addV.getValueType()), CC);
15957 }
15958 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15959 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15960 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15961 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15962 RHS.getValueType(), LHS, RHS.getOperand(1));
15963 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15964 addV, DAG.getConstant(0, addV.getValueType()), CC);
15965 }
15966 return SDValue();
15967}
15968
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015969// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015970static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15971 TargetLowering::DAGCombinerInfo &DCI,
15972 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015973 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015974 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15975 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015976
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015977 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15978 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15979 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015980 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015981 return DAG.getNode(ISD::AND, DL, MVT::i8,
15982 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015983 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015984 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015985
Michael Liao2a33cec2012-08-10 19:58:13 +000015986 SDValue Flags;
15987
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015988 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15989 if (Flags.getNode()) {
15990 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15991 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15992 }
15993
Michael Liao2a33cec2012-08-10 19:58:13 +000015994 return SDValue();
15995}
15996
15997// Optimize branch condition evaluation.
15998//
15999static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16000 TargetLowering::DAGCombinerInfo &DCI,
16001 const X86Subtarget *Subtarget) {
16002 DebugLoc DL = N->getDebugLoc();
16003 SDValue Chain = N->getOperand(0);
16004 SDValue Dest = N->getOperand(1);
16005 SDValue EFLAGS = N->getOperand(3);
16006 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16007
16008 SDValue Flags;
16009
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016010 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16011 if (Flags.getNode()) {
16012 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16013 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16014 Flags);
16015 }
16016
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016017 return SDValue();
16018}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016019
Craig Topper7fd5e162012-04-24 06:02:29 +000016020static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000016021 SDValue Op0 = N->getOperand(0);
16022 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016023
16024 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016025 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016026 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016027 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016028 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
16029 // Notice that we use SINT_TO_FP because we know that the high bits
16030 // are zero and SINT_TO_FP is better supported by the hardware.
16031 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16032 }
16033
16034 return SDValue();
16035}
16036
Benjamin Kramer1396c402011-06-18 11:09:41 +000016037static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16038 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016039 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016040 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016041
16042 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016043 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016044 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016045 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016046 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16047 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16048 }
16049
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016050 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16051 // a 32-bit target where SSE doesn't support i64->FP operations.
16052 if (Op0.getOpcode() == ISD::LOAD) {
16053 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16054 EVT VT = Ld->getValueType(0);
16055 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16056 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16057 !XTLI->getSubtarget()->is64Bit() &&
16058 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016059 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16060 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016061 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16062 return FILDChain;
16063 }
16064 }
16065 return SDValue();
16066}
16067
Craig Topper7fd5e162012-04-24 06:02:29 +000016068static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
16069 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016070
16071 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000016072 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
16073 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016074 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016075 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
16076 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
16077 }
16078
16079 return SDValue();
16080}
16081
Chris Lattner23a01992010-12-20 01:37:09 +000016082// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16083static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16084 X86TargetLowering::DAGCombinerInfo &DCI) {
16085 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16086 // the result is either zero or one (depending on the input carry bit).
16087 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16088 if (X86::isZeroNode(N->getOperand(0)) &&
16089 X86::isZeroNode(N->getOperand(1)) &&
16090 // We don't have a good way to replace an EFLAGS use, so only do this when
16091 // dead right now.
16092 SDValue(N, 1).use_empty()) {
16093 DebugLoc DL = N->getDebugLoc();
16094 EVT VT = N->getValueType(0);
16095 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16096 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16097 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16098 DAG.getConstant(X86::COND_B,MVT::i8),
16099 N->getOperand(2)),
16100 DAG.getConstant(1, VT));
16101 return DCI.CombineTo(N, Res1, CarryOut);
16102 }
16103
16104 return SDValue();
16105}
16106
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016107// fold (add Y, (sete X, 0)) -> adc 0, Y
16108// (add Y, (setne X, 0)) -> sbb -1, Y
16109// (sub (sete X, 0), Y) -> sbb 0, Y
16110// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016111static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016112 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016113
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016114 // Look through ZExts.
16115 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16116 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16117 return SDValue();
16118
16119 SDValue SetCC = Ext.getOperand(0);
16120 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16121 return SDValue();
16122
16123 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16124 if (CC != X86::COND_E && CC != X86::COND_NE)
16125 return SDValue();
16126
16127 SDValue Cmp = SetCC.getOperand(1);
16128 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016129 !X86::isZeroNode(Cmp.getOperand(1)) ||
16130 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016131 return SDValue();
16132
16133 SDValue CmpOp0 = Cmp.getOperand(0);
16134 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16135 DAG.getConstant(1, CmpOp0.getValueType()));
16136
16137 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16138 if (CC == X86::COND_NE)
16139 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16140 DL, OtherVal.getValueType(), OtherVal,
16141 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16142 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16143 DL, OtherVal.getValueType(), OtherVal,
16144 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16145}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016146
Craig Topper54f952a2011-11-19 09:02:40 +000016147/// PerformADDCombine - Do target-specific dag combines on integer adds.
16148static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16149 const X86Subtarget *Subtarget) {
16150 EVT VT = N->getValueType(0);
16151 SDValue Op0 = N->getOperand(0);
16152 SDValue Op1 = N->getOperand(1);
16153
16154 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016155 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016156 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016157 isHorizontalBinOp(Op0, Op1, true))
16158 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16159
16160 return OptimizeConditionalInDecrement(N, DAG);
16161}
16162
16163static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16164 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016165 SDValue Op0 = N->getOperand(0);
16166 SDValue Op1 = N->getOperand(1);
16167
16168 // X86 can't encode an immediate LHS of a sub. See if we can push the
16169 // negation into a preceding instruction.
16170 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016171 // If the RHS of the sub is a XOR with one use and a constant, invert the
16172 // immediate. Then add one to the LHS of the sub so we can turn
16173 // X-Y -> X+~Y+1, saving one register.
16174 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16175 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016176 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016177 EVT VT = Op0.getValueType();
16178 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16179 Op1.getOperand(0),
16180 DAG.getConstant(~XorC, VT));
16181 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016182 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016183 }
16184 }
16185
Craig Topper54f952a2011-11-19 09:02:40 +000016186 // Try to synthesize horizontal adds from adds of shuffles.
16187 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016188 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016189 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16190 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016191 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16192
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016193 return OptimizeConditionalInDecrement(N, DAG);
16194}
16195
Dan Gohman475871a2008-07-27 21:46:04 +000016196SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016197 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016198 SelectionDAG &DAG = DCI.DAG;
16199 switch (N->getOpcode()) {
16200 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016201 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016202 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016203 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016204 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016205 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016206 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16207 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016208 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016209 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016210 case ISD::SHL:
16211 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016212 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016213 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016214 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016215 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016216 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016217 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016218 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016219 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016220 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016221 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16222 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016223 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016224 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016225 case X86ISD::FMIN:
16226 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016227 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016228 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016229 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016230 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016231 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016232 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016233 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016234 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016235 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016236 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016237 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016238 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016239 case X86ISD::UNPCKH:
16240 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016241 case X86ISD::MOVHLPS:
16242 case X86ISD::MOVLHPS:
16243 case X86ISD::PSHUFD:
16244 case X86ISD::PSHUFHW:
16245 case X86ISD::PSHUFLW:
16246 case X86ISD::MOVSS:
16247 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016248 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016249 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016250 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016251 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016252 }
16253
Dan Gohman475871a2008-07-27 21:46:04 +000016254 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016255}
16256
Evan Chenge5b51ac2010-04-17 06:13:15 +000016257/// isTypeDesirableForOp - Return true if the target has native support for
16258/// the specified value type and it is 'desirable' to use the type for the
16259/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16260/// instruction encodings are longer and some i16 instructions are slow.
16261bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16262 if (!isTypeLegal(VT))
16263 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016264 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016265 return true;
16266
16267 switch (Opc) {
16268 default:
16269 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016270 case ISD::LOAD:
16271 case ISD::SIGN_EXTEND:
16272 case ISD::ZERO_EXTEND:
16273 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016274 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016275 case ISD::SRL:
16276 case ISD::SUB:
16277 case ISD::ADD:
16278 case ISD::MUL:
16279 case ISD::AND:
16280 case ISD::OR:
16281 case ISD::XOR:
16282 return false;
16283 }
16284}
16285
16286/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016287/// beneficial for dag combiner to promote the specified node. If true, it
16288/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016289bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016290 EVT VT = Op.getValueType();
16291 if (VT != MVT::i16)
16292 return false;
16293
Evan Cheng4c26e932010-04-19 19:29:22 +000016294 bool Promote = false;
16295 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016296 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016297 default: break;
16298 case ISD::LOAD: {
16299 LoadSDNode *LD = cast<LoadSDNode>(Op);
16300 // If the non-extending load has a single use and it's not live out, then it
16301 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016302 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16303 Op.hasOneUse()*/) {
16304 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16305 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16306 // The only case where we'd want to promote LOAD (rather then it being
16307 // promoted as an operand is when it's only use is liveout.
16308 if (UI->getOpcode() != ISD::CopyToReg)
16309 return false;
16310 }
16311 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016312 Promote = true;
16313 break;
16314 }
16315 case ISD::SIGN_EXTEND:
16316 case ISD::ZERO_EXTEND:
16317 case ISD::ANY_EXTEND:
16318 Promote = true;
16319 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016320 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016321 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016322 SDValue N0 = Op.getOperand(0);
16323 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016324 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016325 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016326 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016327 break;
16328 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016329 case ISD::ADD:
16330 case ISD::MUL:
16331 case ISD::AND:
16332 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016333 case ISD::XOR:
16334 Commute = true;
16335 // fallthrough
16336 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016337 SDValue N0 = Op.getOperand(0);
16338 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016339 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016340 return false;
16341 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016342 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016343 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016344 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016345 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016346 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016347 }
16348 }
16349
16350 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016351 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016352}
16353
Evan Cheng60c07e12006-07-05 22:17:51 +000016354//===----------------------------------------------------------------------===//
16355// X86 Inline Assembly Support
16356//===----------------------------------------------------------------------===//
16357
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016358namespace {
16359 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016360 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016361 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016362
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016363 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016364 StringRef piece(*args[i]);
16365 if (!s.startswith(piece)) // Check if the piece matches.
16366 return false;
16367
16368 s = s.substr(piece.size());
16369 StringRef::size_type pos = s.find_first_not_of(" \t");
16370 if (pos == 0) // We matched a prefix.
16371 return false;
16372
16373 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016374 }
16375
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016376 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016377 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016378 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016379}
16380
Chris Lattnerb8105652009-07-20 17:51:36 +000016381bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16382 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016383
16384 std::string AsmStr = IA->getAsmString();
16385
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016386 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16387 if (!Ty || Ty->getBitWidth() % 16 != 0)
16388 return false;
16389
Chris Lattnerb8105652009-07-20 17:51:36 +000016390 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016391 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016392 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016393
16394 switch (AsmPieces.size()) {
16395 default: return false;
16396 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016397 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016398 // we will turn this bswap into something that will be lowered to logical
16399 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16400 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016401 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016402 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16403 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16404 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16405 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16406 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16407 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016408 // No need to check constraints, nothing other than the equivalent of
16409 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016410 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016411 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016412
Chris Lattnerb8105652009-07-20 17:51:36 +000016413 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016414 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016415 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016416 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16417 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016418 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016419 const std::string &ConstraintsStr = IA->getConstraintString();
16420 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016421 std::sort(AsmPieces.begin(), AsmPieces.end());
16422 if (AsmPieces.size() == 4 &&
16423 AsmPieces[0] == "~{cc}" &&
16424 AsmPieces[1] == "~{dirflag}" &&
16425 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016426 AsmPieces[3] == "~{fpsr}")
16427 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016428 }
16429 break;
16430 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016431 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016432 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016433 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16434 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16435 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016436 AsmPieces.clear();
16437 const std::string &ConstraintsStr = IA->getConstraintString();
16438 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16439 std::sort(AsmPieces.begin(), AsmPieces.end());
16440 if (AsmPieces.size() == 4 &&
16441 AsmPieces[0] == "~{cc}" &&
16442 AsmPieces[1] == "~{dirflag}" &&
16443 AsmPieces[2] == "~{flags}" &&
16444 AsmPieces[3] == "~{fpsr}")
16445 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016446 }
Evan Cheng55d42002011-01-08 01:24:27 +000016447
16448 if (CI->getType()->isIntegerTy(64)) {
16449 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16450 if (Constraints.size() >= 2 &&
16451 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16452 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16453 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016454 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16455 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16456 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016457 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016458 }
16459 }
16460 break;
16461 }
16462 return false;
16463}
16464
16465
16466
Chris Lattnerf4dff842006-07-11 02:54:03 +000016467/// getConstraintType - Given a constraint letter, return the type of
16468/// constraint it is for this target.
16469X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016470X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16471 if (Constraint.size() == 1) {
16472 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016473 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016474 case 'q':
16475 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016476 case 'f':
16477 case 't':
16478 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016479 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016480 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016481 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016482 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016483 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016484 case 'a':
16485 case 'b':
16486 case 'c':
16487 case 'd':
16488 case 'S':
16489 case 'D':
16490 case 'A':
16491 return C_Register;
16492 case 'I':
16493 case 'J':
16494 case 'K':
16495 case 'L':
16496 case 'M':
16497 case 'N':
16498 case 'G':
16499 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016500 case 'e':
16501 case 'Z':
16502 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016503 default:
16504 break;
16505 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016506 }
Chris Lattner4234f572007-03-25 02:14:49 +000016507 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016508}
16509
John Thompson44ab89e2010-10-29 17:29:13 +000016510/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016511/// This object must already have been set up with the operand type
16512/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016513TargetLowering::ConstraintWeight
16514 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016515 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016516 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016517 Value *CallOperandVal = info.CallOperandVal;
16518 // If we don't have a value, we can't do a match,
16519 // but allow it at the lowest weight.
16520 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016521 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016522 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016523 // Look at the constraint type.
16524 switch (*constraint) {
16525 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016526 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16527 case 'R':
16528 case 'q':
16529 case 'Q':
16530 case 'a':
16531 case 'b':
16532 case 'c':
16533 case 'd':
16534 case 'S':
16535 case 'D':
16536 case 'A':
16537 if (CallOperandVal->getType()->isIntegerTy())
16538 weight = CW_SpecificReg;
16539 break;
16540 case 'f':
16541 case 't':
16542 case 'u':
16543 if (type->isFloatingPointTy())
16544 weight = CW_SpecificReg;
16545 break;
16546 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016547 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016548 weight = CW_SpecificReg;
16549 break;
16550 case 'x':
16551 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016552 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016553 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016554 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016555 break;
16556 case 'I':
16557 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16558 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016559 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016560 }
16561 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016562 case 'J':
16563 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16564 if (C->getZExtValue() <= 63)
16565 weight = CW_Constant;
16566 }
16567 break;
16568 case 'K':
16569 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16570 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16571 weight = CW_Constant;
16572 }
16573 break;
16574 case 'L':
16575 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16576 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16577 weight = CW_Constant;
16578 }
16579 break;
16580 case 'M':
16581 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16582 if (C->getZExtValue() <= 3)
16583 weight = CW_Constant;
16584 }
16585 break;
16586 case 'N':
16587 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16588 if (C->getZExtValue() <= 0xff)
16589 weight = CW_Constant;
16590 }
16591 break;
16592 case 'G':
16593 case 'C':
16594 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16595 weight = CW_Constant;
16596 }
16597 break;
16598 case 'e':
16599 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16600 if ((C->getSExtValue() >= -0x80000000LL) &&
16601 (C->getSExtValue() <= 0x7fffffffLL))
16602 weight = CW_Constant;
16603 }
16604 break;
16605 case 'Z':
16606 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16607 if (C->getZExtValue() <= 0xffffffff)
16608 weight = CW_Constant;
16609 }
16610 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016611 }
16612 return weight;
16613}
16614
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016615/// LowerXConstraint - try to replace an X constraint, which matches anything,
16616/// with another that has more specific requirements based on the type of the
16617/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016618const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016619LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016620 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16621 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016622 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016623 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016624 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016625 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016626 return "x";
16627 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016628
Chris Lattner5e764232008-04-26 23:02:14 +000016629 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016630}
16631
Chris Lattner48884cd2007-08-25 00:47:38 +000016632/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16633/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016634void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016635 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016636 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016637 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016638 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016639
Eric Christopher100c8332011-06-02 23:16:42 +000016640 // Only support length 1 constraints for now.
16641 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016642
Eric Christopher100c8332011-06-02 23:16:42 +000016643 char ConstraintLetter = Constraint[0];
16644 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016645 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016646 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016648 if (C->getZExtValue() <= 31) {
16649 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016650 break;
16651 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016652 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016653 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016654 case 'J':
16655 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016656 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016657 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16658 break;
16659 }
16660 }
16661 return;
16662 case 'K':
16663 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016664 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016665 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16666 break;
16667 }
16668 }
16669 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016670 case 'N':
16671 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016672 if (C->getZExtValue() <= 255) {
16673 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016674 break;
16675 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016676 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016677 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016678 case 'e': {
16679 // 32-bit signed value
16680 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016681 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16682 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016683 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016684 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016685 break;
16686 }
16687 // FIXME gcc accepts some relocatable values here too, but only in certain
16688 // memory models; it's complicated.
16689 }
16690 return;
16691 }
16692 case 'Z': {
16693 // 32-bit unsigned value
16694 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016695 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16696 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016697 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16698 break;
16699 }
16700 }
16701 // FIXME gcc accepts some relocatable values here too, but only in certain
16702 // memory models; it's complicated.
16703 return;
16704 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016705 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016706 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016707 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016708 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016709 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016710 break;
16711 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016712
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016713 // In any sort of PIC mode addresses need to be computed at runtime by
16714 // adding in a register or some sort of table lookup. These can't
16715 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016716 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016717 return;
16718
Chris Lattnerdc43a882007-05-03 16:52:29 +000016719 // If we are in non-pic codegen mode, we allow the address of a global (with
16720 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016721 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016722 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016723
Chris Lattner49921962009-05-08 18:23:14 +000016724 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16725 while (1) {
16726 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16727 Offset += GA->getOffset();
16728 break;
16729 } else if (Op.getOpcode() == ISD::ADD) {
16730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16731 Offset += C->getZExtValue();
16732 Op = Op.getOperand(0);
16733 continue;
16734 }
16735 } else if (Op.getOpcode() == ISD::SUB) {
16736 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16737 Offset += -C->getZExtValue();
16738 Op = Op.getOperand(0);
16739 continue;
16740 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016741 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016742
Chris Lattner49921962009-05-08 18:23:14 +000016743 // Otherwise, this isn't something we can handle, reject it.
16744 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016745 }
Eric Christopherfd179292009-08-27 18:07:15 +000016746
Dan Gohman46510a72010-04-15 01:51:59 +000016747 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016748 // If we require an extra load to get this address, as in PIC mode, we
16749 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016750 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16751 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016752 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016753
Devang Patel0d881da2010-07-06 22:08:15 +000016754 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16755 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016756 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016757 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016758 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016759
Gabor Greifba36cb52008-08-28 21:40:38 +000016760 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016761 Ops.push_back(Result);
16762 return;
16763 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016764 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016765}
16766
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016767std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016768X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016769 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016770 // First, see if this is a constraint that directly corresponds to an LLVM
16771 // register class.
16772 if (Constraint.size() == 1) {
16773 // GCC Constraint Letters
16774 switch (Constraint[0]) {
16775 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016776 // TODO: Slight differences here in allocation order and leaving
16777 // RIP in the class. Do they matter any more here than they do
16778 // in the normal allocation?
16779 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16780 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016781 if (VT == MVT::i32 || VT == MVT::f32)
16782 return std::make_pair(0U, &X86::GR32RegClass);
16783 if (VT == MVT::i16)
16784 return std::make_pair(0U, &X86::GR16RegClass);
16785 if (VT == MVT::i8 || VT == MVT::i1)
16786 return std::make_pair(0U, &X86::GR8RegClass);
16787 if (VT == MVT::i64 || VT == MVT::f64)
16788 return std::make_pair(0U, &X86::GR64RegClass);
16789 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016790 }
16791 // 32-bit fallthrough
16792 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016793 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016794 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16795 if (VT == MVT::i16)
16796 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16797 if (VT == MVT::i8 || VT == MVT::i1)
16798 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16799 if (VT == MVT::i64)
16800 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016801 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016802 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016803 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016804 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016805 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016806 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016807 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016808 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016809 return std::make_pair(0U, &X86::GR32RegClass);
16810 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016811 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016812 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016813 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016814 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016815 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016816 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016817 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16818 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016819 case 'f': // FP Stack registers.
16820 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16821 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016822 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016823 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016824 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016825 return std::make_pair(0U, &X86::RFP64RegClass);
16826 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016827 case 'y': // MMX_REGS if MMX allowed.
16828 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016829 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016830 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016831 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016832 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016833 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016834 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016835
Owen Anderson825b72b2009-08-11 20:47:22 +000016836 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016837 default: break;
16838 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016839 case MVT::f32:
16840 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016841 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016842 case MVT::f64:
16843 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016844 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016845 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016846 case MVT::v16i8:
16847 case MVT::v8i16:
16848 case MVT::v4i32:
16849 case MVT::v2i64:
16850 case MVT::v4f32:
16851 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016852 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016853 // AVX types.
16854 case MVT::v32i8:
16855 case MVT::v16i16:
16856 case MVT::v8i32:
16857 case MVT::v4i64:
16858 case MVT::v8f32:
16859 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016860 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016861 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016862 break;
16863 }
16864 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016865
Chris Lattnerf76d1802006-07-31 23:26:50 +000016866 // Use the default implementation in TargetLowering to convert the register
16867 // constraint into a member of a register class.
16868 std::pair<unsigned, const TargetRegisterClass*> Res;
16869 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016870
16871 // Not found as a standard register?
16872 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016873 // Map st(0) -> st(7) -> ST0
16874 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16875 tolower(Constraint[1]) == 's' &&
16876 tolower(Constraint[2]) == 't' &&
16877 Constraint[3] == '(' &&
16878 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16879 Constraint[5] == ')' &&
16880 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016881
Chris Lattner56d77c72009-09-13 22:41:48 +000016882 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016883 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016884 return Res;
16885 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016886
Chris Lattner56d77c72009-09-13 22:41:48 +000016887 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016888 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016889 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016890 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016891 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016892 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016893
16894 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016895 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016896 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016897 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016898 return Res;
16899 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016900
Dale Johannesen330169f2008-11-13 21:52:36 +000016901 // 'A' means EAX + EDX.
16902 if (Constraint == "A") {
16903 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016904 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016905 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016906 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016907 return Res;
16908 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016909
Chris Lattnerf76d1802006-07-31 23:26:50 +000016910 // Otherwise, check to see if this is a register class of the wrong value
16911 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16912 // turn into {ax},{dx}.
16913 if (Res.second->hasType(VT))
16914 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016915
Chris Lattnerf76d1802006-07-31 23:26:50 +000016916 // All of the single-register GCC register classes map their values onto
16917 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16918 // really want an 8-bit or 32-bit register, map to the appropriate register
16919 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016920 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016921 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016922 unsigned DestReg = 0;
16923 switch (Res.first) {
16924 default: break;
16925 case X86::AX: DestReg = X86::AL; break;
16926 case X86::DX: DestReg = X86::DL; break;
16927 case X86::CX: DestReg = X86::CL; break;
16928 case X86::BX: DestReg = X86::BL; break;
16929 }
16930 if (DestReg) {
16931 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016932 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016933 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016934 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016935 unsigned DestReg = 0;
16936 switch (Res.first) {
16937 default: break;
16938 case X86::AX: DestReg = X86::EAX; break;
16939 case X86::DX: DestReg = X86::EDX; break;
16940 case X86::CX: DestReg = X86::ECX; break;
16941 case X86::BX: DestReg = X86::EBX; break;
16942 case X86::SI: DestReg = X86::ESI; break;
16943 case X86::DI: DestReg = X86::EDI; break;
16944 case X86::BP: DestReg = X86::EBP; break;
16945 case X86::SP: DestReg = X86::ESP; break;
16946 }
16947 if (DestReg) {
16948 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016949 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016950 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016951 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016952 unsigned DestReg = 0;
16953 switch (Res.first) {
16954 default: break;
16955 case X86::AX: DestReg = X86::RAX; break;
16956 case X86::DX: DestReg = X86::RDX; break;
16957 case X86::CX: DestReg = X86::RCX; break;
16958 case X86::BX: DestReg = X86::RBX; break;
16959 case X86::SI: DestReg = X86::RSI; break;
16960 case X86::DI: DestReg = X86::RDI; break;
16961 case X86::BP: DestReg = X86::RBP; break;
16962 case X86::SP: DestReg = X86::RSP; break;
16963 }
16964 if (DestReg) {
16965 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016966 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016967 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016968 }
Craig Topperc9099502012-04-20 06:31:50 +000016969 } else if (Res.second == &X86::FR32RegClass ||
16970 Res.second == &X86::FR64RegClass ||
16971 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016972 // Handle references to XMM physical registers that got mapped into the
16973 // wrong class. This can happen with constraints like {xmm0} where the
16974 // target independent register mapper will just pick the first match it can
16975 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016976
16977 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016978 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016979 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016980 Res.second = &X86::FR64RegClass;
16981 else if (X86::VR128RegClass.hasType(VT))
16982 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016983 else if (X86::VR256RegClass.hasType(VT))
16984 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016985 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016986
Chris Lattnerf76d1802006-07-31 23:26:50 +000016987 return Res;
16988}