blob: ce9b0a89c0e3cd8a5dec6f247bbe3d5c28fdd3bd [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();
Micah Villmow3574eca2012-10-08 16:38:25 +0000164 TD = getDataLayout();
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)
Preston Gurd8d662b52012-10-04 21:33:40 +0000187 addBypassSlowDiv(32, 8);
Preston Gurd2e2efd92012-09-04 18:22:17 +0000188
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);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000460 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
461 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000462 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000463 // other SjLj exception interfaces are implemented and please don't build
464 // your own exception handling based on them.
465 // LLVM/Clang supports zero-cost DWARF exception handling.
466 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
467 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000468
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000469 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
471 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
472 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
473 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000474 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
476 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000477 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000478 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
480 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
481 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
482 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000483 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000484 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000485 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
487 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
488 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000489 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
491 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
492 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000493 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000494
Craig Topper1accb7e2012-01-10 06:54:16 +0000495 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000497
Eric Christopher9a9d2752010-07-22 02:48:34 +0000498 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000499 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000500
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000501 // On X86 and X86-64, atomic operations are lowered to locked instructions.
502 // Locked instructions, in turn, have implicit fence semantics (all memory
503 // operations are flushed before issuing the locked instruction, and they
504 // are not buffered), so we can fold away the common pattern of
505 // fence-atomic-fence.
506 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000507
Mon P Wang63307c32008-05-05 19:05:59 +0000508 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000509 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000510 MVT VT = IntVTs[i];
511 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000513 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000514 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000515
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000516 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000517 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
524 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000525 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
527 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
528 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000529 }
530
Eli Friedman43f51ae2011-08-26 21:21:21 +0000531 if (Subtarget->hasCmpxchg16b()) {
532 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
533 }
534
Evan Cheng3c992d22006-03-07 02:02:57 +0000535 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000536 if (!Subtarget->isTargetDarwin() &&
537 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000538 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000540 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000541
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
543 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
544 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
545 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000546 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000547 setExceptionPointerRegister(X86::RAX);
548 setExceptionSelectorRegister(X86::RDX);
549 } else {
550 setExceptionPointerRegister(X86::EAX);
551 setExceptionSelectorRegister(X86::EDX);
552 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
554 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000555
Duncan Sands4a544a72011-09-06 13:37:06 +0000556 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
557 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000558
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000560 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000561
Nate Begemanacc398c2006-01-25 18:21:52 +0000562 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::VASTART , MVT::Other, Custom);
564 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000565 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::VAARG , MVT::Other, Custom);
567 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000568 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::VAARG , MVT::Other, Expand);
570 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000571 }
Evan Chengae642192007-03-02 23:16:35 +0000572
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
574 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000575
576 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
577 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
578 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000579 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000580 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
581 MVT::i64 : MVT::i32, Custom);
582 else
583 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
584 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000585
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000586 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000588 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000589 addRegisterClass(MVT::f32, &X86::FR32RegClass);
590 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000591
Evan Cheng223547a2006-01-31 22:28:30 +0000592 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::FABS , MVT::f64, Custom);
594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f64, Custom);
598 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000599
Evan Cheng68c47cb2007-01-05 07:55:56 +0000600 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
602 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000603
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000604 // Lower this to FGETSIGNx86 plus an AND.
605 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
606 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
607
Evan Chengd25e9e82006-02-02 00:28:23 +0000608 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64, Expand);
610 setOperationAction(ISD::FCOS , MVT::f64, Expand);
611 setOperationAction(ISD::FSIN , MVT::f32, Expand);
612 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000613
Chris Lattnera54aa942006-01-29 06:26:08 +0000614 // Expand FP immediates into loads from the stack, except for the special
615 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 addLegalFPImmediate(APFloat(+0.0)); // xorpd
617 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000618 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 // Use SSE for f32, x87 for f64.
620 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000621 addRegisterClass(MVT::f32, &X86::FR32RegClass);
622 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623
624 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
627 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631
632 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
634 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000635
636 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::FSIN , MVT::f32, Expand);
638 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639
Nate Begemane1795842008-02-14 08:57:00 +0000640 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641 addLegalFPImmediate(APFloat(+0.0f)); // xorps
642 addLegalFPImmediate(APFloat(+0.0)); // FLD0
643 addLegalFPImmediate(APFloat(+1.0)); // FLD1
644 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
645 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
646
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000647 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
649 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000650 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000651 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000652 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000653 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000654 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
655 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000656
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
658 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
660 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000661
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000662 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000663 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000665 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000667 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000668 addLegalFPImmediate(APFloat(+0.0)); // FLD0
669 addLegalFPImmediate(APFloat(+1.0)); // FLD1
670 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
671 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000672 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
673 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
674 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
675 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000676 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000677
Cameron Zwarich33390842011-07-08 21:39:21 +0000678 // We don't support FMA.
679 setOperationAction(ISD::FMA, MVT::f64, Expand);
680 setOperationAction(ISD::FMA, MVT::f32, Expand);
681
Dale Johannesen59a58732007-08-05 18:49:15 +0000682 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000683 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000684 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
686 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000687 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000688 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000689 addLegalFPImmediate(TmpFlt); // FLD0
690 TmpFlt.changeSign();
691 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000692
693 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000694 APFloat TmpFlt2(+1.0);
695 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
696 &ignored);
697 addLegalFPImmediate(TmpFlt2); // FLD1
698 TmpFlt2.changeSign();
699 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
700 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000702 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
704 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000705 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000706
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000707 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
708 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
709 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
710 setOperationAction(ISD::FRINT, MVT::f80, Expand);
711 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000712 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000713 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000714
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000715 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
718 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000719
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::FLOG, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
722 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP, MVT::f80, Expand);
724 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000725
Mon P Wangf007a8b2008-11-06 05:31:54 +0000726 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000727 // (for widening) or expand (for scalarization). Then we will selectively
728 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000729 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
730 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000747 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
748 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000753 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000757 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000765 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000767 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
769 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000774 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
776 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
777 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
778 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
779 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
780 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
781 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
782 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
783 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000784 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000785 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
786 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
787 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
788 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000789 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000790 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
791 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000792 setTruncStoreAction((MVT::SimpleValueType)VT,
793 (MVT::SimpleValueType)InnerVT, Expand);
794 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
795 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
796 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000797 }
798
Evan Chengc7ce29b2009-02-13 22:36:38 +0000799 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
800 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000801 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000802 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000803 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Dale Johannesen0488fb62010-09-30 23:57:10 +0000806 // MMX-sized vectors (other than x86mmx) are expected to be expanded
807 // into smaller operations.
808 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
809 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
810 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
811 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
812 setOperationAction(ISD::AND, MVT::v8i8, Expand);
813 setOperationAction(ISD::AND, MVT::v4i16, Expand);
814 setOperationAction(ISD::AND, MVT::v2i32, Expand);
815 setOperationAction(ISD::AND, MVT::v1i64, Expand);
816 setOperationAction(ISD::OR, MVT::v8i8, Expand);
817 setOperationAction(ISD::OR, MVT::v4i16, Expand);
818 setOperationAction(ISD::OR, MVT::v2i32, Expand);
819 setOperationAction(ISD::OR, MVT::v1i64, Expand);
820 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
821 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
822 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
823 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
824 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
825 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
826 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
827 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
828 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
829 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
830 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
831 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
832 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000833 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
834 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
835 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
836 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000837
Craig Topper1accb7e2012-01-10 06:54:16 +0000838 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000839 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000840
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
842 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
843 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
844 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
845 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
846 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000847 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
849 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
850 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
851 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
852 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000853 }
854
Craig Topper1accb7e2012-01-10 06:54:16 +0000855 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000856 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000857
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000858 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
859 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000860 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
861 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
862 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
863 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000864
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
866 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
867 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
868 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
869 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
870 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
871 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
872 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
873 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
874 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
875 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
876 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
877 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
878 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
879 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
880 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000881 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Nadav Rotem354efd82011-09-18 14:57:03 +0000883 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000884 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
885 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
886 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000887
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
889 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
890 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
891 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
892 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000893
Evan Cheng2c3ae372006-04-12 21:21:57 +0000894 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000895 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000896 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000897 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000898 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000899 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000900 // Do not attempt to custom lower non-128-bit vectors
901 if (!VT.is128BitVector())
902 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000903 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
904 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
905 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
909 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
910 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
911 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
912 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
913 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000914
Nate Begemancdd1eec2008-02-12 22:51:28 +0000915 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
917 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000918 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000919
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000920 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000921 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000922 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000923
924 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000925 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000926 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000927
Craig Topper0d1f1762012-08-12 00:34:56 +0000928 setOperationAction(ISD::AND, VT, Promote);
929 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
930 setOperationAction(ISD::OR, VT, Promote);
931 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
932 setOperationAction(ISD::XOR, VT, Promote);
933 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
934 setOperationAction(ISD::LOAD, VT, Promote);
935 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
936 setOperationAction(ISD::SELECT, VT, Promote);
937 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000938 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000939
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000941
Evan Cheng2c3ae372006-04-12 21:21:57 +0000942 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
944 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
945 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
946 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000947
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
949 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000950
Michael Liaoa7554632012-10-23 17:36:08 +0000951 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
952 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000953 // As there is no 64-bit GPR available, we need build a special custom
954 // sequence to convert from v2i32 to v2f32.
955 if (!Subtarget->is64Bit())
956 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000957
Michael Liao9d796db2012-10-10 16:32:15 +0000958 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000959 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000960
Michael Liaob8150d82012-09-10 18:33:51 +0000961 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000962 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000963
Craig Topperd0a31172012-01-10 06:37:29 +0000964 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000965 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
966 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
967 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
968 setOperationAction(ISD::FRINT, MVT::f32, Legal);
969 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
970 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
971 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
972 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
973 setOperationAction(ISD::FRINT, MVT::f64, Legal);
974 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
975
Craig Topper12fb5c62012-09-08 17:42:27 +0000976 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
977 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
978
Nate Begeman14d12ca2008-02-11 04:19:36 +0000979 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000980 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000981
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000982 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
983 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
984 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
985 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
986 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000987
Nate Begeman14d12ca2008-02-11 04:19:36 +0000988 // i8 and i16 vectors are custom , because the source register and source
989 // source memory operand types are not the same width. f32 vectors are
990 // custom since the immediate controlling the insert encodes additional
991 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000992 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
993 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
994 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
995 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000996
Owen Anderson825b72b2009-08-11 20:47:22 +0000997 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
998 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
999 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1000 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001001
Pete Coopera77214a2011-11-14 19:38:42 +00001002 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001003 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001004 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001005 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1006 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001007 }
1008 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001009
Craig Topper1accb7e2012-01-10 06:54:16 +00001010 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001011 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001012 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001013
Nadav Rotem43012222011-05-11 08:12:09 +00001014 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001015 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001016
Nadav Rotem43012222011-05-11 08:12:09 +00001017 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001018 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001019
1020 if (Subtarget->hasAVX2()) {
1021 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1022 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1023
1024 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1025 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1026
1027 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1028 } else {
1029 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1030 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1031
1032 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1033 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1034
1035 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1036 }
Nadav Rotem43012222011-05-11 08:12:09 +00001037 }
1038
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001039 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001040 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1041 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1042 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1043 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1044 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1045 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001046
Owen Anderson825b72b2009-08-11 20:47:22 +00001047 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001048 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1049 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001050
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1052 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1053 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1054 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1055 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001056 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001057 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001058 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001059
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1061 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1062 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1063 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1064 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001065 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001066 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001067 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001068
Michael Liaobedcbd42012-10-16 18:14:11 +00001069 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
1070
1071 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1072
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001073 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1074 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001075 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001076
Michael Liaoa7554632012-10-23 17:36:08 +00001077 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1078 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1079 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1080
Michael Liaob8150d82012-09-10 18:33:51 +00001081 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1082
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001083 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1084 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1085
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001086 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1087 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1088
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001089 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001090 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001091
Duncan Sands28b77e92011-09-06 19:07:46 +00001092 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1093 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1094 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1095 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001096
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001097 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1098 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1099 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1100
Craig Topperaaa643c2011-11-09 07:28:55 +00001101 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1102 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1103 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1104 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001105
Craig Topperbf404372012-08-31 15:40:30 +00001106 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001107 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1108 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1109 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1110 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1111 setOperationAction(ISD::FMA, MVT::f32, Custom);
1112 setOperationAction(ISD::FMA, MVT::f64, Custom);
1113 }
Craig Topper880ef452012-08-11 22:34:26 +00001114
Craig Topperaaa643c2011-11-09 07:28:55 +00001115 if (Subtarget->hasAVX2()) {
1116 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1117 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1118 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1119 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001120
Craig Topperaaa643c2011-11-09 07:28:55 +00001121 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1122 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1123 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1124 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001125
Craig Topperaaa643c2011-11-09 07:28:55 +00001126 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1127 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1128 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001129 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001130
1131 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001132
1133 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1134 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1135
1136 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1137 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1138
1139 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001140 } else {
1141 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1142 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1143 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1144 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1145
1146 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1147 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1148 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1149 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1150
1151 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1152 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1153 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1154 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001155
1156 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1157 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1158
1159 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1160 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1161
1162 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001163 }
Craig Topper13894fa2011-08-24 06:14:18 +00001164
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001165 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001166 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1167 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001168 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001169
1170 // Extract subvector is special because the value type
1171 // (result) is 128-bit but the source is 256-bit wide.
1172 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001173 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001174
1175 // Do not attempt to custom lower other non-256-bit vectors
1176 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001177 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001178
Craig Topper0d1f1762012-08-12 00:34:56 +00001179 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1180 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1181 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1182 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1183 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1184 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1185 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001186 }
1187
David Greene54d8eba2011-01-27 22:38:56 +00001188 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001189 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001190 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001191
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001192 // Do not attempt to promote non-256-bit vectors
1193 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001194 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001195
Craig Topper0d1f1762012-08-12 00:34:56 +00001196 setOperationAction(ISD::AND, VT, Promote);
1197 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1198 setOperationAction(ISD::OR, VT, Promote);
1199 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1200 setOperationAction(ISD::XOR, VT, Promote);
1201 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1202 setOperationAction(ISD::LOAD, VT, Promote);
1203 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1204 setOperationAction(ISD::SELECT, VT, Promote);
1205 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001206 }
David Greene9b9838d2009-06-29 16:47:10 +00001207 }
1208
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001209 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1210 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001211 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1212 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001213 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1214 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001215 }
1216
Evan Cheng6be2c582006-04-05 23:38:46 +00001217 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001219 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001220
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001221
Eli Friedman962f5492010-06-02 19:35:46 +00001222 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1223 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001224 //
Eli Friedman962f5492010-06-02 19:35:46 +00001225 // FIXME: We really should do custom legalization for addition and
1226 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1227 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001228 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1229 // Add/Sub/Mul with overflow operations are custom lowered.
1230 MVT VT = IntVTs[i];
1231 setOperationAction(ISD::SADDO, VT, Custom);
1232 setOperationAction(ISD::UADDO, VT, Custom);
1233 setOperationAction(ISD::SSUBO, VT, Custom);
1234 setOperationAction(ISD::USUBO, VT, Custom);
1235 setOperationAction(ISD::SMULO, VT, Custom);
1236 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001237 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001238
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001239 // There are no 8-bit 3-address imul/mul instructions
1240 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1241 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001242
Evan Chengd54f2d52009-03-31 19:38:51 +00001243 if (!Subtarget->is64Bit()) {
1244 // These libcalls are not available in 32-bit.
1245 setLibcallName(RTLIB::SHL_I128, 0);
1246 setLibcallName(RTLIB::SRL_I128, 0);
1247 setLibcallName(RTLIB::SRA_I128, 0);
1248 }
1249
Evan Cheng206ee9d2006-07-07 08:33:52 +00001250 // We have target-specific dag combine patterns for the following nodes:
1251 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001252 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001253 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001254 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001255 setTargetDAGCombine(ISD::SHL);
1256 setTargetDAGCombine(ISD::SRA);
1257 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001258 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001259 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001260 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001261 setTargetDAGCombine(ISD::FADD);
1262 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001263 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001264 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001265 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001266 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001267 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001268 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001269 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001270 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001271 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001272 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001273 if (Subtarget->is64Bit())
1274 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001275 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001276
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001277 computeRegisterProperties();
1278
Evan Cheng05219282011-01-06 06:52:41 +00001279 // On Darwin, -Os means optimize for size without hurting performance,
1280 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001281 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001282 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001283 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001284 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1285 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1286 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001287 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001288 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001289
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001290 // Predictable cmov don't hurt on atom because it's in-order.
1291 predictableSelectIsExpensive = !Subtarget->isAtom();
1292
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001293 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001294}
1295
Scott Michel5b8f82e2008-03-10 15:42:14 +00001296
Duncan Sands28b77e92011-09-06 19:07:46 +00001297EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1298 if (!VT.isVector()) return MVT::i8;
1299 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001300}
1301
1302
Evan Cheng29286502008-01-23 23:17:41 +00001303/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1304/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001305static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001306 if (MaxAlign == 16)
1307 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001308 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001309 if (VTy->getBitWidth() == 128)
1310 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001311 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001312 unsigned EltAlign = 0;
1313 getMaxByValAlign(ATy->getElementType(), EltAlign);
1314 if (EltAlign > MaxAlign)
1315 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001316 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001317 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1318 unsigned EltAlign = 0;
1319 getMaxByValAlign(STy->getElementType(i), EltAlign);
1320 if (EltAlign > MaxAlign)
1321 MaxAlign = EltAlign;
1322 if (MaxAlign == 16)
1323 break;
1324 }
1325 }
Evan Cheng29286502008-01-23 23:17:41 +00001326}
1327
1328/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1329/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001330/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1331/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001332unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001333 if (Subtarget->is64Bit()) {
1334 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001335 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001336 if (TyAlign > 8)
1337 return TyAlign;
1338 return 8;
1339 }
1340
Evan Cheng29286502008-01-23 23:17:41 +00001341 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001342 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001343 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001344 return Align;
1345}
Chris Lattner2b02a442007-02-25 08:29:00 +00001346
Evan Chengf0df0312008-05-15 08:39:06 +00001347/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001348/// and store operations as a result of memset, memcpy, and memmove
1349/// lowering. If DstAlign is zero that means it's safe to destination
1350/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1351/// means there isn't a need to check it against alignment requirement,
1352/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001353/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001354/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1355/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1356/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001357/// It returns EVT::Other if the type should be determined using generic
1358/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001359EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001360X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1361 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001362 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001363 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001364 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001365 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1366 // linux. This is because the stack realignment code can't handle certain
1367 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001368 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001369 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00001370 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001371 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001372 (Subtarget->isUnalignedMemAccessFast() ||
1373 ((DstAlign == 0 || DstAlign >= 16) &&
1374 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001375 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001376 if (Subtarget->getStackAlignment() >= 32) {
1377 if (Subtarget->hasAVX2())
1378 return MVT::v8i32;
1379 if (Subtarget->hasAVX())
1380 return MVT::v8f32;
1381 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001382 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001383 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001384 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001385 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001386 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001387 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001388 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001389 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001390 // Do not use f64 to lower memcpy if source is string constant. It's
1391 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001392 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001393 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001394 }
Evan Chengf0df0312008-05-15 08:39:06 +00001395 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 return MVT::i64;
1397 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001398}
1399
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001400/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1401/// current function. The returned value is a member of the
1402/// MachineJumpTableInfo::JTEntryKind enum.
1403unsigned X86TargetLowering::getJumpTableEncoding() const {
1404 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1405 // symbol.
1406 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1407 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001408 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001409
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001410 // Otherwise, use the normal jump table encoding heuristics.
1411 return TargetLowering::getJumpTableEncoding();
1412}
1413
Chris Lattnerc64daab2010-01-26 05:02:42 +00001414const MCExpr *
1415X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1416 const MachineBasicBlock *MBB,
1417 unsigned uid,MCContext &Ctx) const{
1418 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1419 Subtarget->isPICStyleGOT());
1420 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1421 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001422 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1423 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001424}
1425
Evan Chengcc415862007-11-09 01:32:10 +00001426/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1427/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001428SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001429 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001430 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001431 // This doesn't have DebugLoc associated with it, but is not really the
1432 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001433 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001434 return Table;
1435}
1436
Chris Lattner589c6f62010-01-26 06:28:43 +00001437/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1438/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1439/// MCExpr.
1440const MCExpr *X86TargetLowering::
1441getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1442 MCContext &Ctx) const {
1443 // X86-64 uses RIP relative addressing based on the jump table label.
1444 if (Subtarget->isPICStyleRIPRel())
1445 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1446
1447 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001448 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001449}
1450
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001451// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001452std::pair<const TargetRegisterClass*, uint8_t>
1453X86TargetLowering::findRepresentativeClass(EVT VT) const{
1454 const TargetRegisterClass *RRC = 0;
1455 uint8_t Cost = 1;
1456 switch (VT.getSimpleVT().SimpleTy) {
1457 default:
1458 return TargetLowering::findRepresentativeClass(VT);
1459 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001460 RRC = Subtarget->is64Bit() ?
1461 (const TargetRegisterClass*)&X86::GR64RegClass :
1462 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001463 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001464 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001465 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001466 break;
1467 case MVT::f32: case MVT::f64:
1468 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1469 case MVT::v4f32: case MVT::v2f64:
1470 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1471 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001472 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001473 break;
1474 }
1475 return std::make_pair(RRC, Cost);
1476}
1477
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001478bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1479 unsigned &Offset) const {
1480 if (!Subtarget->isTargetLinux())
1481 return false;
1482
1483 if (Subtarget->is64Bit()) {
1484 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1485 Offset = 0x28;
1486 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1487 AddressSpace = 256;
1488 else
1489 AddressSpace = 257;
1490 } else {
1491 // %gs:0x14 on i386
1492 Offset = 0x14;
1493 AddressSpace = 256;
1494 }
1495 return true;
1496}
1497
1498
Chris Lattner2b02a442007-02-25 08:29:00 +00001499//===----------------------------------------------------------------------===//
1500// Return Value Calling Convention Implementation
1501//===----------------------------------------------------------------------===//
1502
Chris Lattner59ed56b2007-02-28 04:55:35 +00001503#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001504
Michael J. Spencerec38de22010-10-10 22:04:20 +00001505bool
Eric Christopher471e4222011-06-08 23:55:35 +00001506X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001507 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001508 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001509 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001510 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001511 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001512 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001513 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001514}
1515
Dan Gohman98ca4f22009-08-05 01:29:28 +00001516SDValue
1517X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001518 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001519 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001520 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001521 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001522 MachineFunction &MF = DAG.getMachineFunction();
1523 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001524
Chris Lattner9774c912007-02-27 05:28:59 +00001525 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001526 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001527 RVLocs, *DAG.getContext());
1528 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Evan Chengdcea1632010-02-04 02:40:39 +00001530 // Add the regs to the liveout set for the function.
1531 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1532 for (unsigned i = 0; i != RVLocs.size(); ++i)
1533 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1534 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001535
Dan Gohman475871a2008-07-27 21:46:04 +00001536 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001537
Dan Gohman475871a2008-07-27 21:46:04 +00001538 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001539 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1540 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001541 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1542 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001544 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001545 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1546 CCValAssign &VA = RVLocs[i];
1547 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001548 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001549 EVT ValVT = ValToCopy.getValueType();
1550
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001551 // Promote values to the appropriate types
1552 if (VA.getLocInfo() == CCValAssign::SExt)
1553 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1554 else if (VA.getLocInfo() == CCValAssign::ZExt)
1555 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1556 else if (VA.getLocInfo() == CCValAssign::AExt)
1557 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1558 else if (VA.getLocInfo() == CCValAssign::BCvt)
1559 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1560
Dale Johannesenc4510512010-09-24 19:05:48 +00001561 // If this is x86-64, and we disabled SSE, we can't return FP values,
1562 // or SSE or MMX vectors.
1563 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1564 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001565 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001566 report_fatal_error("SSE register return with SSE disabled");
1567 }
1568 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1569 // llvm-gcc has never done it right and no one has noticed, so this
1570 // should be OK for now.
1571 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001572 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001573 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001574
Chris Lattner447ff682008-03-11 03:23:40 +00001575 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1576 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001577 if (VA.getLocReg() == X86::ST0 ||
1578 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001579 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1580 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001581 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001583 RetOps.push_back(ValToCopy);
1584 // Don't emit a copytoreg.
1585 continue;
1586 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001587
Evan Cheng242b38b2009-02-23 09:03:22 +00001588 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1589 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001590 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001591 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001592 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001593 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001594 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1595 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001596 // If we don't have SSE2 available, convert to v4f32 so the generated
1597 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001598 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001599 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001600 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001601 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001602 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001603
Dale Johannesendd64c412009-02-04 00:33:20 +00001604 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001605 Flag = Chain.getValue(1);
1606 }
Dan Gohman61a92132008-04-21 23:59:07 +00001607
1608 // The x86-64 ABI for returning structs by value requires that we copy
1609 // the sret argument into %rax for the return. We saved the argument into
1610 // a virtual register in the entry block, so now we copy the value out
1611 // and into %rax.
1612 if (Subtarget->is64Bit() &&
1613 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1614 MachineFunction &MF = DAG.getMachineFunction();
1615 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1616 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001617 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001618 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001619 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001620
Dale Johannesendd64c412009-02-04 00:33:20 +00001621 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001622 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001623
1624 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001625 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001626 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001627
Chris Lattner447ff682008-03-11 03:23:40 +00001628 RetOps[0] = Chain; // Update chain.
1629
1630 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001631 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001632 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001633
1634 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001635 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001636}
1637
Evan Chengbf010eb2012-04-10 01:51:00 +00001638bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001639 if (N->getNumValues() != 1)
1640 return false;
1641 if (!N->hasNUsesOfValue(1, 0))
1642 return false;
1643
Evan Chengbf010eb2012-04-10 01:51:00 +00001644 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001645 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001646 if (Copy->getOpcode() == ISD::CopyToReg) {
1647 // If the copy has a glue operand, we conservatively assume it isn't safe to
1648 // perform a tail call.
1649 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1650 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001651 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001652 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001653 return false;
1654
Evan Cheng1bf891a2010-12-01 22:59:46 +00001655 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001656 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001657 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001658 if (UI->getOpcode() != X86ISD::RET_FLAG)
1659 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001660 HasRet = true;
1661 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001662
Evan Chengbf010eb2012-04-10 01:51:00 +00001663 if (!HasRet)
1664 return false;
1665
1666 Chain = TCChain;
1667 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001668}
1669
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001670EVT
1671X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001672 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001673 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001674 // TODO: Is this also valid on 32-bit?
1675 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001676 ReturnMVT = MVT::i8;
1677 else
1678 ReturnMVT = MVT::i32;
1679
1680 EVT MinVT = getRegisterType(Context, ReturnMVT);
1681 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001682}
1683
Dan Gohman98ca4f22009-08-05 01:29:28 +00001684/// LowerCallResult - Lower the result values of a call into the
1685/// appropriate copies out of appropriate physical registers.
1686///
1687SDValue
1688X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001689 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001690 const SmallVectorImpl<ISD::InputArg> &Ins,
1691 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001692 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001693
Chris Lattnere32bbf62007-02-28 07:09:55 +00001694 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001695 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001696 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001697 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001698 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001699 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Chris Lattner3085e152007-02-25 08:59:22 +00001701 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001702 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001703 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001704 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001705
Torok Edwin3f142c32009-02-01 18:15:56 +00001706 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001708 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001709 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001710 }
1711
Evan Cheng79fb3b42009-02-20 20:43:02 +00001712 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001713
1714 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001715 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001716 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001717 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001718 // instead.
1719 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1720 // If we prefer to use the value in xmm registers, copy it out as f80 and
1721 // use a truncate to move it from fp stack reg to xmm reg.
1722 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001723 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001724 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1725 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001726 Val = Chain.getValue(0);
1727
1728 // Round the f80 to the right size, which also moves it to the appropriate
1729 // xmm register.
1730 if (CopyVT != VA.getValVT())
1731 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1732 // This truncation won't change the value.
1733 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001734 } else {
1735 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1736 CopyVT, InFlag).getValue(1);
1737 Val = Chain.getValue(0);
1738 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001739 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001741 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001742
Dan Gohman98ca4f22009-08-05 01:29:28 +00001743 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001744}
1745
1746
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001747//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001748// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001749//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001750// StdCall calling convention seems to be standard for many Windows' API
1751// routines and around. It differs from C calling convention just a little:
1752// callee should clean up the stack, not caller. Symbols should be also
1753// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001754// For info on fast calling convention see Fast Calling Convention (tail call)
1755// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001756
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001758/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001759enum StructReturnType {
1760 NotStructReturn,
1761 RegStructReturn,
1762 StackStructReturn
1763};
1764static StructReturnType
1765callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001766 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001767 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001768
Rafael Espindola1cee7102012-07-25 13:41:10 +00001769 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1770 if (!Flags.isSRet())
1771 return NotStructReturn;
1772 if (Flags.isInReg())
1773 return RegStructReturn;
1774 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001775}
1776
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001777/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001778/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001779static StructReturnType
1780argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001782 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001783
Rafael Espindola1cee7102012-07-25 13:41:10 +00001784 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1785 if (!Flags.isSRet())
1786 return NotStructReturn;
1787 if (Flags.isInReg())
1788 return RegStructReturn;
1789 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001790}
1791
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001792/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1793/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001794/// the specific parameter attribute. The copy will be passed as a byval
1795/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001796static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001797CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001798 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1799 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001800 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001801
Dale Johannesendd64c412009-02-04 00:33:20 +00001802 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001803 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001804 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001805}
1806
Chris Lattner29689432010-03-11 00:22:57 +00001807/// IsTailCallConvention - Return true if the calling convention is one that
1808/// supports tail call optimization.
1809static bool IsTailCallConvention(CallingConv::ID CC) {
1810 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1811}
1812
Evan Cheng485fafc2011-03-21 01:19:09 +00001813bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001814 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001815 return false;
1816
1817 CallSite CS(CI);
1818 CallingConv::ID CalleeCC = CS.getCallingConv();
1819 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1820 return false;
1821
1822 return true;
1823}
1824
Evan Cheng0c439eb2010-01-27 00:07:07 +00001825/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1826/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001827static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1828 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001829 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001830}
1831
Dan Gohman98ca4f22009-08-05 01:29:28 +00001832SDValue
1833X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001834 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001835 const SmallVectorImpl<ISD::InputArg> &Ins,
1836 DebugLoc dl, SelectionDAG &DAG,
1837 const CCValAssign &VA,
1838 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001839 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001840 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001841 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001842 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1843 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001844 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001845 EVT ValVT;
1846
1847 // If value is passed by pointer we have address passed instead of the value
1848 // itself.
1849 if (VA.getLocInfo() == CCValAssign::Indirect)
1850 ValVT = VA.getLocVT();
1851 else
1852 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001853
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001854 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001855 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001856 // In case of tail call optimization mark all arguments mutable. Since they
1857 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001858 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001859 unsigned Bytes = Flags.getByValSize();
1860 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1861 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001862 return DAG.getFrameIndex(FI, getPointerTy());
1863 } else {
1864 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001865 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001866 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1867 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001868 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001869 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001870 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001871}
1872
Dan Gohman475871a2008-07-27 21:46:04 +00001873SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001874X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001875 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 bool isVarArg,
1877 const SmallVectorImpl<ISD::InputArg> &Ins,
1878 DebugLoc dl,
1879 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001880 SmallVectorImpl<SDValue> &InVals)
1881 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001882 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001883 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001884
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 const Function* Fn = MF.getFunction();
1886 if (Fn->hasExternalLinkage() &&
1887 Subtarget->isTargetCygMing() &&
1888 Fn->getName() == "main")
1889 FuncInfo->setForceFramePointer(true);
1890
Evan Cheng1bc78042006-04-26 01:20:17 +00001891 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001893 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001894 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001895
Chris Lattner29689432010-03-11 00:22:57 +00001896 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1897 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001898
Chris Lattner638402b2007-02-28 07:00:42 +00001899 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001900 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001901 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001903
1904 // Allocate shadow area for Win64
1905 if (IsWin64) {
1906 CCInfo.AllocateStack(32, 8);
1907 }
1908
Duncan Sands45907662010-10-31 13:21:44 +00001909 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001910
Chris Lattnerf39f7712007-02-28 05:46:49 +00001911 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001912 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001913 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1914 CCValAssign &VA = ArgLocs[i];
1915 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1916 // places.
1917 assert(VA.getValNo() != LastVal &&
1918 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001919 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001920 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001921
Chris Lattnerf39f7712007-02-28 05:46:49 +00001922 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001923 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001924 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001925 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001926 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001928 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001929 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001930 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001932 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001933 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001934 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001935 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001936 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001937 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001938 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001939 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001940 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001941
Devang Patel68e6bee2011-02-21 23:21:26 +00001942 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Chris Lattnerf39f7712007-02-28 05:46:49 +00001945 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1946 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1947 // right size.
1948 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001949 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001950 DAG.getValueType(VA.getValVT()));
1951 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001952 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001953 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001954 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001955 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001956
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001957 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001958 // Handle MMX values passed in XMM regs.
1959 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001960 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1961 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001962 } else
1963 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001964 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001965 } else {
1966 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001967 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001968 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001969
1970 // If value is passed via pointer - do a load.
1971 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001972 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001973 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001974
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001976 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001977
Dan Gohman61a92132008-04-21 23:59:07 +00001978 // The x86-64 ABI for returning structs by value requires that we copy
1979 // the sret argument into %rax for the return. Save the argument into
1980 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001981 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001982 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1983 unsigned Reg = FuncInfo->getSRetReturnReg();
1984 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001986 FuncInfo->setSRetReturnReg(Reg);
1987 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001988 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001990 }
1991
Chris Lattnerf39f7712007-02-28 05:46:49 +00001992 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001993 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001994 if (FuncIsMadeTailCallSafe(CallConv,
1995 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001996 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001997
Evan Cheng1bc78042006-04-26 01:20:17 +00001998 // If the function takes variable number of arguments, make a frame index for
1999 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002000 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002001 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2002 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002003 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002004 }
2005 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002006 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2007
2008 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002009 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002010 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002011 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002012 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002013 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2014 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002015 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2017 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2018 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002019 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002020 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002021
2022 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002023 // The XMM registers which might contain var arg parameters are shadowed
2024 // in their paired GPR. So we only need to save the GPR to their home
2025 // slots.
2026 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002027 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002028 } else {
2029 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2030 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002031
Chad Rosier30450e82011-12-22 22:35:21 +00002032 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2033 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002034 }
2035 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2036 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002037
Bill Wendling67658342012-10-09 07:45:08 +00002038 bool NoImplicitFloatOps = Fn->getFnAttributes().
2039 hasAttribute(Attributes::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002040 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002041 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002042 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2043 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002044 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002045 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002046 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002047 // Kernel mode asks for SSE to be disabled, so don't push them
2048 // on the stack.
2049 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002050
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002051 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002052 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002053 // Get to the caller-allocated home save location. Add 8 to account
2054 // for the return address.
2055 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002056 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002057 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002058 // Fixup to set vararg frame on shadow area (4 x i64).
2059 if (NumIntRegs < 4)
2060 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002061 } else {
2062 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002063 // registers, then we must store them to their spots on the stack so
2064 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002065 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2066 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2067 FuncInfo->setRegSaveFrameIndex(
2068 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002069 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002070 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002071
Gordon Henriksen86737662008-01-05 16:56:59 +00002072 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002073 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002074 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2075 getPointerTy());
2076 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002077 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002078 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2079 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002080 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002081 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002082 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002084 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002085 MachinePointerInfo::getFixedStack(
2086 FuncInfo->getRegSaveFrameIndex(), Offset),
2087 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002088 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002089 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002090 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002091
Dan Gohmanface41a2009-08-16 21:24:25 +00002092 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2093 // Now store the XMM (fp + vector) parameter registers.
2094 SmallVector<SDValue, 11> SaveXMMOps;
2095 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002096
Craig Topperc9099502012-04-20 06:31:50 +00002097 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002098 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2099 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002100
Dan Gohman1e93df62010-04-17 14:41:14 +00002101 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2102 FuncInfo->getRegSaveFrameIndex()));
2103 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2104 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002105
Dan Gohmanface41a2009-08-16 21:24:25 +00002106 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002107 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002108 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002109 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2110 SaveXMMOps.push_back(Val);
2111 }
2112 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2113 MVT::Other,
2114 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002115 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002116
2117 if (!MemOps.empty())
2118 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2119 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002120 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Gordon Henriksen86737662008-01-05 16:56:59 +00002123 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002124 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2125 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002126 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002127 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002128 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002129 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002130 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002131 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002132 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002133 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002134
Gordon Henriksen86737662008-01-05 16:56:59 +00002135 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002136 // RegSaveFrameIndex is X86-64 only.
2137 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002138 if (CallConv == CallingConv::X86_FastCall ||
2139 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002140 // fastcc functions can't have varargs.
2141 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002142 }
Evan Cheng25caf632006-05-23 21:06:34 +00002143
Rafael Espindola76927d752011-08-30 19:39:58 +00002144 FuncInfo->setArgumentStackSize(StackSize);
2145
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002147}
2148
Dan Gohman475871a2008-07-27 21:46:04 +00002149SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002150X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2151 SDValue StackPtr, SDValue Arg,
2152 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002153 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002154 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002155 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002156 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002157 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002158 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002159 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002160
2161 return DAG.getStore(Chain, dl, Arg, PtrOff,
2162 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002163 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002164}
2165
Bill Wendling64e87322009-01-16 19:25:27 +00002166/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002167/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002168SDValue
2169X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002170 SDValue &OutRetAddr, SDValue Chain,
2171 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002172 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002173 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002174 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002175 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002176
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002177 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002178 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002179 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002180 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002181}
2182
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002183/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002184/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002185static SDValue
2186EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002187 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2188 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002189 // Store the return address to the appropriate stack slot.
2190 if (!FPDiff) return Chain;
2191 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002192 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002193 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002194 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002195 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002196 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002197 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002198 return Chain;
2199}
2200
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002202X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002203 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002204 SelectionDAG &DAG = CLI.DAG;
2205 DebugLoc &dl = CLI.DL;
2206 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2207 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2208 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2209 SDValue Chain = CLI.Chain;
2210 SDValue Callee = CLI.Callee;
2211 CallingConv::ID CallConv = CLI.CallConv;
2212 bool &isTailCall = CLI.IsTailCall;
2213 bool isVarArg = CLI.IsVarArg;
2214
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215 MachineFunction &MF = DAG.getMachineFunction();
2216 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002217 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002218 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002219 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002220 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002221
Nick Lewycky22de16d2012-01-19 00:34:10 +00002222 if (MF.getTarget().Options.DisableTailCalls)
2223 isTailCall = false;
2224
Evan Cheng5f941932010-02-05 02:21:12 +00002225 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002226 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002227 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002228 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002229 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002230 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002231
2232 // Sibcalls are automatically detected tailcalls which do not require
2233 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002234 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002235 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002236
2237 if (isTailCall)
2238 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002239 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002240
Chris Lattner29689432010-03-11 00:22:57 +00002241 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2242 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002243
Chris Lattner638402b2007-02-28 07:00:42 +00002244 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002245 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002246 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002247 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002248
2249 // Allocate shadow area for Win64
2250 if (IsWin64) {
2251 CCInfo.AllocateStack(32, 8);
2252 }
2253
Duncan Sands45907662010-10-31 13:21:44 +00002254 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002255
Chris Lattner423c5f42007-02-28 05:31:48 +00002256 // Get a count of how many bytes are to be pushed on the stack.
2257 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002258 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002259 // This is a sibcall. The memory operands are available in caller's
2260 // own caller's stack.
2261 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002262 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2263 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002264 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002265
Gordon Henriksen86737662008-01-05 16:56:59 +00002266 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002267 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002268 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002269 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2270 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2271
Gordon Henriksen86737662008-01-05 16:56:59 +00002272 FPDiff = NumBytesCallerPushed - NumBytes;
2273
2274 // Set the delta of movement of the returnaddr stackslot.
2275 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002276 if (FPDiff < X86Info->getTCReturnAddrDelta())
2277 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002278 }
2279
Evan Chengf22f9b32010-02-06 03:28:46 +00002280 if (!IsSibcall)
2281 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002282
Dan Gohman475871a2008-07-27 21:46:04 +00002283 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002284 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002285 if (isTailCall && FPDiff)
2286 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2287 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002288
Dan Gohman475871a2008-07-27 21:46:04 +00002289 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2290 SmallVector<SDValue, 8> MemOpChains;
2291 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002292
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002293 // Walk the register/memloc assignments, inserting copies/loads. In the case
2294 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002295 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2296 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002297 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002298 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002299 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002300 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002301
Chris Lattner423c5f42007-02-28 05:31:48 +00002302 // Promote the value if needed.
2303 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002304 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002305 case CCValAssign::Full: break;
2306 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002307 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002308 break;
2309 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002310 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002311 break;
2312 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002313 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002314 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002315 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2317 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002318 } else
2319 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2320 break;
2321 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002322 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002323 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002324 case CCValAssign::Indirect: {
2325 // Store the argument.
2326 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002327 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002328 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002329 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002330 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002331 Arg = SpillSlot;
2332 break;
2333 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002335
Chris Lattner423c5f42007-02-28 05:31:48 +00002336 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002337 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2338 if (isVarArg && IsWin64) {
2339 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2340 // shadow reg if callee is a varargs function.
2341 unsigned ShadowReg = 0;
2342 switch (VA.getLocReg()) {
2343 case X86::XMM0: ShadowReg = X86::RCX; break;
2344 case X86::XMM1: ShadowReg = X86::RDX; break;
2345 case X86::XMM2: ShadowReg = X86::R8; break;
2346 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002347 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002348 if (ShadowReg)
2349 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002350 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002351 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002352 assert(VA.isMemLoc());
2353 if (StackPtr.getNode() == 0)
2354 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2355 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2356 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002357 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002358 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002359
Evan Cheng32fe1032006-05-25 00:59:30 +00002360 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002361 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002362 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002363
Chris Lattner88e1fd52009-07-09 04:24:46 +00002364 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002365 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2366 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002367 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002368 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2369 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002370 } else {
2371 // If we are tail calling and generating PIC/GOT style code load the
2372 // address of the callee into ECX. The value in ecx is used as target of
2373 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2374 // for tail calls on PIC/GOT architectures. Normally we would just put the
2375 // address of GOT into ebx and then call target@PLT. But for tail calls
2376 // ebx would be restored (since ebx is callee saved) before jumping to the
2377 // target@PLT.
2378
2379 // Note: The actual moving to ECX is done further down.
2380 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2381 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2382 !G->getGlobal()->hasProtectedVisibility())
2383 Callee = LowerGlobalAddress(Callee, DAG);
2384 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002385 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002386 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002387 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002388
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002389 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002390 // From AMD64 ABI document:
2391 // For calls that may call functions that use varargs or stdargs
2392 // (prototype-less calls or calls to functions containing ellipsis (...) in
2393 // the declaration) %al is used as hidden argument to specify the number
2394 // of SSE registers used. The contents of %al do not need to match exactly
2395 // the number of registers, but must be an ubound on the number of SSE
2396 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002397
Gordon Henriksen86737662008-01-05 16:56:59 +00002398 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002399 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002400 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2401 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2402 };
2403 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002404 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002405 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002406
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002407 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2408 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002409 }
2410
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002411 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002412 if (isTailCall) {
2413 // Force all the incoming stack arguments to be loaded from the stack
2414 // before any new outgoing arguments are stored to the stack, because the
2415 // outgoing stack slots may alias the incoming argument stack slots, and
2416 // the alias isn't otherwise explicit. This is slightly more conservative
2417 // than necessary, because it means that each store effectively depends
2418 // on every argument instead of just those arguments it would clobber.
2419 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2420
Dan Gohman475871a2008-07-27 21:46:04 +00002421 SmallVector<SDValue, 8> MemOpChains2;
2422 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002423 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002424 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002425 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2426 CCValAssign &VA = ArgLocs[i];
2427 if (VA.isRegLoc())
2428 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002429 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002430 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002431 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002432 // Create frame index.
2433 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002434 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002435 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002436 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002437
Duncan Sands276dcbd2008-03-21 09:14:45 +00002438 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002439 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002440 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002441 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002442 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002443 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002444 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002445
Dan Gohman98ca4f22009-08-05 01:29:28 +00002446 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2447 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002448 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002449 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002450 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002451 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002453 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002454 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002455 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002456 }
2457 }
2458
2459 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002460 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002461 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002462
2463 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002464 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2465 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002466 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 }
2468
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002469 // Build a sequence of copy-to-reg nodes chained together with token chain
2470 // and flag operands which copy the outgoing args into registers.
2471 SDValue InFlag;
2472 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2473 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2474 RegsToPass[i].second, InFlag);
2475 InFlag = Chain.getValue(1);
2476 }
2477
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002478 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2479 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2480 // In the 64-bit large code model, we have to make all calls
2481 // through a register, since the call instruction's 32-bit
2482 // pc-relative offset may not be large enough to hold the whole
2483 // address.
2484 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002485 // If the callee is a GlobalAddress node (quite common, every direct call
2486 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2487 // it.
2488
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002489 // We should use extra load for direct calls to dllimported functions in
2490 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002491 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002492 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002493 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002494 bool ExtraLoad = false;
2495 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002496
Chris Lattner48a7d022009-07-09 05:02:21 +00002497 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2498 // external symbols most go through the PLT in PIC mode. If the symbol
2499 // has hidden or protected visibility, or if it is static or local, then
2500 // we don't need to use the PLT - we can directly call it.
2501 if (Subtarget->isTargetELF() &&
2502 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002503 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002504 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002505 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002506 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002507 (!Subtarget->getTargetTriple().isMacOSX() ||
2508 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002509 // PC-relative references to external symbols should go through $stub,
2510 // unless we're building with the leopard linker or later, which
2511 // automatically synthesizes these stubs.
2512 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002513 } else if (Subtarget->isPICStyleRIPRel() &&
2514 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002515 cast<Function>(GV)->getFnAttributes().
2516 hasAttribute(Attributes::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002517 // If the function is marked as non-lazy, generate an indirect call
2518 // which loads from the GOT directly. This avoids runtime overhead
2519 // at the cost of eager binding (and one extra byte of encoding).
2520 OpFlags = X86II::MO_GOTPCREL;
2521 WrapperKind = X86ISD::WrapperRIP;
2522 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002523 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002524
Devang Patel0d881da2010-07-06 22:08:15 +00002525 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002526 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002527
2528 // Add a wrapper if needed.
2529 if (WrapperKind != ISD::DELETED_NODE)
2530 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2531 // Add extra indirection if needed.
2532 if (ExtraLoad)
2533 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2534 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002535 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002536 }
Bill Wendling056292f2008-09-16 21:48:12 +00002537 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002538 unsigned char OpFlags = 0;
2539
Evan Cheng1bf891a2010-12-01 22:59:46 +00002540 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2541 // external symbols should go through the PLT.
2542 if (Subtarget->isTargetELF() &&
2543 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2544 OpFlags = X86II::MO_PLT;
2545 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002546 (!Subtarget->getTargetTriple().isMacOSX() ||
2547 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002548 // PC-relative references to external symbols should go through $stub,
2549 // unless we're building with the leopard linker or later, which
2550 // automatically synthesizes these stubs.
2551 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002552 }
Eric Christopherfd179292009-08-27 18:07:15 +00002553
Chris Lattner48a7d022009-07-09 05:02:21 +00002554 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2555 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002556 }
2557
Chris Lattnerd96d0722007-02-25 06:40:16 +00002558 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002559 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002560 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002561
Evan Chengf22f9b32010-02-06 03:28:46 +00002562 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002563 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2564 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002565 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002567
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002568 Ops.push_back(Chain);
2569 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002570
Dan Gohman98ca4f22009-08-05 01:29:28 +00002571 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002572 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002573
Gordon Henriksen86737662008-01-05 16:56:59 +00002574 // Add argument registers to the end of the list so that they are known live
2575 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002576 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2577 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2578 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002579
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002580 // Add a register mask operand representing the call-preserved registers.
2581 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2582 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2583 assert(Mask && "Missing call preserved mask for calling convention");
2584 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002585
Gabor Greifba36cb52008-08-28 21:40:38 +00002586 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002587 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002588
Dan Gohman98ca4f22009-08-05 01:29:28 +00002589 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002590 // We used to do:
2591 //// If this is the first return lowered for this function, add the regs
2592 //// to the liveout set for the function.
2593 // This isn't right, although it's probably harmless on x86; liveouts
2594 // should be computed from returns not tail calls. Consider a void
2595 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002596 return DAG.getNode(X86ISD::TC_RETURN, dl,
2597 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002598 }
2599
Dale Johannesenace16102009-02-03 19:33:06 +00002600 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002601 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002602
Chris Lattner2d297092006-05-23 18:50:38 +00002603 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002604 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002605 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2606 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002607 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002608 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002609 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002610 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002611 // pops the hidden struct pointer, so we have to push it back.
2612 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002613 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002614 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002615 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002616 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002617
Gordon Henriksenae636f82008-01-03 16:47:34 +00002618 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002619 if (!IsSibcall) {
2620 Chain = DAG.getCALLSEQ_END(Chain,
2621 DAG.getIntPtrConstant(NumBytes, true),
2622 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2623 true),
2624 InFlag);
2625 InFlag = Chain.getValue(1);
2626 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002627
Chris Lattner3085e152007-02-25 08:59:22 +00002628 // Handle result values, copying them out of physregs into vregs that we
2629 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002630 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2631 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002632}
2633
Evan Cheng25ab6902006-09-08 06:48:29 +00002634
2635//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002636// Fast Calling Convention (tail call) implementation
2637//===----------------------------------------------------------------------===//
2638
2639// Like std call, callee cleans arguments, convention except that ECX is
2640// reserved for storing the tail called function address. Only 2 registers are
2641// free for argument passing (inreg). Tail call optimization is performed
2642// provided:
2643// * tailcallopt is enabled
2644// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002645// On X86_64 architecture with GOT-style position independent code only local
2646// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002647// To keep the stack aligned according to platform abi the function
2648// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2649// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002650// If a tail called function callee has more arguments than the caller the
2651// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002652// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002653// original REtADDR, but before the saved framepointer or the spilled registers
2654// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2655// stack layout:
2656// arg1
2657// arg2
2658// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002659// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002660// move area ]
2661// (possible EBP)
2662// ESI
2663// EDI
2664// local1 ..
2665
2666/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2667/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002668unsigned
2669X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2670 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002671 MachineFunction &MF = DAG.getMachineFunction();
2672 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002673 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002674 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002675 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002676 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002677 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002678 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2679 // Number smaller than 12 so just add the difference.
2680 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2681 } else {
2682 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002683 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002684 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002685 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002686 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002687}
2688
Evan Cheng5f941932010-02-05 02:21:12 +00002689/// MatchingStackOffset - Return true if the given stack call argument is
2690/// already available in the same position (relatively) of the caller's
2691/// incoming argument stack.
2692static
2693bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2694 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2695 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002696 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2697 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002698 if (Arg.getOpcode() == ISD::CopyFromReg) {
2699 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002700 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002701 return false;
2702 MachineInstr *Def = MRI->getVRegDef(VR);
2703 if (!Def)
2704 return false;
2705 if (!Flags.isByVal()) {
2706 if (!TII->isLoadFromStackSlot(Def, FI))
2707 return false;
2708 } else {
2709 unsigned Opcode = Def->getOpcode();
2710 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2711 Def->getOperand(1).isFI()) {
2712 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002713 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002714 } else
2715 return false;
2716 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002717 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2718 if (Flags.isByVal())
2719 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002720 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002721 // define @foo(%struct.X* %A) {
2722 // tail call @bar(%struct.X* byval %A)
2723 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002724 return false;
2725 SDValue Ptr = Ld->getBasePtr();
2726 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2727 if (!FINode)
2728 return false;
2729 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002730 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002731 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002732 FI = FINode->getIndex();
2733 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002734 } else
2735 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002736
Evan Cheng4cae1332010-03-05 08:38:04 +00002737 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002738 if (!MFI->isFixedObjectIndex(FI))
2739 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002740 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002741}
2742
Dan Gohman98ca4f22009-08-05 01:29:28 +00002743/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2744/// for tail call optimization. Targets which want to do tail call
2745/// optimization should implement this function.
2746bool
2747X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002748 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002749 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002750 bool isCalleeStructRet,
2751 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002752 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002753 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002754 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002755 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002756 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002757 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002758 CalleeCC != CallingConv::C)
2759 return false;
2760
Evan Cheng7096ae42010-01-29 06:45:59 +00002761 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002762 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002763 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002764
2765 // If the function return type is x86_fp80 and the callee return type is not,
2766 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2767 // perform a tailcall optimization here.
2768 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2769 return false;
2770
Evan Cheng13617962010-04-30 01:12:32 +00002771 CallingConv::ID CallerCC = CallerF->getCallingConv();
2772 bool CCMatch = CallerCC == CalleeCC;
2773
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002774 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002775 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002776 return true;
2777 return false;
2778 }
2779
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002780 // Look for obvious safe cases to perform tail call optimization that do not
2781 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002782
Evan Cheng2c12cb42010-03-26 16:26:03 +00002783 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2784 // emit a special epilogue.
2785 if (RegInfo->needsStackRealignment(MF))
2786 return false;
2787
Evan Chenga375d472010-03-15 18:54:48 +00002788 // Also avoid sibcall optimization if either caller or callee uses struct
2789 // return semantics.
2790 if (isCalleeStructRet || isCallerStructRet)
2791 return false;
2792
Chad Rosier2416da32011-06-24 21:15:36 +00002793 // An stdcall caller is expected to clean up its arguments; the callee
2794 // isn't going to do that.
2795 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2796 return false;
2797
Chad Rosier871f6642011-05-18 19:59:50 +00002798 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002799 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002800 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002801
2802 // Optimizing for varargs on Win64 is unlikely to be safe without
2803 // additional testing.
2804 if (Subtarget->isTargetWin64())
2805 return false;
2806
Chad Rosier871f6642011-05-18 19:59:50 +00002807 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002808 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002809 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002810
Chad Rosier871f6642011-05-18 19:59:50 +00002811 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2812 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2813 if (!ArgLocs[i].isRegLoc())
2814 return false;
2815 }
2816
Chad Rosier30450e82011-12-22 22:35:21 +00002817 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2818 // stack. Therefore, if it's not used by the call it is not safe to optimize
2819 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002820 bool Unused = false;
2821 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2822 if (!Ins[i].Used) {
2823 Unused = true;
2824 break;
2825 }
2826 }
2827 if (Unused) {
2828 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002829 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002830 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002831 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002832 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002833 CCValAssign &VA = RVLocs[i];
2834 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2835 return false;
2836 }
2837 }
2838
Evan Cheng13617962010-04-30 01:12:32 +00002839 // If the calling conventions do not match, then we'd better make sure the
2840 // results are returned in the same way as what the caller expects.
2841 if (!CCMatch) {
2842 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002843 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002844 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002845 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2846
2847 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002848 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002849 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002850 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2851
2852 if (RVLocs1.size() != RVLocs2.size())
2853 return false;
2854 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2855 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2856 return false;
2857 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2858 return false;
2859 if (RVLocs1[i].isRegLoc()) {
2860 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2861 return false;
2862 } else {
2863 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2864 return false;
2865 }
2866 }
2867 }
2868
Evan Chenga6bff982010-01-30 01:22:00 +00002869 // If the callee takes no arguments then go on to check the results of the
2870 // call.
2871 if (!Outs.empty()) {
2872 // Check if stack adjustment is needed. For now, do not do this if any
2873 // argument is passed on the stack.
2874 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002875 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002876 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002877
2878 // Allocate shadow area for Win64
2879 if (Subtarget->isTargetWin64()) {
2880 CCInfo.AllocateStack(32, 8);
2881 }
2882
Duncan Sands45907662010-10-31 13:21:44 +00002883 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002884 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002885 MachineFunction &MF = DAG.getMachineFunction();
2886 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2887 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002888
2889 // Check if the arguments are already laid out in the right way as
2890 // the caller's fixed stack objects.
2891 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002892 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2893 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002894 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002895 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2896 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002897 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002898 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002899 if (VA.getLocInfo() == CCValAssign::Indirect)
2900 return false;
2901 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002902 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2903 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002904 return false;
2905 }
2906 }
2907 }
Evan Cheng9c044672010-05-29 01:35:22 +00002908
2909 // If the tailcall address may be in a register, then make sure it's
2910 // possible to register allocate for it. In 32-bit, the call address can
2911 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002912 // callee-saved registers are restored. These happen to be the same
2913 // registers used to pass 'inreg' arguments so watch out for those.
2914 if (!Subtarget->is64Bit() &&
2915 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002916 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002917 unsigned NumInRegs = 0;
2918 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2919 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002920 if (!VA.isRegLoc())
2921 continue;
2922 unsigned Reg = VA.getLocReg();
2923 switch (Reg) {
2924 default: break;
2925 case X86::EAX: case X86::EDX: case X86::ECX:
2926 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002927 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002928 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002929 }
2930 }
2931 }
Evan Chenga6bff982010-01-30 01:22:00 +00002932 }
Evan Chengb1712452010-01-27 06:25:16 +00002933
Evan Cheng86809cc2010-02-03 03:28:02 +00002934 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002935}
2936
Dan Gohman3df24e62008-09-03 23:12:08 +00002937FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002938X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2939 const TargetLibraryInfo *libInfo) const {
2940 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002941}
2942
2943
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002944//===----------------------------------------------------------------------===//
2945// Other Lowering Hooks
2946//===----------------------------------------------------------------------===//
2947
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002948static bool MayFoldLoad(SDValue Op) {
2949 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2950}
2951
2952static bool MayFoldIntoStore(SDValue Op) {
2953 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2954}
2955
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002956static bool isTargetShuffle(unsigned Opcode) {
2957 switch(Opcode) {
2958 default: return false;
2959 case X86ISD::PSHUFD:
2960 case X86ISD::PSHUFHW:
2961 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002962 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002963 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002964 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002965 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002966 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002967 case X86ISD::MOVLPS:
2968 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002969 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002970 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002971 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002972 case X86ISD::MOVSS:
2973 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002974 case X86ISD::UNPCKL:
2975 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002976 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002977 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002978 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002979 return true;
2980 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002981}
2982
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002983static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002984 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002985 switch(Opc) {
2986 default: llvm_unreachable("Unknown x86 shuffle node");
2987 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002988 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002989 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002990 return DAG.getNode(Opc, dl, VT, V1);
2991 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002992}
2993
2994static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002995 SDValue V1, unsigned TargetMask,
2996 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002997 switch(Opc) {
2998 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002999 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003000 case X86ISD::PSHUFHW:
3001 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003002 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003003 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003004 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3005 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003006}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003007
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003008static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003009 SDValue V1, SDValue V2, unsigned TargetMask,
3010 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003011 switch(Opc) {
3012 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003013 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003014 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003015 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003016 return DAG.getNode(Opc, dl, VT, V1, V2,
3017 DAG.getConstant(TargetMask, MVT::i8));
3018 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003019}
3020
3021static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3022 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3023 switch(Opc) {
3024 default: llvm_unreachable("Unknown x86 shuffle node");
3025 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003026 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003027 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003028 case X86ISD::MOVLPS:
3029 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003030 case X86ISD::MOVSS:
3031 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003032 case X86ISD::UNPCKL:
3033 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003034 return DAG.getNode(Opc, dl, VT, V1, V2);
3035 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003036}
3037
Dan Gohmand858e902010-04-17 15:26:15 +00003038SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003039 MachineFunction &MF = DAG.getMachineFunction();
3040 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3041 int ReturnAddrIndex = FuncInfo->getRAIndex();
3042
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003043 if (ReturnAddrIndex == 0) {
3044 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003045 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003046 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003047 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003048 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003049 }
3050
Evan Cheng25ab6902006-09-08 06:48:29 +00003051 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003052}
3053
3054
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003055bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3056 bool hasSymbolicDisplacement) {
3057 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003058 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003059 return false;
3060
3061 // If we don't have a symbolic displacement - we don't have any extra
3062 // restrictions.
3063 if (!hasSymbolicDisplacement)
3064 return true;
3065
3066 // FIXME: Some tweaks might be needed for medium code model.
3067 if (M != CodeModel::Small && M != CodeModel::Kernel)
3068 return false;
3069
3070 // For small code model we assume that latest object is 16MB before end of 31
3071 // bits boundary. We may also accept pretty large negative constants knowing
3072 // that all objects are in the positive half of address space.
3073 if (M == CodeModel::Small && Offset < 16*1024*1024)
3074 return true;
3075
3076 // For kernel code model we know that all object resist in the negative half
3077 // of 32bits address space. We may not accept negative offsets, since they may
3078 // be just off and we may accept pretty large positive ones.
3079 if (M == CodeModel::Kernel && Offset > 0)
3080 return true;
3081
3082 return false;
3083}
3084
Evan Chengef41ff62011-06-23 17:54:54 +00003085/// isCalleePop - Determines whether the callee is required to pop its
3086/// own arguments. Callee pop is necessary to support tail calls.
3087bool X86::isCalleePop(CallingConv::ID CallingConv,
3088 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3089 if (IsVarArg)
3090 return false;
3091
3092 switch (CallingConv) {
3093 default:
3094 return false;
3095 case CallingConv::X86_StdCall:
3096 return !is64Bit;
3097 case CallingConv::X86_FastCall:
3098 return !is64Bit;
3099 case CallingConv::X86_ThisCall:
3100 return !is64Bit;
3101 case CallingConv::Fast:
3102 return TailCallOpt;
3103 case CallingConv::GHC:
3104 return TailCallOpt;
3105 }
3106}
3107
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003108/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3109/// specific condition code, returning the condition code and the LHS/RHS of the
3110/// comparison to make.
3111static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3112 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003113 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003114 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3115 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3116 // X > -1 -> X == 0, jump !sign.
3117 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003118 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003119 }
3120 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003121 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003122 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003123 }
3124 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003125 // X < 1 -> X <= 0
3126 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003127 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003128 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003129 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003130
Evan Chengd9558e02006-01-06 00:43:03 +00003131 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003132 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003133 case ISD::SETEQ: return X86::COND_E;
3134 case ISD::SETGT: return X86::COND_G;
3135 case ISD::SETGE: return X86::COND_GE;
3136 case ISD::SETLT: return X86::COND_L;
3137 case ISD::SETLE: return X86::COND_LE;
3138 case ISD::SETNE: return X86::COND_NE;
3139 case ISD::SETULT: return X86::COND_B;
3140 case ISD::SETUGT: return X86::COND_A;
3141 case ISD::SETULE: return X86::COND_BE;
3142 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003143 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003145
Chris Lattner4c78e022008-12-23 23:42:27 +00003146 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003147
Chris Lattner4c78e022008-12-23 23:42:27 +00003148 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003149 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3150 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003151 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3152 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003153 }
3154
Chris Lattner4c78e022008-12-23 23:42:27 +00003155 switch (SetCCOpcode) {
3156 default: break;
3157 case ISD::SETOLT:
3158 case ISD::SETOLE:
3159 case ISD::SETUGT:
3160 case ISD::SETUGE:
3161 std::swap(LHS, RHS);
3162 break;
3163 }
3164
3165 // On a floating point condition, the flags are set as follows:
3166 // ZF PF CF op
3167 // 0 | 0 | 0 | X > Y
3168 // 0 | 0 | 1 | X < Y
3169 // 1 | 0 | 0 | X == Y
3170 // 1 | 1 | 1 | unordered
3171 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003172 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003173 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003174 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003175 case ISD::SETOLT: // flipped
3176 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003177 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003178 case ISD::SETOLE: // flipped
3179 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003180 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003181 case ISD::SETUGT: // flipped
3182 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003183 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003184 case ISD::SETUGE: // flipped
3185 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003186 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003187 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003188 case ISD::SETNE: return X86::COND_NE;
3189 case ISD::SETUO: return X86::COND_P;
3190 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003191 case ISD::SETOEQ:
3192 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003193 }
Evan Chengd9558e02006-01-06 00:43:03 +00003194}
3195
Evan Cheng4a460802006-01-11 00:33:36 +00003196/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3197/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003198/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003199static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003200 switch (X86CC) {
3201 default:
3202 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003203 case X86::COND_B:
3204 case X86::COND_BE:
3205 case X86::COND_E:
3206 case X86::COND_P:
3207 case X86::COND_A:
3208 case X86::COND_AE:
3209 case X86::COND_NE:
3210 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003211 return true;
3212 }
3213}
3214
Evan Chengeb2f9692009-10-27 19:56:55 +00003215/// isFPImmLegal - Returns true if the target can instruction select the
3216/// specified FP immediate natively. If false, the legalizer will
3217/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003218bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003219 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3220 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3221 return true;
3222 }
3223 return false;
3224}
3225
Nate Begeman9008ca62009-04-27 18:41:29 +00003226/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3227/// the specified range (L, H].
3228static bool isUndefOrInRange(int Val, int Low, int Hi) {
3229 return (Val < 0) || (Val >= Low && Val < Hi);
3230}
3231
3232/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3233/// specified value.
3234static bool isUndefOrEqual(int Val, int CmpVal) {
3235 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003236 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003238}
3239
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003240/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003241/// from position Pos and ending in Pos+Size, falls within the specified
3242/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003243static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003244 unsigned Pos, unsigned Size, int Low) {
3245 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003246 if (!isUndefOrEqual(Mask[i], Low))
3247 return false;
3248 return true;
3249}
3250
Nate Begeman9008ca62009-04-27 18:41:29 +00003251/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3252/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3253/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003254static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003255 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 return (Mask[0] < 2 && Mask[1] < 2);
3259 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003260}
3261
Nate Begeman9008ca62009-04-27 18:41:29 +00003262/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3263/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003264static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3265 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003266 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003267
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003269 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3270 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003271
Evan Cheng506d3df2006-03-29 23:07:14 +00003272 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003273 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003274 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003275 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003276
Craig Toppera9a568a2012-05-02 08:03:44 +00003277 if (VT == MVT::v16i16) {
3278 // Lower quadword copied in order or undef.
3279 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3280 return false;
3281
3282 // Upper quadword shuffled.
3283 for (unsigned i = 12; i != 16; ++i)
3284 if (!isUndefOrInRange(Mask[i], 12, 16))
3285 return false;
3286 }
3287
Evan Cheng506d3df2006-03-29 23:07:14 +00003288 return true;
3289}
3290
Nate Begeman9008ca62009-04-27 18:41:29 +00003291/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3292/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003293static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3294 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003295 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003296
Rafael Espindola15684b22009-04-24 12:40:33 +00003297 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003298 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3299 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003300
Rafael Espindola15684b22009-04-24 12:40:33 +00003301 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003302 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003303 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003304 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003305
Craig Toppera9a568a2012-05-02 08:03:44 +00003306 if (VT == MVT::v16i16) {
3307 // Upper quadword copied in order.
3308 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3309 return false;
3310
3311 // Lower quadword shuffled.
3312 for (unsigned i = 8; i != 12; ++i)
3313 if (!isUndefOrInRange(Mask[i], 8, 12))
3314 return false;
3315 }
3316
Rafael Espindola15684b22009-04-24 12:40:33 +00003317 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003318}
3319
Nate Begemana09008b2009-10-19 02:17:23 +00003320/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3321/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003322static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3323 const X86Subtarget *Subtarget) {
3324 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3325 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003326 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003327
Craig Topper0e2037b2012-01-20 05:53:00 +00003328 unsigned NumElts = VT.getVectorNumElements();
3329 unsigned NumLanes = VT.getSizeInBits()/128;
3330 unsigned NumLaneElts = NumElts/NumLanes;
3331
3332 // Do not handle 64-bit element shuffles with palignr.
3333 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003334 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003335
Craig Topper0e2037b2012-01-20 05:53:00 +00003336 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3337 unsigned i;
3338 for (i = 0; i != NumLaneElts; ++i) {
3339 if (Mask[i+l] >= 0)
3340 break;
3341 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003342
Craig Topper0e2037b2012-01-20 05:53:00 +00003343 // Lane is all undef, go to next lane
3344 if (i == NumLaneElts)
3345 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003346
Craig Topper0e2037b2012-01-20 05:53:00 +00003347 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003348
Craig Topper0e2037b2012-01-20 05:53:00 +00003349 // Make sure its in this lane in one of the sources
3350 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3351 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003352 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003353
3354 // If not lane 0, then we must match lane 0
3355 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3356 return false;
3357
3358 // Correct second source to be contiguous with first source
3359 if (Start >= (int)NumElts)
3360 Start -= NumElts - NumLaneElts;
3361
3362 // Make sure we're shifting in the right direction.
3363 if (Start <= (int)(i+l))
3364 return false;
3365
3366 Start -= i;
3367
3368 // Check the rest of the elements to see if they are consecutive.
3369 for (++i; i != NumLaneElts; ++i) {
3370 int Idx = Mask[i+l];
3371
3372 // Make sure its in this lane
3373 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3374 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3375 return false;
3376
3377 // If not lane 0, then we must match lane 0
3378 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3379 return false;
3380
3381 if (Idx >= (int)NumElts)
3382 Idx -= NumElts - NumLaneElts;
3383
3384 if (!isUndefOrEqual(Idx, Start+i))
3385 return false;
3386
3387 }
Nate Begemana09008b2009-10-19 02:17:23 +00003388 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003389
Nate Begemana09008b2009-10-19 02:17:23 +00003390 return true;
3391}
3392
Craig Topper1a7700a2012-01-19 08:19:12 +00003393/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3394/// the two vector operands have swapped position.
3395static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3396 unsigned NumElems) {
3397 for (unsigned i = 0; i != NumElems; ++i) {
3398 int idx = Mask[i];
3399 if (idx < 0)
3400 continue;
3401 else if (idx < (int)NumElems)
3402 Mask[i] = idx + NumElems;
3403 else
3404 Mask[i] = idx - NumElems;
3405 }
3406}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003407
Craig Topper1a7700a2012-01-19 08:19:12 +00003408/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3409/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3410/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3411/// reverse of what x86 shuffles want.
3412static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3413 bool Commuted = false) {
3414 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003415 return false;
3416
Craig Topper1a7700a2012-01-19 08:19:12 +00003417 unsigned NumElems = VT.getVectorNumElements();
3418 unsigned NumLanes = VT.getSizeInBits()/128;
3419 unsigned NumLaneElems = NumElems/NumLanes;
3420
3421 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003422 return false;
3423
3424 // VSHUFPSY divides the resulting vector into 4 chunks.
3425 // The sources are also splitted into 4 chunks, and each destination
3426 // chunk must come from a different source chunk.
3427 //
3428 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3429 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3430 //
3431 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3432 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3433 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003434 // VSHUFPDY divides the resulting vector into 4 chunks.
3435 // The sources are also splitted into 4 chunks, and each destination
3436 // chunk must come from a different source chunk.
3437 //
3438 // SRC1 => X3 X2 X1 X0
3439 // SRC2 => Y3 Y2 Y1 Y0
3440 //
3441 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3442 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003443 unsigned HalfLaneElems = NumLaneElems/2;
3444 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3445 for (unsigned i = 0; i != NumLaneElems; ++i) {
3446 int Idx = Mask[i+l];
3447 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3448 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3449 return false;
3450 // For VSHUFPSY, the mask of the second half must be the same as the
3451 // first but with the appropriate offsets. This works in the same way as
3452 // VPERMILPS works with masks.
3453 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3454 continue;
3455 if (!isUndefOrEqual(Idx, Mask[i]+l))
3456 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003457 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003458 }
3459
3460 return true;
3461}
3462
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003463/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3464/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003465static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003466 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003467 return false;
3468
Craig Topper7a9a28b2012-08-12 02:23:29 +00003469 unsigned NumElems = VT.getVectorNumElements();
3470
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003471 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003472 return false;
3473
Evan Cheng2064a2b2006-03-28 06:50:32 +00003474 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003475 return isUndefOrEqual(Mask[0], 6) &&
3476 isUndefOrEqual(Mask[1], 7) &&
3477 isUndefOrEqual(Mask[2], 2) &&
3478 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003479}
3480
Nate Begeman0b10b912009-11-07 23:17:15 +00003481/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3482/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3483/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003484static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003485 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003486 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003487
Craig Topper7a9a28b2012-08-12 02:23:29 +00003488 unsigned NumElems = VT.getVectorNumElements();
3489
Nate Begeman0b10b912009-11-07 23:17:15 +00003490 if (NumElems != 4)
3491 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003492
Craig Topperdd637ae2012-02-19 05:41:45 +00003493 return isUndefOrEqual(Mask[0], 2) &&
3494 isUndefOrEqual(Mask[1], 3) &&
3495 isUndefOrEqual(Mask[2], 2) &&
3496 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003497}
3498
Evan Cheng5ced1d82006-04-06 23:23:56 +00003499/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3500/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003501static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003502 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003503 return false;
3504
Craig Topperdd637ae2012-02-19 05:41:45 +00003505 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003506
Evan Cheng5ced1d82006-04-06 23:23:56 +00003507 if (NumElems != 2 && NumElems != 4)
3508 return false;
3509
Chad Rosier238ae312012-04-30 17:47:15 +00003510 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003511 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003512 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003513
Chad Rosier238ae312012-04-30 17:47:15 +00003514 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003515 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003516 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003517
3518 return true;
3519}
3520
Nate Begeman0b10b912009-11-07 23:17:15 +00003521/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3522/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003523static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003524 if (!VT.is128BitVector())
3525 return false;
3526
Craig Topperdd637ae2012-02-19 05:41:45 +00003527 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003528
Craig Topper7a9a28b2012-08-12 02:23:29 +00003529 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003530 return false;
3531
Chad Rosier238ae312012-04-30 17:47:15 +00003532 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003533 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003534 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003535
Chad Rosier238ae312012-04-30 17:47:15 +00003536 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3537 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003538 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003539
3540 return true;
3541}
3542
Elena Demikhovsky15963732012-06-26 08:04:10 +00003543//
3544// Some special combinations that can be optimized.
3545//
3546static
3547SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3548 SelectionDAG &DAG) {
3549 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003550 DebugLoc dl = SVOp->getDebugLoc();
3551
3552 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3553 return SDValue();
3554
3555 ArrayRef<int> Mask = SVOp->getMask();
3556
3557 // These are the special masks that may be optimized.
3558 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3559 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3560 bool MatchEvenMask = true;
3561 bool MatchOddMask = true;
3562 for (int i=0; i<8; ++i) {
3563 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3564 MatchEvenMask = false;
3565 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3566 MatchOddMask = false;
3567 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003568
Elena Demikhovsky32510202012-09-04 12:49:02 +00003569 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003570 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003571
Elena Demikhovsky15963732012-06-26 08:04:10 +00003572 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3573
Elena Demikhovsky32510202012-09-04 12:49:02 +00003574 SDValue Op0 = SVOp->getOperand(0);
3575 SDValue Op1 = SVOp->getOperand(1);
3576
3577 if (MatchEvenMask) {
3578 // Shift the second operand right to 32 bits.
3579 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3580 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3581 } else {
3582 // Shift the first operand left to 32 bits.
3583 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3584 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3585 }
3586 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3587 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003588}
3589
Evan Cheng0038e592006-03-28 00:39:58 +00003590/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3591/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003592static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003593 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003594 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003595
3596 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3597 "Unsupported vector type for unpckh");
3598
Craig Topper6347e862011-11-21 06:57:39 +00003599 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003600 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003601 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003602
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003603 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3604 // independently on 128-bit lanes.
3605 unsigned NumLanes = VT.getSizeInBits()/128;
3606 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003607
Craig Topper94438ba2011-12-16 08:06:31 +00003608 for (unsigned l = 0; l != NumLanes; ++l) {
3609 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3610 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003611 i += 2, ++j) {
3612 int BitI = Mask[i];
3613 int BitI1 = Mask[i+1];
3614 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003615 return false;
David Greenea20244d2011-03-02 17:23:43 +00003616 if (V2IsSplat) {
3617 if (!isUndefOrEqual(BitI1, NumElts))
3618 return false;
3619 } else {
3620 if (!isUndefOrEqual(BitI1, j + NumElts))
3621 return false;
3622 }
Evan Cheng39623da2006-04-20 08:58:49 +00003623 }
Evan Cheng0038e592006-03-28 00:39:58 +00003624 }
David Greenea20244d2011-03-02 17:23:43 +00003625
Evan Cheng0038e592006-03-28 00:39:58 +00003626 return true;
3627}
3628
Evan Cheng4fcb9222006-03-28 02:43:26 +00003629/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3630/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003631static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003632 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003633 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003634
3635 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3636 "Unsupported vector type for unpckh");
3637
Craig Topper6347e862011-11-21 06:57:39 +00003638 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003639 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003640 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003641
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003642 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3643 // independently on 128-bit lanes.
3644 unsigned NumLanes = VT.getSizeInBits()/128;
3645 unsigned NumLaneElts = NumElts/NumLanes;
3646
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003647 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003648 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3649 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003650 int BitI = Mask[i];
3651 int BitI1 = Mask[i+1];
3652 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003653 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003654 if (V2IsSplat) {
3655 if (isUndefOrEqual(BitI1, NumElts))
3656 return false;
3657 } else {
3658 if (!isUndefOrEqual(BitI1, j+NumElts))
3659 return false;
3660 }
Evan Cheng39623da2006-04-20 08:58:49 +00003661 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003662 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003663 return true;
3664}
3665
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003666/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3667/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3668/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003669static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003670 bool HasAVX2) {
3671 unsigned NumElts = VT.getVectorNumElements();
3672
3673 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3674 "Unsupported vector type for unpckh");
3675
3676 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3677 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003678 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003679
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003680 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3681 // FIXME: Need a better way to get rid of this, there's no latency difference
3682 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3683 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003684 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003685 return false;
3686
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003687 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3688 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003689 unsigned NumLanes = VT.getSizeInBits()/128;
3690 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003691
Craig Topper94438ba2011-12-16 08:06:31 +00003692 for (unsigned l = 0; l != NumLanes; ++l) {
3693 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3694 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003695 i += 2, ++j) {
3696 int BitI = Mask[i];
3697 int BitI1 = Mask[i+1];
3698
3699 if (!isUndefOrEqual(BitI, j))
3700 return false;
3701 if (!isUndefOrEqual(BitI1, j))
3702 return false;
3703 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003704 }
David Greenea20244d2011-03-02 17:23:43 +00003705
Rafael Espindola15684b22009-04-24 12:40:33 +00003706 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003707}
3708
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003709/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3710/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3711/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003712static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003713 unsigned NumElts = VT.getVectorNumElements();
3714
3715 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3716 "Unsupported vector type for unpckh");
3717
3718 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3719 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003720 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003721
Craig Topper94438ba2011-12-16 08:06:31 +00003722 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3723 // independently on 128-bit lanes.
3724 unsigned NumLanes = VT.getSizeInBits()/128;
3725 unsigned NumLaneElts = NumElts/NumLanes;
3726
3727 for (unsigned l = 0; l != NumLanes; ++l) {
3728 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3729 i != (l+1)*NumLaneElts; i += 2, ++j) {
3730 int BitI = Mask[i];
3731 int BitI1 = Mask[i+1];
3732 if (!isUndefOrEqual(BitI, j))
3733 return false;
3734 if (!isUndefOrEqual(BitI1, j))
3735 return false;
3736 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003737 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003738 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003739}
3740
Evan Cheng017dcc62006-04-21 01:05:10 +00003741/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3742/// specifies a shuffle of elements that is suitable for input to MOVSS,
3743/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003744static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003745 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003746 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003747 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003748 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003749
Craig Topperc612d792012-01-02 09:17:37 +00003750 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003751
Nate Begeman9008ca62009-04-27 18:41:29 +00003752 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003753 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003754
Craig Topperc612d792012-01-02 09:17:37 +00003755 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003756 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003757 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003758
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003759 return true;
3760}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003761
Craig Topper70b883b2011-11-28 10:14:51 +00003762/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003763/// as permutations between 128-bit chunks or halves. As an example: this
3764/// shuffle bellow:
3765/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3766/// The first half comes from the second half of V1 and the second half from the
3767/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003768static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003769 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003770 return false;
3771
3772 // The shuffle result is divided into half A and half B. In total the two
3773 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3774 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003775 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003776 bool MatchA = false, MatchB = false;
3777
3778 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003779 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003780 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3781 MatchA = true;
3782 break;
3783 }
3784 }
3785
3786 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003787 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003788 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3789 MatchB = true;
3790 break;
3791 }
3792 }
3793
3794 return MatchA && MatchB;
3795}
3796
Craig Topper70b883b2011-11-28 10:14:51 +00003797/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3798/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003799static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003800 EVT VT = SVOp->getValueType(0);
3801
Craig Topperc612d792012-01-02 09:17:37 +00003802 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003803
Craig Topperc612d792012-01-02 09:17:37 +00003804 unsigned FstHalf = 0, SndHalf = 0;
3805 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003806 if (SVOp->getMaskElt(i) > 0) {
3807 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3808 break;
3809 }
3810 }
Craig Topperc612d792012-01-02 09:17:37 +00003811 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003812 if (SVOp->getMaskElt(i) > 0) {
3813 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3814 break;
3815 }
3816 }
3817
3818 return (FstHalf | (SndHalf << 4));
3819}
3820
Craig Topper70b883b2011-11-28 10:14:51 +00003821/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003822/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3823/// Note that VPERMIL mask matching is different depending whether theunderlying
3824/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3825/// to the same elements of the low, but to the higher half of the source.
3826/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003827/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003828static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003829 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003830 return false;
3831
Craig Topperc612d792012-01-02 09:17:37 +00003832 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003833 // Only match 256-bit with 32/64-bit types
3834 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003835 return false;
3836
Craig Topperc612d792012-01-02 09:17:37 +00003837 unsigned NumLanes = VT.getSizeInBits()/128;
3838 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003839 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003840 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003841 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003842 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003843 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003844 continue;
3845 // VPERMILPS handling
3846 if (Mask[i] < 0)
3847 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003848 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003849 return false;
3850 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003851 }
3852
3853 return true;
3854}
3855
Craig Topper5aaffa82012-02-19 02:53:47 +00003856/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003857/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003858/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003859static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003861 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003862 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003863
3864 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003865 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003866 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003867
Nate Begeman9008ca62009-04-27 18:41:29 +00003868 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003869 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003870
Craig Topperc612d792012-01-02 09:17:37 +00003871 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003872 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3873 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3874 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003875 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003876
Evan Cheng39623da2006-04-20 08:58:49 +00003877 return true;
3878}
3879
Evan Chengd9539472006-04-14 21:59:03 +00003880/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3881/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003882/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003883static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003884 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003885 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003886 return false;
3887
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003888 unsigned NumElems = VT.getVectorNumElements();
3889
3890 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3891 (VT.getSizeInBits() == 256 && NumElems != 8))
3892 return false;
3893
3894 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003895 for (unsigned i = 0; i != NumElems; i += 2)
3896 if (!isUndefOrEqual(Mask[i], i+1) ||
3897 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003899
3900 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003901}
3902
3903/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3904/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003905/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003906static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003907 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003908 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003909 return false;
3910
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003911 unsigned NumElems = VT.getVectorNumElements();
3912
3913 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3914 (VT.getSizeInBits() == 256 && NumElems != 8))
3915 return false;
3916
3917 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003918 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003919 if (!isUndefOrEqual(Mask[i], i) ||
3920 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003922
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003923 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003924}
3925
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003926/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3927/// specifies a shuffle of elements that is suitable for input to 256-bit
3928/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003929static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003930 if (!HasAVX || !VT.is256BitVector())
3931 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003932
Craig Topper7a9a28b2012-08-12 02:23:29 +00003933 unsigned NumElts = VT.getVectorNumElements();
3934 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003935 return false;
3936
Craig Topperc612d792012-01-02 09:17:37 +00003937 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003938 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003939 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003940 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003941 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003942 return false;
3943 return true;
3944}
3945
Evan Cheng0b457f02008-09-25 20:50:48 +00003946/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003947/// specifies a shuffle of elements that is suitable for input to 128-bit
3948/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003949static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003950 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003951 return false;
3952
Craig Topperc612d792012-01-02 09:17:37 +00003953 unsigned e = VT.getVectorNumElements() / 2;
3954 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003955 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003956 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003957 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003958 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003959 return false;
3960 return true;
3961}
3962
David Greenec38a03e2011-02-03 15:50:00 +00003963/// isVEXTRACTF128Index - Return true if the specified
3964/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3965/// suitable for input to VEXTRACTF128.
3966bool X86::isVEXTRACTF128Index(SDNode *N) {
3967 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3968 return false;
3969
3970 // The index should be aligned on a 128-bit boundary.
3971 uint64_t Index =
3972 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3973
3974 unsigned VL = N->getValueType(0).getVectorNumElements();
3975 unsigned VBits = N->getValueType(0).getSizeInBits();
3976 unsigned ElSize = VBits / VL;
3977 bool Result = (Index * ElSize) % 128 == 0;
3978
3979 return Result;
3980}
3981
David Greeneccacdc12011-02-04 16:08:29 +00003982/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3983/// operand specifies a subvector insert that is suitable for input to
3984/// VINSERTF128.
3985bool X86::isVINSERTF128Index(SDNode *N) {
3986 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3987 return false;
3988
3989 // The index should be aligned on a 128-bit boundary.
3990 uint64_t Index =
3991 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3992
3993 unsigned VL = N->getValueType(0).getVectorNumElements();
3994 unsigned VBits = N->getValueType(0).getSizeInBits();
3995 unsigned ElSize = VBits / VL;
3996 bool Result = (Index * ElSize) % 128 == 0;
3997
3998 return Result;
3999}
4000
Evan Cheng63d33002006-03-22 08:01:21 +00004001/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004002/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004003/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004004static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004005 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004006
Craig Topper1a7700a2012-01-19 08:19:12 +00004007 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4008 "Unsupported vector type for PSHUF/SHUFP");
4009
4010 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4011 // independently on 128-bit lanes.
4012 unsigned NumElts = VT.getVectorNumElements();
4013 unsigned NumLanes = VT.getSizeInBits()/128;
4014 unsigned NumLaneElts = NumElts/NumLanes;
4015
4016 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4017 "Only supports 2 or 4 elements per lane");
4018
4019 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004020 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004021 for (unsigned i = 0; i != NumElts; ++i) {
4022 int Elt = N->getMaskElt(i);
4023 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004024 Elt &= NumLaneElts - 1;
4025 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004026 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004027 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004028
Evan Cheng63d33002006-03-22 08:01:21 +00004029 return Mask;
4030}
4031
Evan Cheng506d3df2006-03-29 23:07:14 +00004032/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004033/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004034static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004035 EVT VT = N->getValueType(0);
4036
4037 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4038 "Unsupported vector type for PSHUFHW");
4039
4040 unsigned NumElts = VT.getVectorNumElements();
4041
Evan Cheng506d3df2006-03-29 23:07:14 +00004042 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004043 for (unsigned l = 0; l != NumElts; l += 8) {
4044 // 8 nodes per lane, but we only care about the last 4.
4045 for (unsigned i = 0; i < 4; ++i) {
4046 int Elt = N->getMaskElt(l+i+4);
4047 if (Elt < 0) continue;
4048 Elt &= 0x3; // only 2-bits.
4049 Mask |= Elt << (i * 2);
4050 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004051 }
Craig Topper6b28d352012-05-03 07:12:59 +00004052
Evan Cheng506d3df2006-03-29 23:07:14 +00004053 return Mask;
4054}
4055
4056/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004057/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004058static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004059 EVT VT = N->getValueType(0);
4060
4061 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4062 "Unsupported vector type for PSHUFHW");
4063
4064 unsigned NumElts = VT.getVectorNumElements();
4065
Evan Cheng506d3df2006-03-29 23:07:14 +00004066 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004067 for (unsigned l = 0; l != NumElts; l += 8) {
4068 // 8 nodes per lane, but we only care about the first 4.
4069 for (unsigned i = 0; i < 4; ++i) {
4070 int Elt = N->getMaskElt(l+i);
4071 if (Elt < 0) continue;
4072 Elt &= 0x3; // only 2-bits
4073 Mask |= Elt << (i * 2);
4074 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004075 }
Craig Topper6b28d352012-05-03 07:12:59 +00004076
Evan Cheng506d3df2006-03-29 23:07:14 +00004077 return Mask;
4078}
4079
Nate Begemana09008b2009-10-19 02:17:23 +00004080/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4081/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004082static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4083 EVT VT = SVOp->getValueType(0);
4084 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004085
Craig Topper0e2037b2012-01-20 05:53:00 +00004086 unsigned NumElts = VT.getVectorNumElements();
4087 unsigned NumLanes = VT.getSizeInBits()/128;
4088 unsigned NumLaneElts = NumElts/NumLanes;
4089
4090 int Val = 0;
4091 unsigned i;
4092 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004093 Val = SVOp->getMaskElt(i);
4094 if (Val >= 0)
4095 break;
4096 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004097 if (Val >= (int)NumElts)
4098 Val -= NumElts - NumLaneElts;
4099
Eli Friedman63f8dde2011-07-25 21:36:45 +00004100 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004101 return (Val - i) * EltSize;
4102}
4103
David Greenec38a03e2011-02-03 15:50:00 +00004104/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4105/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4106/// instructions.
4107unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4108 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4109 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4110
4111 uint64_t Index =
4112 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4113
4114 EVT VecVT = N->getOperand(0).getValueType();
4115 EVT ElVT = VecVT.getVectorElementType();
4116
4117 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004118 return Index / NumElemsPerChunk;
4119}
4120
David Greeneccacdc12011-02-04 16:08:29 +00004121/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4122/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4123/// instructions.
4124unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4125 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4126 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4127
4128 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004129 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004130
4131 EVT VecVT = N->getValueType(0);
4132 EVT ElVT = VecVT.getVectorElementType();
4133
4134 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004135 return Index / NumElemsPerChunk;
4136}
4137
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004138/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4139/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4140/// Handles 256-bit.
4141static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4142 EVT VT = N->getValueType(0);
4143
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004144 unsigned NumElts = VT.getVectorNumElements();
4145
Craig Topper095c5282012-04-15 23:48:57 +00004146 assert((VT.is256BitVector() && NumElts == 4) &&
4147 "Unsupported vector type for VPERMQ/VPERMPD");
4148
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004149 unsigned Mask = 0;
4150 for (unsigned i = 0; i != NumElts; ++i) {
4151 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004152 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004153 continue;
4154 Mask |= Elt << (i*2);
4155 }
4156
4157 return Mask;
4158}
Evan Cheng37b73872009-07-30 08:33:02 +00004159/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4160/// constant +0.0.
4161bool X86::isZeroNode(SDValue Elt) {
4162 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004163 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004164 (isa<ConstantFPSDNode>(Elt) &&
4165 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4166}
4167
Nate Begeman9008ca62009-04-27 18:41:29 +00004168/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4169/// their permute mask.
4170static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4171 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004172 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004173 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004175
Nate Begeman5a5ca152009-04-29 05:20:52 +00004176 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004177 int Idx = SVOp->getMaskElt(i);
4178 if (Idx >= 0) {
4179 if (Idx < (int)NumElems)
4180 Idx += NumElems;
4181 else
4182 Idx -= NumElems;
4183 }
4184 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004185 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004186 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4187 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004188}
4189
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4191/// match movhlps. The lower half elements should come from upper half of
4192/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004193/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004194static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004195 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004196 return false;
4197 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004198 return false;
4199 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004200 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004201 return false;
4202 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004203 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004204 return false;
4205 return true;
4206}
4207
Evan Cheng5ced1d82006-04-06 23:23:56 +00004208/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004209/// is promoted to a vector. It also returns the LoadSDNode by reference if
4210/// required.
4211static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004212 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4213 return false;
4214 N = N->getOperand(0).getNode();
4215 if (!ISD::isNON_EXTLoad(N))
4216 return false;
4217 if (LD)
4218 *LD = cast<LoadSDNode>(N);
4219 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004220}
4221
Dan Gohman65fd6562011-11-03 21:49:52 +00004222// Test whether the given value is a vector value which will be legalized
4223// into a load.
4224static bool WillBeConstantPoolLoad(SDNode *N) {
4225 if (N->getOpcode() != ISD::BUILD_VECTOR)
4226 return false;
4227
4228 // Check for any non-constant elements.
4229 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4230 switch (N->getOperand(i).getNode()->getOpcode()) {
4231 case ISD::UNDEF:
4232 case ISD::ConstantFP:
4233 case ISD::Constant:
4234 break;
4235 default:
4236 return false;
4237 }
4238
4239 // Vectors of all-zeros and all-ones are materialized with special
4240 // instructions rather than being loaded.
4241 return !ISD::isBuildVectorAllZeros(N) &&
4242 !ISD::isBuildVectorAllOnes(N);
4243}
4244
Evan Cheng533a0aa2006-04-19 20:35:22 +00004245/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4246/// match movlp{s|d}. The lower half elements should come from lower half of
4247/// V1 (and in order), and the upper half elements should come from the upper
4248/// half of V2 (and in order). And since V1 will become the source of the
4249/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004250static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004251 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004252 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004253 return false;
4254
Evan Cheng466685d2006-10-09 20:57:25 +00004255 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004256 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004257 // Is V2 is a vector load, don't do this transformation. We will try to use
4258 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004259 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004260 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004261
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004262 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004263
Evan Cheng533a0aa2006-04-19 20:35:22 +00004264 if (NumElems != 2 && NumElems != 4)
4265 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004266 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004267 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004268 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004269 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004270 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004271 return false;
4272 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004273}
4274
Evan Cheng39623da2006-04-20 08:58:49 +00004275/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4276/// all the same.
4277static bool isSplatVector(SDNode *N) {
4278 if (N->getOpcode() != ISD::BUILD_VECTOR)
4279 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004280
Dan Gohman475871a2008-07-27 21:46:04 +00004281 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004282 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4283 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004284 return false;
4285 return true;
4286}
4287
Evan Cheng213d2cf2007-05-17 18:45:50 +00004288/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004289/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004290/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004291static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004292 SDValue V1 = N->getOperand(0);
4293 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004294 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4295 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004296 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004297 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004298 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004299 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4300 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004301 if (Opc != ISD::BUILD_VECTOR ||
4302 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004303 return false;
4304 } else if (Idx >= 0) {
4305 unsigned Opc = V1.getOpcode();
4306 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4307 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004308 if (Opc != ISD::BUILD_VECTOR ||
4309 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004310 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004311 }
4312 }
4313 return true;
4314}
4315
4316/// getZeroVector - Returns a vector of specified type with all zero elements.
4317///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004318static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004319 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004320 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004321 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004322
Dale Johannesen0488fb62010-09-30 23:57:10 +00004323 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004324 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004325 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004326 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004327 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004328 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4329 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4330 } else { // SSE1
4331 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4332 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4333 }
Craig Topper9d352402012-04-23 07:24:41 +00004334 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004335 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004336 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4337 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4338 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4339 } else {
4340 // 256-bit logic and arithmetic instructions in AVX are all
4341 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4342 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4343 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4344 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4345 }
Craig Topper9d352402012-04-23 07:24:41 +00004346 } else
4347 llvm_unreachable("Unexpected vector type");
4348
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004349 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004350}
4351
Chris Lattner8a594482007-11-25 00:24:49 +00004352/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004353/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4354/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4355/// Then bitcast to their original type, ensuring they get CSE'd.
4356static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4357 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004358 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004359 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004360
Owen Anderson825b72b2009-08-11 20:47:22 +00004361 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004362 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004363 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004364 if (HasAVX2) { // AVX2
4365 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4366 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4367 } else { // AVX
4368 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004369 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004370 }
Craig Topper9d352402012-04-23 07:24:41 +00004371 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004372 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004373 } else
4374 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004375
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004376 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004377}
4378
Evan Cheng39623da2006-04-20 08:58:49 +00004379/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4380/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004381static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004382 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004383 if (Mask[i] > (int)NumElems) {
4384 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004385 }
Evan Cheng39623da2006-04-20 08:58:49 +00004386 }
Evan Cheng39623da2006-04-20 08:58:49 +00004387}
4388
Evan Cheng017dcc62006-04-21 01:05:10 +00004389/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4390/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004391static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 SDValue V2) {
4393 unsigned NumElems = VT.getVectorNumElements();
4394 SmallVector<int, 8> Mask;
4395 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004396 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 Mask.push_back(i);
4398 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004399}
4400
Nate Begeman9008ca62009-04-27 18:41:29 +00004401/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004402static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004403 SDValue V2) {
4404 unsigned NumElems = VT.getVectorNumElements();
4405 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004406 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 Mask.push_back(i);
4408 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004409 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004410 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004411}
4412
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004413/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004414static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 SDValue V2) {
4416 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004418 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 Mask.push_back(i + Half);
4420 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004421 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004422 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004423}
4424
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004425// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426// a generic shuffle instruction because the target has no such instructions.
4427// Generate shuffles which repeat i16 and i8 several times until they can be
4428// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004429static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004431 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004432 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004433
Nate Begeman9008ca62009-04-27 18:41:29 +00004434 while (NumElems > 4) {
4435 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004438 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 EltNo -= NumElems/2;
4440 }
4441 NumElems >>= 1;
4442 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004443 return V;
4444}
Eric Christopherfd179292009-08-27 18:07:15 +00004445
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004446/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4447static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4448 EVT VT = V.getValueType();
4449 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004450 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004451
Craig Topper9d352402012-04-23 07:24:41 +00004452 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004453 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004454 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004455 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4456 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004457 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004458 // To use VPERMILPS to splat scalars, the second half of indicies must
4459 // refer to the higher part, which is a duplication of the lower one,
4460 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004461 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4462 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004463
4464 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4465 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4466 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004467 } else
4468 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469
4470 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4471}
4472
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004473/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4475 EVT SrcVT = SV->getValueType(0);
4476 SDValue V1 = SV->getOperand(0);
4477 DebugLoc dl = SV->getDebugLoc();
4478
4479 int EltNo = SV->getSplatIndex();
4480 int NumElems = SrcVT.getVectorNumElements();
4481 unsigned Size = SrcVT.getSizeInBits();
4482
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004483 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4484 "Unknown how to promote splat for type");
4485
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004486 // Extract the 128-bit part containing the splat element and update
4487 // the splat element index when it refers to the higher register.
4488 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004489 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4490 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491 EltNo -= NumElems/2;
4492 }
4493
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004494 // All i16 and i8 vector types can't be used directly by a generic shuffle
4495 // instruction because the target has no such instruction. Generate shuffles
4496 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004497 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004498 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004499 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004500 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004501
4502 // Recreate the 256-bit vector and place the same 128-bit vector
4503 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004504 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004505 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004506 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507 }
4508
4509 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004510}
4511
Evan Chengba05f722006-04-21 23:03:30 +00004512/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004513/// vector of zero or undef vector. This produces a shuffle where the low
4514/// element of V2 is swizzled into the zero/undef vector, landing at element
4515/// 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 +00004516static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004517 bool IsZero,
4518 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004519 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004520 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004521 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004522 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004523 unsigned NumElems = VT.getVectorNumElements();
4524 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004525 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004526 // If this is the insertion idx, put the low elt of V2 here.
4527 MaskVec.push_back(i == Idx ? NumElems : i);
4528 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004529}
4530
Craig Toppera1ffc682012-03-20 06:42:26 +00004531/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4532/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004533/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004534static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004535 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004536 unsigned NumElems = VT.getVectorNumElements();
4537 SDValue ImmN;
4538
Craig Topper89f4e662012-03-20 07:17:59 +00004539 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004540 switch(N->getOpcode()) {
4541 case X86ISD::SHUFP:
4542 ImmN = N->getOperand(N->getNumOperands()-1);
4543 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4544 break;
4545 case X86ISD::UNPCKH:
4546 DecodeUNPCKHMask(VT, Mask);
4547 break;
4548 case X86ISD::UNPCKL:
4549 DecodeUNPCKLMask(VT, Mask);
4550 break;
4551 case X86ISD::MOVHLPS:
4552 DecodeMOVHLPSMask(NumElems, Mask);
4553 break;
4554 case X86ISD::MOVLHPS:
4555 DecodeMOVLHPSMask(NumElems, Mask);
4556 break;
4557 case X86ISD::PSHUFD:
4558 case X86ISD::VPERMILP:
4559 ImmN = N->getOperand(N->getNumOperands()-1);
4560 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004561 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004562 break;
4563 case X86ISD::PSHUFHW:
4564 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004565 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004566 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004567 break;
4568 case X86ISD::PSHUFLW:
4569 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004570 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004571 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004572 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004573 case X86ISD::VPERMI:
4574 ImmN = N->getOperand(N->getNumOperands()-1);
4575 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4576 IsUnary = true;
4577 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004578 case X86ISD::MOVSS:
4579 case X86ISD::MOVSD: {
4580 // The index 0 always comes from the first element of the second source,
4581 // this is why MOVSS and MOVSD are used in the first place. The other
4582 // elements come from the other positions of the first source vector
4583 Mask.push_back(NumElems);
4584 for (unsigned i = 1; i != NumElems; ++i) {
4585 Mask.push_back(i);
4586 }
4587 break;
4588 }
4589 case X86ISD::VPERM2X128:
4590 ImmN = N->getOperand(N->getNumOperands()-1);
4591 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004592 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004593 break;
4594 case X86ISD::MOVDDUP:
4595 case X86ISD::MOVLHPD:
4596 case X86ISD::MOVLPD:
4597 case X86ISD::MOVLPS:
4598 case X86ISD::MOVSHDUP:
4599 case X86ISD::MOVSLDUP:
4600 case X86ISD::PALIGN:
4601 // Not yet implemented
4602 return false;
4603 default: llvm_unreachable("unknown target shuffle node");
4604 }
4605
4606 return true;
4607}
4608
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004609/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4610/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004611static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004612 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004613 if (Depth == 6)
4614 return SDValue(); // Limit search depth.
4615
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004616 SDValue V = SDValue(N, 0);
4617 EVT VT = V.getValueType();
4618 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004619
4620 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4621 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004622 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004623
Craig Topper3d092db2012-03-21 02:14:01 +00004624 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004625 return DAG.getUNDEF(VT.getVectorElementType());
4626
Craig Topperd156dc12012-02-06 07:17:51 +00004627 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004628 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4629 : SV->getOperand(1);
4630 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004631 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004632
4633 // Recurse into target specific vector shuffles to find scalars.
4634 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004635 MVT ShufVT = V.getValueType().getSimpleVT();
4636 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004637 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004638 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004639
Craig Topperd978c542012-05-06 19:46:21 +00004640 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004641 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004642
Craig Topper3d092db2012-03-21 02:14:01 +00004643 int Elt = ShuffleMask[Index];
4644 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004645 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004646
Craig Topper3d092db2012-03-21 02:14:01 +00004647 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004648 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004649 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004650 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651 }
4652
4653 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004654 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004655 V = V.getOperand(0);
4656 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004657 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004659 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660 return SDValue();
4661 }
4662
4663 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4664 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004665 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004666
4667 if (V.getOpcode() == ISD::BUILD_VECTOR)
4668 return V.getOperand(Index);
4669
4670 return SDValue();
4671}
4672
4673/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4674/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004675/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676static
Craig Topper3d092db2012-03-21 02:14:01 +00004677unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004679 unsigned i;
4680 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004681 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004682 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004683 if (!(Elt.getNode() &&
4684 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4685 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004686 }
4687
4688 return i;
4689}
4690
Craig Topper3d092db2012-03-21 02:14:01 +00004691/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4692/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004693/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4694static
Craig Topper3d092db2012-03-21 02:14:01 +00004695bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4696 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4697 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698 bool SeenV1 = false;
4699 bool SeenV2 = false;
4700
Craig Topper3d092db2012-03-21 02:14:01 +00004701 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004702 int Idx = SVOp->getMaskElt(i);
4703 // Ignore undef indicies
4704 if (Idx < 0)
4705 continue;
4706
Craig Topper3d092db2012-03-21 02:14:01 +00004707 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004708 SeenV1 = true;
4709 else
4710 SeenV2 = true;
4711
4712 // Only accept consecutive elements from the same vector
4713 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4714 return false;
4715 }
4716
4717 OpNum = SeenV1 ? 0 : 1;
4718 return true;
4719}
4720
4721/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4722/// logical left shift of a vector.
4723static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4724 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4725 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4726 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4727 false /* check zeros from right */, DAG);
4728 unsigned OpSrc;
4729
4730 if (!NumZeros)
4731 return false;
4732
4733 // Considering the elements in the mask that are not consecutive zeros,
4734 // check if they consecutively come from only one of the source vectors.
4735 //
4736 // V1 = {X, A, B, C} 0
4737 // \ \ \ /
4738 // vector_shuffle V1, V2 <1, 2, 3, X>
4739 //
4740 if (!isShuffleMaskConsecutive(SVOp,
4741 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004742 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004743 NumZeros, // Where to start looking in the src vector
4744 NumElems, // Number of elements in vector
4745 OpSrc)) // Which source operand ?
4746 return false;
4747
4748 isLeft = false;
4749 ShAmt = NumZeros;
4750 ShVal = SVOp->getOperand(OpSrc);
4751 return true;
4752}
4753
4754/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4755/// logical left shift of a vector.
4756static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4757 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4758 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4759 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4760 true /* check zeros from left */, DAG);
4761 unsigned OpSrc;
4762
4763 if (!NumZeros)
4764 return false;
4765
4766 // Considering the elements in the mask that are not consecutive zeros,
4767 // check if they consecutively come from only one of the source vectors.
4768 //
4769 // 0 { A, B, X, X } = V2
4770 // / \ / /
4771 // vector_shuffle V1, V2 <X, X, 4, 5>
4772 //
4773 if (!isShuffleMaskConsecutive(SVOp,
4774 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004775 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004776 0, // Where to start looking in the src vector
4777 NumElems, // Number of elements in vector
4778 OpSrc)) // Which source operand ?
4779 return false;
4780
4781 isLeft = true;
4782 ShAmt = NumZeros;
4783 ShVal = SVOp->getOperand(OpSrc);
4784 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004785}
4786
4787/// isVectorShift - Returns true if the shuffle can be implemented as a
4788/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004789static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004790 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004791 // Although the logic below support any bitwidth size, there are no
4792 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004793 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004794 return false;
4795
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004796 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4797 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4798 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004799
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004800 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004801}
4802
Evan Chengc78d3b42006-04-24 18:01:45 +00004803/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4804///
Dan Gohman475871a2008-07-27 21:46:04 +00004805static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004806 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004807 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004808 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004809 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004810 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004811 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004812
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004813 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004814 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 bool First = true;
4816 for (unsigned i = 0; i < 16; ++i) {
4817 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4818 if (ThisIsNonZero && First) {
4819 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004820 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004821 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 First = false;
4824 }
4825
4826 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004827 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4829 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004830 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 }
4833 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4835 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4836 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004837 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 } else
4840 ThisElt = LastElt;
4841
Gabor Greifba36cb52008-08-28 21:40:38 +00004842 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004843 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004844 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 }
4846 }
4847
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004848 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004849}
4850
Bill Wendlinga348c562007-03-22 18:42:45 +00004851/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004852///
Dan Gohman475871a2008-07-27 21:46:04 +00004853static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004854 unsigned NumNonZero, unsigned NumZero,
4855 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004856 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004857 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004858 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004859 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004860
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004861 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004862 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 bool First = true;
4864 for (unsigned i = 0; i < 8; ++i) {
4865 bool isNonZero = (NonZeros & (1 << i)) != 0;
4866 if (isNonZero) {
4867 if (First) {
4868 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004869 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004870 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004872 First = false;
4873 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004874 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004876 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004877 }
4878 }
4879
4880 return V;
4881}
4882
Evan Chengf26ffe92008-05-29 08:22:04 +00004883/// getVShift - Return a vector logical shift node.
4884///
Owen Andersone50ed302009-08-10 22:56:29 +00004885static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004886 unsigned NumBits, SelectionDAG &DAG,
4887 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004888 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004889 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004890 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004891 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4892 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004893 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004894 DAG.getConstant(NumBits,
4895 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004896}
4897
Dan Gohman475871a2008-07-27 21:46:04 +00004898SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004899X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004900 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004901
Evan Chengc3630942009-12-09 21:00:30 +00004902 // Check if the scalar load can be widened into a vector load. And if
4903 // the address is "base + cst" see if the cst can be "absorbed" into
4904 // the shuffle mask.
4905 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4906 SDValue Ptr = LD->getBasePtr();
4907 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4908 return SDValue();
4909 EVT PVT = LD->getValueType(0);
4910 if (PVT != MVT::i32 && PVT != MVT::f32)
4911 return SDValue();
4912
4913 int FI = -1;
4914 int64_t Offset = 0;
4915 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4916 FI = FINode->getIndex();
4917 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004918 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004919 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4920 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4921 Offset = Ptr.getConstantOperandVal(1);
4922 Ptr = Ptr.getOperand(0);
4923 } else {
4924 return SDValue();
4925 }
4926
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004927 // FIXME: 256-bit vector instructions don't require a strict alignment,
4928 // improve this code to support it better.
4929 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004930 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004932 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004933 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004934 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004935 // Can't change the alignment. FIXME: It's possible to compute
4936 // the exact stack offset and reference FI + adjust offset instead.
4937 // If someone *really* cares about this. That's the way to implement it.
4938 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004939 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004940 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004941 }
4942 }
4943
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004944 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004945 // Ptr + (Offset & ~15).
4946 if (Offset < 0)
4947 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004948 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004949 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004950 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004951 if (StartOffset)
4952 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4953 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4954
4955 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004956 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004957
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004958 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4959 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004960 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004961 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004962
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004963 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004964 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004965 Mask.push_back(EltNo);
4966
Craig Toppercc3000632012-01-30 07:50:31 +00004967 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004968 }
4969
4970 return SDValue();
4971}
4972
Michael J. Spencerec38de22010-10-10 22:04:20 +00004973/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4974/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004975/// load which has the same value as a build_vector whose operands are 'elts'.
4976///
4977/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004978///
Nate Begeman1449f292010-03-24 22:19:06 +00004979/// FIXME: we'd also like to handle the case where the last elements are zero
4980/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4981/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004982static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004983 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004984 EVT EltVT = VT.getVectorElementType();
4985 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004986
Nate Begemanfdea31a2010-03-24 20:49:50 +00004987 LoadSDNode *LDBase = NULL;
4988 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004989
Nate Begeman1449f292010-03-24 22:19:06 +00004990 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004991 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004992 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004993 for (unsigned i = 0; i < NumElems; ++i) {
4994 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004995
Nate Begemanfdea31a2010-03-24 20:49:50 +00004996 if (!Elt.getNode() ||
4997 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4998 return SDValue();
4999 if (!LDBase) {
5000 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5001 return SDValue();
5002 LDBase = cast<LoadSDNode>(Elt.getNode());
5003 LastLoadedElt = i;
5004 continue;
5005 }
5006 if (Elt.getOpcode() == ISD::UNDEF)
5007 continue;
5008
5009 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5010 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5011 return SDValue();
5012 LastLoadedElt = i;
5013 }
Nate Begeman1449f292010-03-24 22:19:06 +00005014
5015 // If we have found an entire vector of loads and undefs, then return a large
5016 // load of the entire vector width starting at the base pointer. If we found
5017 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005018 if (LastLoadedElt == NumElems - 1) {
5019 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005020 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005021 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005022 LDBase->isVolatile(), LDBase->isNonTemporal(),
5023 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005024 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005025 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005026 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005027 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005028 }
5029 if (NumElems == 4 && LastLoadedElt == 1 &&
5030 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005031 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5032 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005033 SDValue ResNode =
5034 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5035 LDBase->getPointerInfo(),
5036 LDBase->getAlignment(),
5037 false/*isVolatile*/, true/*ReadMem*/,
5038 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005039
5040 // Make sure the newly-created LOAD is in the same position as LDBase in
5041 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5042 // update uses of LDBase's output chain to use the TokenFactor.
5043 if (LDBase->hasAnyUseOfValue(1)) {
5044 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5045 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5046 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5047 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5048 SDValue(ResNode.getNode(), 1));
5049 }
5050
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005051 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005052 }
5053 return SDValue();
5054}
5055
Nadav Rotem9d68b062012-04-08 12:54:54 +00005056/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5057/// to generate a splat value for the following cases:
5058/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005059/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005060/// a scalar load, or a constant.
5061/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005062/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005063SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005064X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005065 if (!Subtarget->hasAVX())
5066 return SDValue();
5067
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005068 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005069 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005070
Craig Topper5da8a802012-05-04 05:49:51 +00005071 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5072 "Unsupported vector type for broadcast.");
5073
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005074 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005075 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005076
Nadav Rotem9d68b062012-04-08 12:54:54 +00005077 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005078 default:
5079 // Unknown pattern found.
5080 return SDValue();
5081
5082 case ISD::BUILD_VECTOR: {
5083 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005084 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005085 return SDValue();
5086
Nadav Rotem9d68b062012-04-08 12:54:54 +00005087 Ld = Op.getOperand(0);
5088 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5089 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005090
5091 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005092 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005093 // Constants may have multiple users.
5094 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005095 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005096 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005097 }
5098
5099 case ISD::VECTOR_SHUFFLE: {
5100 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5101
5102 // Shuffles must have a splat mask where the first element is
5103 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005104 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005105 return SDValue();
5106
5107 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005108 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005109 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5110
5111 if (!Subtarget->hasAVX2())
5112 return SDValue();
5113
5114 // Use the register form of the broadcast instruction available on AVX2.
5115 if (VT.is256BitVector())
5116 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5117 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5118 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005119
5120 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005121 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005122 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005123
5124 // The scalar_to_vector node and the suspected
5125 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005126 // Constants may have multiple users.
5127 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005128 return SDValue();
5129 break;
5130 }
5131 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005132
Craig Topper7a9a28b2012-08-12 02:23:29 +00005133 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005134
5135 // Handle the broadcasting a single constant scalar from the constant pool
5136 // into a vector. On Sandybridge it is still better to load a constant vector
5137 // from the constant pool and not to broadcast it from a scalar.
5138 if (ConstSplatVal && Subtarget->hasAVX2()) {
5139 EVT CVT = Ld.getValueType();
5140 assert(!CVT.isVector() && "Must not broadcast a vector type");
5141 unsigned ScalarSize = CVT.getSizeInBits();
5142
Craig Topper5da8a802012-05-04 05:49:51 +00005143 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005144 const Constant *C = 0;
5145 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5146 C = CI->getConstantIntValue();
5147 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5148 C = CF->getConstantFPValue();
5149
5150 assert(C && "Invalid constant type");
5151
Nadav Rotem154819d2012-04-09 07:45:58 +00005152 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005153 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005154 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005155 MachinePointerInfo::getConstantPool(),
5156 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005157
Nadav Rotem9d68b062012-04-08 12:54:54 +00005158 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5159 }
5160 }
5161
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005162 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005163 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5164
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005165 // Handle AVX2 in-register broadcasts.
5166 if (!IsLoad && Subtarget->hasAVX2() &&
5167 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5168 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5169
5170 // The scalar source must be a normal load.
5171 if (!IsLoad)
5172 return SDValue();
5173
Craig Topper5da8a802012-05-04 05:49:51 +00005174 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005175 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005176
Craig Toppera9376332012-01-10 08:23:59 +00005177 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005178 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005179 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005180 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005181 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005182 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005183
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005184 // Unsupported broadcast.
5185 return SDValue();
5186}
5187
Evan Chengc3630942009-12-09 21:00:30 +00005188SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005189X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5190 EVT VT = Op.getValueType();
5191
5192 // Skip if insert_vec_elt is not supported.
5193 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5194 return SDValue();
5195
5196 DebugLoc DL = Op.getDebugLoc();
5197 unsigned NumElems = Op.getNumOperands();
5198
5199 SDValue VecIn1;
5200 SDValue VecIn2;
5201 SmallVector<unsigned, 4> InsertIndices;
5202 SmallVector<int, 8> Mask(NumElems, -1);
5203
5204 for (unsigned i = 0; i != NumElems; ++i) {
5205 unsigned Opc = Op.getOperand(i).getOpcode();
5206
5207 if (Opc == ISD::UNDEF)
5208 continue;
5209
5210 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5211 // Quit if more than 1 elements need inserting.
5212 if (InsertIndices.size() > 1)
5213 return SDValue();
5214
5215 InsertIndices.push_back(i);
5216 continue;
5217 }
5218
5219 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5220 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5221
5222 // Quit if extracted from vector of different type.
5223 if (ExtractedFromVec.getValueType() != VT)
5224 return SDValue();
5225
5226 // Quit if non-constant index.
5227 if (!isa<ConstantSDNode>(ExtIdx))
5228 return SDValue();
5229
5230 if (VecIn1.getNode() == 0)
5231 VecIn1 = ExtractedFromVec;
5232 else if (VecIn1 != ExtractedFromVec) {
5233 if (VecIn2.getNode() == 0)
5234 VecIn2 = ExtractedFromVec;
5235 else if (VecIn2 != ExtractedFromVec)
5236 // Quit if more than 2 vectors to shuffle
5237 return SDValue();
5238 }
5239
5240 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5241
5242 if (ExtractedFromVec == VecIn1)
5243 Mask[i] = Idx;
5244 else if (ExtractedFromVec == VecIn2)
5245 Mask[i] = Idx + NumElems;
5246 }
5247
5248 if (VecIn1.getNode() == 0)
5249 return SDValue();
5250
5251 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5252 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5253 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5254 unsigned Idx = InsertIndices[i];
5255 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5256 DAG.getIntPtrConstant(Idx));
5257 }
5258
5259 return NV;
5260}
5261
5262SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005263X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005264 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005265
David Greenef125a292011-02-08 19:04:41 +00005266 EVT VT = Op.getValueType();
5267 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005268 unsigned NumElems = Op.getNumOperands();
5269
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005270 // Vectors containing all zeros can be matched by pxor and xorps later
5271 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5272 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5273 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005274 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005275 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005277 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005278 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005279
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005280 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005281 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5282 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005283 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005284 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005285 return Op;
5286
Craig Topper07a27622012-01-22 03:07:48 +00005287 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005288 }
5289
Nadav Rotem154819d2012-04-09 07:45:58 +00005290 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005291 if (Broadcast.getNode())
5292 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005293
Owen Andersone50ed302009-08-10 22:56:29 +00005294 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005295
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296 unsigned NumZero = 0;
5297 unsigned NumNonZero = 0;
5298 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005299 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005300 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005301 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005302 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005303 if (Elt.getOpcode() == ISD::UNDEF)
5304 continue;
5305 Values.insert(Elt);
5306 if (Elt.getOpcode() != ISD::Constant &&
5307 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005308 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005309 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005310 NumZero++;
5311 else {
5312 NonZeros |= (1 << i);
5313 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314 }
5315 }
5316
Chris Lattner97a2a562010-08-26 05:24:29 +00005317 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5318 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005319 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005320
Chris Lattner67f453a2008-03-09 05:42:06 +00005321 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005322 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005323 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005325
Chris Lattner62098042008-03-09 01:05:04 +00005326 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5327 // the value are obviously zero, truncate the value to i32 and do the
5328 // insertion that way. Only do this if the value is non-constant or if the
5329 // value is a constant being inserted into element 0. It is cheaper to do
5330 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005331 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005332 (!IsAllConstants || Idx == 0)) {
5333 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005334 // Handle SSE only.
5335 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5336 EVT VecVT = MVT::v4i32;
5337 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005338
Chris Lattner62098042008-03-09 01:05:04 +00005339 // Truncate the value (which may itself be a constant) to i32, and
5340 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005341 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005342 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005343 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005344
Chris Lattner62098042008-03-09 01:05:04 +00005345 // Now we have our 32-bit value zero extended in the low element of
5346 // a vector. If Idx != 0, swizzle it into place.
5347 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005348 SmallVector<int, 4> Mask;
5349 Mask.push_back(Idx);
5350 for (unsigned i = 1; i != VecElts; ++i)
5351 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005352 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005353 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005354 }
Craig Topper07a27622012-01-22 03:07:48 +00005355 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005356 }
5357 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005358
Chris Lattner19f79692008-03-08 22:59:52 +00005359 // If we have a constant or non-constant insertion into the low element of
5360 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5361 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005362 // depending on what the source datatype is.
5363 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005364 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005365 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005366
5367 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005368 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005369 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005370 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005371 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5372 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005373 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005374 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005375 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5376 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005377 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005378 }
5379
5380 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005381 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005382 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005383 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005384 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005385 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005386 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005387 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005388 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005389 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005390 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005391 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005392 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005393
5394 // Is it a vector logical left shift?
5395 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005396 X86::isZeroNode(Op.getOperand(0)) &&
5397 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005398 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005399 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005400 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005401 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005402 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005403 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005404
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005405 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005406 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407
Chris Lattner19f79692008-03-08 22:59:52 +00005408 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5409 // is a non-constant being inserted into an element other than the low one,
5410 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5411 // movd/movss) to move this into the low element, then shuffle it into
5412 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005413 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005414 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005415
Evan Cheng0db9fe62006-04-25 20:13:52 +00005416 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005417 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005418 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005419 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 MaskVec.push_back(i == Idx ? 0 : 1);
5421 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005422 }
5423 }
5424
Chris Lattner67f453a2008-03-09 05:42:06 +00005425 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005426 if (Values.size() == 1) {
5427 if (EVTBits == 32) {
5428 // Instead of a shuffle like this:
5429 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5430 // Check if it's possible to issue this instead.
5431 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5432 unsigned Idx = CountTrailingZeros_32(NonZeros);
5433 SDValue Item = Op.getOperand(Idx);
5434 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5435 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5436 }
Dan Gohman475871a2008-07-27 21:46:04 +00005437 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005438 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005439
Dan Gohmana3941172007-07-24 22:55:08 +00005440 // A vector full of immediates; various special cases are already
5441 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005442 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005443 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005444
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005445 // For AVX-length vectors, build the individual 128-bit pieces and use
5446 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005447 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005448 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005449 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005450 V.push_back(Op.getOperand(i));
5451
5452 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5453
5454 // Build both the lower and upper subvector.
5455 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5456 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5457 NumElems/2);
5458
5459 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005460 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005461 }
5462
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005463 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005464 if (EVTBits == 64) {
5465 if (NumNonZero == 1) {
5466 // One half is zero or undef.
5467 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005468 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005469 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005470 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005471 }
Dan Gohman475871a2008-07-27 21:46:04 +00005472 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005473 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005474
5475 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005476 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005477 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005478 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005479 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005480 }
5481
Bill Wendling826f36f2007-03-28 00:57:11 +00005482 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005483 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005484 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005485 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005486 }
5487
5488 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005489 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005490 if (NumElems == 4 && NumZero > 0) {
5491 for (unsigned i = 0; i < 4; ++i) {
5492 bool isZero = !(NonZeros & (1 << i));
5493 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005494 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005495 else
Dale Johannesenace16102009-02-03 19:33:06 +00005496 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005497 }
5498
5499 for (unsigned i = 0; i < 2; ++i) {
5500 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5501 default: break;
5502 case 0:
5503 V[i] = V[i*2]; // Must be a zero vector.
5504 break;
5505 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005506 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005507 break;
5508 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005509 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005510 break;
5511 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005512 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005513 break;
5514 }
5515 }
5516
Benjamin Kramer9c683542012-01-30 15:16:21 +00005517 bool Reverse1 = (NonZeros & 0x3) == 2;
5518 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5519 int MaskVec[] = {
5520 Reverse1 ? 1 : 0,
5521 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005522 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5523 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005524 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005525 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005526 }
5527
Craig Topper7a9a28b2012-08-12 02:23:29 +00005528 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005529 // Check for a build vector of consecutive loads.
5530 for (unsigned i = 0; i < NumElems; ++i)
5531 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005532
Nate Begemanfdea31a2010-03-24 20:49:50 +00005533 // Check for elements which are consecutive loads.
5534 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5535 if (LD.getNode())
5536 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005537
Michael Liaofacace82012-10-19 17:15:18 +00005538 // Check for a build vector from mostly shuffle plus few inserting.
5539 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5540 if (Sh.getNode())
5541 return Sh;
5542
Michael J. Spencerec38de22010-10-10 22:04:20 +00005543 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005544 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005545 SDValue Result;
5546 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5547 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5548 else
5549 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005550
Chris Lattner24faf612010-08-28 17:59:08 +00005551 for (unsigned i = 1; i < NumElems; ++i) {
5552 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5553 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005554 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005555 }
5556 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005557 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005558
Chris Lattner6e80e442010-08-28 17:15:43 +00005559 // Otherwise, expand into a number of unpckl*, start by extending each of
5560 // our (non-undef) elements to the full vector width with the element in the
5561 // bottom slot of the vector (which generates no code for SSE).
5562 for (unsigned i = 0; i < NumElems; ++i) {
5563 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5564 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5565 else
5566 V[i] = DAG.getUNDEF(VT);
5567 }
5568
5569 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005570 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5571 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5572 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005573 unsigned EltStride = NumElems >> 1;
5574 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005575 for (unsigned i = 0; i < EltStride; ++i) {
5576 // If V[i+EltStride] is undef and this is the first round of mixing,
5577 // then it is safe to just drop this shuffle: V[i] is already in the
5578 // right place, the one element (since it's the first round) being
5579 // inserted as undef can be dropped. This isn't safe for successive
5580 // rounds because they will permute elements within both vectors.
5581 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5582 EltStride == NumElems/2)
5583 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005584
Chris Lattner6e80e442010-08-28 17:15:43 +00005585 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005586 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005587 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005588 }
5589 return V[0];
5590 }
Dan Gohman475871a2008-07-27 21:46:04 +00005591 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005592}
5593
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005594// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5595// to create 256-bit vectors from two other 128-bit ones.
5596static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5597 DebugLoc dl = Op.getDebugLoc();
5598 EVT ResVT = Op.getValueType();
5599
Craig Topper7a9a28b2012-08-12 02:23:29 +00005600 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005601
5602 SDValue V1 = Op.getOperand(0);
5603 SDValue V2 = Op.getOperand(1);
5604 unsigned NumElems = ResVT.getVectorNumElements();
5605
Craig Topper4c7972d2012-04-22 18:15:59 +00005606 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005607}
5608
Craig Topper55b24052012-09-11 06:15:32 +00005609static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005610 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005611
5612 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5613 // from two other 128-bit ones.
5614 return LowerAVXCONCAT_VECTORS(Op, DAG);
5615}
5616
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005617// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005618static SDValue
5619LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5620 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005621 SDValue V1 = SVOp->getOperand(0);
5622 SDValue V2 = SVOp->getOperand(1);
5623 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005624 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005625 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005626
Nadav Roteme6113782012-04-11 06:40:27 +00005627 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005628 return SDValue();
5629
Craig Topper1842ba02012-04-23 06:38:28 +00005630 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005631 MVT OpTy;
5632
Craig Topper708e44f2012-04-23 07:36:33 +00005633 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005634 default: return SDValue();
5635 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005636 ISDNo = X86ISD::BLENDPW;
5637 OpTy = MVT::v8i16;
5638 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005639 case MVT::v4i32:
5640 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005641 ISDNo = X86ISD::BLENDPS;
5642 OpTy = MVT::v4f32;
5643 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005644 case MVT::v2i64:
5645 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005646 ISDNo = X86ISD::BLENDPD;
5647 OpTy = MVT::v2f64;
5648 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005649 case MVT::v8i32:
5650 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005651 if (!Subtarget->hasAVX())
5652 return SDValue();
5653 ISDNo = X86ISD::BLENDPS;
5654 OpTy = MVT::v8f32;
5655 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005656 case MVT::v4i64:
5657 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005658 if (!Subtarget->hasAVX())
5659 return SDValue();
5660 ISDNo = X86ISD::BLENDPD;
5661 OpTy = MVT::v4f64;
5662 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005663 }
5664 assert(ISDNo && "Invalid Op Number");
5665
5666 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005667
Craig Topper1842ba02012-04-23 06:38:28 +00005668 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005669 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005670 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005671 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005672 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005673 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005674 else
5675 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005676 }
5677
Nadav Roteme6113782012-04-11 06:40:27 +00005678 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5679 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5680 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5681 DAG.getConstant(MaskVals, MVT::i32));
5682 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005683}
5684
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685// v8i16 shuffles - Prefer shuffles in the following order:
5686// 1. [all] pshuflw, pshufhw, optional move
5687// 2. [ssse3] 1 x pshufb
5688// 3. [ssse3] 2 x pshufb + 1 x por
5689// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005690static SDValue
5691LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5692 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005693 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005694 SDValue V1 = SVOp->getOperand(0);
5695 SDValue V2 = SVOp->getOperand(1);
5696 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005698
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 // Determine if more than 1 of the words in each of the low and high quadwords
5700 // of the result come from the same quadword of one of the two inputs. Undef
5701 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005702 unsigned LoQuad[] = { 0, 0, 0, 0 };
5703 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005704 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005706 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005707 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 MaskVals.push_back(EltIdx);
5709 if (EltIdx < 0) {
5710 ++Quad[0];
5711 ++Quad[1];
5712 ++Quad[2];
5713 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005714 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 }
5716 ++Quad[EltIdx / 4];
5717 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005718 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005719
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005721 unsigned MaxQuad = 1;
5722 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 if (LoQuad[i] > MaxQuad) {
5724 BestLoQuad = i;
5725 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005726 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005727 }
5728
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005730 MaxQuad = 1;
5731 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 if (HiQuad[i] > MaxQuad) {
5733 BestHiQuad = i;
5734 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005735 }
5736 }
5737
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005739 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 // single pshufb instruction is necessary. If There are more than 2 input
5741 // quads, disable the next transformation since it does not help SSSE3.
5742 bool V1Used = InputQuads[0] || InputQuads[1];
5743 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005744 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005745 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005746 BestLoQuad = InputQuads[0] ? 0 : 1;
5747 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 }
5749 if (InputQuads.count() > 2) {
5750 BestLoQuad = -1;
5751 BestHiQuad = -1;
5752 }
5753 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005754
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5756 // the shuffle mask. If a quad is scored as -1, that means that it contains
5757 // words from all 4 input quadwords.
5758 SDValue NewV;
5759 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005760 int MaskV[] = {
5761 BestLoQuad < 0 ? 0 : BestLoQuad,
5762 BestHiQuad < 0 ? 1 : BestHiQuad
5763 };
Eric Christopherfd179292009-08-27 18:07:15 +00005764 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005765 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5766 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5767 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005768
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5770 // source words for the shuffle, to aid later transformations.
5771 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005772 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005773 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005775 if (idx != (int)i)
5776 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005777 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005778 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 AllWordsInNewV = false;
5780 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005781 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005782
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5784 if (AllWordsInNewV) {
5785 for (int i = 0; i != 8; ++i) {
5786 int idx = MaskVals[i];
5787 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005788 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005789 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 if ((idx != i) && idx < 4)
5791 pshufhw = false;
5792 if ((idx != i) && idx > 3)
5793 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005794 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 V1 = NewV;
5796 V2Used = false;
5797 BestLoQuad = 0;
5798 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005799 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005800
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5802 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005803 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005804 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5805 unsigned TargetMask = 0;
5806 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005807 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005808 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5809 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5810 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005811 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005812 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005813 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005814 }
Eric Christopherfd179292009-08-27 18:07:15 +00005815
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 // If we have SSSE3, and all words of the result are from 1 input vector,
5817 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5818 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005819 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005821
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005823 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 // mask, and elements that come from V1 in the V2 mask, so that the two
5825 // results can be OR'd together.
5826 bool TwoInputs = V1Used && V2Used;
5827 for (unsigned i = 0; i != 8; ++i) {
5828 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005829 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5830 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5831 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5832 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005833 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005834 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005835 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005836 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005837 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005838 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005839 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005840
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 // Calculate the shuffle mask for the second input, shuffle it, and
5842 // OR it with the first shuffled input.
5843 pshufbMask.clear();
5844 for (unsigned i = 0; i != 8; ++i) {
5845 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005846 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5847 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5848 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5849 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005850 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005851 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005852 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005853 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005854 MVT::v16i8, &pshufbMask[0], 16));
5855 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005856 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 }
5858
5859 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5860 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005861 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005863 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005864 for (int i = 0; i != 4; ++i) {
5865 int idx = MaskVals[i];
5866 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005867 InOrder.set(i);
5868 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005869 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 }
5872 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005874 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005875
Craig Topperdd637ae2012-02-19 05:41:45 +00005876 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5877 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005878 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005879 NewV.getOperand(0),
5880 getShufflePSHUFLWImmediate(SVOp), DAG);
5881 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 }
Eric Christopherfd179292009-08-27 18:07:15 +00005883
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5885 // and update MaskVals with the new element order.
5886 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005887 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 for (unsigned i = 4; i != 8; ++i) {
5889 int idx = MaskVals[i];
5890 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 InOrder.set(i);
5892 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005893 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005894 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 }
5896 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005898 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005899
Craig Topperdd637ae2012-02-19 05:41:45 +00005900 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5901 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005902 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005903 NewV.getOperand(0),
5904 getShufflePSHUFHWImmediate(SVOp), DAG);
5905 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 }
Eric Christopherfd179292009-08-27 18:07:15 +00005907
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 // In case BestHi & BestLo were both -1, which means each quadword has a word
5909 // from each of the four input quadwords, calculate the InOrder bitvector now
5910 // before falling through to the insert/extract cleanup.
5911 if (BestLoQuad == -1 && BestHiQuad == -1) {
5912 NewV = V1;
5913 for (int i = 0; i != 8; ++i)
5914 if (MaskVals[i] < 0 || MaskVals[i] == i)
5915 InOrder.set(i);
5916 }
Eric Christopherfd179292009-08-27 18:07:15 +00005917
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 // The other elements are put in the right place using pextrw and pinsrw.
5919 for (unsigned i = 0; i != 8; ++i) {
5920 if (InOrder[i])
5921 continue;
5922 int EltIdx = MaskVals[i];
5923 if (EltIdx < 0)
5924 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005925 SDValue ExtOp = (EltIdx < 8) ?
5926 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5927 DAG.getIntPtrConstant(EltIdx)) :
5928 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005930 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005931 DAG.getIntPtrConstant(i));
5932 }
5933 return NewV;
5934}
5935
5936// v16i8 shuffles - Prefer shuffles in the following order:
5937// 1. [ssse3] 1 x pshufb
5938// 2. [ssse3] 2 x pshufb + 1 x por
5939// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5940static
Nate Begeman9008ca62009-04-27 18:41:29 +00005941SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005942 SelectionDAG &DAG,
5943 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005944 SDValue V1 = SVOp->getOperand(0);
5945 SDValue V2 = SVOp->getOperand(1);
5946 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005947 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005948
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005950 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005952
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005954 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005956
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005958 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 //
5960 // Otherwise, we have elements from both input vectors, and must zero out
5961 // elements that come from V2 in the first mask, and V1 in the second mask
5962 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005963 for (unsigned i = 0; i != 16; ++i) {
5964 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005965 if (EltIdx < 0 || EltIdx >= 16)
5966 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005967 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005969 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005970 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005971 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005972
5973 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5974 // the 2nd operand if it's undefined or zero.
5975 if (V2.getOpcode() == ISD::UNDEF ||
5976 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005978
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 // Calculate the shuffle mask for the second input, shuffle it, and
5980 // OR it with the first shuffled input.
5981 pshufbMask.clear();
5982 for (unsigned i = 0; i != 16; ++i) {
5983 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005984 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005985 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005986 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005987 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005988 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 MVT::v16i8, &pshufbMask[0], 16));
5990 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005991 }
Eric Christopherfd179292009-08-27 18:07:15 +00005992
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 // No SSSE3 - Calculate in place words and then fix all out of place words
5994 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5995 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005996 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5997 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005998 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005999 for (int i = 0; i != 8; ++i) {
6000 int Elt0 = MaskVals[i*2];
6001 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006002
Nate Begemanb9a47b82009-02-23 08:49:38 +00006003 // This word of the result is all undef, skip it.
6004 if (Elt0 < 0 && Elt1 < 0)
6005 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006006
Nate Begemanb9a47b82009-02-23 08:49:38 +00006007 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006008 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006009 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006010
Nate Begemanb9a47b82009-02-23 08:49:38 +00006011 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6012 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6013 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006014
6015 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6016 // using a single extract together, load it and store it.
6017 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006018 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006019 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006020 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006021 DAG.getIntPtrConstant(i));
6022 continue;
6023 }
6024
Nate Begemanb9a47b82009-02-23 08:49:38 +00006025 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006026 // source byte is not also odd, shift the extracted word left 8 bits
6027 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006028 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006029 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006030 DAG.getIntPtrConstant(Elt1 / 2));
6031 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006033 DAG.getConstant(8,
6034 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006035 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006036 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6037 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006038 }
6039 // If Elt0 is defined, extract it from the appropriate source. If the
6040 // source byte is not also even, shift the extracted word right 8 bits. If
6041 // Elt1 was also defined, OR the extracted values together before
6042 // inserting them in the result.
6043 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006044 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6046 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006047 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006048 DAG.getConstant(8,
6049 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006050 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006051 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6052 DAG.getConstant(0x00FF, MVT::i16));
6053 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006054 : InsElt0;
6055 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006056 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006057 DAG.getIntPtrConstant(i));
6058 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006059 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006060}
6061
Elena Demikhovsky41789462012-09-06 12:42:01 +00006062// v32i8 shuffles - Translate to VPSHUFB if possible.
6063static
6064SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006065 const X86Subtarget *Subtarget,
6066 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006067 EVT VT = SVOp->getValueType(0);
6068 SDValue V1 = SVOp->getOperand(0);
6069 SDValue V2 = SVOp->getOperand(1);
6070 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006071 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006072
6073 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006074 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6075 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006076
Michael Liao471b9172012-10-03 23:43:52 +00006077 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006078 // (1) one of input vector is undefined or zeroinitializer.
6079 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6080 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006081 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006082 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006083 return SDValue();
6084
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006085 if (V1IsAllZero && !V2IsAllZero) {
6086 CommuteVectorShuffleMask(MaskVals, 32);
6087 V1 = V2;
6088 }
6089 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006090 for (unsigned i = 0; i != 32; i++) {
6091 int EltIdx = MaskVals[i];
6092 if (EltIdx < 0 || EltIdx >= 32)
6093 EltIdx = 0x80;
6094 else {
6095 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6096 // Cross lane is not allowed.
6097 return SDValue();
6098 EltIdx &= 0xf;
6099 }
6100 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6101 }
6102 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6103 DAG.getNode(ISD::BUILD_VECTOR, dl,
6104 MVT::v32i8, &pshufbMask[0], 32));
6105}
6106
Evan Cheng7a831ce2007-12-15 03:00:47 +00006107/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006108/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006109/// done when every pair / quad of shuffle mask elements point to elements in
6110/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006111/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006112static
Nate Begeman9008ca62009-04-27 18:41:29 +00006113SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006114 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006115 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006117 MVT NewVT;
6118 unsigned Scale;
6119 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006120 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006121 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6122 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6123 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6124 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6125 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6126 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006127 }
6128
Nate Begeman9008ca62009-04-27 18:41:29 +00006129 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006130 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006132 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006133 int EltIdx = SVOp->getMaskElt(i+j);
6134 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006135 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006136 if (StartIdx < 0)
6137 StartIdx = (EltIdx / Scale);
6138 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006139 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006140 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006141 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006142 }
6143
Craig Topper11ac1f82012-05-04 04:08:44 +00006144 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6145 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006146 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006147}
6148
Evan Chengd880b972008-05-09 21:53:03 +00006149/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006150///
Owen Andersone50ed302009-08-10 22:56:29 +00006151static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006152 SDValue SrcOp, SelectionDAG &DAG,
6153 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006154 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006155 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006156 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006157 LD = dyn_cast<LoadSDNode>(SrcOp);
6158 if (!LD) {
6159 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6160 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006161 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006162 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006163 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006164 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006165 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006166 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006167 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006168 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006169 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6170 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6171 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006172 SrcOp.getOperand(0)
6173 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006174 }
6175 }
6176 }
6177
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006178 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006179 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006180 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006181 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006182}
6183
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006184/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6185/// which could not be matched by any known target speficic shuffle
6186static SDValue
6187LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006188
6189 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6190 if (NewOp.getNode())
6191 return NewOp;
6192
Craig Topper8f35c132012-01-20 09:29:03 +00006193 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006194
Craig Topper8f35c132012-01-20 09:29:03 +00006195 unsigned NumElems = VT.getVectorNumElements();
6196 unsigned NumLaneElems = NumElems / 2;
6197
Craig Topper8f35c132012-01-20 09:29:03 +00006198 DebugLoc dl = SVOp->getDebugLoc();
6199 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006200 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006201 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006202
Craig Topper9a2b6e12012-04-06 07:45:23 +00006203 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006204 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006205 // Build a shuffle mask for the output, discovering on the fly which
6206 // input vectors to use as shuffle operands (recorded in InputUsed).
6207 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006208 // out with UseBuildVector set.
6209 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006210 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006211 unsigned LaneStart = l * NumLaneElems;
6212 for (unsigned i = 0; i != NumLaneElems; ++i) {
6213 // The mask element. This indexes into the input.
6214 int Idx = SVOp->getMaskElt(i+LaneStart);
6215 if (Idx < 0) {
6216 // the mask element does not index into any input vector.
6217 Mask.push_back(-1);
6218 continue;
6219 }
Craig Topper8f35c132012-01-20 09:29:03 +00006220
Craig Topper9a2b6e12012-04-06 07:45:23 +00006221 // The input vector this mask element indexes into.
6222 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006223
Craig Topper9a2b6e12012-04-06 07:45:23 +00006224 // Turn the index into an offset from the start of the input vector.
6225 Idx -= Input * NumLaneElems;
6226
6227 // Find or create a shuffle vector operand to hold this input.
6228 unsigned OpNo;
6229 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6230 if (InputUsed[OpNo] == Input)
6231 // This input vector is already an operand.
6232 break;
6233 if (InputUsed[OpNo] < 0) {
6234 // Create a new operand for this input vector.
6235 InputUsed[OpNo] = Input;
6236 break;
6237 }
6238 }
6239
6240 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006241 // More than two input vectors used! Give up on trying to create a
6242 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6243 UseBuildVector = true;
6244 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006245 }
6246
6247 // Add the mask index for the new shuffle vector.
6248 Mask.push_back(Idx + OpNo * NumLaneElems);
6249 }
6250
Craig Topper8ae97ba2012-05-21 06:40:16 +00006251 if (UseBuildVector) {
6252 SmallVector<SDValue, 16> SVOps;
6253 for (unsigned i = 0; i != NumLaneElems; ++i) {
6254 // The mask element. This indexes into the input.
6255 int Idx = SVOp->getMaskElt(i+LaneStart);
6256 if (Idx < 0) {
6257 SVOps.push_back(DAG.getUNDEF(EltVT));
6258 continue;
6259 }
6260
6261 // The input vector this mask element indexes into.
6262 int Input = Idx / NumElems;
6263
6264 // Turn the index into an offset from the start of the input vector.
6265 Idx -= Input * NumElems;
6266
6267 // Extract the vector element by hand.
6268 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6269 SVOp->getOperand(Input),
6270 DAG.getIntPtrConstant(Idx)));
6271 }
6272
6273 // Construct the output using a BUILD_VECTOR.
6274 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6275 SVOps.size());
6276 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006277 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006278 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006279 } else {
6280 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006281 (InputUsed[0] % 2) * NumLaneElems,
6282 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006283 // If only one input was used, use an undefined vector for the other.
6284 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6285 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006286 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006287 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006288 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006289 }
6290
6291 Mask.clear();
6292 }
Craig Topper8f35c132012-01-20 09:29:03 +00006293
6294 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006295 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006296}
6297
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006298/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6299/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006300static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006301LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006302 SDValue V1 = SVOp->getOperand(0);
6303 SDValue V2 = SVOp->getOperand(1);
6304 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006305 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006306
Craig Topper7a9a28b2012-08-12 02:23:29 +00006307 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006308
Benjamin Kramer9c683542012-01-30 15:16:21 +00006309 std::pair<int, int> Locs[4];
6310 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006311 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006312
Evan Chengace3c172008-07-22 21:13:36 +00006313 unsigned NumHi = 0;
6314 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006315 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006316 int Idx = PermMask[i];
6317 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006318 Locs[i] = std::make_pair(-1, -1);
6319 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006320 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6321 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006322 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006323 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006324 NumLo++;
6325 } else {
6326 Locs[i] = std::make_pair(1, NumHi);
6327 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006328 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006329 NumHi++;
6330 }
6331 }
6332 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006333
Evan Chengace3c172008-07-22 21:13:36 +00006334 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006335 // If no more than two elements come from either vector. This can be
6336 // implemented with two shuffles. First shuffle gather the elements.
6337 // The second shuffle, which takes the first shuffle as both of its
6338 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006339 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006340
Benjamin Kramer9c683542012-01-30 15:16:21 +00006341 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006342
Benjamin Kramer9c683542012-01-30 15:16:21 +00006343 for (unsigned i = 0; i != 4; ++i)
6344 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006345 unsigned Idx = (i < 2) ? 0 : 4;
6346 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006347 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006348 }
Evan Chengace3c172008-07-22 21:13:36 +00006349
Nate Begeman9008ca62009-04-27 18:41:29 +00006350 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006351 }
6352
6353 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006354 // Otherwise, we must have three elements from one vector, call it X, and
6355 // one element from the other, call it Y. First, use a shufps to build an
6356 // intermediate vector with the one element from Y and the element from X
6357 // that will be in the same half in the final destination (the indexes don't
6358 // matter). Then, use a shufps to build the final vector, taking the half
6359 // containing the element from Y from the intermediate, and the other half
6360 // from X.
6361 if (NumHi == 3) {
6362 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006363 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006364 std::swap(V1, V2);
6365 }
6366
6367 // Find the element from V2.
6368 unsigned HiIndex;
6369 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006370 int Val = PermMask[HiIndex];
6371 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006372 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006373 if (Val >= 4)
6374 break;
6375 }
6376
Nate Begeman9008ca62009-04-27 18:41:29 +00006377 Mask1[0] = PermMask[HiIndex];
6378 Mask1[1] = -1;
6379 Mask1[2] = PermMask[HiIndex^1];
6380 Mask1[3] = -1;
6381 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006382
6383 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006384 Mask1[0] = PermMask[0];
6385 Mask1[1] = PermMask[1];
6386 Mask1[2] = HiIndex & 1 ? 6 : 4;
6387 Mask1[3] = HiIndex & 1 ? 4 : 6;
6388 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006389 }
Craig Topper69947b92012-04-23 06:57:04 +00006390
6391 Mask1[0] = HiIndex & 1 ? 2 : 0;
6392 Mask1[1] = HiIndex & 1 ? 0 : 2;
6393 Mask1[2] = PermMask[2];
6394 Mask1[3] = PermMask[3];
6395 if (Mask1[2] >= 0)
6396 Mask1[2] += 4;
6397 if (Mask1[3] >= 0)
6398 Mask1[3] += 4;
6399 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006400 }
6401
6402 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006403 int LoMask[] = { -1, -1, -1, -1 };
6404 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006405
Benjamin Kramer9c683542012-01-30 15:16:21 +00006406 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006407 unsigned MaskIdx = 0;
6408 unsigned LoIdx = 0;
6409 unsigned HiIdx = 2;
6410 for (unsigned i = 0; i != 4; ++i) {
6411 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006412 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006413 MaskIdx = 1;
6414 LoIdx = 0;
6415 HiIdx = 2;
6416 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006417 int Idx = PermMask[i];
6418 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006419 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006420 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006421 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006422 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006423 LoIdx++;
6424 } else {
6425 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006426 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006427 HiIdx++;
6428 }
6429 }
6430
Nate Begeman9008ca62009-04-27 18:41:29 +00006431 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6432 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006433 int MaskOps[] = { -1, -1, -1, -1 };
6434 for (unsigned i = 0; i != 4; ++i)
6435 if (Locs[i].first != -1)
6436 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006437 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006438}
6439
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006440static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006441 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006442 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006443
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006444 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6445 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006446 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6447 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6448 // BUILD_VECTOR (load), undef
6449 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006450
6451 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006452}
6453
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006454// FIXME: the version above should always be used. Since there's
6455// a bug where several vector shuffles can't be folded because the
6456// DAG is not updated during lowering and a node claims to have two
6457// uses while it only has one, use this version, and let isel match
6458// another instruction if the load really happens to have more than
6459// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006460// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006461static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006462 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006463 V = V.getOperand(0);
6464 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6465 V = V.getOperand(0);
6466 if (ISD::isNormalLoad(V.getNode()))
6467 return true;
6468 return false;
6469}
6470
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006471static
Evan Cheng835580f2010-10-07 20:50:20 +00006472SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6473 EVT VT = Op.getValueType();
6474
6475 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006476 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6477 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006478 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6479 V1, DAG));
6480}
6481
6482static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006483SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006484 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006485 SDValue V1 = Op.getOperand(0);
6486 SDValue V2 = Op.getOperand(1);
6487 EVT VT = Op.getValueType();
6488
6489 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6490
Craig Topper1accb7e2012-01-10 06:54:16 +00006491 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006492 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6493
Evan Cheng0899f5c2011-08-31 02:05:24 +00006494 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6495 return DAG.getNode(ISD::BITCAST, dl, VT,
6496 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6497 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6498 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006499}
6500
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006501static
6502SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6503 SDValue V1 = Op.getOperand(0);
6504 SDValue V2 = Op.getOperand(1);
6505 EVT VT = Op.getValueType();
6506
6507 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6508 "unsupported shuffle type");
6509
6510 if (V2.getOpcode() == ISD::UNDEF)
6511 V2 = V1;
6512
6513 // v4i32 or v4f32
6514 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6515}
6516
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006517static
Craig Topper1accb7e2012-01-10 06:54:16 +00006518SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006519 SDValue V1 = Op.getOperand(0);
6520 SDValue V2 = Op.getOperand(1);
6521 EVT VT = Op.getValueType();
6522 unsigned NumElems = VT.getVectorNumElements();
6523
6524 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6525 // operand of these instructions is only memory, so check if there's a
6526 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6527 // same masks.
6528 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006529
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006530 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006531 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006532 CanFoldLoad = true;
6533
6534 // When V1 is a load, it can be folded later into a store in isel, example:
6535 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6536 // turns into:
6537 // (MOVLPSmr addr:$src1, VR128:$src2)
6538 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006539 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006540 CanFoldLoad = true;
6541
Dan Gohman65fd6562011-11-03 21:49:52 +00006542 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006543 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006544 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006545 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6546
6547 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006548 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006549 if (SVOp->getMaskElt(1) != -1)
6550 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006551 }
6552
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006553 // movl and movlp will both match v2i64, but v2i64 is never matched by
6554 // movl earlier because we make it strict to avoid messing with the movlp load
6555 // folding logic (see the code above getMOVLP call). Match it here then,
6556 // this is horrible, but will stay like this until we move all shuffle
6557 // matching to x86 specific nodes. Note that for the 1st condition all
6558 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006559 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006560 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6561 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006562 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006563 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006564 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006565 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006566
6567 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6568
6569 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006570 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006571 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006572}
6573
Michael Liaod9d09602012-10-23 17:34:00 +00006574// Reduce a vector shuffle to zext.
6575SDValue
6576X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6577 // PMOVZX is only available from SSE41.
6578 if (!Subtarget->hasSSE41())
6579 return SDValue();
6580
6581 EVT VT = Op.getValueType();
6582
6583 // Only AVX2 support 256-bit vector integer extending.
6584 if (!Subtarget->hasAVX2() && VT.is256BitVector())
6585 return SDValue();
6586
6587 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6588 DebugLoc DL = Op.getDebugLoc();
6589 SDValue V1 = Op.getOperand(0);
6590 SDValue V2 = Op.getOperand(1);
6591 unsigned NumElems = VT.getVectorNumElements();
6592
6593 // Extending is an unary operation and the element type of the source vector
6594 // won't be equal to or larger than i64.
6595 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6596 VT.getVectorElementType() == MVT::i64)
6597 return SDValue();
6598
6599 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6600 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006601 while ((1U << Shift) < NumElems) {
6602 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006603 break;
6604 Shift += 1;
6605 // The maximal ratio is 8, i.e. from i8 to i64.
6606 if (Shift > 3)
6607 return SDValue();
6608 }
6609
6610 // Check the shuffle mask.
6611 unsigned Mask = (1U << Shift) - 1;
6612 for (unsigned i = 0; i != NumElems; ++i) {
6613 int EltIdx = SVOp->getMaskElt(i);
6614 if ((i & Mask) != 0 && EltIdx != -1)
6615 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006616 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006617 return SDValue();
6618 }
6619
6620 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6621 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6622 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6623
6624 if (!isTypeLegal(NVT))
6625 return SDValue();
6626
6627 // Simplify the operand as it's prepared to be fed into shuffle.
6628 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6629 if (V1.getOpcode() == ISD::BITCAST &&
6630 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6631 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6632 V1.getOperand(0)
6633 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6634 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6635 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006636 ConstantSDNode *CIdx =
6637 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006638 // If it's foldable, i.e. normal load with single use, we will let code
6639 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006640 if (CIdx && CIdx->getZExtValue() == 0 &&
6641 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006642 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6643 }
6644
6645 return DAG.getNode(ISD::BITCAST, DL, VT,
6646 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6647}
6648
Nadav Rotem154819d2012-04-09 07:45:58 +00006649SDValue
6650X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006651 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6652 EVT VT = Op.getValueType();
6653 DebugLoc dl = Op.getDebugLoc();
6654 SDValue V1 = Op.getOperand(0);
6655 SDValue V2 = Op.getOperand(1);
6656
6657 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006658 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006659
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006660 // Handle splat operations
6661 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006662 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006663 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006664
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006665 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006666 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006667 if (Broadcast.getNode())
6668 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006669
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006670 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006671 if ((Size == 128 && NumElem <= 4) ||
6672 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006673 return SDValue();
6674
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006675 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006676 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006677 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006678
Michael Liaod9d09602012-10-23 17:34:00 +00006679 // Check integer expanding shuffles.
6680 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6681 if (NewOp.getNode())
6682 return NewOp;
6683
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006684 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6685 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006686 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6687 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006688 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6689 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006690 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006691 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006692 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006693 // FIXME: Figure out a cleaner way to do this.
6694 // Try to make use of movq to zero out the top part.
6695 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6696 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6697 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006698 EVT NewVT = NewOp.getValueType();
6699 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6700 NewVT, true, false))
6701 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006702 DAG, Subtarget, dl);
6703 }
6704 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6705 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006706 if (NewOp.getNode()) {
6707 EVT NewVT = NewOp.getValueType();
6708 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6709 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6710 DAG, Subtarget, dl);
6711 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006712 }
6713 }
6714 return SDValue();
6715}
6716
Dan Gohman475871a2008-07-27 21:46:04 +00006717SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006718X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006719 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006720 SDValue V1 = Op.getOperand(0);
6721 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006722 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006723 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006724 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006725 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006726 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006727 bool V1IsSplat = false;
6728 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006729 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006730 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006731 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006732 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006733 bool OptForSize = MF.getFunction()->getFnAttributes().
6734 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006735
Craig Topper3426a3e2011-11-14 06:46:21 +00006736 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006737
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006738 if (V1IsUndef && V2IsUndef)
6739 return DAG.getUNDEF(VT);
6740
6741 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006742
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006743 // Vector shuffle lowering takes 3 steps:
6744 //
6745 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6746 // narrowing and commutation of operands should be handled.
6747 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6748 // shuffle nodes.
6749 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6750 // so the shuffle can be broken into other shuffles and the legalizer can
6751 // try the lowering again.
6752 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006753 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006754 // be matched during isel, all of them must be converted to a target specific
6755 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006756
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006757 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6758 // narrowing and commutation of operands should be handled. The actual code
6759 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006760 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006761 if (NewOp.getNode())
6762 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006763
Craig Topper5aaffa82012-02-19 02:53:47 +00006764 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6765
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006766 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6767 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006768 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006769 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006770 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006771 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006772
Craig Topperdd637ae2012-02-19 05:41:45 +00006773 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006774 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006775 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006776
Craig Topperdd637ae2012-02-19 05:41:45 +00006777 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006778 return getMOVHighToLow(Op, dl, DAG);
6779
6780 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006781 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006782 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006783 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006784
Craig Topper5aaffa82012-02-19 02:53:47 +00006785 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006786 // The actual implementation will match the mask in the if above and then
6787 // during isel it can match several different instructions, not only pshufd
6788 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006789 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6790 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006791
Craig Topper5aaffa82012-02-19 02:53:47 +00006792 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006793
Craig Topperdbd98a42012-02-07 06:28:42 +00006794 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6795 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6796
Craig Topper1accb7e2012-01-10 06:54:16 +00006797 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006798 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6799
Craig Topperb3982da2011-12-31 23:50:21 +00006800 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006801 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006802 }
Eric Christopherfd179292009-08-27 18:07:15 +00006803
Evan Chengf26ffe92008-05-29 08:22:04 +00006804 // Check if this can be converted into a logical shift.
6805 bool isLeft = false;
6806 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006807 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006808 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006809 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006810 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006811 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006812 EVT EltVT = VT.getVectorElementType();
6813 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006814 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006815 }
Eric Christopherfd179292009-08-27 18:07:15 +00006816
Craig Topper5aaffa82012-02-19 02:53:47 +00006817 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006818 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006819 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006820 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006821 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006822 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6823
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006824 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006825 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6826 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006827 }
Eric Christopherfd179292009-08-27 18:07:15 +00006828
Nate Begeman9008ca62009-04-27 18:41:29 +00006829 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006830 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006831 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006832
Craig Topperdd637ae2012-02-19 05:41:45 +00006833 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006834 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006835
Craig Topperdd637ae2012-02-19 05:41:45 +00006836 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006837 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006838
Craig Topperdd637ae2012-02-19 05:41:45 +00006839 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006840 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006841
Craig Topperdd637ae2012-02-19 05:41:45 +00006842 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006843 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006844
Craig Topperdd637ae2012-02-19 05:41:45 +00006845 if (ShouldXformToMOVHLPS(M, VT) ||
6846 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006847 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006848
Evan Chengf26ffe92008-05-29 08:22:04 +00006849 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006850 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006851 EVT EltVT = VT.getVectorElementType();
6852 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006853 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006854 }
Eric Christopherfd179292009-08-27 18:07:15 +00006855
Evan Cheng9eca5e82006-10-25 21:49:50 +00006856 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006857 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6858 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006859 V1IsSplat = isSplatVector(V1.getNode());
6860 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006861
Chris Lattner8a594482007-11-25 00:24:49 +00006862 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006863 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6864 CommuteVectorShuffleMask(M, NumElems);
6865 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006866 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006867 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006868 }
6869
Craig Topperbeabc6c2011-12-05 06:56:46 +00006870 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006871 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006872 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006873 return V1;
6874 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6875 // the instruction selector will not match, so get a canonical MOVL with
6876 // swapped operands to undo the commute.
6877 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006878 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006879
Craig Topperbeabc6c2011-12-05 06:56:46 +00006880 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006881 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006882
Craig Topperbeabc6c2011-12-05 06:56:46 +00006883 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006884 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006885
Evan Cheng9bbbb982006-10-25 20:48:19 +00006886 if (V2IsSplat) {
6887 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006888 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006889 // new vector_shuffle with the corrected mask.p
6890 SmallVector<int, 8> NewMask(M.begin(), M.end());
6891 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006892 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006893 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006894 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006895 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006896 }
6897
Evan Cheng9eca5e82006-10-25 21:49:50 +00006898 if (Commuted) {
6899 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006900 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006901 CommuteVectorShuffleMask(M, NumElems);
6902 std::swap(V1, V2);
6903 std::swap(V1IsSplat, V2IsSplat);
6904 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006905
Craig Topper39a9e482012-02-11 06:24:48 +00006906 if (isUNPCKLMask(M, VT, HasAVX2))
6907 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006908
Craig Topper39a9e482012-02-11 06:24:48 +00006909 if (isUNPCKHMask(M, VT, HasAVX2))
6910 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006911 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006912
Nate Begeman9008ca62009-04-27 18:41:29 +00006913 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006914 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006915 return CommuteVectorShuffle(SVOp, DAG);
6916
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006917 // The checks below are all present in isShuffleMaskLegal, but they are
6918 // inlined here right now to enable us to directly emit target specific
6919 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006920
Craig Topper0e2037b2012-01-20 05:53:00 +00006921 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006922 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006923 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006924 DAG);
6925
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006926 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6927 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006928 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006929 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006930 }
6931
Craig Toppera9a568a2012-05-02 08:03:44 +00006932 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006933 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006934 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006935 DAG);
6936
Craig Toppera9a568a2012-05-02 08:03:44 +00006937 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006938 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006939 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006940 DAG);
6941
Craig Topper1a7700a2012-01-19 08:19:12 +00006942 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006943 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006944 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006945
Craig Topper94438ba2011-12-16 08:06:31 +00006946 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006947 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006948 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006949 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006950
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006951 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006952 // Generate target specific nodes for 128 or 256-bit shuffles only
6953 // supported in the AVX instruction set.
6954 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006955
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006956 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006957 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006958 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6959
Craig Topper70b883b2011-11-28 10:14:51 +00006960 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006961 if (isVPERMILPMask(M, VT, HasAVX)) {
6962 if (HasAVX2 && VT == MVT::v8i32)
6963 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006964 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006965 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006966 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006967 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006968
Craig Topper70b883b2011-11-28 10:14:51 +00006969 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006970 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006971 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006972 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006973
Craig Topper1842ba02012-04-23 06:38:28 +00006974 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006975 if (BlendOp.getNode())
6976 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006977
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006978 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006979 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006980 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006981 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006982 }
Craig Topper92040742012-04-16 06:43:40 +00006983 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6984 &permclMask[0], 8);
6985 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006986 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006987 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006988 }
Craig Topper095c5282012-04-15 23:48:57 +00006989
Craig Topper8325c112012-04-16 00:41:45 +00006990 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6991 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006992 getShuffleCLImmediate(SVOp), DAG);
6993
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006994
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006995 //===--------------------------------------------------------------------===//
6996 // Since no target specific shuffle was selected for this generic one,
6997 // lower it into other known shuffles. FIXME: this isn't true yet, but
6998 // this is the plan.
6999 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007000
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007001 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7002 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007003 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007004 if (NewOp.getNode())
7005 return NewOp;
7006 }
7007
7008 if (VT == MVT::v16i8) {
7009 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7010 if (NewOp.getNode())
7011 return NewOp;
7012 }
7013
Elena Demikhovsky41789462012-09-06 12:42:01 +00007014 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007015 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007016 if (NewOp.getNode())
7017 return NewOp;
7018 }
7019
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007020 // Handle all 128-bit wide vectors with 4 elements, and match them with
7021 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007022 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007023 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7024
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007025 // Handle general 256-bit shuffles
7026 if (VT.is256BitVector())
7027 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7028
Dan Gohman475871a2008-07-27 21:46:04 +00007029 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007030}
7031
Dan Gohman475871a2008-07-27 21:46:04 +00007032SDValue
7033X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007034 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007035 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007036 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007037
Craig Topper7a9a28b2012-08-12 02:23:29 +00007038 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007039 return SDValue();
7040
Duncan Sands83ec4b62008-06-06 12:08:01 +00007041 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007042 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007043 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007044 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007045 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007046 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007047 }
7048
7049 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007050 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7051 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7052 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7054 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007055 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007056 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007057 Op.getOperand(0)),
7058 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007060 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007062 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007063 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007064 }
7065
7066 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007067 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7068 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007069 // result has a single use which is a store or a bitcast to i32. And in
7070 // the case of a store, it's not worth it if the index is a constant 0,
7071 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007072 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007073 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007074 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007075 if ((User->getOpcode() != ISD::STORE ||
7076 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7077 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007078 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007080 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007081 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007082 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007083 Op.getOperand(0)),
7084 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007085 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007086 }
7087
7088 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007089 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007090 if (isa<ConstantSDNode>(Op.getOperand(1)))
7091 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007092 }
Dan Gohman475871a2008-07-27 21:46:04 +00007093 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007094}
7095
7096
Dan Gohman475871a2008-07-27 21:46:04 +00007097SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007098X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7099 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007100 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007101 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007102
David Greene74a579d2011-02-10 16:57:36 +00007103 SDValue Vec = Op.getOperand(0);
7104 EVT VecVT = Vec.getValueType();
7105
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007106 // If this is a 256-bit vector result, first extract the 128-bit vector and
7107 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007108 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007109 DebugLoc dl = Op.getNode()->getDebugLoc();
7110 unsigned NumElems = VecVT.getVectorNumElements();
7111 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007112 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7113
7114 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007115 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007116
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007117 if (IdxVal >= NumElems/2)
7118 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007119 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007120 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007121 }
7122
Craig Topper7a9a28b2012-08-12 02:23:29 +00007123 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007124
Craig Topperd0a31172012-01-10 06:37:29 +00007125 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007126 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007127 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007128 return Res;
7129 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007130
Owen Andersone50ed302009-08-10 22:56:29 +00007131 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007132 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007133 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007134 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007135 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007136 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007137 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7139 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007140 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007141 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007142 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007143 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007144 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007145 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007146 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007147 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007148 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007149 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007150 }
7151
7152 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007153 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154 if (Idx == 0)
7155 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007156
Evan Cheng0db9fe62006-04-25 20:13:52 +00007157 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007158 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007159 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007160 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007161 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007162 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007163 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007164 }
7165
7166 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007167 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7168 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7169 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007170 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007171 if (Idx == 0)
7172 return Op;
7173
7174 // UNPCKHPD the element to the lowest double word, then movsd.
7175 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7176 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007177 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007178 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007179 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007180 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007181 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007182 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007183 }
7184
Dan Gohman475871a2008-07-27 21:46:04 +00007185 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007186}
7187
Dan Gohman475871a2008-07-27 21:46:04 +00007188SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007189X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7190 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007191 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007192 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007193 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007194
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue N0 = Op.getOperand(0);
7196 SDValue N1 = Op.getOperand(1);
7197 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007198
Craig Topper7a9a28b2012-08-12 02:23:29 +00007199 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007200 return SDValue();
7201
Dan Gohman8a55ce42009-09-23 21:02:20 +00007202 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007203 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007204 unsigned Opc;
7205 if (VT == MVT::v8i16)
7206 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007207 else if (VT == MVT::v16i8)
7208 Opc = X86ISD::PINSRB;
7209 else
7210 Opc = X86ISD::PINSRB;
7211
Nate Begeman14d12ca2008-02-11 04:19:36 +00007212 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7213 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007214 if (N1.getValueType() != MVT::i32)
7215 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7216 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007217 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007218 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007219 }
7220
7221 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007222 // Bits [7:6] of the constant are the source select. This will always be
7223 // zero here. The DAG Combiner may combine an extract_elt index into these
7224 // bits. For example (insert (extract, 3), 2) could be matched by putting
7225 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007226 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007227 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007228 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007229 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007230 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007231 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007232 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007233 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007234 }
7235
7236 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007237 // PINSR* works with constant index.
7238 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007239 }
Dan Gohman475871a2008-07-27 21:46:04 +00007240 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007241}
7242
Dan Gohman475871a2008-07-27 21:46:04 +00007243SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007244X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007245 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007246 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007247
David Greene6b381262011-02-09 15:32:06 +00007248 DebugLoc dl = Op.getDebugLoc();
7249 SDValue N0 = Op.getOperand(0);
7250 SDValue N1 = Op.getOperand(1);
7251 SDValue N2 = Op.getOperand(2);
7252
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007253 // If this is a 256-bit vector result, first extract the 128-bit vector,
7254 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007255 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007256 if (!isa<ConstantSDNode>(N2))
7257 return SDValue();
7258
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007259 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007260 unsigned NumElems = VT.getVectorNumElements();
7261 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007262 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007263
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007264 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007265 bool Upper = IdxVal >= NumElems/2;
7266 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7267 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007268
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007269 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007270 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007271 }
7272
Craig Topperd0a31172012-01-10 06:37:29 +00007273 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007274 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7275
Dan Gohman8a55ce42009-09-23 21:02:20 +00007276 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007277 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007278
Dan Gohman8a55ce42009-09-23 21:02:20 +00007279 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007280 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7281 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007282 if (N1.getValueType() != MVT::i32)
7283 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7284 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007285 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007286 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007287 }
Dan Gohman475871a2008-07-27 21:46:04 +00007288 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007289}
7290
Craig Topper55b24052012-09-11 06:15:32 +00007291static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007292 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007293 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007294 EVT OpVT = Op.getValueType();
7295
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007296 // If this is a 256-bit vector result, first insert into a 128-bit
7297 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007298 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007299 // Insert into a 128-bit vector.
7300 EVT VT128 = EVT::getVectorVT(*Context,
7301 OpVT.getVectorElementType(),
7302 OpVT.getVectorNumElements() / 2);
7303
7304 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7305
7306 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007307 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007308 }
7309
Craig Topperd77d2fe2012-04-29 20:22:05 +00007310 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007311 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007312 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007313
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007315 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007316 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007317 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007318}
7319
David Greene91585092011-01-26 15:38:49 +00007320// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7321// a simple subregister reference or explicit instructions to grab
7322// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007323static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7324 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007325 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007326 DebugLoc dl = Op.getNode()->getDebugLoc();
7327 SDValue Vec = Op.getNode()->getOperand(0);
7328 SDValue Idx = Op.getNode()->getOperand(1);
7329
Craig Topper7a9a28b2012-08-12 02:23:29 +00007330 if (Op.getNode()->getValueType(0).is128BitVector() &&
7331 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007332 isa<ConstantSDNode>(Idx)) {
7333 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7334 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007335 }
David Greene91585092011-01-26 15:38:49 +00007336 }
7337 return SDValue();
7338}
7339
David Greenecfe33c42011-01-26 19:13:22 +00007340// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7341// simple superregister reference or explicit instructions to insert
7342// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007343static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7344 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007345 if (Subtarget->hasAVX()) {
7346 DebugLoc dl = Op.getNode()->getDebugLoc();
7347 SDValue Vec = Op.getNode()->getOperand(0);
7348 SDValue SubVec = Op.getNode()->getOperand(1);
7349 SDValue Idx = Op.getNode()->getOperand(2);
7350
Craig Topper7a9a28b2012-08-12 02:23:29 +00007351 if (Op.getNode()->getValueType(0).is256BitVector() &&
7352 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007353 isa<ConstantSDNode>(Idx)) {
7354 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7355 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007356 }
7357 }
7358 return SDValue();
7359}
7360
Bill Wendling056292f2008-09-16 21:48:12 +00007361// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7362// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7363// one of the above mentioned nodes. It has to be wrapped because otherwise
7364// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7365// be used to form addressing mode. These wrapped nodes will be selected
7366// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007367SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007368X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007369 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007370
Chris Lattner41621a22009-06-26 19:22:52 +00007371 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7372 // global base reg.
7373 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007374 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007375 CodeModel::Model M = getTargetMachine().getCodeModel();
7376
Chris Lattner4f066492009-07-11 20:29:19 +00007377 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007378 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007379 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007380 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007381 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007382 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007383 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007384
Evan Cheng1606e8e2009-03-13 07:51:59 +00007385 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007386 CP->getAlignment(),
7387 CP->getOffset(), OpFlag);
7388 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007389 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007390 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007391 if (OpFlag) {
7392 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007393 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007394 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007395 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007396 }
7397
7398 return Result;
7399}
7400
Dan Gohmand858e902010-04-17 15:26:15 +00007401SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007402 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007403
Chris Lattner18c59872009-06-27 04:16:01 +00007404 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7405 // global base reg.
7406 unsigned char OpFlag = 0;
7407 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007408 CodeModel::Model M = getTargetMachine().getCodeModel();
7409
Chris Lattner4f066492009-07-11 20:29:19 +00007410 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007411 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007412 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007413 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007414 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007415 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007416 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007417
Chris Lattner18c59872009-06-27 04:16:01 +00007418 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7419 OpFlag);
7420 DebugLoc DL = JT->getDebugLoc();
7421 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007422
Chris Lattner18c59872009-06-27 04:16:01 +00007423 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007424 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007425 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7426 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007427 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007428 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007429
Chris Lattner18c59872009-06-27 04:16:01 +00007430 return Result;
7431}
7432
7433SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007434X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007435 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007436
Chris Lattner18c59872009-06-27 04:16:01 +00007437 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7438 // global base reg.
7439 unsigned char OpFlag = 0;
7440 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007441 CodeModel::Model M = getTargetMachine().getCodeModel();
7442
Chris Lattner4f066492009-07-11 20:29:19 +00007443 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007444 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7445 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7446 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007447 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007448 } else if (Subtarget->isPICStyleGOT()) {
7449 OpFlag = X86II::MO_GOT;
7450 } else if (Subtarget->isPICStyleStubPIC()) {
7451 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7452 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7453 OpFlag = X86II::MO_DARWIN_NONLAZY;
7454 }
Eric Christopherfd179292009-08-27 18:07:15 +00007455
Chris Lattner18c59872009-06-27 04:16:01 +00007456 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007457
Chris Lattner18c59872009-06-27 04:16:01 +00007458 DebugLoc DL = Op.getDebugLoc();
7459 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007460
7461
Chris Lattner18c59872009-06-27 04:16:01 +00007462 // With PIC, the address is actually $g + Offset.
7463 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007464 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007465 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7466 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007467 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007468 Result);
7469 }
Eric Christopherfd179292009-08-27 18:07:15 +00007470
Eli Friedman586272d2011-08-11 01:48:05 +00007471 // For symbols that require a load from a stub to get the address, emit the
7472 // load.
7473 if (isGlobalStubReference(OpFlag))
7474 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007475 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007476
Chris Lattner18c59872009-06-27 04:16:01 +00007477 return Result;
7478}
7479
Dan Gohman475871a2008-07-27 21:46:04 +00007480SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007481X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007482 // Create the TargetBlockAddressAddress node.
7483 unsigned char OpFlags =
7484 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007485 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007486 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007487 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007488 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007489 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7490 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007491
Dan Gohmanf705adb2009-10-30 01:28:02 +00007492 if (Subtarget->isPICStyleRIPRel() &&
7493 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007494 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7495 else
7496 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007497
Dan Gohman29cbade2009-11-20 23:18:13 +00007498 // With PIC, the address is actually $g + Offset.
7499 if (isGlobalRelativeToPICBase(OpFlags)) {
7500 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7501 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7502 Result);
7503 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007504
7505 return Result;
7506}
7507
7508SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007509X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007510 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007511 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007512 // Create the TargetGlobalAddress node, folding in the constant
7513 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007514 unsigned char OpFlags =
7515 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007516 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007517 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007518 if (OpFlags == X86II::MO_NO_FLAG &&
7519 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007520 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007521 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007522 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007523 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007524 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007525 }
Eric Christopherfd179292009-08-27 18:07:15 +00007526
Chris Lattner4f066492009-07-11 20:29:19 +00007527 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007528 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007529 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7530 else
7531 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007532
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007533 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007534 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007535 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7536 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007537 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007539
Chris Lattner36c25012009-07-10 07:34:39 +00007540 // For globals that require a load from a stub to get the address, emit the
7541 // load.
7542 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007543 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007544 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007545
Dan Gohman6520e202008-10-18 02:06:02 +00007546 // If there was a non-zero offset that we didn't fold, create an explicit
7547 // addition for it.
7548 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007549 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007550 DAG.getConstant(Offset, getPointerTy()));
7551
Evan Cheng0db9fe62006-04-25 20:13:52 +00007552 return Result;
7553}
7554
Evan Chengda43bcf2008-09-24 00:05:32 +00007555SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007556X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007557 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007558 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007559 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007560}
7561
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007562static SDValue
7563GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007564 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007565 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007566 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007567 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007568 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007569 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007570 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007571 GA->getOffset(),
7572 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007573
7574 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7575 : X86ISD::TLSADDR;
7576
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007577 if (InFlag) {
7578 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007579 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007580 } else {
7581 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007582 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007583 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007584
7585 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007586 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007587
Rafael Espindola15f1b662009-04-24 12:59:40 +00007588 SDValue Flag = Chain.getValue(1);
7589 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007590}
7591
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007592// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007593static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007594LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007595 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007596 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007597 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7598 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007599 DAG.getNode(X86ISD::GlobalBaseReg,
7600 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007601 InFlag = Chain.getValue(1);
7602
Chris Lattnerb903bed2009-06-26 21:20:29 +00007603 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007604}
7605
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007606// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007607static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007608LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007609 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007610 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7611 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007612}
7613
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007614static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7615 SelectionDAG &DAG,
7616 const EVT PtrVT,
7617 bool is64Bit) {
7618 DebugLoc dl = GA->getDebugLoc();
7619
7620 // Get the start address of the TLS block for this module.
7621 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7622 .getInfo<X86MachineFunctionInfo>();
7623 MFI->incNumLocalDynamicTLSAccesses();
7624
7625 SDValue Base;
7626 if (is64Bit) {
7627 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7628 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7629 } else {
7630 SDValue InFlag;
7631 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7632 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7633 InFlag = Chain.getValue(1);
7634 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7635 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7636 }
7637
7638 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7639 // of Base.
7640
7641 // Build x@dtpoff.
7642 unsigned char OperandFlags = X86II::MO_DTPOFF;
7643 unsigned WrapperKind = X86ISD::Wrapper;
7644 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7645 GA->getValueType(0),
7646 GA->getOffset(), OperandFlags);
7647 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7648
7649 // Add x@dtpoff with the base.
7650 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7651}
7652
Hans Wennborg228756c2012-05-11 10:11:01 +00007653// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007654static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007655 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007656 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007657 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007658
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007659 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7660 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7661 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007662
Michael J. Spencerec38de22010-10-10 22:04:20 +00007663 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007664 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007665 MachinePointerInfo(Ptr),
7666 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007667
Chris Lattnerb903bed2009-06-26 21:20:29 +00007668 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007669 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7670 // initialexec.
7671 unsigned WrapperKind = X86ISD::Wrapper;
7672 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007673 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007674 } else if (model == TLSModel::InitialExec) {
7675 if (is64Bit) {
7676 OperandFlags = X86II::MO_GOTTPOFF;
7677 WrapperKind = X86ISD::WrapperRIP;
7678 } else {
7679 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7680 }
Chris Lattner18c59872009-06-27 04:16:01 +00007681 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007682 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007683 }
Eric Christopherfd179292009-08-27 18:07:15 +00007684
Hans Wennborg228756c2012-05-11 10:11:01 +00007685 // emit "addl x@ntpoff,%eax" (local exec)
7686 // or "addl x@indntpoff,%eax" (initial exec)
7687 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007688 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007689 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007690 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007691 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007692
Hans Wennborg228756c2012-05-11 10:11:01 +00007693 if (model == TLSModel::InitialExec) {
7694 if (isPIC && !is64Bit) {
7695 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7696 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7697 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007698 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007699
7700 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7701 MachinePointerInfo::getGOT(), false, false, false,
7702 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007703 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007704
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007705 // The address of the thread local variable is the add of the thread
7706 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007707 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007708}
7709
Dan Gohman475871a2008-07-27 21:46:04 +00007710SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007711X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007712
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007713 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007714 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007715
Eric Christopher30ef0e52010-06-03 04:07:48 +00007716 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007717 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007718
Eric Christopher30ef0e52010-06-03 04:07:48 +00007719 switch (model) {
7720 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007721 if (Subtarget->is64Bit())
7722 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7723 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007724 case TLSModel::LocalDynamic:
7725 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7726 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007727 case TLSModel::InitialExec:
7728 case TLSModel::LocalExec:
7729 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007730 Subtarget->is64Bit(),
7731 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007732 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007733 llvm_unreachable("Unknown TLS model.");
7734 }
7735
7736 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007737 // Darwin only has one model of TLS. Lower to that.
7738 unsigned char OpFlag = 0;
7739 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7740 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007741
Eric Christopher30ef0e52010-06-03 04:07:48 +00007742 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7743 // global base reg.
7744 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7745 !Subtarget->is64Bit();
7746 if (PIC32)
7747 OpFlag = X86II::MO_TLVP_PIC_BASE;
7748 else
7749 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007750 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007751 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007752 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007753 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007754 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007755
Eric Christopher30ef0e52010-06-03 04:07:48 +00007756 // With PIC32, the address is actually $g + Offset.
7757 if (PIC32)
7758 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7759 DAG.getNode(X86ISD::GlobalBaseReg,
7760 DebugLoc(), getPointerTy()),
7761 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007762
Eric Christopher30ef0e52010-06-03 04:07:48 +00007763 // Lowering the machine isd will make sure everything is in the right
7764 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007765 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007766 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007767 SDValue Args[] = { Chain, Offset };
7768 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007769
Eric Christopher30ef0e52010-06-03 04:07:48 +00007770 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7771 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7772 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007773
Eric Christopher30ef0e52010-06-03 04:07:48 +00007774 // And our return value (tls address) is in the standard call return value
7775 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007776 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007777 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7778 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007779 }
7780
7781 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007782 // Just use the implicit TLS architecture
7783 // Need to generate someting similar to:
7784 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7785 // ; from TEB
7786 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7787 // mov rcx, qword [rdx+rcx*8]
7788 // mov eax, .tls$:tlsvar
7789 // [rax+rcx] contains the address
7790 // Windows 64bit: gs:0x58
7791 // Windows 32bit: fs:__tls_array
7792
7793 // If GV is an alias then use the aliasee for determining
7794 // thread-localness.
7795 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7796 GV = GA->resolveAliasedGlobal(false);
7797 DebugLoc dl = GA->getDebugLoc();
7798 SDValue Chain = DAG.getEntryNode();
7799
7800 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7801 // %gs:0x58 (64-bit).
7802 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7803 ? Type::getInt8PtrTy(*DAG.getContext(),
7804 256)
7805 : Type::getInt32PtrTy(*DAG.getContext(),
7806 257));
7807
7808 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7809 Subtarget->is64Bit()
7810 ? DAG.getIntPtrConstant(0x58)
7811 : DAG.getExternalSymbol("_tls_array",
7812 getPointerTy()),
7813 MachinePointerInfo(Ptr),
7814 false, false, false, 0);
7815
7816 // Load the _tls_index variable
7817 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7818 if (Subtarget->is64Bit())
7819 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7820 IDX, MachinePointerInfo(), MVT::i32,
7821 false, false, 0);
7822 else
7823 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7824 false, false, false, 0);
7825
Micah Villmow2c39b152012-10-15 16:24:29 +00007826 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize(0)),
Craig Topper0fbf3642012-04-23 03:28:34 +00007827 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007828 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7829
7830 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7831 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7832 false, false, false, 0);
7833
7834 // Get the offset of start of .tls section
7835 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7836 GA->getValueType(0),
7837 GA->getOffset(), X86II::MO_SECREL);
7838 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7839
7840 // The address of the thread local variable is the add of the thread
7841 // pointer with the offset of the variable.
7842 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007843 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007844
David Blaikie4d6ccb52012-01-20 21:51:11 +00007845 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007846}
7847
Evan Cheng0db9fe62006-04-25 20:13:52 +00007848
Chad Rosierb90d2a92012-01-03 23:19:12 +00007849/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7850/// and take a 2 x i32 value to shift plus a shift amount.
7851SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007852 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007853 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007854 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007855 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007856 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007857 SDValue ShOpLo = Op.getOperand(0);
7858 SDValue ShOpHi = Op.getOperand(1);
7859 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007860 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007861 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007862 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007863
Dan Gohman475871a2008-07-27 21:46:04 +00007864 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007865 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007866 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7867 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007868 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007869 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7870 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007871 }
Evan Chenge3413162006-01-09 18:33:28 +00007872
Owen Anderson825b72b2009-08-11 20:47:22 +00007873 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7874 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007875 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007877
Dan Gohman475871a2008-07-27 21:46:04 +00007878 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007879 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007880 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7881 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007882
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007883 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007884 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7885 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007886 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007887 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7888 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007889 }
7890
Dan Gohman475871a2008-07-27 21:46:04 +00007891 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007892 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007893}
Evan Chenga3195e82006-01-12 22:54:21 +00007894
Dan Gohmand858e902010-04-17 15:26:15 +00007895SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7896 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007897 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007898
Dale Johannesen0488fb62010-09-30 23:57:10 +00007899 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007900 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007901
Owen Anderson825b72b2009-08-11 20:47:22 +00007902 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007903 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007904
Eli Friedman36df4992009-05-27 00:47:34 +00007905 // These are really Legal; return the operand so the caller accepts it as
7906 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007907 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007908 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007909 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007910 Subtarget->is64Bit()) {
7911 return Op;
7912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007913
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007914 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007915 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007916 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007917 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007918 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007919 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007920 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007921 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007922 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007923 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7924}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007925
Owen Andersone50ed302009-08-10 22:56:29 +00007926SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007927 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007928 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007929 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007930 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007931 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007932 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007933 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007934 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007935 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007936 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007937
Chris Lattner492a43e2010-09-22 01:28:21 +00007938 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007939
Stuart Hastings84be9582011-06-02 15:57:11 +00007940 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7941 MachineMemOperand *MMO;
7942 if (FI) {
7943 int SSFI = FI->getIndex();
7944 MMO =
7945 DAG.getMachineFunction()
7946 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7947 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7948 } else {
7949 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7950 StackSlot = StackSlot.getOperand(1);
7951 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007952 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007953 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7954 X86ISD::FILD, DL,
7955 Tys, Ops, array_lengthof(Ops),
7956 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007957
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007958 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007959 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007960 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007961
7962 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7963 // shouldn't be necessary except that RFP cannot be live across
7964 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007965 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007966 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7967 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007968 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007969 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007970 SDValue Ops[] = {
7971 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7972 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007973 MachineMemOperand *MMO =
7974 DAG.getMachineFunction()
7975 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007976 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007977
Chris Lattner492a43e2010-09-22 01:28:21 +00007978 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7979 Ops, array_lengthof(Ops),
7980 Op.getValueType(), MMO);
7981 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007982 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007983 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007984 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007985
Evan Cheng0db9fe62006-04-25 20:13:52 +00007986 return Result;
7987}
7988
Bill Wendling8b8a6362009-01-17 03:56:04 +00007989// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007990SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7991 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007992 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007993 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007994 movq %rax, %xmm0
7995 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7996 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7997 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007998 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007999 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008000 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008001 addpd %xmm1, %xmm0
8002 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008003 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008004
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008005 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008006 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008007
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008008 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008009 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8010 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008011 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008012
Chris Lattner97484792012-01-25 09:56:22 +00008013 SmallVector<Constant*,2> CV1;
8014 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008015 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008016 CV1.push_back(
8017 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8018 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008019 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008020
Bill Wendling397ae212012-01-05 02:13:20 +00008021 // Load the 64-bit value into an XMM register.
8022 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8023 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008024 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008025 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008026 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008027 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8028 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8029 CLod0);
8030
Owen Anderson825b72b2009-08-11 20:47:22 +00008031 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008032 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008033 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008034 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008035 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008036 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008037
Craig Topperd0a31172012-01-10 06:37:29 +00008038 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008039 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8040 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8041 } else {
8042 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8043 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8044 S2F, 0x4E, DAG);
8045 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8046 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8047 Sub);
8048 }
8049
8050 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008051 DAG.getIntPtrConstant(0));
8052}
8053
Bill Wendling8b8a6362009-01-17 03:56:04 +00008054// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008055SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8056 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008057 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008058 // FP constant to bias correct the final result.
8059 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008060 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008061
8062 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008064 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008065
Eli Friedmanf3704762011-08-29 21:15:46 +00008066 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008067 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008068
Owen Anderson825b72b2009-08-11 20:47:22 +00008069 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008070 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008071 DAG.getIntPtrConstant(0));
8072
8073 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008074 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008075 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008076 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008078 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008079 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 MVT::v2f64, Bias)));
8081 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008082 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008083 DAG.getIntPtrConstant(0));
8084
8085 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008086 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008087
8088 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008089 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008090
Craig Topper69947b92012-04-23 06:57:04 +00008091 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008092 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008093 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008094 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008095 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008096
8097 // Handle final rounding.
8098 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008099}
8100
Michael Liaoa7554632012-10-23 17:36:08 +00008101SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8102 SelectionDAG &DAG) const {
8103 SDValue N0 = Op.getOperand(0);
8104 EVT SVT = N0.getValueType();
8105 DebugLoc dl = Op.getDebugLoc();
8106
8107 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8108 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8109 "Custom UINT_TO_FP is not supported!");
8110
8111 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8112 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8113 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8114}
8115
Dan Gohmand858e902010-04-17 15:26:15 +00008116SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8117 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008118 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008119 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008120
Michael Liaoa7554632012-10-23 17:36:08 +00008121 if (Op.getValueType().isVector())
8122 return lowerUINT_TO_FP_vec(Op, DAG);
8123
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008124 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008125 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8126 // the optimization here.
8127 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008128 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008129
Owen Andersone50ed302009-08-10 22:56:29 +00008130 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008131 EVT DstVT = Op.getValueType();
8132 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008133 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008134 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008135 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008136 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008137 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008138
8139 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008140 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008141 if (SrcVT == MVT::i32) {
8142 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8143 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8144 getPointerTy(), StackSlot, WordOff);
8145 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008146 StackSlot, MachinePointerInfo(),
8147 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008148 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008149 OffsetSlot, MachinePointerInfo(),
8150 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008151 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8152 return Fild;
8153 }
8154
8155 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8156 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008157 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008158 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008159 // For i64 source, we need to add the appropriate power of 2 if the input
8160 // was negative. This is the same as the optimization in
8161 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8162 // we must be careful to do the computation in x87 extended precision, not
8163 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008164 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8165 MachineMemOperand *MMO =
8166 DAG.getMachineFunction()
8167 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8168 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008169
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008170 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8171 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008172 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8173 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008174
8175 APInt FF(32, 0x5F800000ULL);
8176
8177 // Check whether the sign bit is set.
8178 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8179 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8180 ISD::SETLT);
8181
8182 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8183 SDValue FudgePtr = DAG.getConstantPool(
8184 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8185 getPointerTy());
8186
8187 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8188 SDValue Zero = DAG.getIntPtrConstant(0);
8189 SDValue Four = DAG.getIntPtrConstant(4);
8190 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8191 Zero, Four);
8192 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8193
8194 // Load the value out, extending it from f32 to f80.
8195 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008196 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008197 FudgePtr, MachinePointerInfo::getConstantPool(),
8198 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008199 // Extend everything to 80 bits to force it to be done on x87.
8200 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8201 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008202}
8203
Dan Gohman475871a2008-07-27 21:46:04 +00008204std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008205FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008206 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008207
Owen Andersone50ed302009-08-10 22:56:29 +00008208 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008209
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008210 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008211 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8212 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008213 }
8214
Owen Anderson825b72b2009-08-11 20:47:22 +00008215 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8216 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008217 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008218
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008219 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008220 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008221 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008222 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008223 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008224 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008225 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008226 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008227
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008228 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8229 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008230 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008231 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008232 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008233 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008234
Evan Cheng0db9fe62006-04-25 20:13:52 +00008235 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008236 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8237 Opc = X86ISD::WIN_FTOL;
8238 else
8239 switch (DstTy.getSimpleVT().SimpleTy) {
8240 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8241 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8242 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8243 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8244 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008245
Dan Gohman475871a2008-07-27 21:46:04 +00008246 SDValue Chain = DAG.getEntryNode();
8247 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008248 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008249 // FIXME This causes a redundant load/store if the SSE-class value is already
8250 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008251 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008252 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008253 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008254 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008255 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008256 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008257 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008258 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008259 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008260
Chris Lattner492a43e2010-09-22 01:28:21 +00008261 MachineMemOperand *MMO =
8262 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8263 MachineMemOperand::MOLoad, MemSize, MemSize);
8264 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8265 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008266 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008267 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008268 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8269 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008270
Chris Lattner07290932010-09-22 01:05:16 +00008271 MachineMemOperand *MMO =
8272 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8273 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008274
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008275 if (Opc != X86ISD::WIN_FTOL) {
8276 // Build the FP_TO_INT*_IN_MEM
8277 SDValue Ops[] = { Chain, Value, StackSlot };
8278 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8279 Ops, 3, DstTy, MMO);
8280 return std::make_pair(FIST, StackSlot);
8281 } else {
8282 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8283 DAG.getVTList(MVT::Other, MVT::Glue),
8284 Chain, Value);
8285 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8286 MVT::i32, ftol.getValue(1));
8287 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8288 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008289 SDValue Ops[] = { eax, edx };
8290 SDValue pair = IsReplace
8291 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8292 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008293 return std::make_pair(pair, SDValue());
8294 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008295}
8296
Michael Liaoa7554632012-10-23 17:36:08 +00008297SDValue X86TargetLowering::lowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const {
8298 DebugLoc DL = Op.getDebugLoc();
8299 EVT VT = Op.getValueType();
8300 SDValue In = Op.getOperand(0);
8301 EVT SVT = In.getValueType();
8302
8303 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8304 VT.getVectorNumElements() != SVT.getVectorNumElements())
8305 return SDValue();
8306
8307 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8308
8309 // AVX2 has better support of integer extending.
8310 if (Subtarget->hasAVX2())
8311 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8312
8313 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8314 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8315 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
8316 DAG.getVectorShuffle(MVT::v8i16, DL, In, DAG.getUNDEF(MVT::v8i16), &Mask[0]));
8317
8318 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8319}
8320
Michael Liaobedcbd42012-10-16 18:14:11 +00008321SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8322 DebugLoc DL = Op.getDebugLoc();
8323 EVT VT = Op.getValueType();
8324 EVT SVT = Op.getOperand(0).getValueType();
8325
8326 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8327 VT.getVectorNumElements() != SVT.getVectorNumElements())
8328 return SDValue();
8329
8330 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8331
8332 unsigned NumElems = VT.getVectorNumElements();
8333 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8334 NumElems * 2);
8335
8336 SDValue In = Op.getOperand(0);
8337 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8338 // Prepare truncation shuffle mask
8339 for (unsigned i = 0; i != NumElems; ++i)
8340 MaskVec[i] = i * 2;
8341 SDValue V = DAG.getVectorShuffle(NVT, DL,
8342 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8343 DAG.getUNDEF(NVT), &MaskVec[0]);
8344 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8345 DAG.getIntPtrConstant(0));
8346}
8347
Dan Gohmand858e902010-04-17 15:26:15 +00008348SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8349 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008350 if (Op.getValueType().isVector()) {
8351 if (Op.getValueType() == MVT::v8i16)
8352 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8353 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8354 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008355 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008356 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008357
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008358 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8359 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008360 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008361 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8362 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008363
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008364 if (StackSlot.getNode())
8365 // Load the result.
8366 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8367 FIST, StackSlot, MachinePointerInfo(),
8368 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008369
8370 // The node is the result.
8371 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008372}
8373
Dan Gohmand858e902010-04-17 15:26:15 +00008374SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8375 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008376 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8377 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008378 SDValue FIST = Vals.first, StackSlot = Vals.second;
8379 assert(FIST.getNode() && "Unexpected failure");
8380
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008381 if (StackSlot.getNode())
8382 // Load the result.
8383 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8384 FIST, StackSlot, MachinePointerInfo(),
8385 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008386
8387 // The node is the result.
8388 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008389}
8390
Michael Liao9d796db2012-10-10 16:32:15 +00008391SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8392 SelectionDAG &DAG) const {
8393 DebugLoc DL = Op.getDebugLoc();
8394 EVT VT = Op.getValueType();
8395 SDValue In = Op.getOperand(0);
8396 EVT SVT = In.getValueType();
8397
8398 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8399
8400 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8401 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8402 In, DAG.getUNDEF(SVT)));
8403}
8404
Craig Topper43620672012-09-08 07:31:51 +00008405SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008406 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008407 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008408 EVT VT = Op.getValueType();
8409 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008410 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8411 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008412 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008413 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008414 }
Craig Topper43620672012-09-08 07:31:51 +00008415 Constant *C;
8416 if (EltVT == MVT::f64)
8417 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8418 else
8419 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8420 C = ConstantVector::getSplat(NumElts, C);
8421 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8422 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008423 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008424 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008425 false, false, false, Alignment);
8426 if (VT.isVector()) {
8427 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8428 return DAG.getNode(ISD::BITCAST, dl, VT,
8429 DAG.getNode(ISD::AND, dl, ANDVT,
8430 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8431 Op.getOperand(0)),
8432 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8433 }
Dale Johannesenace16102009-02-03 19:33:06 +00008434 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008435}
8436
Dan Gohmand858e902010-04-17 15:26:15 +00008437SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008438 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008439 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008440 EVT VT = Op.getValueType();
8441 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008442 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8443 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008444 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008445 NumElts = VT.getVectorNumElements();
8446 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008447 Constant *C;
8448 if (EltVT == MVT::f64)
8449 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8450 else
8451 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8452 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008453 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8454 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008455 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008456 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008457 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008458 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008459 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008460 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008461 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008462 DAG.getNode(ISD::BITCAST, dl, XORVT,
8463 Op.getOperand(0)),
8464 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008465 }
Craig Topper69947b92012-04-23 06:57:04 +00008466
8467 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008468}
8469
Dan Gohmand858e902010-04-17 15:26:15 +00008470SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008471 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008472 SDValue Op0 = Op.getOperand(0);
8473 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008474 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008475 EVT VT = Op.getValueType();
8476 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008477
8478 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008479 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008480 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008481 SrcVT = VT;
8482 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008483 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008484 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008485 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008486 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008487 }
8488
8489 // At this point the operands and the result should have the same
8490 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008491
Evan Cheng68c47cb2007-01-05 07:55:56 +00008492 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008493 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008494 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008495 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8496 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008497 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008498 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8499 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8500 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8501 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008502 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008503 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008504 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008505 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008506 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008507 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008508 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008509
8510 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008511 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008512 // Op0 is MVT::f32, Op1 is MVT::f64.
8513 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8514 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8515 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008516 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008517 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008518 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008519 }
8520
Evan Cheng73d6cf12007-01-05 21:37:56 +00008521 // Clear first operand sign bit.
8522 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008523 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008524 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8525 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008526 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008527 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8528 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8529 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8530 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008531 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008532 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008533 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008534 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008535 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008536 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008537 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008538
8539 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008540 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008541}
8542
Craig Topper55b24052012-09-11 06:15:32 +00008543static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008544 SDValue N0 = Op.getOperand(0);
8545 DebugLoc dl = Op.getDebugLoc();
8546 EVT VT = Op.getValueType();
8547
8548 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8549 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8550 DAG.getConstant(1, VT));
8551 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8552}
8553
Michael Liaof966e4e2012-09-13 20:24:54 +00008554// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8555//
8556SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8557 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8558
8559 if (!Subtarget->hasSSE41())
8560 return SDValue();
8561
8562 if (!Op->hasOneUse())
8563 return SDValue();
8564
8565 SDNode *N = Op.getNode();
8566 DebugLoc DL = N->getDebugLoc();
8567
8568 SmallVector<SDValue, 8> Opnds;
8569 DenseMap<SDValue, unsigned> VecInMap;
8570 EVT VT = MVT::Other;
8571
8572 // Recognize a special case where a vector is casted into wide integer to
8573 // test all 0s.
8574 Opnds.push_back(N->getOperand(0));
8575 Opnds.push_back(N->getOperand(1));
8576
8577 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8578 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8579 // BFS traverse all OR'd operands.
8580 if (I->getOpcode() == ISD::OR) {
8581 Opnds.push_back(I->getOperand(0));
8582 Opnds.push_back(I->getOperand(1));
8583 // Re-evaluate the number of nodes to be traversed.
8584 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8585 continue;
8586 }
8587
8588 // Quit if a non-EXTRACT_VECTOR_ELT
8589 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8590 return SDValue();
8591
8592 // Quit if without a constant index.
8593 SDValue Idx = I->getOperand(1);
8594 if (!isa<ConstantSDNode>(Idx))
8595 return SDValue();
8596
8597 SDValue ExtractedFromVec = I->getOperand(0);
8598 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8599 if (M == VecInMap.end()) {
8600 VT = ExtractedFromVec.getValueType();
8601 // Quit if not 128/256-bit vector.
8602 if (!VT.is128BitVector() && !VT.is256BitVector())
8603 return SDValue();
8604 // Quit if not the same type.
8605 if (VecInMap.begin() != VecInMap.end() &&
8606 VT != VecInMap.begin()->first.getValueType())
8607 return SDValue();
8608 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8609 }
8610 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8611 }
8612
8613 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008614 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008615
8616 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8617 SmallVector<SDValue, 8> VecIns;
8618
8619 for (DenseMap<SDValue, unsigned>::const_iterator
8620 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8621 // Quit if not all elements are used.
8622 if (I->second != FullMask)
8623 return SDValue();
8624 VecIns.push_back(I->first);
8625 }
8626
8627 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8628
8629 // Cast all vectors into TestVT for PTEST.
8630 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8631 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8632
8633 // If more than one full vectors are evaluated, OR them first before PTEST.
8634 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8635 // Each iteration will OR 2 nodes and append the result until there is only
8636 // 1 node left, i.e. the final OR'd value of all vectors.
8637 SDValue LHS = VecIns[Slot];
8638 SDValue RHS = VecIns[Slot + 1];
8639 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8640 }
8641
8642 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8643 VecIns.back(), VecIns.back());
8644}
8645
Dan Gohman076aee32009-03-04 19:44:21 +00008646/// Emit nodes that will be selected as "test Op0,Op0", or something
8647/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008648SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008649 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008650 DebugLoc dl = Op.getDebugLoc();
8651
Dan Gohman31125812009-03-07 01:58:32 +00008652 // CF and OF aren't always set the way we want. Determine which
8653 // of these we need.
8654 bool NeedCF = false;
8655 bool NeedOF = false;
8656 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008657 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008658 case X86::COND_A: case X86::COND_AE:
8659 case X86::COND_B: case X86::COND_BE:
8660 NeedCF = true;
8661 break;
8662 case X86::COND_G: case X86::COND_GE:
8663 case X86::COND_L: case X86::COND_LE:
8664 case X86::COND_O: case X86::COND_NO:
8665 NeedOF = true;
8666 break;
Dan Gohman31125812009-03-07 01:58:32 +00008667 }
8668
Dan Gohman076aee32009-03-04 19:44:21 +00008669 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008670 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8671 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008672 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8673 // Emit a CMP with 0, which is the TEST pattern.
8674 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8675 DAG.getConstant(0, Op.getValueType()));
8676
8677 unsigned Opcode = 0;
8678 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008679
8680 // Truncate operations may prevent the merge of the SETCC instruction
8681 // and the arithmetic intruction before it. Attempt to truncate the operands
8682 // of the arithmetic instruction and use a reduced bit-width instruction.
8683 bool NeedTruncation = false;
8684 SDValue ArithOp = Op;
8685 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8686 SDValue Arith = Op->getOperand(0);
8687 // Both the trunc and the arithmetic op need to have one user each.
8688 if (Arith->hasOneUse())
8689 switch (Arith.getOpcode()) {
8690 default: break;
8691 case ISD::ADD:
8692 case ISD::SUB:
8693 case ISD::AND:
8694 case ISD::OR:
8695 case ISD::XOR: {
8696 NeedTruncation = true;
8697 ArithOp = Arith;
8698 }
8699 }
8700 }
8701
8702 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8703 // which may be the result of a CAST. We use the variable 'Op', which is the
8704 // non-casted variable when we check for possible users.
8705 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008706 case ISD::ADD:
8707 // Due to an isel shortcoming, be conservative if this add is likely to be
8708 // selected as part of a load-modify-store instruction. When the root node
8709 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8710 // uses of other nodes in the match, such as the ADD in this case. This
8711 // leads to the ADD being left around and reselected, with the result being
8712 // two adds in the output. Alas, even if none our users are stores, that
8713 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8714 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8715 // climbing the DAG back to the root, and it doesn't seem to be worth the
8716 // effort.
8717 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008718 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8719 if (UI->getOpcode() != ISD::CopyToReg &&
8720 UI->getOpcode() != ISD::SETCC &&
8721 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008722 goto default_case;
8723
8724 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008725 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008726 // An add of one will be selected as an INC.
8727 if (C->getAPIntValue() == 1) {
8728 Opcode = X86ISD::INC;
8729 NumOperands = 1;
8730 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008731 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008732
8733 // An add of negative one (subtract of one) will be selected as a DEC.
8734 if (C->getAPIntValue().isAllOnesValue()) {
8735 Opcode = X86ISD::DEC;
8736 NumOperands = 1;
8737 break;
8738 }
Dan Gohman076aee32009-03-04 19:44:21 +00008739 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008740
8741 // Otherwise use a regular EFLAGS-setting add.
8742 Opcode = X86ISD::ADD;
8743 NumOperands = 2;
8744 break;
8745 case ISD::AND: {
8746 // If the primary and result isn't used, don't bother using X86ISD::AND,
8747 // because a TEST instruction will be better.
8748 bool NonFlagUse = false;
8749 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8750 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8751 SDNode *User = *UI;
8752 unsigned UOpNo = UI.getOperandNo();
8753 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8754 // Look pass truncate.
8755 UOpNo = User->use_begin().getOperandNo();
8756 User = *User->use_begin();
8757 }
8758
8759 if (User->getOpcode() != ISD::BRCOND &&
8760 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008761 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008762 NonFlagUse = true;
8763 break;
8764 }
Dan Gohman076aee32009-03-04 19:44:21 +00008765 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008766
8767 if (!NonFlagUse)
8768 break;
8769 }
8770 // FALL THROUGH
8771 case ISD::SUB:
8772 case ISD::OR:
8773 case ISD::XOR:
8774 // Due to the ISEL shortcoming noted above, be conservative if this op is
8775 // likely to be selected as part of a load-modify-store instruction.
8776 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8777 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8778 if (UI->getOpcode() == ISD::STORE)
8779 goto default_case;
8780
8781 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008782 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008783 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008784 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008785 case ISD::XOR: Opcode = X86ISD::XOR; break;
8786 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008787 case ISD::OR: {
8788 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8789 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8790 if (EFLAGS.getNode())
8791 return EFLAGS;
8792 }
8793 Opcode = X86ISD::OR;
8794 break;
8795 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008796 }
8797
8798 NumOperands = 2;
8799 break;
8800 case X86ISD::ADD:
8801 case X86ISD::SUB:
8802 case X86ISD::INC:
8803 case X86ISD::DEC:
8804 case X86ISD::OR:
8805 case X86ISD::XOR:
8806 case X86ISD::AND:
8807 return SDValue(Op.getNode(), 1);
8808 default:
8809 default_case:
8810 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008811 }
8812
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008813 // If we found that truncation is beneficial, perform the truncation and
8814 // update 'Op'.
8815 if (NeedTruncation) {
8816 EVT VT = Op.getValueType();
8817 SDValue WideVal = Op->getOperand(0);
8818 EVT WideVT = WideVal.getValueType();
8819 unsigned ConvertedOp = 0;
8820 // Use a target machine opcode to prevent further DAGCombine
8821 // optimizations that may separate the arithmetic operations
8822 // from the setcc node.
8823 switch (WideVal.getOpcode()) {
8824 default: break;
8825 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8826 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8827 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8828 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8829 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8830 }
8831
8832 if (ConvertedOp) {
8833 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8834 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8835 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8836 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8837 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8838 }
8839 }
8840 }
8841
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008842 if (Opcode == 0)
8843 // Emit a CMP with 0, which is the TEST pattern.
8844 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8845 DAG.getConstant(0, Op.getValueType()));
8846
8847 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8848 SmallVector<SDValue, 4> Ops;
8849 for (unsigned i = 0; i != NumOperands; ++i)
8850 Ops.push_back(Op.getOperand(i));
8851
8852 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8853 DAG.ReplaceAllUsesWith(Op, New);
8854 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008855}
8856
8857/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8858/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008859SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008860 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8862 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008863 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008864
8865 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008866 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8867 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8868 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8869 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8870 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8871 Op0, Op1);
8872 return SDValue(Sub.getNode(), 1);
8873 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008874 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008875}
8876
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008877/// Convert a comparison if required by the subtarget.
8878SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8879 SelectionDAG &DAG) const {
8880 // If the subtarget does not support the FUCOMI instruction, floating-point
8881 // comparisons have to be converted.
8882 if (Subtarget->hasCMov() ||
8883 Cmp.getOpcode() != X86ISD::CMP ||
8884 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8885 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8886 return Cmp;
8887
8888 // The instruction selector will select an FUCOM instruction instead of
8889 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8890 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8891 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8892 DebugLoc dl = Cmp.getDebugLoc();
8893 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8894 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8895 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8896 DAG.getConstant(8, MVT::i8));
8897 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8898 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8899}
8900
Evan Chengd40d03e2010-01-06 19:38:29 +00008901/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8902/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008903SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8904 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008905 SDValue Op0 = And.getOperand(0);
8906 SDValue Op1 = And.getOperand(1);
8907 if (Op0.getOpcode() == ISD::TRUNCATE)
8908 Op0 = Op0.getOperand(0);
8909 if (Op1.getOpcode() == ISD::TRUNCATE)
8910 Op1 = Op1.getOperand(0);
8911
Evan Chengd40d03e2010-01-06 19:38:29 +00008912 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008913 if (Op1.getOpcode() == ISD::SHL)
8914 std::swap(Op0, Op1);
8915 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008916 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8917 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008918 // If we looked past a truncate, check that it's only truncating away
8919 // known zeros.
8920 unsigned BitWidth = Op0.getValueSizeInBits();
8921 unsigned AndBitWidth = And.getValueSizeInBits();
8922 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008923 APInt Zeros, Ones;
8924 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008925 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8926 return SDValue();
8927 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008928 LHS = Op1;
8929 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008930 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008931 } else if (Op1.getOpcode() == ISD::Constant) {
8932 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008933 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008934 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008935
8936 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008937 LHS = AndLHS.getOperand(0);
8938 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008939 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008940
8941 // Use BT if the immediate can't be encoded in a TEST instruction.
8942 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8943 LHS = AndLHS;
8944 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8945 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008946 }
Evan Cheng0488db92007-09-25 01:57:46 +00008947
Evan Chengd40d03e2010-01-06 19:38:29 +00008948 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008949 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008950 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008951 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008952 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008953 // Also promote i16 to i32 for performance / code size reason.
8954 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008955 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008956 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008957
Evan Chengd40d03e2010-01-06 19:38:29 +00008958 // If the operand types disagree, extend the shift amount to match. Since
8959 // BT ignores high bits (like shifts) we can use anyextend.
8960 if (LHS.getValueType() != RHS.getValueType())
8961 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008962
Evan Chengd40d03e2010-01-06 19:38:29 +00008963 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8964 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8965 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8966 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008967 }
8968
Evan Cheng54de3ea2010-01-05 06:52:31 +00008969 return SDValue();
8970}
8971
Dan Gohmand858e902010-04-17 15:26:15 +00008972SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008973
8974 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8975
Evan Cheng54de3ea2010-01-05 06:52:31 +00008976 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8977 SDValue Op0 = Op.getOperand(0);
8978 SDValue Op1 = Op.getOperand(1);
8979 DebugLoc dl = Op.getDebugLoc();
8980 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8981
8982 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008983 // Lower (X & (1 << N)) == 0 to BT(X, N).
8984 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8985 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008986 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008987 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008988 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008989 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8990 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8991 if (NewSetCC.getNode())
8992 return NewSetCC;
8993 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008994
Chris Lattner481eebc2010-12-19 21:23:48 +00008995 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8996 // these.
8997 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008998 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008999 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9000 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009001
Chris Lattner481eebc2010-12-19 21:23:48 +00009002 // If the input is a setcc, then reuse the input setcc or use a new one with
9003 // the inverted condition.
9004 if (Op0.getOpcode() == X86ISD::SETCC) {
9005 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9006 bool Invert = (CC == ISD::SETNE) ^
9007 cast<ConstantSDNode>(Op1)->isNullValue();
9008 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009009
Evan Cheng2c755ba2010-02-27 07:36:59 +00009010 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009011 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9012 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9013 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009014 }
9015
Evan Chenge5b51ac2010-04-17 06:13:15 +00009016 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009017 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009018 if (X86CC == X86::COND_INVALID)
9019 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009020
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009021 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009022 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009023 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009024 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009025}
9026
Craig Topper89af15e2011-09-18 08:03:58 +00009027// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009028// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009029static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009030 EVT VT = Op.getValueType();
9031
Craig Topper7a9a28b2012-08-12 02:23:29 +00009032 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009033 "Unsupported value type for operation");
9034
Craig Topper66ddd152012-04-27 22:54:43 +00009035 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009036 DebugLoc dl = Op.getDebugLoc();
9037 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009038
9039 // Extract the LHS vectors
9040 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009041 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9042 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009043
9044 // Extract the RHS vectors
9045 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009046 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9047 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009048
9049 // Issue the operation on the smaller types and concatenate the result back
9050 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9051 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9052 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9053 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9054 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9055}
9056
9057
Dan Gohmand858e902010-04-17 15:26:15 +00009058SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009059 SDValue Cond;
9060 SDValue Op0 = Op.getOperand(0);
9061 SDValue Op1 = Op.getOperand(1);
9062 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009063 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009064 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9065 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009066 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009067
9068 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009069#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009070 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009071 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9072#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009073
Craig Topper523908d2012-08-13 02:34:03 +00009074 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009075 bool Swap = false;
9076
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009077 // SSE Condition code mapping:
9078 // 0 - EQ
9079 // 1 - LT
9080 // 2 - LE
9081 // 3 - UNORD
9082 // 4 - NEQ
9083 // 5 - NLT
9084 // 6 - NLE
9085 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009086 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009087 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009088 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009089 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009090 case ISD::SETOGT:
9091 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009092 case ISD::SETLT:
9093 case ISD::SETOLT: SSECC = 1; break;
9094 case ISD::SETOGE:
9095 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009096 case ISD::SETLE:
9097 case ISD::SETOLE: SSECC = 2; break;
9098 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009099 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009100 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009101 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009102 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009103 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009104 case ISD::SETUGT: SSECC = 6; break;
9105 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009106 case ISD::SETUEQ:
9107 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009108 }
9109 if (Swap)
9110 std::swap(Op0, Op1);
9111
Nate Begemanfb8ead02008-07-25 19:05:58 +00009112 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009113 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009114 unsigned CC0, CC1;
9115 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009116 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009117 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9118 } else {
9119 assert(SetCCOpcode == ISD::SETONE);
9120 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009121 }
Craig Topper523908d2012-08-13 02:34:03 +00009122
9123 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9124 DAG.getConstant(CC0, MVT::i8));
9125 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9126 DAG.getConstant(CC1, MVT::i8));
9127 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009128 }
9129 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009130 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9131 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009133
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009134 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00009135 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00009136 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009137
Nate Begeman30a0de92008-07-17 16:51:19 +00009138 // We are handling one of the integer comparisons here. Since SSE only has
9139 // GT and EQ comparisons for integer, swapping operands and multiple
9140 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009141 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009142 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009143
Nate Begeman30a0de92008-07-17 16:51:19 +00009144 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009145 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009146 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009147 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009148 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009149 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009150 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009151 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009152 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009153 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009154 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009155 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009156 }
9157 if (Swap)
9158 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009159
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009160 // Check that the operation in question is available (most are plain SSE2,
9161 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009162 if (VT == MVT::v2i64) {
9163 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9164 return SDValue();
9165 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
9166 return SDValue();
9167 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009168
Nate Begeman30a0de92008-07-17 16:51:19 +00009169 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9170 // bits of the inputs before performing those operations.
9171 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009172 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009173 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9174 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009175 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009176 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9177 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009178 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9179 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009181
Dale Johannesenace16102009-02-03 19:33:06 +00009182 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009183
9184 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009185 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009186 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009187
Nate Begeman30a0de92008-07-17 16:51:19 +00009188 return Result;
9189}
Evan Cheng0488db92007-09-25 01:57:46 +00009190
Evan Cheng370e5342008-12-03 08:38:43 +00009191// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009192static bool isX86LogicalCmp(SDValue Op) {
9193 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009194 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9195 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009196 return true;
9197 if (Op.getResNo() == 1 &&
9198 (Opc == X86ISD::ADD ||
9199 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009200 Opc == X86ISD::ADC ||
9201 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009202 Opc == X86ISD::SMUL ||
9203 Opc == X86ISD::UMUL ||
9204 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009205 Opc == X86ISD::DEC ||
9206 Opc == X86ISD::OR ||
9207 Opc == X86ISD::XOR ||
9208 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009209 return true;
9210
Chris Lattner9637d5b2010-12-05 07:49:54 +00009211 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9212 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009213
Dan Gohman076aee32009-03-04 19:44:21 +00009214 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009215}
9216
Chris Lattnera2b56002010-12-05 01:23:24 +00009217static bool isZero(SDValue V) {
9218 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9219 return C && C->isNullValue();
9220}
9221
Chris Lattner96908b12010-12-05 02:00:51 +00009222static bool isAllOnes(SDValue V) {
9223 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9224 return C && C->isAllOnesValue();
9225}
9226
Evan Chengb64dd5f2012-08-07 22:21:00 +00009227static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9228 if (V.getOpcode() != ISD::TRUNCATE)
9229 return false;
9230
9231 SDValue VOp0 = V.getOperand(0);
9232 unsigned InBits = VOp0.getValueSizeInBits();
9233 unsigned Bits = V.getValueSizeInBits();
9234 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9235}
9236
Dan Gohmand858e902010-04-17 15:26:15 +00009237SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009238 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009239 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009240 SDValue Op1 = Op.getOperand(1);
9241 SDValue Op2 = Op.getOperand(2);
9242 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009243 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009244
Dan Gohman1a492952009-10-20 16:22:37 +00009245 if (Cond.getOpcode() == ISD::SETCC) {
9246 SDValue NewCond = LowerSETCC(Cond, DAG);
9247 if (NewCond.getNode())
9248 Cond = NewCond;
9249 }
Evan Cheng734503b2006-09-11 02:19:56 +00009250
Chris Lattnera2b56002010-12-05 01:23:24 +00009251 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009252 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009253 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009254 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009255 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009256 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9257 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009258 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009259
Chris Lattnera2b56002010-12-05 01:23:24 +00009260 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009261
9262 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009263 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9264 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009265
9266 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009267 // Apply further optimizations for special cases
9268 // (select (x != 0), -1, 0) -> neg & sbb
9269 // (select (x == 0), 0, -1) -> neg & sbb
9270 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009271 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009272 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9273 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009274 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9275 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009276 CmpOp0);
9277 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9278 DAG.getConstant(X86::COND_B, MVT::i8),
9279 SDValue(Neg.getNode(), 1));
9280 return Res;
9281 }
9282
Chris Lattnera2b56002010-12-05 01:23:24 +00009283 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9284 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009285 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009286
Chris Lattner96908b12010-12-05 02:00:51 +00009287 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009288 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9289 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009290
Chris Lattner96908b12010-12-05 02:00:51 +00009291 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9292 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009293
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009294 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009295 if (N2C == 0 || !N2C->isNullValue())
9296 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9297 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009298 }
9299 }
9300
Chris Lattnera2b56002010-12-05 01:23:24 +00009301 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009302 if (Cond.getOpcode() == ISD::AND &&
9303 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9304 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009305 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009306 Cond = Cond.getOperand(0);
9307 }
9308
Evan Cheng3f41d662007-10-08 22:16:29 +00009309 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9310 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009311 unsigned CondOpcode = Cond.getOpcode();
9312 if (CondOpcode == X86ISD::SETCC ||
9313 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009314 CC = Cond.getOperand(0);
9315
Dan Gohman475871a2008-07-27 21:46:04 +00009316 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009317 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009318 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009319
Evan Cheng3f41d662007-10-08 22:16:29 +00009320 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009321 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009322 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009323 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009324
Chris Lattnerd1980a52009-03-12 06:52:53 +00009325 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9326 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009327 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009328 addTest = false;
9329 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009330 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9331 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9332 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9333 Cond.getOperand(0).getValueType() != MVT::i8)) {
9334 SDValue LHS = Cond.getOperand(0);
9335 SDValue RHS = Cond.getOperand(1);
9336 unsigned X86Opcode;
9337 unsigned X86Cond;
9338 SDVTList VTs;
9339 switch (CondOpcode) {
9340 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9341 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9342 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9343 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9344 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9345 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9346 default: llvm_unreachable("unexpected overflowing operator");
9347 }
9348 if (CondOpcode == ISD::UMULO)
9349 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9350 MVT::i32);
9351 else
9352 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9353
9354 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9355
9356 if (CondOpcode == ISD::UMULO)
9357 Cond = X86Op.getValue(2);
9358 else
9359 Cond = X86Op.getValue(1);
9360
9361 CC = DAG.getConstant(X86Cond, MVT::i8);
9362 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009363 }
9364
9365 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009366 // Look pass the truncate if the high bits are known zero.
9367 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9368 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009369
9370 // We know the result of AND is compared against zero. Try to match
9371 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009372 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009373 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009374 if (NewSetCC.getNode()) {
9375 CC = NewSetCC.getOperand(0);
9376 Cond = NewSetCC.getOperand(1);
9377 addTest = false;
9378 }
9379 }
9380 }
9381
9382 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009383 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009384 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009385 }
9386
Benjamin Kramere915ff32010-12-22 23:09:28 +00009387 // a < b ? -1 : 0 -> RES = ~setcc_carry
9388 // a < b ? 0 : -1 -> RES = setcc_carry
9389 // a >= b ? -1 : 0 -> RES = setcc_carry
9390 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009391 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009392 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009393 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9394
9395 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9396 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9397 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9398 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9399 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9400 return DAG.getNOT(DL, Res, Res.getValueType());
9401 return Res;
9402 }
9403 }
9404
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009405 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9406 // widen the cmov and push the truncate through. This avoids introducing a new
9407 // branch during isel and doesn't add any extensions.
9408 if (Op.getValueType() == MVT::i8 &&
9409 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9410 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9411 if (T1.getValueType() == T2.getValueType() &&
9412 // Blacklist CopyFromReg to avoid partial register stalls.
9413 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9414 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009415 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009416 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9417 }
9418 }
9419
Evan Cheng0488db92007-09-25 01:57:46 +00009420 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9421 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009422 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009423 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009424 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009425}
9426
Evan Cheng370e5342008-12-03 08:38:43 +00009427// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9428// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9429// from the AND / OR.
9430static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9431 Opc = Op.getOpcode();
9432 if (Opc != ISD::OR && Opc != ISD::AND)
9433 return false;
9434 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9435 Op.getOperand(0).hasOneUse() &&
9436 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9437 Op.getOperand(1).hasOneUse());
9438}
9439
Evan Cheng961d6d42009-02-02 08:19:07 +00009440// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9441// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009442static bool isXor1OfSetCC(SDValue Op) {
9443 if (Op.getOpcode() != ISD::XOR)
9444 return false;
9445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9446 if (N1C && N1C->getAPIntValue() == 1) {
9447 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9448 Op.getOperand(0).hasOneUse();
9449 }
9450 return false;
9451}
9452
Dan Gohmand858e902010-04-17 15:26:15 +00009453SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009454 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009455 SDValue Chain = Op.getOperand(0);
9456 SDValue Cond = Op.getOperand(1);
9457 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009458 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009459 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009460 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009461
Dan Gohman1a492952009-10-20 16:22:37 +00009462 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009463 // Check for setcc([su]{add,sub,mul}o == 0).
9464 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9465 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9466 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9467 Cond.getOperand(0).getResNo() == 1 &&
9468 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9469 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9470 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9471 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9472 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9473 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9474 Inverted = true;
9475 Cond = Cond.getOperand(0);
9476 } else {
9477 SDValue NewCond = LowerSETCC(Cond, DAG);
9478 if (NewCond.getNode())
9479 Cond = NewCond;
9480 }
Dan Gohman1a492952009-10-20 16:22:37 +00009481 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009482#if 0
9483 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009484 else if (Cond.getOpcode() == X86ISD::ADD ||
9485 Cond.getOpcode() == X86ISD::SUB ||
9486 Cond.getOpcode() == X86ISD::SMUL ||
9487 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009488 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009489#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009490
Evan Chengad9c0a32009-12-15 00:53:42 +00009491 // Look pass (and (setcc_carry (cmp ...)), 1).
9492 if (Cond.getOpcode() == ISD::AND &&
9493 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9494 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009495 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009496 Cond = Cond.getOperand(0);
9497 }
9498
Evan Cheng3f41d662007-10-08 22:16:29 +00009499 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9500 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009501 unsigned CondOpcode = Cond.getOpcode();
9502 if (CondOpcode == X86ISD::SETCC ||
9503 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009504 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009505
Dan Gohman475871a2008-07-27 21:46:04 +00009506 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009507 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009508 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009509 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009510 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009511 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009512 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009513 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009514 default: break;
9515 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009516 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009517 // These can only come from an arithmetic instruction with overflow,
9518 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009519 Cond = Cond.getNode()->getOperand(1);
9520 addTest = false;
9521 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009522 }
Evan Cheng0488db92007-09-25 01:57:46 +00009523 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009524 }
9525 CondOpcode = Cond.getOpcode();
9526 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9527 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9528 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9529 Cond.getOperand(0).getValueType() != MVT::i8)) {
9530 SDValue LHS = Cond.getOperand(0);
9531 SDValue RHS = Cond.getOperand(1);
9532 unsigned X86Opcode;
9533 unsigned X86Cond;
9534 SDVTList VTs;
9535 switch (CondOpcode) {
9536 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9537 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9538 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9539 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9540 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9541 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9542 default: llvm_unreachable("unexpected overflowing operator");
9543 }
9544 if (Inverted)
9545 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9546 if (CondOpcode == ISD::UMULO)
9547 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9548 MVT::i32);
9549 else
9550 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9551
9552 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9553
9554 if (CondOpcode == ISD::UMULO)
9555 Cond = X86Op.getValue(2);
9556 else
9557 Cond = X86Op.getValue(1);
9558
9559 CC = DAG.getConstant(X86Cond, MVT::i8);
9560 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009561 } else {
9562 unsigned CondOpc;
9563 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9564 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009565 if (CondOpc == ISD::OR) {
9566 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9567 // two branches instead of an explicit OR instruction with a
9568 // separate test.
9569 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009570 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009571 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009572 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009573 Chain, Dest, CC, Cmp);
9574 CC = Cond.getOperand(1).getOperand(0);
9575 Cond = Cmp;
9576 addTest = false;
9577 }
9578 } else { // ISD::AND
9579 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9580 // two branches instead of an explicit AND instruction with a
9581 // separate test. However, we only do this if this block doesn't
9582 // have a fall-through edge, because this requires an explicit
9583 // jmp when the condition is false.
9584 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009585 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009586 Op.getNode()->hasOneUse()) {
9587 X86::CondCode CCode =
9588 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9589 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009591 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009592 // Look for an unconditional branch following this conditional branch.
9593 // We need this because we need to reverse the successors in order
9594 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009595 if (User->getOpcode() == ISD::BR) {
9596 SDValue FalseBB = User->getOperand(1);
9597 SDNode *NewBR =
9598 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009599 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009600 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009601 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009602
Dale Johannesene4d209d2009-02-03 20:21:25 +00009603 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009604 Chain, Dest, CC, Cmp);
9605 X86::CondCode CCode =
9606 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9607 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009609 Cond = Cmp;
9610 addTest = false;
9611 }
9612 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009613 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009614 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9615 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9616 // It should be transformed during dag combiner except when the condition
9617 // is set by a arithmetics with overflow node.
9618 X86::CondCode CCode =
9619 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9620 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009621 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009622 Cond = Cond.getOperand(0).getOperand(1);
9623 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009624 } else if (Cond.getOpcode() == ISD::SETCC &&
9625 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9626 // For FCMP_OEQ, we can emit
9627 // two branches instead of an explicit AND instruction with a
9628 // separate test. However, we only do this if this block doesn't
9629 // have a fall-through edge, because this requires an explicit
9630 // jmp when the condition is false.
9631 if (Op.getNode()->hasOneUse()) {
9632 SDNode *User = *Op.getNode()->use_begin();
9633 // Look for an unconditional branch following this conditional branch.
9634 // We need this because we need to reverse the successors in order
9635 // to implement FCMP_OEQ.
9636 if (User->getOpcode() == ISD::BR) {
9637 SDValue FalseBB = User->getOperand(1);
9638 SDNode *NewBR =
9639 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9640 assert(NewBR == User);
9641 (void)NewBR;
9642 Dest = FalseBB;
9643
9644 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9645 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009646 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009647 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9648 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9649 Chain, Dest, CC, Cmp);
9650 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9651 Cond = Cmp;
9652 addTest = false;
9653 }
9654 }
9655 } else if (Cond.getOpcode() == ISD::SETCC &&
9656 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9657 // For FCMP_UNE, we can emit
9658 // two branches instead of an explicit AND instruction with a
9659 // separate test. However, we only do this if this block doesn't
9660 // have a fall-through edge, because this requires an explicit
9661 // jmp when the condition is false.
9662 if (Op.getNode()->hasOneUse()) {
9663 SDNode *User = *Op.getNode()->use_begin();
9664 // Look for an unconditional branch following this conditional branch.
9665 // We need this because we need to reverse the successors in order
9666 // to implement FCMP_UNE.
9667 if (User->getOpcode() == ISD::BR) {
9668 SDValue FalseBB = User->getOperand(1);
9669 SDNode *NewBR =
9670 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9671 assert(NewBR == User);
9672 (void)NewBR;
9673
9674 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9675 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009676 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009677 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9678 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9679 Chain, Dest, CC, Cmp);
9680 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9681 Cond = Cmp;
9682 addTest = false;
9683 Dest = FalseBB;
9684 }
9685 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009686 }
Evan Cheng0488db92007-09-25 01:57:46 +00009687 }
9688
9689 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009690 // Look pass the truncate if the high bits are known zero.
9691 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9692 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009693
9694 // We know the result of AND is compared against zero. Try to match
9695 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009696 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009697 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9698 if (NewSetCC.getNode()) {
9699 CC = NewSetCC.getOperand(0);
9700 Cond = NewSetCC.getOperand(1);
9701 addTest = false;
9702 }
9703 }
9704 }
9705
9706 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009707 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009708 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009709 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009710 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009711 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009712 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009713}
9714
Anton Korobeynikove060b532007-04-17 19:34:00 +00009715
9716// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9717// Calls to _alloca is needed to probe the stack when allocating more than 4k
9718// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9719// that the guard pages used by the OS virtual memory manager are allocated in
9720// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009721SDValue
9722X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009723 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009724 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009725 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009726 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009727 "are being used");
9728 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009729 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009730
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009731 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009732 SDValue Chain = Op.getOperand(0);
9733 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009734 // FIXME: Ensure alignment here
9735
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009736 bool Is64Bit = Subtarget->is64Bit();
9737 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009738
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009739 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009740 MachineFunction &MF = DAG.getMachineFunction();
9741 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009742
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009743 if (Is64Bit) {
9744 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009745 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009746 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009747
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009748 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009749 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009750 if (I->hasNestAttr())
9751 report_fatal_error("Cannot use segmented stacks with functions that "
9752 "have nested arguments.");
9753 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009754
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009755 const TargetRegisterClass *AddrRegClass =
9756 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9757 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9758 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9759 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9760 DAG.getRegister(Vreg, SPTy));
9761 SDValue Ops1[2] = { Value, Chain };
9762 return DAG.getMergeValues(Ops1, 2, dl);
9763 } else {
9764 SDValue Flag;
9765 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009766
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009767 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9768 Flag = Chain.getValue(1);
9769 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009770
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009771 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9772 Flag = Chain.getValue(1);
9773
9774 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9775
9776 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9777 return DAG.getMergeValues(Ops1, 2, dl);
9778 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009779}
9780
Dan Gohmand858e902010-04-17 15:26:15 +00009781SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009782 MachineFunction &MF = DAG.getMachineFunction();
9783 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9784
Dan Gohman69de1932008-02-06 22:27:42 +00009785 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009786 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009787
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009788 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009789 // vastart just stores the address of the VarArgsFrameIndex slot into the
9790 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009791 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9792 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009793 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9794 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009795 }
9796
9797 // __va_list_tag:
9798 // gp_offset (0 - 6 * 8)
9799 // fp_offset (48 - 48 + 8 * 16)
9800 // overflow_arg_area (point to parameters coming in memory).
9801 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009802 SmallVector<SDValue, 8> MemOps;
9803 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009804 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009805 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009806 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9807 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009808 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009809 MemOps.push_back(Store);
9810
9811 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009812 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009813 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009814 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009815 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9816 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009817 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009818 MemOps.push_back(Store);
9819
9820 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009821 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009822 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009823 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9824 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009825 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9826 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009827 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009828 MemOps.push_back(Store);
9829
9830 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009831 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009832 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009833 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9834 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009835 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9836 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009837 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009838 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009839 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009840}
9841
Dan Gohmand858e902010-04-17 15:26:15 +00009842SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009843 assert(Subtarget->is64Bit() &&
9844 "LowerVAARG only handles 64-bit va_arg!");
9845 assert((Subtarget->isTargetLinux() ||
9846 Subtarget->isTargetDarwin()) &&
9847 "Unhandled target in LowerVAARG");
9848 assert(Op.getNode()->getNumOperands() == 4);
9849 SDValue Chain = Op.getOperand(0);
9850 SDValue SrcPtr = Op.getOperand(1);
9851 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9852 unsigned Align = Op.getConstantOperandVal(3);
9853 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009854
Dan Gohman320afb82010-10-12 18:00:49 +00009855 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009856 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009857 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009858 uint8_t ArgMode;
9859
9860 // Decide which area this value should be read from.
9861 // TODO: Implement the AMD64 ABI in its entirety. This simple
9862 // selection mechanism works only for the basic types.
9863 if (ArgVT == MVT::f80) {
9864 llvm_unreachable("va_arg for f80 not yet implemented");
9865 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9866 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9867 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9868 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9869 } else {
9870 llvm_unreachable("Unhandled argument type in LowerVAARG");
9871 }
9872
9873 if (ArgMode == 2) {
9874 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009875 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009876 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009877 .getFunction()->getFnAttributes()
9878 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009879 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009880 }
9881
9882 // Insert VAARG_64 node into the DAG
9883 // VAARG_64 returns two values: Variable Argument Address, Chain
9884 SmallVector<SDValue, 11> InstOps;
9885 InstOps.push_back(Chain);
9886 InstOps.push_back(SrcPtr);
9887 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9888 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9889 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9890 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9891 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9892 VTs, &InstOps[0], InstOps.size(),
9893 MVT::i64,
9894 MachinePointerInfo(SV),
9895 /*Align=*/0,
9896 /*Volatile=*/false,
9897 /*ReadMem=*/true,
9898 /*WriteMem=*/true);
9899 Chain = VAARG.getValue(1);
9900
9901 // Load the next argument and return it
9902 return DAG.getLoad(ArgVT, dl,
9903 Chain,
9904 VAARG,
9905 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009906 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009907}
9908
Craig Topper55b24052012-09-11 06:15:32 +00009909static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9910 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009911 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009912 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009913 SDValue Chain = Op.getOperand(0);
9914 SDValue DstPtr = Op.getOperand(1);
9915 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009916 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9917 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009918 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009919
Chris Lattnere72f2022010-09-21 05:40:29 +00009920 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009921 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009922 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009923 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009924}
9925
Craig Topper80e46362012-01-23 06:16:53 +00009926// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9927// may or may not be a constant. Takes immediate version of shift as input.
9928static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9929 SDValue SrcOp, SDValue ShAmt,
9930 SelectionDAG &DAG) {
9931 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9932
9933 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009934 // Constant may be a TargetConstant. Use a regular constant.
9935 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009936 switch (Opc) {
9937 default: llvm_unreachable("Unknown target vector shift node");
9938 case X86ISD::VSHLI:
9939 case X86ISD::VSRLI:
9940 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009941 return DAG.getNode(Opc, dl, VT, SrcOp,
9942 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009943 }
9944 }
9945
9946 // Change opcode to non-immediate version
9947 switch (Opc) {
9948 default: llvm_unreachable("Unknown target vector shift node");
9949 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9950 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9951 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9952 }
9953
9954 // Need to build a vector containing shift amount
9955 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9956 SDValue ShOps[4];
9957 ShOps[0] = ShAmt;
9958 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009959 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009960 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009961
9962 // The return type has to be a 128-bit type with the same element
9963 // type as the input type.
9964 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9965 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9966
9967 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009968 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9969}
9970
Craig Topper55b24052012-09-11 06:15:32 +00009971static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009972 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009973 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009974 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009975 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009976 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009977 case Intrinsic::x86_sse_comieq_ss:
9978 case Intrinsic::x86_sse_comilt_ss:
9979 case Intrinsic::x86_sse_comile_ss:
9980 case Intrinsic::x86_sse_comigt_ss:
9981 case Intrinsic::x86_sse_comige_ss:
9982 case Intrinsic::x86_sse_comineq_ss:
9983 case Intrinsic::x86_sse_ucomieq_ss:
9984 case Intrinsic::x86_sse_ucomilt_ss:
9985 case Intrinsic::x86_sse_ucomile_ss:
9986 case Intrinsic::x86_sse_ucomigt_ss:
9987 case Intrinsic::x86_sse_ucomige_ss:
9988 case Intrinsic::x86_sse_ucomineq_ss:
9989 case Intrinsic::x86_sse2_comieq_sd:
9990 case Intrinsic::x86_sse2_comilt_sd:
9991 case Intrinsic::x86_sse2_comile_sd:
9992 case Intrinsic::x86_sse2_comigt_sd:
9993 case Intrinsic::x86_sse2_comige_sd:
9994 case Intrinsic::x86_sse2_comineq_sd:
9995 case Intrinsic::x86_sse2_ucomieq_sd:
9996 case Intrinsic::x86_sse2_ucomilt_sd:
9997 case Intrinsic::x86_sse2_ucomile_sd:
9998 case Intrinsic::x86_sse2_ucomigt_sd:
9999 case Intrinsic::x86_sse2_ucomige_sd:
10000 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010001 unsigned Opc;
10002 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010003 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010004 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010005 case Intrinsic::x86_sse_comieq_ss:
10006 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010007 Opc = X86ISD::COMI;
10008 CC = ISD::SETEQ;
10009 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010010 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010011 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010012 Opc = X86ISD::COMI;
10013 CC = ISD::SETLT;
10014 break;
10015 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010016 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010017 Opc = X86ISD::COMI;
10018 CC = ISD::SETLE;
10019 break;
10020 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010021 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010022 Opc = X86ISD::COMI;
10023 CC = ISD::SETGT;
10024 break;
10025 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010026 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010027 Opc = X86ISD::COMI;
10028 CC = ISD::SETGE;
10029 break;
10030 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010031 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010032 Opc = X86ISD::COMI;
10033 CC = ISD::SETNE;
10034 break;
10035 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010036 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010037 Opc = X86ISD::UCOMI;
10038 CC = ISD::SETEQ;
10039 break;
10040 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010041 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010042 Opc = X86ISD::UCOMI;
10043 CC = ISD::SETLT;
10044 break;
10045 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010046 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010047 Opc = X86ISD::UCOMI;
10048 CC = ISD::SETLE;
10049 break;
10050 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010051 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010052 Opc = X86ISD::UCOMI;
10053 CC = ISD::SETGT;
10054 break;
10055 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010056 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010057 Opc = X86ISD::UCOMI;
10058 CC = ISD::SETGE;
10059 break;
10060 case Intrinsic::x86_sse_ucomineq_ss:
10061 case Intrinsic::x86_sse2_ucomineq_sd:
10062 Opc = X86ISD::UCOMI;
10063 CC = ISD::SETNE;
10064 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010065 }
Evan Cheng734503b2006-09-11 02:19:56 +000010066
Dan Gohman475871a2008-07-27 21:46:04 +000010067 SDValue LHS = Op.getOperand(1);
10068 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010069 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010070 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010071 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10072 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10073 DAG.getConstant(X86CC, MVT::i8), Cond);
10074 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010075 }
Craig Topper6d688152012-08-14 07:43:25 +000010076
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010077 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010078 case Intrinsic::x86_sse2_pmulu_dq:
10079 case Intrinsic::x86_avx2_pmulu_dq:
10080 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10081 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010082
10083 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010084 case Intrinsic::x86_sse3_hadd_ps:
10085 case Intrinsic::x86_sse3_hadd_pd:
10086 case Intrinsic::x86_avx_hadd_ps_256:
10087 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010088 case Intrinsic::x86_sse3_hsub_ps:
10089 case Intrinsic::x86_sse3_hsub_pd:
10090 case Intrinsic::x86_avx_hsub_ps_256:
10091 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010092 case Intrinsic::x86_ssse3_phadd_w_128:
10093 case Intrinsic::x86_ssse3_phadd_d_128:
10094 case Intrinsic::x86_avx2_phadd_w:
10095 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010096 case Intrinsic::x86_ssse3_phsub_w_128:
10097 case Intrinsic::x86_ssse3_phsub_d_128:
10098 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010099 case Intrinsic::x86_avx2_phsub_d: {
10100 unsigned Opcode;
10101 switch (IntNo) {
10102 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10103 case Intrinsic::x86_sse3_hadd_ps:
10104 case Intrinsic::x86_sse3_hadd_pd:
10105 case Intrinsic::x86_avx_hadd_ps_256:
10106 case Intrinsic::x86_avx_hadd_pd_256:
10107 Opcode = X86ISD::FHADD;
10108 break;
10109 case Intrinsic::x86_sse3_hsub_ps:
10110 case Intrinsic::x86_sse3_hsub_pd:
10111 case Intrinsic::x86_avx_hsub_ps_256:
10112 case Intrinsic::x86_avx_hsub_pd_256:
10113 Opcode = X86ISD::FHSUB;
10114 break;
10115 case Intrinsic::x86_ssse3_phadd_w_128:
10116 case Intrinsic::x86_ssse3_phadd_d_128:
10117 case Intrinsic::x86_avx2_phadd_w:
10118 case Intrinsic::x86_avx2_phadd_d:
10119 Opcode = X86ISD::HADD;
10120 break;
10121 case Intrinsic::x86_ssse3_phsub_w_128:
10122 case Intrinsic::x86_ssse3_phsub_d_128:
10123 case Intrinsic::x86_avx2_phsub_w:
10124 case Intrinsic::x86_avx2_phsub_d:
10125 Opcode = X86ISD::HSUB;
10126 break;
10127 }
10128 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010129 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010130 }
10131
10132 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010133 case Intrinsic::x86_avx2_psllv_d:
10134 case Intrinsic::x86_avx2_psllv_q:
10135 case Intrinsic::x86_avx2_psllv_d_256:
10136 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010137 case Intrinsic::x86_avx2_psrlv_d:
10138 case Intrinsic::x86_avx2_psrlv_q:
10139 case Intrinsic::x86_avx2_psrlv_d_256:
10140 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010141 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010142 case Intrinsic::x86_avx2_psrav_d_256: {
10143 unsigned Opcode;
10144 switch (IntNo) {
10145 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10146 case Intrinsic::x86_avx2_psllv_d:
10147 case Intrinsic::x86_avx2_psllv_q:
10148 case Intrinsic::x86_avx2_psllv_d_256:
10149 case Intrinsic::x86_avx2_psllv_q_256:
10150 Opcode = ISD::SHL;
10151 break;
10152 case Intrinsic::x86_avx2_psrlv_d:
10153 case Intrinsic::x86_avx2_psrlv_q:
10154 case Intrinsic::x86_avx2_psrlv_d_256:
10155 case Intrinsic::x86_avx2_psrlv_q_256:
10156 Opcode = ISD::SRL;
10157 break;
10158 case Intrinsic::x86_avx2_psrav_d:
10159 case Intrinsic::x86_avx2_psrav_d_256:
10160 Opcode = ISD::SRA;
10161 break;
10162 }
10163 return DAG.getNode(Opcode, dl, Op.getValueType(),
10164 Op.getOperand(1), Op.getOperand(2));
10165 }
10166
Craig Topper969ba282012-01-25 06:43:11 +000010167 case Intrinsic::x86_ssse3_pshuf_b_128:
10168 case Intrinsic::x86_avx2_pshuf_b:
10169 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10170 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010171
Craig Topper969ba282012-01-25 06:43:11 +000010172 case Intrinsic::x86_ssse3_psign_b_128:
10173 case Intrinsic::x86_ssse3_psign_w_128:
10174 case Intrinsic::x86_ssse3_psign_d_128:
10175 case Intrinsic::x86_avx2_psign_b:
10176 case Intrinsic::x86_avx2_psign_w:
10177 case Intrinsic::x86_avx2_psign_d:
10178 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10179 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010180
Craig Toppere566cd02012-01-26 07:18:03 +000010181 case Intrinsic::x86_sse41_insertps:
10182 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10183 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010184
Craig Toppere566cd02012-01-26 07:18:03 +000010185 case Intrinsic::x86_avx_vperm2f128_ps_256:
10186 case Intrinsic::x86_avx_vperm2f128_pd_256:
10187 case Intrinsic::x86_avx_vperm2f128_si_256:
10188 case Intrinsic::x86_avx2_vperm2i128:
10189 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10190 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010191
Craig Topperffa6c402012-04-16 07:13:00 +000010192 case Intrinsic::x86_avx2_permd:
10193 case Intrinsic::x86_avx2_permps:
10194 // Operands intentionally swapped. Mask is last operand to intrinsic,
10195 // but second operand for node/intruction.
10196 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10197 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010198
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010199 // ptest and testp intrinsics. The intrinsic these come from are designed to
10200 // return an integer value, not just an instruction so lower it to the ptest
10201 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010202 case Intrinsic::x86_sse41_ptestz:
10203 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010204 case Intrinsic::x86_sse41_ptestnzc:
10205 case Intrinsic::x86_avx_ptestz_256:
10206 case Intrinsic::x86_avx_ptestc_256:
10207 case Intrinsic::x86_avx_ptestnzc_256:
10208 case Intrinsic::x86_avx_vtestz_ps:
10209 case Intrinsic::x86_avx_vtestc_ps:
10210 case Intrinsic::x86_avx_vtestnzc_ps:
10211 case Intrinsic::x86_avx_vtestz_pd:
10212 case Intrinsic::x86_avx_vtestc_pd:
10213 case Intrinsic::x86_avx_vtestnzc_pd:
10214 case Intrinsic::x86_avx_vtestz_ps_256:
10215 case Intrinsic::x86_avx_vtestc_ps_256:
10216 case Intrinsic::x86_avx_vtestnzc_ps_256:
10217 case Intrinsic::x86_avx_vtestz_pd_256:
10218 case Intrinsic::x86_avx_vtestc_pd_256:
10219 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10220 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010221 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010222 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010223 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010224 case Intrinsic::x86_avx_vtestz_ps:
10225 case Intrinsic::x86_avx_vtestz_pd:
10226 case Intrinsic::x86_avx_vtestz_ps_256:
10227 case Intrinsic::x86_avx_vtestz_pd_256:
10228 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010229 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010230 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010231 // ZF = 1
10232 X86CC = X86::COND_E;
10233 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010234 case Intrinsic::x86_avx_vtestc_ps:
10235 case Intrinsic::x86_avx_vtestc_pd:
10236 case Intrinsic::x86_avx_vtestc_ps_256:
10237 case Intrinsic::x86_avx_vtestc_pd_256:
10238 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010239 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010240 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010241 // CF = 1
10242 X86CC = X86::COND_B;
10243 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010244 case Intrinsic::x86_avx_vtestnzc_ps:
10245 case Intrinsic::x86_avx_vtestnzc_pd:
10246 case Intrinsic::x86_avx_vtestnzc_ps_256:
10247 case Intrinsic::x86_avx_vtestnzc_pd_256:
10248 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010249 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010250 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010251 // ZF and CF = 0
10252 X86CC = X86::COND_A;
10253 break;
10254 }
Eric Christopherfd179292009-08-27 18:07:15 +000010255
Eric Christopher71c67532009-07-29 00:28:05 +000010256 SDValue LHS = Op.getOperand(1);
10257 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010258 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10259 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010260 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10261 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10262 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010263 }
Evan Cheng5759f972008-05-04 09:15:50 +000010264
Craig Topper80e46362012-01-23 06:16:53 +000010265 // SSE/AVX shift intrinsics
10266 case Intrinsic::x86_sse2_psll_w:
10267 case Intrinsic::x86_sse2_psll_d:
10268 case Intrinsic::x86_sse2_psll_q:
10269 case Intrinsic::x86_avx2_psll_w:
10270 case Intrinsic::x86_avx2_psll_d:
10271 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010272 case Intrinsic::x86_sse2_psrl_w:
10273 case Intrinsic::x86_sse2_psrl_d:
10274 case Intrinsic::x86_sse2_psrl_q:
10275 case Intrinsic::x86_avx2_psrl_w:
10276 case Intrinsic::x86_avx2_psrl_d:
10277 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010278 case Intrinsic::x86_sse2_psra_w:
10279 case Intrinsic::x86_sse2_psra_d:
10280 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010281 case Intrinsic::x86_avx2_psra_d: {
10282 unsigned Opcode;
10283 switch (IntNo) {
10284 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10285 case Intrinsic::x86_sse2_psll_w:
10286 case Intrinsic::x86_sse2_psll_d:
10287 case Intrinsic::x86_sse2_psll_q:
10288 case Intrinsic::x86_avx2_psll_w:
10289 case Intrinsic::x86_avx2_psll_d:
10290 case Intrinsic::x86_avx2_psll_q:
10291 Opcode = X86ISD::VSHL;
10292 break;
10293 case Intrinsic::x86_sse2_psrl_w:
10294 case Intrinsic::x86_sse2_psrl_d:
10295 case Intrinsic::x86_sse2_psrl_q:
10296 case Intrinsic::x86_avx2_psrl_w:
10297 case Intrinsic::x86_avx2_psrl_d:
10298 case Intrinsic::x86_avx2_psrl_q:
10299 Opcode = X86ISD::VSRL;
10300 break;
10301 case Intrinsic::x86_sse2_psra_w:
10302 case Intrinsic::x86_sse2_psra_d:
10303 case Intrinsic::x86_avx2_psra_w:
10304 case Intrinsic::x86_avx2_psra_d:
10305 Opcode = X86ISD::VSRA;
10306 break;
10307 }
10308 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010309 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010310 }
10311
10312 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010313 case Intrinsic::x86_sse2_pslli_w:
10314 case Intrinsic::x86_sse2_pslli_d:
10315 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010316 case Intrinsic::x86_avx2_pslli_w:
10317 case Intrinsic::x86_avx2_pslli_d:
10318 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010319 case Intrinsic::x86_sse2_psrli_w:
10320 case Intrinsic::x86_sse2_psrli_d:
10321 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010322 case Intrinsic::x86_avx2_psrli_w:
10323 case Intrinsic::x86_avx2_psrli_d:
10324 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010325 case Intrinsic::x86_sse2_psrai_w:
10326 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010327 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010328 case Intrinsic::x86_avx2_psrai_d: {
10329 unsigned Opcode;
10330 switch (IntNo) {
10331 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10332 case Intrinsic::x86_sse2_pslli_w:
10333 case Intrinsic::x86_sse2_pslli_d:
10334 case Intrinsic::x86_sse2_pslli_q:
10335 case Intrinsic::x86_avx2_pslli_w:
10336 case Intrinsic::x86_avx2_pslli_d:
10337 case Intrinsic::x86_avx2_pslli_q:
10338 Opcode = X86ISD::VSHLI;
10339 break;
10340 case Intrinsic::x86_sse2_psrli_w:
10341 case Intrinsic::x86_sse2_psrli_d:
10342 case Intrinsic::x86_sse2_psrli_q:
10343 case Intrinsic::x86_avx2_psrli_w:
10344 case Intrinsic::x86_avx2_psrli_d:
10345 case Intrinsic::x86_avx2_psrli_q:
10346 Opcode = X86ISD::VSRLI;
10347 break;
10348 case Intrinsic::x86_sse2_psrai_w:
10349 case Intrinsic::x86_sse2_psrai_d:
10350 case Intrinsic::x86_avx2_psrai_w:
10351 case Intrinsic::x86_avx2_psrai_d:
10352 Opcode = X86ISD::VSRAI;
10353 break;
10354 }
10355 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010356 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010357 }
10358
Craig Topper4feb6472012-08-06 06:22:36 +000010359 case Intrinsic::x86_sse42_pcmpistria128:
10360 case Intrinsic::x86_sse42_pcmpestria128:
10361 case Intrinsic::x86_sse42_pcmpistric128:
10362 case Intrinsic::x86_sse42_pcmpestric128:
10363 case Intrinsic::x86_sse42_pcmpistrio128:
10364 case Intrinsic::x86_sse42_pcmpestrio128:
10365 case Intrinsic::x86_sse42_pcmpistris128:
10366 case Intrinsic::x86_sse42_pcmpestris128:
10367 case Intrinsic::x86_sse42_pcmpistriz128:
10368 case Intrinsic::x86_sse42_pcmpestriz128: {
10369 unsigned Opcode;
10370 unsigned X86CC;
10371 switch (IntNo) {
10372 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10373 case Intrinsic::x86_sse42_pcmpistria128:
10374 Opcode = X86ISD::PCMPISTRI;
10375 X86CC = X86::COND_A;
10376 break;
10377 case Intrinsic::x86_sse42_pcmpestria128:
10378 Opcode = X86ISD::PCMPESTRI;
10379 X86CC = X86::COND_A;
10380 break;
10381 case Intrinsic::x86_sse42_pcmpistric128:
10382 Opcode = X86ISD::PCMPISTRI;
10383 X86CC = X86::COND_B;
10384 break;
10385 case Intrinsic::x86_sse42_pcmpestric128:
10386 Opcode = X86ISD::PCMPESTRI;
10387 X86CC = X86::COND_B;
10388 break;
10389 case Intrinsic::x86_sse42_pcmpistrio128:
10390 Opcode = X86ISD::PCMPISTRI;
10391 X86CC = X86::COND_O;
10392 break;
10393 case Intrinsic::x86_sse42_pcmpestrio128:
10394 Opcode = X86ISD::PCMPESTRI;
10395 X86CC = X86::COND_O;
10396 break;
10397 case Intrinsic::x86_sse42_pcmpistris128:
10398 Opcode = X86ISD::PCMPISTRI;
10399 X86CC = X86::COND_S;
10400 break;
10401 case Intrinsic::x86_sse42_pcmpestris128:
10402 Opcode = X86ISD::PCMPESTRI;
10403 X86CC = X86::COND_S;
10404 break;
10405 case Intrinsic::x86_sse42_pcmpistriz128:
10406 Opcode = X86ISD::PCMPISTRI;
10407 X86CC = X86::COND_E;
10408 break;
10409 case Intrinsic::x86_sse42_pcmpestriz128:
10410 Opcode = X86ISD::PCMPESTRI;
10411 X86CC = X86::COND_E;
10412 break;
10413 }
10414 SmallVector<SDValue, 5> NewOps;
10415 NewOps.append(Op->op_begin()+1, Op->op_end());
10416 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10417 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10418 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10419 DAG.getConstant(X86CC, MVT::i8),
10420 SDValue(PCMP.getNode(), 1));
10421 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10422 }
Craig Topper6d688152012-08-14 07:43:25 +000010423
Craig Topper4feb6472012-08-06 06:22:36 +000010424 case Intrinsic::x86_sse42_pcmpistri128:
10425 case Intrinsic::x86_sse42_pcmpestri128: {
10426 unsigned Opcode;
10427 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10428 Opcode = X86ISD::PCMPISTRI;
10429 else
10430 Opcode = X86ISD::PCMPESTRI;
10431
10432 SmallVector<SDValue, 5> NewOps;
10433 NewOps.append(Op->op_begin()+1, Op->op_end());
10434 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10435 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10436 }
Craig Topper0e292372012-08-24 04:03:22 +000010437 case Intrinsic::x86_fma_vfmadd_ps:
10438 case Intrinsic::x86_fma_vfmadd_pd:
10439 case Intrinsic::x86_fma_vfmsub_ps:
10440 case Intrinsic::x86_fma_vfmsub_pd:
10441 case Intrinsic::x86_fma_vfnmadd_ps:
10442 case Intrinsic::x86_fma_vfnmadd_pd:
10443 case Intrinsic::x86_fma_vfnmsub_ps:
10444 case Intrinsic::x86_fma_vfnmsub_pd:
10445 case Intrinsic::x86_fma_vfmaddsub_ps:
10446 case Intrinsic::x86_fma_vfmaddsub_pd:
10447 case Intrinsic::x86_fma_vfmsubadd_ps:
10448 case Intrinsic::x86_fma_vfmsubadd_pd:
10449 case Intrinsic::x86_fma_vfmadd_ps_256:
10450 case Intrinsic::x86_fma_vfmadd_pd_256:
10451 case Intrinsic::x86_fma_vfmsub_ps_256:
10452 case Intrinsic::x86_fma_vfmsub_pd_256:
10453 case Intrinsic::x86_fma_vfnmadd_ps_256:
10454 case Intrinsic::x86_fma_vfnmadd_pd_256:
10455 case Intrinsic::x86_fma_vfnmsub_ps_256:
10456 case Intrinsic::x86_fma_vfnmsub_pd_256:
10457 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10458 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10459 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10460 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010461 unsigned Opc;
10462 switch (IntNo) {
10463 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10464 case Intrinsic::x86_fma_vfmadd_ps:
10465 case Intrinsic::x86_fma_vfmadd_pd:
10466 case Intrinsic::x86_fma_vfmadd_ps_256:
10467 case Intrinsic::x86_fma_vfmadd_pd_256:
10468 Opc = X86ISD::FMADD;
10469 break;
10470 case Intrinsic::x86_fma_vfmsub_ps:
10471 case Intrinsic::x86_fma_vfmsub_pd:
10472 case Intrinsic::x86_fma_vfmsub_ps_256:
10473 case Intrinsic::x86_fma_vfmsub_pd_256:
10474 Opc = X86ISD::FMSUB;
10475 break;
10476 case Intrinsic::x86_fma_vfnmadd_ps:
10477 case Intrinsic::x86_fma_vfnmadd_pd:
10478 case Intrinsic::x86_fma_vfnmadd_ps_256:
10479 case Intrinsic::x86_fma_vfnmadd_pd_256:
10480 Opc = X86ISD::FNMADD;
10481 break;
10482 case Intrinsic::x86_fma_vfnmsub_ps:
10483 case Intrinsic::x86_fma_vfnmsub_pd:
10484 case Intrinsic::x86_fma_vfnmsub_ps_256:
10485 case Intrinsic::x86_fma_vfnmsub_pd_256:
10486 Opc = X86ISD::FNMSUB;
10487 break;
10488 case Intrinsic::x86_fma_vfmaddsub_ps:
10489 case Intrinsic::x86_fma_vfmaddsub_pd:
10490 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10491 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10492 Opc = X86ISD::FMADDSUB;
10493 break;
10494 case Intrinsic::x86_fma_vfmsubadd_ps:
10495 case Intrinsic::x86_fma_vfmsubadd_pd:
10496 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10497 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10498 Opc = X86ISD::FMSUBADD;
10499 break;
10500 }
10501
10502 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10503 Op.getOperand(2), Op.getOperand(3));
10504 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010505 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010506}
Evan Cheng72261582005-12-20 06:22:03 +000010507
Craig Topper55b24052012-09-11 06:15:32 +000010508static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010509 DebugLoc dl = Op.getDebugLoc();
10510 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10511 switch (IntNo) {
10512 default: return SDValue(); // Don't custom lower most intrinsics.
10513
10514 // RDRAND intrinsics.
10515 case Intrinsic::x86_rdrand_16:
10516 case Intrinsic::x86_rdrand_32:
10517 case Intrinsic::x86_rdrand_64: {
10518 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010519 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10520 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010521
10522 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10523 // return the value from Rand, which is always 0, casted to i32.
10524 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10525 DAG.getConstant(1, Op->getValueType(1)),
10526 DAG.getConstant(X86::COND_B, MVT::i32),
10527 SDValue(Result.getNode(), 1) };
10528 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10529 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10530 Ops, 4);
10531
10532 // Return { result, isValid, chain }.
10533 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010534 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010535 }
10536 }
10537}
10538
Dan Gohmand858e902010-04-17 15:26:15 +000010539SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10540 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010541 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10542 MFI->setReturnAddressIsTaken(true);
10543
Bill Wendling64e87322009-01-16 19:25:27 +000010544 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010545 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010546 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010547
10548 if (Depth > 0) {
10549 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10550 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010551 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10552 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10553 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010554 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010555 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010556 }
10557
10558 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010559 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010560 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010561 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010562}
10563
Dan Gohmand858e902010-04-17 15:26:15 +000010564SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010565 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10566 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010567
Owen Andersone50ed302009-08-10 22:56:29 +000010568 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010569 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010570 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10571 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010572 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010573 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010574 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10575 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010576 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010577 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010578}
10579
Dan Gohman475871a2008-07-27 21:46:04 +000010580SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010581 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010582 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010583}
10584
Dan Gohmand858e902010-04-17 15:26:15 +000010585SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010586 SDValue Chain = Op.getOperand(0);
10587 SDValue Offset = Op.getOperand(1);
10588 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010589 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010590
Dan Gohmand8816272010-08-11 18:14:00 +000010591 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10592 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10593 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010594 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010595
Dan Gohmand8816272010-08-11 18:14:00 +000010596 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010597 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010598 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010599 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10600 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010601 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010602
Dale Johannesene4d209d2009-02-03 20:21:25 +000010603 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010604 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010605 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010606}
10607
Michael Liao6c0e04c2012-10-15 22:39:43 +000010608SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10609 SelectionDAG &DAG) const {
10610 DebugLoc DL = Op.getDebugLoc();
10611 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10612 DAG.getVTList(MVT::i32, MVT::Other),
10613 Op.getOperand(0), Op.getOperand(1));
10614}
10615
10616SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10617 SelectionDAG &DAG) const {
10618 DebugLoc DL = Op.getDebugLoc();
10619 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10620 Op.getOperand(0), Op.getOperand(1));
10621}
10622
Craig Topper55b24052012-09-11 06:15:32 +000010623static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010624 return Op.getOperand(0);
10625}
10626
10627SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10628 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010629 SDValue Root = Op.getOperand(0);
10630 SDValue Trmp = Op.getOperand(1); // trampoline
10631 SDValue FPtr = Op.getOperand(2); // nested function
10632 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010633 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010634
Dan Gohman69de1932008-02-06 22:27:42 +000010635 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010636 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010637
10638 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010639 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010640
10641 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010642 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10643 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010644
Michael Liao7abf67a2012-10-04 19:50:43 +000010645 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10646 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010647
10648 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10649
10650 // Load the pointer to the nested function into R11.
10651 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010652 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010653 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010654 Addr, MachinePointerInfo(TrmpAddr),
10655 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010656
Owen Anderson825b72b2009-08-11 20:47:22 +000010657 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10658 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010659 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10660 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010661 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010662
10663 // Load the 'nest' parameter value into R10.
10664 // R10 is specified in X86CallingConv.td
10665 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010666 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10667 DAG.getConstant(10, MVT::i64));
10668 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010669 Addr, MachinePointerInfo(TrmpAddr, 10),
10670 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010671
Owen Anderson825b72b2009-08-11 20:47:22 +000010672 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10673 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010674 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10675 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010676 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010677
10678 // Jump to the nested function.
10679 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010680 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10681 DAG.getConstant(20, MVT::i64));
10682 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010683 Addr, MachinePointerInfo(TrmpAddr, 20),
10684 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010685
10686 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010687 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10688 DAG.getConstant(22, MVT::i64));
10689 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010690 MachinePointerInfo(TrmpAddr, 22),
10691 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010692
Duncan Sands4a544a72011-09-06 13:37:06 +000010693 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010694 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010695 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010696 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010697 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010698 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010699
10700 switch (CC) {
10701 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010702 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010703 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010704 case CallingConv::X86_StdCall: {
10705 // Pass 'nest' parameter in ECX.
10706 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010707 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010708
10709 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010710 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010711 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010712
Chris Lattner58d74912008-03-12 17:45:29 +000010713 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010714 unsigned InRegCount = 0;
10715 unsigned Idx = 1;
10716
10717 for (FunctionType::param_iterator I = FTy->param_begin(),
10718 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010719 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010720 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010721 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010722
10723 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010724 report_fatal_error("Nest register in use - reduce number of inreg"
10725 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010726 }
10727 }
10728 break;
10729 }
10730 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010731 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010732 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010733 // Pass 'nest' parameter in EAX.
10734 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010735 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010736 break;
10737 }
10738
Dan Gohman475871a2008-07-27 21:46:04 +000010739 SDValue OutChains[4];
10740 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010741
Owen Anderson825b72b2009-08-11 20:47:22 +000010742 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10743 DAG.getConstant(10, MVT::i32));
10744 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010745
Chris Lattnera62fe662010-02-05 19:20:30 +000010746 // This is storing the opcode for MOV32ri.
10747 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010748 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010749 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010750 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010751 Trmp, MachinePointerInfo(TrmpAddr),
10752 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010753
Owen Anderson825b72b2009-08-11 20:47:22 +000010754 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10755 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010756 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10757 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010758 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010759
Chris Lattnera62fe662010-02-05 19:20:30 +000010760 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010761 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10762 DAG.getConstant(5, MVT::i32));
10763 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010764 MachinePointerInfo(TrmpAddr, 5),
10765 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010766
Owen Anderson825b72b2009-08-11 20:47:22 +000010767 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10768 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010769 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10770 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010771 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010772
Duncan Sands4a544a72011-09-06 13:37:06 +000010773 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010774 }
10775}
10776
Dan Gohmand858e902010-04-17 15:26:15 +000010777SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10778 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010779 /*
10780 The rounding mode is in bits 11:10 of FPSR, and has the following
10781 settings:
10782 00 Round to nearest
10783 01 Round to -inf
10784 10 Round to +inf
10785 11 Round to 0
10786
10787 FLT_ROUNDS, on the other hand, expects the following:
10788 -1 Undefined
10789 0 Round to 0
10790 1 Round to nearest
10791 2 Round to +inf
10792 3 Round to -inf
10793
10794 To perform the conversion, we do:
10795 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10796 */
10797
10798 MachineFunction &MF = DAG.getMachineFunction();
10799 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010800 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010801 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010802 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010803 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010804
10805 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010806 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010807 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010808
Michael J. Spencerec38de22010-10-10 22:04:20 +000010809
Chris Lattner2156b792010-09-22 01:11:26 +000010810 MachineMemOperand *MMO =
10811 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10812 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010813
Chris Lattner2156b792010-09-22 01:11:26 +000010814 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10815 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10816 DAG.getVTList(MVT::Other),
10817 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010818
10819 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010820 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010821 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010822
10823 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010824 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010825 DAG.getNode(ISD::SRL, DL, MVT::i16,
10826 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010827 CWD, DAG.getConstant(0x800, MVT::i16)),
10828 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010829 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010830 DAG.getNode(ISD::SRL, DL, MVT::i16,
10831 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010832 CWD, DAG.getConstant(0x400, MVT::i16)),
10833 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010834
Dan Gohman475871a2008-07-27 21:46:04 +000010835 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010836 DAG.getNode(ISD::AND, DL, MVT::i16,
10837 DAG.getNode(ISD::ADD, DL, MVT::i16,
10838 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010839 DAG.getConstant(1, MVT::i16)),
10840 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010841
10842
Duncan Sands83ec4b62008-06-06 12:08:01 +000010843 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010844 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010845}
10846
Craig Topper55b24052012-09-11 06:15:32 +000010847static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010848 EVT VT = Op.getValueType();
10849 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010850 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010851 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010852
10853 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010854 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010855 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010856 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010857 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010858 }
Evan Cheng18efe262007-12-14 02:13:44 +000010859
Evan Cheng152804e2007-12-14 08:30:15 +000010860 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010861 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010862 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010863
10864 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010865 SDValue Ops[] = {
10866 Op,
10867 DAG.getConstant(NumBits+NumBits-1, OpVT),
10868 DAG.getConstant(X86::COND_E, MVT::i8),
10869 Op.getValue(1)
10870 };
10871 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010872
10873 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010874 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010875
Owen Anderson825b72b2009-08-11 20:47:22 +000010876 if (VT == MVT::i8)
10877 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010878 return Op;
10879}
10880
Craig Topper55b24052012-09-11 06:15:32 +000010881static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010882 EVT VT = Op.getValueType();
10883 EVT OpVT = VT;
10884 unsigned NumBits = VT.getSizeInBits();
10885 DebugLoc dl = Op.getDebugLoc();
10886
10887 Op = Op.getOperand(0);
10888 if (VT == MVT::i8) {
10889 // Zero extend to i32 since there is not an i8 bsr.
10890 OpVT = MVT::i32;
10891 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10892 }
10893
10894 // Issue a bsr (scan bits in reverse).
10895 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10896 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10897
10898 // And xor with NumBits-1.
10899 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10900
10901 if (VT == MVT::i8)
10902 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10903 return Op;
10904}
10905
Craig Topper55b24052012-09-11 06:15:32 +000010906static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010907 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010908 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010909 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010910 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010911
10912 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010913 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010914 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010915
10916 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010917 SDValue Ops[] = {
10918 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010919 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010920 DAG.getConstant(X86::COND_E, MVT::i8),
10921 Op.getValue(1)
10922 };
Chandler Carruth77821022011-12-24 12:12:34 +000010923 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010924}
10925
Craig Topper13894fa2011-08-24 06:14:18 +000010926// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10927// ones, and then concatenate the result back.
10928static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010929 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010930
Craig Topper7a9a28b2012-08-12 02:23:29 +000010931 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010932 "Unsupported value type for operation");
10933
Craig Topper66ddd152012-04-27 22:54:43 +000010934 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010935 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010936
10937 // Extract the LHS vectors
10938 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010939 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10940 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010941
10942 // Extract the RHS vectors
10943 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010944 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10945 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010946
10947 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10948 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10949
10950 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10951 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10952 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10953}
10954
Craig Topper55b24052012-09-11 06:15:32 +000010955static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010956 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010957 Op.getValueType().isInteger() &&
10958 "Only handle AVX 256-bit vector integer operation");
10959 return Lower256IntArith(Op, DAG);
10960}
10961
Craig Topper55b24052012-09-11 06:15:32 +000010962static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010963 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010964 Op.getValueType().isInteger() &&
10965 "Only handle AVX 256-bit vector integer operation");
10966 return Lower256IntArith(Op, DAG);
10967}
10968
Craig Topper55b24052012-09-11 06:15:32 +000010969static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10970 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010971 EVT VT = Op.getValueType();
10972
10973 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010974 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010975 return Lower256IntArith(Op, DAG);
10976
Craig Topper5b209e82012-02-05 03:14:49 +000010977 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10978 "Only know how to lower V2I64/V4I64 multiply");
10979
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010980 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010981
Craig Topper5b209e82012-02-05 03:14:49 +000010982 // Ahi = psrlqi(a, 32);
10983 // Bhi = psrlqi(b, 32);
10984 //
10985 // AloBlo = pmuludq(a, b);
10986 // AloBhi = pmuludq(a, Bhi);
10987 // AhiBlo = pmuludq(Ahi, b);
10988
10989 // AloBhi = psllqi(AloBhi, 32);
10990 // AhiBlo = psllqi(AhiBlo, 32);
10991 // return AloBlo + AloBhi + AhiBlo;
10992
Craig Topperaaa643c2011-11-09 07:28:55 +000010993 SDValue A = Op.getOperand(0);
10994 SDValue B = Op.getOperand(1);
10995
Craig Topper5b209e82012-02-05 03:14:49 +000010996 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010997
Craig Topper5b209e82012-02-05 03:14:49 +000010998 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10999 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011000
Craig Topper5b209e82012-02-05 03:14:49 +000011001 // Bit cast to 32-bit vectors for MULUDQ
11002 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11003 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11004 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11005 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11006 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011007
Craig Topper5b209e82012-02-05 03:14:49 +000011008 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11009 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11010 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011011
Craig Topper5b209e82012-02-05 03:14:49 +000011012 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11013 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011014
Dale Johannesene4d209d2009-02-03 20:21:25 +000011015 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011016 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011017}
11018
Nadav Rotem43012222011-05-11 08:12:09 +000011019SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11020
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011021 EVT VT = Op.getValueType();
11022 DebugLoc dl = Op.getDebugLoc();
11023 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011024 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011025 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011026
Craig Topper1accb7e2012-01-10 06:54:16 +000011027 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011028 return SDValue();
11029
Nadav Rotem43012222011-05-11 08:12:09 +000011030 // Optimize shl/srl/sra with constant shift amount.
11031 if (isSplatVector(Amt.getNode())) {
11032 SDValue SclrAmt = Amt->getOperand(0);
11033 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11034 uint64_t ShiftAmt = C->getZExtValue();
11035
Craig Toppered2e13d2012-01-22 19:15:14 +000011036 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
11037 (Subtarget->hasAVX2() &&
11038 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11039 if (Op.getOpcode() == ISD::SHL)
11040 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11041 DAG.getConstant(ShiftAmt, MVT::i32));
11042 if (Op.getOpcode() == ISD::SRL)
11043 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11044 DAG.getConstant(ShiftAmt, MVT::i32));
11045 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11046 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11047 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011048 }
11049
Craig Toppered2e13d2012-01-22 19:15:14 +000011050 if (VT == MVT::v16i8) {
11051 if (Op.getOpcode() == ISD::SHL) {
11052 // Make a large shift.
11053 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11054 DAG.getConstant(ShiftAmt, MVT::i32));
11055 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11056 // Zero out the rightmost bits.
11057 SmallVector<SDValue, 16> V(16,
11058 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11059 MVT::i8));
11060 return DAG.getNode(ISD::AND, dl, VT, SHL,
11061 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011062 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011063 if (Op.getOpcode() == ISD::SRL) {
11064 // Make a large shift.
11065 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11066 DAG.getConstant(ShiftAmt, MVT::i32));
11067 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11068 // Zero out the leftmost bits.
11069 SmallVector<SDValue, 16> V(16,
11070 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11071 MVT::i8));
11072 return DAG.getNode(ISD::AND, dl, VT, SRL,
11073 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11074 }
11075 if (Op.getOpcode() == ISD::SRA) {
11076 if (ShiftAmt == 7) {
11077 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011078 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011079 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011080 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011081
Craig Toppered2e13d2012-01-22 19:15:14 +000011082 // R s>> a === ((R u>> a) ^ m) - m
11083 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11084 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11085 MVT::i8));
11086 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11087 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11088 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11089 return Res;
11090 }
Craig Topper731dfd02012-04-23 03:42:40 +000011091 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011092 }
Craig Topper46154eb2011-11-11 07:39:23 +000011093
Craig Topper0d86d462011-11-20 00:12:05 +000011094 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
11095 if (Op.getOpcode() == ISD::SHL) {
11096 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011097 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11098 DAG.getConstant(ShiftAmt, MVT::i32));
11099 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011100 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011101 SmallVector<SDValue, 32> V(32,
11102 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11103 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011104 return DAG.getNode(ISD::AND, dl, VT, SHL,
11105 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011106 }
Craig Topper0d86d462011-11-20 00:12:05 +000011107 if (Op.getOpcode() == ISD::SRL) {
11108 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011109 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11110 DAG.getConstant(ShiftAmt, MVT::i32));
11111 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011112 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011113 SmallVector<SDValue, 32> V(32,
11114 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11115 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011116 return DAG.getNode(ISD::AND, dl, VT, SRL,
11117 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11118 }
11119 if (Op.getOpcode() == ISD::SRA) {
11120 if (ShiftAmt == 7) {
11121 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011122 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011123 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011124 }
11125
11126 // R s>> a === ((R u>> a) ^ m) - m
11127 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11128 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11129 MVT::i8));
11130 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11131 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11132 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11133 return Res;
11134 }
Craig Topper731dfd02012-04-23 03:42:40 +000011135 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011136 }
Nadav Rotem43012222011-05-11 08:12:09 +000011137 }
11138 }
11139
11140 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011141 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011142 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11143 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011144
Chris Lattner7302d802012-02-06 21:56:39 +000011145 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11146 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011147 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11148 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011149 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011150 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011151
11152 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011153 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011154 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11155 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11156 }
Nadav Rotem43012222011-05-11 08:12:09 +000011157 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011158 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011159
Nate Begeman51409212010-07-28 00:21:48 +000011160 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011161 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11162 DAG.getConstant(5, MVT::i32));
11163 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011164
Lang Hames8b99c1e2011-12-17 01:08:46 +000011165 // Turn 'a' into a mask suitable for VSELECT
11166 SDValue VSelM = DAG.getConstant(0x80, VT);
11167 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011168 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011169
Lang Hames8b99c1e2011-12-17 01:08:46 +000011170 SDValue CM1 = DAG.getConstant(0x0f, VT);
11171 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011172
Lang Hames8b99c1e2011-12-17 01:08:46 +000011173 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11174 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011175 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11176 DAG.getConstant(4, MVT::i32), DAG);
11177 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011178 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11179
Nate Begeman51409212010-07-28 00:21:48 +000011180 // a += a
11181 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011182 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011183 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011184
Lang Hames8b99c1e2011-12-17 01:08:46 +000011185 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11186 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011187 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11188 DAG.getConstant(2, MVT::i32), DAG);
11189 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011190 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11191
Nate Begeman51409212010-07-28 00:21:48 +000011192 // a += a
11193 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011194 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011195 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011196
Lang Hames8b99c1e2011-12-17 01:08:46 +000011197 // return VSELECT(r, r+r, a);
11198 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011199 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011200 return R;
11201 }
Craig Topper46154eb2011-11-11 07:39:23 +000011202
11203 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011204 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011205 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011206 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11207 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11208
11209 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011210 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11211 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011212
11213 // Recreate the shift amount vectors
11214 SDValue Amt1, Amt2;
11215 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11216 // Constant shift amount
11217 SmallVector<SDValue, 4> Amt1Csts;
11218 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011219 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011220 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011221 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011222 Amt2Csts.push_back(Amt->getOperand(i));
11223
11224 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11225 &Amt1Csts[0], NumElems/2);
11226 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11227 &Amt2Csts[0], NumElems/2);
11228 } else {
11229 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011230 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11231 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011232 }
11233
11234 // Issue new vector shifts for the smaller types
11235 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11236 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11237
11238 // Concatenate the result back
11239 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11240 }
11241
Nate Begeman51409212010-07-28 00:21:48 +000011242 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011243}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011244
Craig Topper55b24052012-09-11 06:15:32 +000011245static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011246 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11247 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011248 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11249 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011250 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011251 SDValue LHS = N->getOperand(0);
11252 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011253 unsigned BaseOp = 0;
11254 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011255 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011256 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011257 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011258 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011259 // A subtract of one will be selected as a INC. Note that INC doesn't
11260 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011261 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11262 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011263 BaseOp = X86ISD::INC;
11264 Cond = X86::COND_O;
11265 break;
11266 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011267 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011268 Cond = X86::COND_O;
11269 break;
11270 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011271 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011272 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011273 break;
11274 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011275 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11276 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011277 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11278 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011279 BaseOp = X86ISD::DEC;
11280 Cond = X86::COND_O;
11281 break;
11282 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011283 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011284 Cond = X86::COND_O;
11285 break;
11286 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011287 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011288 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011289 break;
11290 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011291 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011292 Cond = X86::COND_O;
11293 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011294 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11295 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11296 MVT::i32);
11297 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011298
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011299 SDValue SetCC =
11300 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11301 DAG.getConstant(X86::COND_O, MVT::i32),
11302 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011303
Dan Gohman6e5fda22011-07-22 18:45:15 +000011304 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011305 }
Bill Wendling74c37652008-12-09 22:08:41 +000011306 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011307
Bill Wendling61edeb52008-12-02 01:06:39 +000011308 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011309 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011310 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011311
Bill Wendling61edeb52008-12-02 01:06:39 +000011312 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011313 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11314 DAG.getConstant(Cond, MVT::i32),
11315 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011316
Dan Gohman6e5fda22011-07-22 18:45:15 +000011317 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011318}
11319
Chad Rosier30450e82011-12-22 22:35:21 +000011320SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11321 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011322 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011323 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11324 EVT VT = Op.getValueType();
11325
Craig Toppered2e13d2012-01-22 19:15:14 +000011326 if (!Subtarget->hasSSE2() || !VT.isVector())
11327 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011328
Craig Toppered2e13d2012-01-22 19:15:14 +000011329 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11330 ExtraVT.getScalarType().getSizeInBits();
11331 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11332
11333 switch (VT.getSimpleVT().SimpleTy) {
11334 default: return SDValue();
11335 case MVT::v8i32:
11336 case MVT::v16i16:
11337 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011338 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011339 if (!Subtarget->hasAVX2()) {
11340 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011341 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011342
Craig Toppered2e13d2012-01-22 19:15:14 +000011343 // Extract the LHS vectors
11344 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011345 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11346 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011347
Craig Toppered2e13d2012-01-22 19:15:14 +000011348 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11349 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011350
Craig Toppered2e13d2012-01-22 19:15:14 +000011351 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011352 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011353 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11354 ExtraNumElems/2);
11355 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011356
Craig Toppered2e13d2012-01-22 19:15:14 +000011357 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11358 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011359
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011360 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011361 }
11362 // fall through
11363 case MVT::v4i32:
11364 case MVT::v8i16: {
11365 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11366 Op.getOperand(0), ShAmt, DAG);
11367 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011368 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011369 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011370}
11371
11372
Craig Topper55b24052012-09-11 06:15:32 +000011373static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11374 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011375 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011376
Eric Christopher77ed1352011-07-08 00:04:56 +000011377 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11378 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011379 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011380 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011381 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011382 SDValue Ops[] = {
11383 DAG.getRegister(X86::ESP, MVT::i32), // Base
11384 DAG.getTargetConstant(1, MVT::i8), // Scale
11385 DAG.getRegister(0, MVT::i32), // Index
11386 DAG.getTargetConstant(0, MVT::i32), // Disp
11387 DAG.getRegister(0, MVT::i32), // Segment.
11388 Zero,
11389 Chain
11390 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011391 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011392 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11393 array_lengthof(Ops));
11394 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011395 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011396
Eric Christopher9a9d2752010-07-22 02:48:34 +000011397 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011398 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011399 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011400
Chris Lattner132929a2010-08-14 17:26:09 +000011401 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11402 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11403 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11404 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011405
Chris Lattner132929a2010-08-14 17:26:09 +000011406 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11407 if (!Op1 && !Op2 && !Op3 && Op4)
11408 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011409
Chris Lattner132929a2010-08-14 17:26:09 +000011410 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11411 if (Op1 && !Op2 && !Op3 && !Op4)
11412 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011413
11414 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011415 // (MFENCE)>;
11416 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011417}
11418
Craig Topper55b24052012-09-11 06:15:32 +000011419static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11420 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011421 DebugLoc dl = Op.getDebugLoc();
11422 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11423 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11424 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11425 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11426
11427 // The only fence that needs an instruction is a sequentially-consistent
11428 // cross-thread fence.
11429 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11430 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11431 // no-sse2). There isn't any reason to disable it if the target processor
11432 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011433 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011434 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11435
11436 SDValue Chain = Op.getOperand(0);
11437 SDValue Zero = DAG.getConstant(0, MVT::i32);
11438 SDValue Ops[] = {
11439 DAG.getRegister(X86::ESP, MVT::i32), // Base
11440 DAG.getTargetConstant(1, MVT::i8), // Scale
11441 DAG.getRegister(0, MVT::i32), // Index
11442 DAG.getTargetConstant(0, MVT::i32), // Disp
11443 DAG.getRegister(0, MVT::i32), // Segment.
11444 Zero,
11445 Chain
11446 };
11447 SDNode *Res =
11448 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11449 array_lengthof(Ops));
11450 return SDValue(Res, 0);
11451 }
11452
11453 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11454 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11455}
11456
11457
Craig Topper55b24052012-09-11 06:15:32 +000011458static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11459 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011460 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011461 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011462 unsigned Reg = 0;
11463 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011464 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011465 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011466 case MVT::i8: Reg = X86::AL; size = 1; break;
11467 case MVT::i16: Reg = X86::AX; size = 2; break;
11468 case MVT::i32: Reg = X86::EAX; size = 4; break;
11469 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011470 assert(Subtarget->is64Bit() && "Node not type legal!");
11471 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011472 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011473 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011474 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011475 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011476 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011477 Op.getOperand(1),
11478 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011479 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011480 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011481 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011482 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11483 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11484 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011485 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011486 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011487 return cpOut;
11488}
11489
Craig Topper55b24052012-09-11 06:15:32 +000011490static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11491 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011492 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011493 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011494 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011495 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011496 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11498 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011499 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011500 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11501 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011502 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011503 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011504 rdx.getValue(1)
11505 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011506 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011507}
11508
Craig Topper55b24052012-09-11 06:15:32 +000011509SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011510 EVT SrcVT = Op.getOperand(0).getValueType();
11511 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011512 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011513 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011514 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011515 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011516 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011517 // i64 <=> MMX conversions are Legal.
11518 if (SrcVT==MVT::i64 && DstVT.isVector())
11519 return Op;
11520 if (DstVT==MVT::i64 && SrcVT.isVector())
11521 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011522 // MMX <=> MMX conversions are Legal.
11523 if (SrcVT.isVector() && DstVT.isVector())
11524 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011525 // All other conversions need to be expanded.
11526 return SDValue();
11527}
Chris Lattner5b856542010-12-20 00:59:46 +000011528
Craig Topper55b24052012-09-11 06:15:32 +000011529static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011530 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011531 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011532 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011533 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011534 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011535 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011536 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011537 Node->getOperand(0),
11538 Node->getOperand(1), negOp,
11539 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011540 cast<AtomicSDNode>(Node)->getAlignment(),
11541 cast<AtomicSDNode>(Node)->getOrdering(),
11542 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011543}
11544
Eli Friedman327236c2011-08-24 20:50:09 +000011545static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11546 SDNode *Node = Op.getNode();
11547 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011548 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011549
11550 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011551 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11552 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11553 // (The only way to get a 16-byte store is cmpxchg16b)
11554 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11555 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11556 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011557 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11558 cast<AtomicSDNode>(Node)->getMemoryVT(),
11559 Node->getOperand(0),
11560 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011561 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011562 cast<AtomicSDNode>(Node)->getOrdering(),
11563 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011564 return Swap.getValue(1);
11565 }
11566 // Other atomic stores have a simple pattern.
11567 return Op;
11568}
11569
Chris Lattner5b856542010-12-20 00:59:46 +000011570static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11571 EVT VT = Op.getNode()->getValueType(0);
11572
11573 // Let legalize expand this if it isn't a legal type yet.
11574 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11575 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011576
Chris Lattner5b856542010-12-20 00:59:46 +000011577 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011578
Chris Lattner5b856542010-12-20 00:59:46 +000011579 unsigned Opc;
11580 bool ExtraOp = false;
11581 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011582 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011583 case ISD::ADDC: Opc = X86ISD::ADD; break;
11584 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11585 case ISD::SUBC: Opc = X86ISD::SUB; break;
11586 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11587 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011588
Chris Lattner5b856542010-12-20 00:59:46 +000011589 if (!ExtraOp)
11590 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11591 Op.getOperand(1));
11592 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11593 Op.getOperand(1), Op.getOperand(2));
11594}
11595
Evan Cheng0db9fe62006-04-25 20:13:52 +000011596/// LowerOperation - Provide custom lowering hooks for some operations.
11597///
Dan Gohmand858e902010-04-17 15:26:15 +000011598SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011599 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011600 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011601 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011602 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11603 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11604 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011605 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011606 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011607 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011608 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011609 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11610 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11611 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011612 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11613 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011614 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11615 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11616 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011617 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011618 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011619 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011620 case ISD::SHL_PARTS:
11621 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011622 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011623 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011624 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011625 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Michael Liaoa7554632012-10-23 17:36:08 +000011626 case ISD::ZERO_EXTEND: return lowerZERO_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011627 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011628 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011629 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011630 case ISD::FABS: return LowerFABS(Op, DAG);
11631 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011632 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011633 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011634 case ISD::SETCC: return LowerSETCC(Op, DAG);
11635 case ISD::SELECT: return LowerSELECT(Op, DAG);
11636 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011637 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011638 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011639 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011640 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011641 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011642 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011643 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11644 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011645 case ISD::FRAME_TO_ARGS_OFFSET:
11646 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011647 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011648 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011649 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11650 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011651 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11652 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011653 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011654 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011655 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011656 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011657 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011658 case ISD::SRA:
11659 case ISD::SRL:
11660 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011661 case ISD::SADDO:
11662 case ISD::UADDO:
11663 case ISD::SSUBO:
11664 case ISD::USUBO:
11665 case ISD::SMULO:
11666 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011667 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011668 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011669 case ISD::ADDC:
11670 case ISD::ADDE:
11671 case ISD::SUBC:
11672 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011673 case ISD::ADD: return LowerADD(Op, DAG);
11674 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011675 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011676}
11677
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011678static void ReplaceATOMIC_LOAD(SDNode *Node,
11679 SmallVectorImpl<SDValue> &Results,
11680 SelectionDAG &DAG) {
11681 DebugLoc dl = Node->getDebugLoc();
11682 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11683
11684 // Convert wide load -> cmpxchg8b/cmpxchg16b
11685 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11686 // (The only way to get a 16-byte load is cmpxchg16b)
11687 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011688 SDValue Zero = DAG.getConstant(0, VT);
11689 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011690 Node->getOperand(0),
11691 Node->getOperand(1), Zero, Zero,
11692 cast<AtomicSDNode>(Node)->getMemOperand(),
11693 cast<AtomicSDNode>(Node)->getOrdering(),
11694 cast<AtomicSDNode>(Node)->getSynchScope());
11695 Results.push_back(Swap.getValue(0));
11696 Results.push_back(Swap.getValue(1));
11697}
11698
Craig Topperc0878702012-08-17 06:55:11 +000011699static void
Duncan Sands1607f052008-12-01 11:39:25 +000011700ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011701 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011702 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011703 assert (Node->getValueType(0) == MVT::i64 &&
11704 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011705
11706 SDValue Chain = Node->getOperand(0);
11707 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011708 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011709 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011710 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011711 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011712 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011713 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011714 SDValue Result =
11715 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11716 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011717 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011718 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011719 Results.push_back(Result.getValue(2));
11720}
11721
Duncan Sands126d9072008-07-04 11:47:58 +000011722/// ReplaceNodeResults - Replace a node with an illegal result type
11723/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011724void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11725 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011726 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011727 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011728 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011729 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011730 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011731 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011732 case ISD::ADDC:
11733 case ISD::ADDE:
11734 case ISD::SUBC:
11735 case ISD::SUBE:
11736 // We don't want to expand or promote these.
11737 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011738 case ISD::FP_TO_SINT:
11739 case ISD::FP_TO_UINT: {
11740 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11741
11742 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11743 return;
11744
Eli Friedman948e95a2009-05-23 09:59:16 +000011745 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011746 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011747 SDValue FIST = Vals.first, StackSlot = Vals.second;
11748 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011749 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011750 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011751 if (StackSlot.getNode() != 0)
11752 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11753 MachinePointerInfo(),
11754 false, false, false, 0));
11755 else
11756 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011757 }
11758 return;
11759 }
Michael Liao991b6a22012-10-24 04:09:32 +000011760 case ISD::UINT_TO_FP: {
11761 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
11762 N->getValueType(0) != MVT::v2f32)
11763 return;
11764 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
11765 N->getOperand(0));
11766 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
11767 MVT::f64);
11768 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
11769 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
11770 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
11771 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
11772 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
11773 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
11774 return;
11775 }
Michael Liao44c2d612012-10-10 16:53:28 +000011776 case ISD::FP_ROUND: {
11777 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11778 Results.push_back(V);
11779 return;
11780 }
Duncan Sands1607f052008-12-01 11:39:25 +000011781 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011782 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011783 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011784 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011785 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011786 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011787 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011788 eax.getValue(2));
11789 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11790 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011791 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011792 Results.push_back(edx.getValue(1));
11793 return;
11794 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011795 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011796 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011797 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011798 bool Regs64bit = T == MVT::i128;
11799 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011800 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011801 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11802 DAG.getConstant(0, HalfT));
11803 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11804 DAG.getConstant(1, HalfT));
11805 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11806 Regs64bit ? X86::RAX : X86::EAX,
11807 cpInL, SDValue());
11808 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11809 Regs64bit ? X86::RDX : X86::EDX,
11810 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011811 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011812 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11813 DAG.getConstant(0, HalfT));
11814 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11815 DAG.getConstant(1, HalfT));
11816 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11817 Regs64bit ? X86::RBX : X86::EBX,
11818 swapInL, cpInH.getValue(1));
11819 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011820 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011821 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011822 SDValue Ops[] = { swapInH.getValue(0),
11823 N->getOperand(1),
11824 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011825 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011826 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011827 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11828 X86ISD::LCMPXCHG8_DAG;
11829 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011830 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011831 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11832 Regs64bit ? X86::RAX : X86::EAX,
11833 HalfT, Result.getValue(1));
11834 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11835 Regs64bit ? X86::RDX : X86::EDX,
11836 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011837 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011838 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011839 Results.push_back(cpOutH.getValue(1));
11840 return;
11841 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011842 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011843 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011844 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011845 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011846 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011847 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011848 case ISD::ATOMIC_LOAD_MAX:
11849 case ISD::ATOMIC_LOAD_MIN:
11850 case ISD::ATOMIC_LOAD_UMAX:
11851 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011852 case ISD::ATOMIC_SWAP: {
11853 unsigned Opc;
11854 switch (N->getOpcode()) {
11855 default: llvm_unreachable("Unexpected opcode");
11856 case ISD::ATOMIC_LOAD_ADD:
11857 Opc = X86ISD::ATOMADD64_DAG;
11858 break;
11859 case ISD::ATOMIC_LOAD_AND:
11860 Opc = X86ISD::ATOMAND64_DAG;
11861 break;
11862 case ISD::ATOMIC_LOAD_NAND:
11863 Opc = X86ISD::ATOMNAND64_DAG;
11864 break;
11865 case ISD::ATOMIC_LOAD_OR:
11866 Opc = X86ISD::ATOMOR64_DAG;
11867 break;
11868 case ISD::ATOMIC_LOAD_SUB:
11869 Opc = X86ISD::ATOMSUB64_DAG;
11870 break;
11871 case ISD::ATOMIC_LOAD_XOR:
11872 Opc = X86ISD::ATOMXOR64_DAG;
11873 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011874 case ISD::ATOMIC_LOAD_MAX:
11875 Opc = X86ISD::ATOMMAX64_DAG;
11876 break;
11877 case ISD::ATOMIC_LOAD_MIN:
11878 Opc = X86ISD::ATOMMIN64_DAG;
11879 break;
11880 case ISD::ATOMIC_LOAD_UMAX:
11881 Opc = X86ISD::ATOMUMAX64_DAG;
11882 break;
11883 case ISD::ATOMIC_LOAD_UMIN:
11884 Opc = X86ISD::ATOMUMIN64_DAG;
11885 break;
Craig Topperc0878702012-08-17 06:55:11 +000011886 case ISD::ATOMIC_SWAP:
11887 Opc = X86ISD::ATOMSWAP64_DAG;
11888 break;
11889 }
11890 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011891 return;
Craig Topperc0878702012-08-17 06:55:11 +000011892 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011893 case ISD::ATOMIC_LOAD:
11894 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011895 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011896}
11897
Evan Cheng72261582005-12-20 06:22:03 +000011898const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11899 switch (Opcode) {
11900 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011901 case X86ISD::BSF: return "X86ISD::BSF";
11902 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011903 case X86ISD::SHLD: return "X86ISD::SHLD";
11904 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011905 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011906 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011907 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011908 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011909 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011910 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011911 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11912 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11913 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011914 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011915 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011916 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011917 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011918 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011919 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011920 case X86ISD::COMI: return "X86ISD::COMI";
11921 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011922 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011923 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011924 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11925 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011926 case X86ISD::CMOV: return "X86ISD::CMOV";
11927 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011928 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011929 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11930 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011931 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011932 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011933 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011934 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011935 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011936 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11937 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011938 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011939 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011940 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011941 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011942 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011943 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11944 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11945 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011946 case X86ISD::HADD: return "X86ISD::HADD";
11947 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011948 case X86ISD::FHADD: return "X86ISD::FHADD";
11949 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011950 case X86ISD::FMAX: return "X86ISD::FMAX";
11951 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011952 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11953 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011954 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11955 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011956 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011957 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011958 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000011959 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
11960 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011961 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011962 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011963 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011964 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011965 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11966 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011967 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11968 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11969 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11970 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11971 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11972 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011973 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011974 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011975 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000011976 case X86ISD::VZEXT: return "X86ISD::VZEXT";
11977 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000011978 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000011979 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000011980 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11981 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011982 case X86ISD::VSHL: return "X86ISD::VSHL";
11983 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011984 case X86ISD::VSRA: return "X86ISD::VSRA";
11985 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11986 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11987 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011988 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011989 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11990 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011991 case X86ISD::ADD: return "X86ISD::ADD";
11992 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011993 case X86ISD::ADC: return "X86ISD::ADC";
11994 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011995 case X86ISD::SMUL: return "X86ISD::SMUL";
11996 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011997 case X86ISD::INC: return "X86ISD::INC";
11998 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011999 case X86ISD::OR: return "X86ISD::OR";
12000 case X86ISD::XOR: return "X86ISD::XOR";
12001 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000012002 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000012003 case X86ISD::BLSI: return "X86ISD::BLSI";
12004 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12005 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012006 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012007 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012008 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012009 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12010 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12011 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012012 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012013 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012014 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012015 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012016 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012017 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12018 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012019 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12020 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12021 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012022 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12023 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012024 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12025 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012026 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012027 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012028 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012029 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12030 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012031 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012032 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012033 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012034 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012035 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012036 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012037 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012038 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012039 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012040 case X86ISD::FMADD: return "X86ISD::FMADD";
12041 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12042 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12043 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12044 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12045 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000012046 }
12047}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012048
Chris Lattnerc9addb72007-03-30 23:15:24 +000012049// isLegalAddressingMode - Return true if the addressing mode represented
12050// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012051bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012052 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012053 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012054 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012055 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012056
Chris Lattnerc9addb72007-03-30 23:15:24 +000012057 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012058 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012059 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012060
Chris Lattnerc9addb72007-03-30 23:15:24 +000012061 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012062 unsigned GVFlags =
12063 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012064
Chris Lattnerdfed4132009-07-10 07:38:24 +000012065 // If a reference to this global requires an extra load, we can't fold it.
12066 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012067 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012068
Chris Lattnerdfed4132009-07-10 07:38:24 +000012069 // If BaseGV requires a register for the PIC base, we cannot also have a
12070 // BaseReg specified.
12071 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012072 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012073
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012074 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012075 if ((M != CodeModel::Small || R != Reloc::Static) &&
12076 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012077 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012078 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012079
Chris Lattnerc9addb72007-03-30 23:15:24 +000012080 switch (AM.Scale) {
12081 case 0:
12082 case 1:
12083 case 2:
12084 case 4:
12085 case 8:
12086 // These scales always work.
12087 break;
12088 case 3:
12089 case 5:
12090 case 9:
12091 // These scales are formed with basereg+scalereg. Only accept if there is
12092 // no basereg yet.
12093 if (AM.HasBaseReg)
12094 return false;
12095 break;
12096 default: // Other stuff never works.
12097 return false;
12098 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012099
Chris Lattnerc9addb72007-03-30 23:15:24 +000012100 return true;
12101}
12102
12103
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012104bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012105 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012106 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012107 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12108 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012109 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000012110 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012111 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012112}
12113
Evan Cheng70e10d32012-07-17 06:53:39 +000012114bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12115 return Imm == (int32_t)Imm;
12116}
12117
12118bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012119 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000012120 return Imm == (int32_t)Imm;
12121}
12122
Owen Andersone50ed302009-08-10 22:56:29 +000012123bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012124 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012125 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012126 unsigned NumBits1 = VT1.getSizeInBits();
12127 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012128 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012129 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012130 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012131}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012132
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012133bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012134 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012135 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012136}
12137
Owen Andersone50ed302009-08-10 22:56:29 +000012138bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012139 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012140 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012141}
12142
Owen Andersone50ed302009-08-10 22:56:29 +000012143bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012144 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012145 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012146}
12147
Evan Cheng60c07e12006-07-05 22:17:51 +000012148/// isShuffleMaskLegal - Targets can use this to indicate that they only
12149/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12150/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12151/// are assumed to be legal.
12152bool
Eric Christopherfd179292009-08-27 18:07:15 +000012153X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012154 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012155 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012156 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012157 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012158
Nate Begemana09008b2009-10-19 02:17:23 +000012159 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012160 return (VT.getVectorNumElements() == 2 ||
12161 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12162 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012163 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012164 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000012165 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
12166 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012167 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000012168 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
12169 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000012170 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
12171 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012172}
12173
Dan Gohman7d8143f2008-04-09 20:09:42 +000012174bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012175X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012176 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012177 unsigned NumElts = VT.getVectorNumElements();
12178 // FIXME: This collection of masks seems suspect.
12179 if (NumElts == 2)
12180 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012181 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012182 return (isMOVLMask(Mask, VT) ||
12183 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012184 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
12185 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012186 }
12187 return false;
12188}
12189
12190//===----------------------------------------------------------------------===//
12191// X86 Scheduler Hooks
12192//===----------------------------------------------------------------------===//
12193
Mon P Wang63307c32008-05-05 19:05:59 +000012194// private utility function
Scott Michelfdc40a02009-02-17 22:15:04 +000012195
Michael Liaob118a072012-09-20 03:06:15 +000012196// Get CMPXCHG opcode for the specified data type.
12197static unsigned getCmpXChgOpcode(EVT VT) {
12198 switch (VT.getSimpleVT().SimpleTy) {
12199 case MVT::i8: return X86::LCMPXCHG8;
12200 case MVT::i16: return X86::LCMPXCHG16;
12201 case MVT::i32: return X86::LCMPXCHG32;
12202 case MVT::i64: return X86::LCMPXCHG64;
12203 default:
12204 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012205 }
Michael Liaob118a072012-09-20 03:06:15 +000012206 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012207}
12208
Michael Liaob118a072012-09-20 03:06:15 +000012209// Get LOAD opcode for the specified data type.
12210static unsigned getLoadOpcode(EVT VT) {
12211 switch (VT.getSimpleVT().SimpleTy) {
12212 case MVT::i8: return X86::MOV8rm;
12213 case MVT::i16: return X86::MOV16rm;
12214 case MVT::i32: return X86::MOV32rm;
12215 case MVT::i64: return X86::MOV64rm;
12216 default:
12217 break;
12218 }
12219 llvm_unreachable("Invalid operand size!");
12220}
12221
12222// Get opcode of the non-atomic one from the specified atomic instruction.
12223static unsigned getNonAtomicOpcode(unsigned Opc) {
12224 switch (Opc) {
12225 case X86::ATOMAND8: return X86::AND8rr;
12226 case X86::ATOMAND16: return X86::AND16rr;
12227 case X86::ATOMAND32: return X86::AND32rr;
12228 case X86::ATOMAND64: return X86::AND64rr;
12229 case X86::ATOMOR8: return X86::OR8rr;
12230 case X86::ATOMOR16: return X86::OR16rr;
12231 case X86::ATOMOR32: return X86::OR32rr;
12232 case X86::ATOMOR64: return X86::OR64rr;
12233 case X86::ATOMXOR8: return X86::XOR8rr;
12234 case X86::ATOMXOR16: return X86::XOR16rr;
12235 case X86::ATOMXOR32: return X86::XOR32rr;
12236 case X86::ATOMXOR64: return X86::XOR64rr;
12237 }
12238 llvm_unreachable("Unhandled atomic-load-op opcode!");
12239}
12240
12241// Get opcode of the non-atomic one from the specified atomic instruction with
12242// extra opcode.
12243static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12244 unsigned &ExtraOpc) {
12245 switch (Opc) {
12246 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12247 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12248 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12249 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012250 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012251 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12252 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12253 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012254 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012255 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12256 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12257 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012258 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012259 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12260 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12261 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012262 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012263 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12264 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12265 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12266 }
12267 llvm_unreachable("Unhandled atomic-load-op opcode!");
12268}
12269
12270// Get opcode of the non-atomic one from the specified atomic instruction for
12271// 64-bit data type on 32-bit target.
12272static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12273 switch (Opc) {
12274 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12275 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12276 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12277 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12278 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12279 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012280 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12281 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12282 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12283 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012284 }
12285 llvm_unreachable("Unhandled atomic-load-op opcode!");
12286}
12287
12288// Get opcode of the non-atomic one from the specified atomic instruction for
12289// 64-bit data type on 32-bit target with extra opcode.
12290static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12291 unsigned &HiOpc,
12292 unsigned &ExtraOpc) {
12293 switch (Opc) {
12294 case X86::ATOMNAND6432:
12295 ExtraOpc = X86::NOT32r;
12296 HiOpc = X86::AND32rr;
12297 return X86::AND32rr;
12298 }
12299 llvm_unreachable("Unhandled atomic-load-op opcode!");
12300}
12301
12302// Get pseudo CMOV opcode from the specified data type.
12303static unsigned getPseudoCMOVOpc(EVT VT) {
12304 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012305 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012306 case MVT::i16: return X86::CMOV_GR16;
12307 case MVT::i32: return X86::CMOV_GR32;
12308 default:
12309 break;
12310 }
12311 llvm_unreachable("Unknown CMOV opcode!");
12312}
12313
12314// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12315// They will be translated into a spin-loop or compare-exchange loop from
12316//
12317// ...
12318// dst = atomic-fetch-op MI.addr, MI.val
12319// ...
12320//
12321// to
12322//
12323// ...
12324// EAX = LOAD MI.addr
12325// loop:
12326// t1 = OP MI.val, EAX
12327// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12328// JNE loop
12329// sink:
12330// dst = EAX
12331// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012332MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012333X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12334 MachineBasicBlock *MBB) const {
12335 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12336 DebugLoc DL = MI->getDebugLoc();
12337
12338 MachineFunction *MF = MBB->getParent();
12339 MachineRegisterInfo &MRI = MF->getRegInfo();
12340
12341 const BasicBlock *BB = MBB->getBasicBlock();
12342 MachineFunction::iterator I = MBB;
12343 ++I;
12344
12345 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12346 "Unexpected number of operands");
12347
12348 assert(MI->hasOneMemOperand() &&
12349 "Expected atomic-load-op to have one memoperand");
12350
12351 // Memory Reference
12352 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12353 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12354
12355 unsigned DstReg, SrcReg;
12356 unsigned MemOpndSlot;
12357
12358 unsigned CurOp = 0;
12359
12360 DstReg = MI->getOperand(CurOp++).getReg();
12361 MemOpndSlot = CurOp;
12362 CurOp += X86::AddrNumOperands;
12363 SrcReg = MI->getOperand(CurOp++).getReg();
12364
12365 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012366 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012367 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12368
12369 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12370 unsigned LOADOpc = getLoadOpcode(VT);
12371
12372 // For the atomic load-arith operator, we generate
12373 //
12374 // thisMBB:
12375 // EAX = LOAD [MI.addr]
12376 // mainMBB:
12377 // t1 = OP MI.val, EAX
12378 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12379 // JNE mainMBB
12380 // sinkMBB:
12381
12382 MachineBasicBlock *thisMBB = MBB;
12383 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12384 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12385 MF->insert(I, mainMBB);
12386 MF->insert(I, sinkMBB);
12387
12388 MachineInstrBuilder MIB;
12389
12390 // Transfer the remainder of BB and its successor edges to sinkMBB.
12391 sinkMBB->splice(sinkMBB->begin(), MBB,
12392 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12393 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12394
12395 // thisMBB:
12396 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12397 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12398 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12399 MIB.setMemRefs(MMOBegin, MMOEnd);
12400
12401 thisMBB->addSuccessor(mainMBB);
12402
12403 // mainMBB:
12404 MachineBasicBlock *origMainMBB = mainMBB;
12405 mainMBB->addLiveIn(AccPhyReg);
12406
12407 // Copy AccPhyReg as it is used more than once.
12408 unsigned AccReg = MRI.createVirtualRegister(RC);
12409 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12410 .addReg(AccPhyReg);
12411
12412 unsigned t1 = MRI.createVirtualRegister(RC);
12413 unsigned Opc = MI->getOpcode();
12414 switch (Opc) {
12415 default:
12416 llvm_unreachable("Unhandled atomic-load-op opcode!");
12417 case X86::ATOMAND8:
12418 case X86::ATOMAND16:
12419 case X86::ATOMAND32:
12420 case X86::ATOMAND64:
12421 case X86::ATOMOR8:
12422 case X86::ATOMOR16:
12423 case X86::ATOMOR32:
12424 case X86::ATOMOR64:
12425 case X86::ATOMXOR8:
12426 case X86::ATOMXOR16:
12427 case X86::ATOMXOR32:
12428 case X86::ATOMXOR64: {
12429 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12430 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12431 .addReg(AccReg);
12432 break;
12433 }
12434 case X86::ATOMNAND8:
12435 case X86::ATOMNAND16:
12436 case X86::ATOMNAND32:
12437 case X86::ATOMNAND64: {
12438 unsigned t2 = MRI.createVirtualRegister(RC);
12439 unsigned NOTOpc;
12440 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12441 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12442 .addReg(AccReg);
12443 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12444 break;
12445 }
Michael Liao08382492012-09-21 03:00:17 +000012446 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012447 case X86::ATOMMAX16:
12448 case X86::ATOMMAX32:
12449 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012450 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012451 case X86::ATOMMIN16:
12452 case X86::ATOMMIN32:
12453 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012454 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012455 case X86::ATOMUMAX16:
12456 case X86::ATOMUMAX32:
12457 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012458 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012459 case X86::ATOMUMIN16:
12460 case X86::ATOMUMIN32:
12461 case X86::ATOMUMIN64: {
12462 unsigned CMPOpc;
12463 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12464
12465 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12466 .addReg(SrcReg)
12467 .addReg(AccReg);
12468
12469 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012470 if (VT != MVT::i8) {
12471 // Native support
12472 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12473 .addReg(SrcReg)
12474 .addReg(AccReg);
12475 } else {
12476 // Promote i8 to i32 to use CMOV32
12477 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12478 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12479 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12480 unsigned t2 = MRI.createVirtualRegister(RC32);
12481
12482 unsigned Undef = MRI.createVirtualRegister(RC32);
12483 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12484
12485 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12486 .addReg(Undef)
12487 .addReg(SrcReg)
12488 .addImm(X86::sub_8bit);
12489 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12490 .addReg(Undef)
12491 .addReg(AccReg)
12492 .addImm(X86::sub_8bit);
12493
12494 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12495 .addReg(SrcReg32)
12496 .addReg(AccReg32);
12497
12498 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12499 .addReg(t2, 0, X86::sub_8bit);
12500 }
Michael Liaob118a072012-09-20 03:06:15 +000012501 } else {
12502 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012503 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012504 "Invalid atomic-load-op transformation!");
12505 unsigned SelOpc = getPseudoCMOVOpc(VT);
12506 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12507 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12508 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12509 .addReg(SrcReg).addReg(AccReg)
12510 .addImm(CC);
12511 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12512 }
12513 break;
12514 }
12515 }
12516
12517 // Copy AccPhyReg back from virtual register.
12518 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12519 .addReg(AccReg);
12520
12521 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12522 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12523 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12524 MIB.addReg(t1);
12525 MIB.setMemRefs(MMOBegin, MMOEnd);
12526
12527 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12528
12529 mainMBB->addSuccessor(origMainMBB);
12530 mainMBB->addSuccessor(sinkMBB);
12531
12532 // sinkMBB:
12533 sinkMBB->addLiveIn(AccPhyReg);
12534
12535 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12536 TII->get(TargetOpcode::COPY), DstReg)
12537 .addReg(AccPhyReg);
12538
12539 MI->eraseFromParent();
12540 return sinkMBB;
12541}
12542
12543// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12544// instructions. They will be translated into a spin-loop or compare-exchange
12545// loop from
12546//
12547// ...
12548// dst = atomic-fetch-op MI.addr, MI.val
12549// ...
12550//
12551// to
12552//
12553// ...
12554// EAX = LOAD [MI.addr + 0]
12555// EDX = LOAD [MI.addr + 4]
12556// loop:
12557// EBX = OP MI.val.lo, EAX
12558// ECX = OP MI.val.hi, EDX
12559// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12560// JNE loop
12561// sink:
12562// dst = EDX:EAX
12563// ...
12564MachineBasicBlock *
12565X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12566 MachineBasicBlock *MBB) const {
12567 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12568 DebugLoc DL = MI->getDebugLoc();
12569
12570 MachineFunction *MF = MBB->getParent();
12571 MachineRegisterInfo &MRI = MF->getRegInfo();
12572
12573 const BasicBlock *BB = MBB->getBasicBlock();
12574 MachineFunction::iterator I = MBB;
12575 ++I;
12576
12577 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12578 "Unexpected number of operands");
12579
12580 assert(MI->hasOneMemOperand() &&
12581 "Expected atomic-load-op32 to have one memoperand");
12582
12583 // Memory Reference
12584 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12585 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12586
12587 unsigned DstLoReg, DstHiReg;
12588 unsigned SrcLoReg, SrcHiReg;
12589 unsigned MemOpndSlot;
12590
12591 unsigned CurOp = 0;
12592
12593 DstLoReg = MI->getOperand(CurOp++).getReg();
12594 DstHiReg = MI->getOperand(CurOp++).getReg();
12595 MemOpndSlot = CurOp;
12596 CurOp += X86::AddrNumOperands;
12597 SrcLoReg = MI->getOperand(CurOp++).getReg();
12598 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012599
Craig Topperc9099502012-04-20 06:31:50 +000012600 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012601 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012602
Michael Liaob118a072012-09-20 03:06:15 +000012603 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12604 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012605
Michael Liaob118a072012-09-20 03:06:15 +000012606 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012607 //
Michael Liaob118a072012-09-20 03:06:15 +000012608 // thisMBB:
12609 // EAX = LOAD [MI.addr + 0]
12610 // EDX = LOAD [MI.addr + 4]
12611 // mainMBB:
12612 // EBX = OP MI.vallo, EAX
12613 // ECX = OP MI.valhi, EDX
12614 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12615 // JNE mainMBB
12616 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012617
Mon P Wang63307c32008-05-05 19:05:59 +000012618 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012619 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12620 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12621 MF->insert(I, mainMBB);
12622 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012623
Michael Liaob118a072012-09-20 03:06:15 +000012624 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012625
Michael Liaob118a072012-09-20 03:06:15 +000012626 // Transfer the remainder of BB and its successor edges to sinkMBB.
12627 sinkMBB->splice(sinkMBB->begin(), MBB,
12628 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12629 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012630
Michael Liaob118a072012-09-20 03:06:15 +000012631 // thisMBB:
12632 // Lo
12633 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12634 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12635 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12636 MIB.setMemRefs(MMOBegin, MMOEnd);
12637 // Hi
12638 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12639 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012640 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012641 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012642 else
Michael Liaob118a072012-09-20 03:06:15 +000012643 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12644 }
12645 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012646
Michael Liaob118a072012-09-20 03:06:15 +000012647 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012648
Michael Liaob118a072012-09-20 03:06:15 +000012649 // mainMBB:
12650 MachineBasicBlock *origMainMBB = mainMBB;
12651 mainMBB->addLiveIn(X86::EAX);
12652 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012653
Michael Liaob118a072012-09-20 03:06:15 +000012654 // Copy EDX:EAX as they are used more than once.
12655 unsigned LoReg = MRI.createVirtualRegister(RC);
12656 unsigned HiReg = MRI.createVirtualRegister(RC);
12657 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12658 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012659
Michael Liaob118a072012-09-20 03:06:15 +000012660 unsigned t1L = MRI.createVirtualRegister(RC);
12661 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012662
Michael Liaob118a072012-09-20 03:06:15 +000012663 unsigned Opc = MI->getOpcode();
12664 switch (Opc) {
12665 default:
12666 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12667 case X86::ATOMAND6432:
12668 case X86::ATOMOR6432:
12669 case X86::ATOMXOR6432:
12670 case X86::ATOMADD6432:
12671 case X86::ATOMSUB6432: {
12672 unsigned HiOpc;
12673 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12674 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg).addReg(LoReg);
12675 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg).addReg(HiReg);
12676 break;
12677 }
12678 case X86::ATOMNAND6432: {
12679 unsigned HiOpc, NOTOpc;
12680 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12681 unsigned t2L = MRI.createVirtualRegister(RC);
12682 unsigned t2H = MRI.createVirtualRegister(RC);
12683 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12684 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12685 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12686 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12687 break;
12688 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012689 case X86::ATOMMAX6432:
12690 case X86::ATOMMIN6432:
12691 case X86::ATOMUMAX6432:
12692 case X86::ATOMUMIN6432: {
12693 unsigned HiOpc;
12694 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12695 unsigned cL = MRI.createVirtualRegister(RC8);
12696 unsigned cH = MRI.createVirtualRegister(RC8);
12697 unsigned cL32 = MRI.createVirtualRegister(RC);
12698 unsigned cH32 = MRI.createVirtualRegister(RC);
12699 unsigned cc = MRI.createVirtualRegister(RC);
12700 // cl := cmp src_lo, lo
12701 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12702 .addReg(SrcLoReg).addReg(LoReg);
12703 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12704 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12705 // ch := cmp src_hi, hi
12706 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12707 .addReg(SrcHiReg).addReg(HiReg);
12708 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12709 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12710 // cc := if (src_hi == hi) ? cl : ch;
12711 if (Subtarget->hasCMov()) {
12712 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12713 .addReg(cH32).addReg(cL32);
12714 } else {
12715 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12716 .addReg(cH32).addReg(cL32)
12717 .addImm(X86::COND_E);
12718 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12719 }
12720 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12721 if (Subtarget->hasCMov()) {
12722 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12723 .addReg(SrcLoReg).addReg(LoReg);
12724 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12725 .addReg(SrcHiReg).addReg(HiReg);
12726 } else {
12727 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12728 .addReg(SrcLoReg).addReg(LoReg)
12729 .addImm(X86::COND_NE);
12730 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12731 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12732 .addReg(SrcHiReg).addReg(HiReg)
12733 .addImm(X86::COND_NE);
12734 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12735 }
12736 break;
12737 }
Michael Liaob118a072012-09-20 03:06:15 +000012738 case X86::ATOMSWAP6432: {
12739 unsigned HiOpc;
12740 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12741 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12742 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12743 break;
12744 }
12745 }
Mon P Wang63307c32008-05-05 19:05:59 +000012746
Michael Liaob118a072012-09-20 03:06:15 +000012747 // Copy EDX:EAX back from HiReg:LoReg
12748 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12749 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12750 // Copy ECX:EBX from t1H:t1L
12751 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12752 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012753
Michael Liaob118a072012-09-20 03:06:15 +000012754 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12755 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12756 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12757 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012758
Michael Liaob118a072012-09-20 03:06:15 +000012759 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012760
Michael Liaob118a072012-09-20 03:06:15 +000012761 mainMBB->addSuccessor(origMainMBB);
12762 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012763
Michael Liaob118a072012-09-20 03:06:15 +000012764 // sinkMBB:
12765 sinkMBB->addLiveIn(X86::EAX);
12766 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012767
Michael Liaob118a072012-09-20 03:06:15 +000012768 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12769 TII->get(TargetOpcode::COPY), DstLoReg)
12770 .addReg(X86::EAX);
12771 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12772 TII->get(TargetOpcode::COPY), DstHiReg)
12773 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012774
Michael Liaob118a072012-09-20 03:06:15 +000012775 MI->eraseFromParent();
12776 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012777}
12778
Eric Christopherf83a5de2009-08-27 18:08:16 +000012779// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012780// or XMM0_V32I8 in AVX all of this code can be replaced with that
12781// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012782MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012783X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012784 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012785 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012786 "Target must have SSE4.2 or AVX features enabled");
12787
Eric Christopherb120ab42009-08-18 22:50:32 +000012788 DebugLoc dl = MI->getDebugLoc();
12789 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012790 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012791 if (!Subtarget->hasAVX()) {
12792 if (memArg)
12793 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12794 else
12795 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12796 } else {
12797 if (memArg)
12798 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12799 else
12800 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12801 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012802
Eric Christopher41c902f2010-11-30 08:20:21 +000012803 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012804 for (unsigned i = 0; i < numArgs; ++i) {
12805 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012806 if (!(Op.isReg() && Op.isImplicit()))
12807 MIB.addOperand(Op);
12808 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012809 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012810 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012811 .addReg(X86::XMM0);
12812
Dan Gohman14152b42010-07-06 20:24:04 +000012813 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012814 return BB;
12815}
12816
12817MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012818X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012819 DebugLoc dl = MI->getDebugLoc();
12820 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012821
Eric Christopher228232b2010-11-30 07:20:12 +000012822 // Address into RAX/EAX, other two args into ECX, EDX.
12823 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12824 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12825 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12826 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012827 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012828
Eric Christopher228232b2010-11-30 07:20:12 +000012829 unsigned ValOps = X86::AddrNumOperands;
12830 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12831 .addReg(MI->getOperand(ValOps).getReg());
12832 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12833 .addReg(MI->getOperand(ValOps+1).getReg());
12834
12835 // The instruction doesn't actually take any operands though.
12836 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012837
Eric Christopher228232b2010-11-30 07:20:12 +000012838 MI->eraseFromParent(); // The pseudo is gone now.
12839 return BB;
12840}
12841
12842MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012843X86TargetLowering::EmitVAARG64WithCustomInserter(
12844 MachineInstr *MI,
12845 MachineBasicBlock *MBB) const {
12846 // Emit va_arg instruction on X86-64.
12847
12848 // Operands to this pseudo-instruction:
12849 // 0 ) Output : destination address (reg)
12850 // 1-5) Input : va_list address (addr, i64mem)
12851 // 6 ) ArgSize : Size (in bytes) of vararg type
12852 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12853 // 8 ) Align : Alignment of type
12854 // 9 ) EFLAGS (implicit-def)
12855
12856 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12857 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12858
12859 unsigned DestReg = MI->getOperand(0).getReg();
12860 MachineOperand &Base = MI->getOperand(1);
12861 MachineOperand &Scale = MI->getOperand(2);
12862 MachineOperand &Index = MI->getOperand(3);
12863 MachineOperand &Disp = MI->getOperand(4);
12864 MachineOperand &Segment = MI->getOperand(5);
12865 unsigned ArgSize = MI->getOperand(6).getImm();
12866 unsigned ArgMode = MI->getOperand(7).getImm();
12867 unsigned Align = MI->getOperand(8).getImm();
12868
12869 // Memory Reference
12870 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12871 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12872 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12873
12874 // Machine Information
12875 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12876 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12877 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12878 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12879 DebugLoc DL = MI->getDebugLoc();
12880
12881 // struct va_list {
12882 // i32 gp_offset
12883 // i32 fp_offset
12884 // i64 overflow_area (address)
12885 // i64 reg_save_area (address)
12886 // }
12887 // sizeof(va_list) = 24
12888 // alignment(va_list) = 8
12889
12890 unsigned TotalNumIntRegs = 6;
12891 unsigned TotalNumXMMRegs = 8;
12892 bool UseGPOffset = (ArgMode == 1);
12893 bool UseFPOffset = (ArgMode == 2);
12894 unsigned MaxOffset = TotalNumIntRegs * 8 +
12895 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12896
12897 /* Align ArgSize to a multiple of 8 */
12898 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12899 bool NeedsAlign = (Align > 8);
12900
12901 MachineBasicBlock *thisMBB = MBB;
12902 MachineBasicBlock *overflowMBB;
12903 MachineBasicBlock *offsetMBB;
12904 MachineBasicBlock *endMBB;
12905
12906 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12907 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12908 unsigned OffsetReg = 0;
12909
12910 if (!UseGPOffset && !UseFPOffset) {
12911 // If we only pull from the overflow region, we don't create a branch.
12912 // We don't need to alter control flow.
12913 OffsetDestReg = 0; // unused
12914 OverflowDestReg = DestReg;
12915
12916 offsetMBB = NULL;
12917 overflowMBB = thisMBB;
12918 endMBB = thisMBB;
12919 } else {
12920 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12921 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12922 // If not, pull from overflow_area. (branch to overflowMBB)
12923 //
12924 // thisMBB
12925 // | .
12926 // | .
12927 // offsetMBB overflowMBB
12928 // | .
12929 // | .
12930 // endMBB
12931
12932 // Registers for the PHI in endMBB
12933 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12934 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12935
12936 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12937 MachineFunction *MF = MBB->getParent();
12938 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12939 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12940 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12941
12942 MachineFunction::iterator MBBIter = MBB;
12943 ++MBBIter;
12944
12945 // Insert the new basic blocks
12946 MF->insert(MBBIter, offsetMBB);
12947 MF->insert(MBBIter, overflowMBB);
12948 MF->insert(MBBIter, endMBB);
12949
12950 // Transfer the remainder of MBB and its successor edges to endMBB.
12951 endMBB->splice(endMBB->begin(), thisMBB,
12952 llvm::next(MachineBasicBlock::iterator(MI)),
12953 thisMBB->end());
12954 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12955
12956 // Make offsetMBB and overflowMBB successors of thisMBB
12957 thisMBB->addSuccessor(offsetMBB);
12958 thisMBB->addSuccessor(overflowMBB);
12959
12960 // endMBB is a successor of both offsetMBB and overflowMBB
12961 offsetMBB->addSuccessor(endMBB);
12962 overflowMBB->addSuccessor(endMBB);
12963
12964 // Load the offset value into a register
12965 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12966 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12967 .addOperand(Base)
12968 .addOperand(Scale)
12969 .addOperand(Index)
12970 .addDisp(Disp, UseFPOffset ? 4 : 0)
12971 .addOperand(Segment)
12972 .setMemRefs(MMOBegin, MMOEnd);
12973
12974 // Check if there is enough room left to pull this argument.
12975 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12976 .addReg(OffsetReg)
12977 .addImm(MaxOffset + 8 - ArgSizeA8);
12978
12979 // Branch to "overflowMBB" if offset >= max
12980 // Fall through to "offsetMBB" otherwise
12981 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12982 .addMBB(overflowMBB);
12983 }
12984
12985 // In offsetMBB, emit code to use the reg_save_area.
12986 if (offsetMBB) {
12987 assert(OffsetReg != 0);
12988
12989 // Read the reg_save_area address.
12990 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12991 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12992 .addOperand(Base)
12993 .addOperand(Scale)
12994 .addOperand(Index)
12995 .addDisp(Disp, 16)
12996 .addOperand(Segment)
12997 .setMemRefs(MMOBegin, MMOEnd);
12998
12999 // Zero-extend the offset
13000 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13001 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13002 .addImm(0)
13003 .addReg(OffsetReg)
13004 .addImm(X86::sub_32bit);
13005
13006 // Add the offset to the reg_save_area to get the final address.
13007 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13008 .addReg(OffsetReg64)
13009 .addReg(RegSaveReg);
13010
13011 // Compute the offset for the next argument
13012 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13013 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13014 .addReg(OffsetReg)
13015 .addImm(UseFPOffset ? 16 : 8);
13016
13017 // Store it back into the va_list.
13018 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13019 .addOperand(Base)
13020 .addOperand(Scale)
13021 .addOperand(Index)
13022 .addDisp(Disp, UseFPOffset ? 4 : 0)
13023 .addOperand(Segment)
13024 .addReg(NextOffsetReg)
13025 .setMemRefs(MMOBegin, MMOEnd);
13026
13027 // Jump to endMBB
13028 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13029 .addMBB(endMBB);
13030 }
13031
13032 //
13033 // Emit code to use overflow area
13034 //
13035
13036 // Load the overflow_area address into a register.
13037 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13038 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13039 .addOperand(Base)
13040 .addOperand(Scale)
13041 .addOperand(Index)
13042 .addDisp(Disp, 8)
13043 .addOperand(Segment)
13044 .setMemRefs(MMOBegin, MMOEnd);
13045
13046 // If we need to align it, do so. Otherwise, just copy the address
13047 // to OverflowDestReg.
13048 if (NeedsAlign) {
13049 // Align the overflow address
13050 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13051 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13052
13053 // aligned_addr = (addr + (align-1)) & ~(align-1)
13054 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13055 .addReg(OverflowAddrReg)
13056 .addImm(Align-1);
13057
13058 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13059 .addReg(TmpReg)
13060 .addImm(~(uint64_t)(Align-1));
13061 } else {
13062 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13063 .addReg(OverflowAddrReg);
13064 }
13065
13066 // Compute the next overflow address after this argument.
13067 // (the overflow address should be kept 8-byte aligned)
13068 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13069 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13070 .addReg(OverflowDestReg)
13071 .addImm(ArgSizeA8);
13072
13073 // Store the new overflow address.
13074 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13075 .addOperand(Base)
13076 .addOperand(Scale)
13077 .addOperand(Index)
13078 .addDisp(Disp, 8)
13079 .addOperand(Segment)
13080 .addReg(NextAddrReg)
13081 .setMemRefs(MMOBegin, MMOEnd);
13082
13083 // If we branched, emit the PHI to the front of endMBB.
13084 if (offsetMBB) {
13085 BuildMI(*endMBB, endMBB->begin(), DL,
13086 TII->get(X86::PHI), DestReg)
13087 .addReg(OffsetDestReg).addMBB(offsetMBB)
13088 .addReg(OverflowDestReg).addMBB(overflowMBB);
13089 }
13090
13091 // Erase the pseudo instruction
13092 MI->eraseFromParent();
13093
13094 return endMBB;
13095}
13096
13097MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013098X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13099 MachineInstr *MI,
13100 MachineBasicBlock *MBB) const {
13101 // Emit code to save XMM registers to the stack. The ABI says that the
13102 // number of registers to save is given in %al, so it's theoretically
13103 // possible to do an indirect jump trick to avoid saving all of them,
13104 // however this code takes a simpler approach and just executes all
13105 // of the stores if %al is non-zero. It's less code, and it's probably
13106 // easier on the hardware branch predictor, and stores aren't all that
13107 // expensive anyway.
13108
13109 // Create the new basic blocks. One block contains all the XMM stores,
13110 // and one block is the final destination regardless of whether any
13111 // stores were performed.
13112 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13113 MachineFunction *F = MBB->getParent();
13114 MachineFunction::iterator MBBIter = MBB;
13115 ++MBBIter;
13116 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13117 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13118 F->insert(MBBIter, XMMSaveMBB);
13119 F->insert(MBBIter, EndMBB);
13120
Dan Gohman14152b42010-07-06 20:24:04 +000013121 // Transfer the remainder of MBB and its successor edges to EndMBB.
13122 EndMBB->splice(EndMBB->begin(), MBB,
13123 llvm::next(MachineBasicBlock::iterator(MI)),
13124 MBB->end());
13125 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13126
Dan Gohmand6708ea2009-08-15 01:38:56 +000013127 // The original block will now fall through to the XMM save block.
13128 MBB->addSuccessor(XMMSaveMBB);
13129 // The XMMSaveMBB will fall through to the end block.
13130 XMMSaveMBB->addSuccessor(EndMBB);
13131
13132 // Now add the instructions.
13133 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13134 DebugLoc DL = MI->getDebugLoc();
13135
13136 unsigned CountReg = MI->getOperand(0).getReg();
13137 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13138 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13139
13140 if (!Subtarget->isTargetWin64()) {
13141 // If %al is 0, branch around the XMM save block.
13142 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013143 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013144 MBB->addSuccessor(EndMBB);
13145 }
13146
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013147 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013148 // In the XMM save block, save all the XMM argument registers.
13149 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13150 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013151 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013152 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013153 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013154 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013155 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013156 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013157 .addFrameIndex(RegSaveFrameIndex)
13158 .addImm(/*Scale=*/1)
13159 .addReg(/*IndexReg=*/0)
13160 .addImm(/*Disp=*/Offset)
13161 .addReg(/*Segment=*/0)
13162 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013163 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013164 }
13165
Dan Gohman14152b42010-07-06 20:24:04 +000013166 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013167
13168 return EndMBB;
13169}
Mon P Wang63307c32008-05-05 19:05:59 +000013170
Lang Hames6e3f7e42012-02-03 01:13:49 +000013171// The EFLAGS operand of SelectItr might be missing a kill marker
13172// because there were multiple uses of EFLAGS, and ISel didn't know
13173// which to mark. Figure out whether SelectItr should have had a
13174// kill marker, and set it if it should. Returns the correct kill
13175// marker value.
13176static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13177 MachineBasicBlock* BB,
13178 const TargetRegisterInfo* TRI) {
13179 // Scan forward through BB for a use/def of EFLAGS.
13180 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13181 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013182 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013183 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013184 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013185 if (mi.definesRegister(X86::EFLAGS))
13186 break; // Should have kill-flag - update below.
13187 }
13188
13189 // If we hit the end of the block, check whether EFLAGS is live into a
13190 // successor.
13191 if (miI == BB->end()) {
13192 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13193 sEnd = BB->succ_end();
13194 sItr != sEnd; ++sItr) {
13195 MachineBasicBlock* succ = *sItr;
13196 if (succ->isLiveIn(X86::EFLAGS))
13197 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013198 }
13199 }
13200
Lang Hames6e3f7e42012-02-03 01:13:49 +000013201 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13202 // out. SelectMI should have a kill flag on EFLAGS.
13203 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013204 return true;
13205}
13206
Evan Cheng60c07e12006-07-05 22:17:51 +000013207MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013208X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013209 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013210 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13211 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013212
Chris Lattner52600972009-09-02 05:57:00 +000013213 // To "insert" a SELECT_CC instruction, we actually have to insert the
13214 // diamond control-flow pattern. The incoming instruction knows the
13215 // destination vreg to set, the condition code register to branch on, the
13216 // true/false values to select between, and a branch opcode to use.
13217 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13218 MachineFunction::iterator It = BB;
13219 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013220
Chris Lattner52600972009-09-02 05:57:00 +000013221 // thisMBB:
13222 // ...
13223 // TrueVal = ...
13224 // cmpTY ccX, r1, r2
13225 // bCC copy1MBB
13226 // fallthrough --> copy0MBB
13227 MachineBasicBlock *thisMBB = BB;
13228 MachineFunction *F = BB->getParent();
13229 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13230 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013231 F->insert(It, copy0MBB);
13232 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013233
Bill Wendling730c07e2010-06-25 20:48:10 +000013234 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13235 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013236 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13237 if (!MI->killsRegister(X86::EFLAGS) &&
13238 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13239 copy0MBB->addLiveIn(X86::EFLAGS);
13240 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013241 }
13242
Dan Gohman14152b42010-07-06 20:24:04 +000013243 // Transfer the remainder of BB and its successor edges to sinkMBB.
13244 sinkMBB->splice(sinkMBB->begin(), BB,
13245 llvm::next(MachineBasicBlock::iterator(MI)),
13246 BB->end());
13247 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13248
13249 // Add the true and fallthrough blocks as its successors.
13250 BB->addSuccessor(copy0MBB);
13251 BB->addSuccessor(sinkMBB);
13252
13253 // Create the conditional branch instruction.
13254 unsigned Opc =
13255 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13256 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13257
Chris Lattner52600972009-09-02 05:57:00 +000013258 // copy0MBB:
13259 // %FalseValue = ...
13260 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013261 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013262
Chris Lattner52600972009-09-02 05:57:00 +000013263 // sinkMBB:
13264 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13265 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013266 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13267 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013268 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13269 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13270
Dan Gohman14152b42010-07-06 20:24:04 +000013271 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013272 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013273}
13274
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013275MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013276X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13277 bool Is64Bit) const {
13278 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13279 DebugLoc DL = MI->getDebugLoc();
13280 MachineFunction *MF = BB->getParent();
13281 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13282
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013283 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013284
13285 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13286 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13287
13288 // BB:
13289 // ... [Till the alloca]
13290 // If stacklet is not large enough, jump to mallocMBB
13291 //
13292 // bumpMBB:
13293 // Allocate by subtracting from RSP
13294 // Jump to continueMBB
13295 //
13296 // mallocMBB:
13297 // Allocate by call to runtime
13298 //
13299 // continueMBB:
13300 // ...
13301 // [rest of original BB]
13302 //
13303
13304 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13305 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13306 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13307
13308 MachineRegisterInfo &MRI = MF->getRegInfo();
13309 const TargetRegisterClass *AddrRegClass =
13310 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13311
13312 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13313 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13314 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013315 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013316 sizeVReg = MI->getOperand(1).getReg(),
13317 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13318
13319 MachineFunction::iterator MBBIter = BB;
13320 ++MBBIter;
13321
13322 MF->insert(MBBIter, bumpMBB);
13323 MF->insert(MBBIter, mallocMBB);
13324 MF->insert(MBBIter, continueMBB);
13325
13326 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13327 (MachineBasicBlock::iterator(MI)), BB->end());
13328 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13329
13330 // Add code to the main basic block to check if the stack limit has been hit,
13331 // and if so, jump to mallocMBB otherwise to bumpMBB.
13332 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013333 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013334 .addReg(tmpSPVReg).addReg(sizeVReg);
13335 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013336 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013337 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013338 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13339
13340 // bumpMBB simply decreases the stack pointer, since we know the current
13341 // stacklet has enough space.
13342 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013343 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013344 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013345 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013346 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13347
13348 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013349 const uint32_t *RegMask =
13350 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013351 if (Is64Bit) {
13352 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13353 .addReg(sizeVReg);
13354 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013355 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013356 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013357 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013358 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013359 } else {
13360 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13361 .addImm(12);
13362 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13363 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013364 .addExternalSymbol("__morestack_allocate_stack_space")
13365 .addRegMask(RegMask)
13366 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013367 }
13368
13369 if (!Is64Bit)
13370 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13371 .addImm(16);
13372
13373 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13374 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13375 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13376
13377 // Set up the CFG correctly.
13378 BB->addSuccessor(bumpMBB);
13379 BB->addSuccessor(mallocMBB);
13380 mallocMBB->addSuccessor(continueMBB);
13381 bumpMBB->addSuccessor(continueMBB);
13382
13383 // Take care of the PHI nodes.
13384 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13385 MI->getOperand(0).getReg())
13386 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13387 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13388
13389 // Delete the original pseudo instruction.
13390 MI->eraseFromParent();
13391
13392 // And we're done.
13393 return continueMBB;
13394}
13395
13396MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013397X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013398 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013399 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13400 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013401
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013402 assert(!Subtarget->isTargetEnvMacho());
13403
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013404 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13405 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013406
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013407 if (Subtarget->isTargetWin64()) {
13408 if (Subtarget->isTargetCygMing()) {
13409 // ___chkstk(Mingw64):
13410 // Clobbers R10, R11, RAX and EFLAGS.
13411 // Updates RSP.
13412 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13413 .addExternalSymbol("___chkstk")
13414 .addReg(X86::RAX, RegState::Implicit)
13415 .addReg(X86::RSP, RegState::Implicit)
13416 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13417 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13418 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13419 } else {
13420 // __chkstk(MSVCRT): does not update stack pointer.
13421 // Clobbers R10, R11 and EFLAGS.
13422 // FIXME: RAX(allocated size) might be reused and not killed.
13423 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13424 .addExternalSymbol("__chkstk")
13425 .addReg(X86::RAX, RegState::Implicit)
13426 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13427 // RAX has the offset to subtracted from RSP.
13428 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13429 .addReg(X86::RSP)
13430 .addReg(X86::RAX);
13431 }
13432 } else {
13433 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013434 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13435
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013436 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13437 .addExternalSymbol(StackProbeSymbol)
13438 .addReg(X86::EAX, RegState::Implicit)
13439 .addReg(X86::ESP, RegState::Implicit)
13440 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13441 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13442 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13443 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013444
Dan Gohman14152b42010-07-06 20:24:04 +000013445 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013446 return BB;
13447}
Chris Lattner52600972009-09-02 05:57:00 +000013448
13449MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013450X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13451 MachineBasicBlock *BB) const {
13452 // This is pretty easy. We're taking the value that we received from
13453 // our load from the relocation, sticking it in either RDI (x86-64)
13454 // or EAX and doing an indirect call. The return value will then
13455 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013456 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013457 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013458 DebugLoc DL = MI->getDebugLoc();
13459 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013460
13461 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013462 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013463
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013464 // Get a register mask for the lowered call.
13465 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13466 // proper register mask.
13467 const uint32_t *RegMask =
13468 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013469 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013470 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13471 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013472 .addReg(X86::RIP)
13473 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013474 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013475 MI->getOperand(3).getTargetFlags())
13476 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013477 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013478 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013479 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013480 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013481 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13482 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013483 .addReg(0)
13484 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013485 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013486 MI->getOperand(3).getTargetFlags())
13487 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013488 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013489 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013490 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013491 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013492 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13493 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013494 .addReg(TII->getGlobalBaseReg(F))
13495 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013496 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013497 MI->getOperand(3).getTargetFlags())
13498 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013499 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013500 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013501 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013502 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013503
Dan Gohman14152b42010-07-06 20:24:04 +000013504 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013505 return BB;
13506}
13507
13508MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013509X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13510 MachineBasicBlock *MBB) const {
13511 DebugLoc DL = MI->getDebugLoc();
13512 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13513
13514 MachineFunction *MF = MBB->getParent();
13515 MachineRegisterInfo &MRI = MF->getRegInfo();
13516
13517 const BasicBlock *BB = MBB->getBasicBlock();
13518 MachineFunction::iterator I = MBB;
13519 ++I;
13520
13521 // Memory Reference
13522 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13523 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13524
13525 unsigned DstReg;
13526 unsigned MemOpndSlot = 0;
13527
13528 unsigned CurOp = 0;
13529
13530 DstReg = MI->getOperand(CurOp++).getReg();
13531 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13532 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13533 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13534 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13535
13536 MemOpndSlot = CurOp;
13537
13538 MVT PVT = getPointerTy();
13539 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13540 "Invalid Pointer Size!");
13541
13542 // For v = setjmp(buf), we generate
13543 //
13544 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013545 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013546 // SjLjSetup restoreMBB
13547 //
13548 // mainMBB:
13549 // v_main = 0
13550 //
13551 // sinkMBB:
13552 // v = phi(main, restore)
13553 //
13554 // restoreMBB:
13555 // v_restore = 1
13556
13557 MachineBasicBlock *thisMBB = MBB;
13558 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13559 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13560 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13561 MF->insert(I, mainMBB);
13562 MF->insert(I, sinkMBB);
13563 MF->push_back(restoreMBB);
13564
13565 MachineInstrBuilder MIB;
13566
13567 // Transfer the remainder of BB and its successor edges to sinkMBB.
13568 sinkMBB->splice(sinkMBB->begin(), MBB,
13569 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13570 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13571
13572 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013573 unsigned PtrStoreOpc = 0;
13574 unsigned LabelReg = 0;
13575 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13576 Reloc::Model RM = getTargetMachine().getRelocationModel();
13577 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13578 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013579
Michael Liao281ae5a2012-10-17 02:22:27 +000013580 // Prepare IP either in reg or imm.
13581 if (!UseImmLabel) {
13582 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13583 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13584 LabelReg = MRI.createVirtualRegister(PtrRC);
13585 if (Subtarget->is64Bit()) {
13586 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13587 .addReg(X86::RIP)
13588 .addImm(0)
13589 .addReg(0)
13590 .addMBB(restoreMBB)
13591 .addReg(0);
13592 } else {
13593 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13594 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13595 .addReg(XII->getGlobalBaseReg(MF))
13596 .addImm(0)
13597 .addReg(0)
13598 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13599 .addReg(0);
13600 }
13601 } else
13602 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013603 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013604 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013605 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13606 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013607 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013608 else
13609 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13610 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013611 if (!UseImmLabel)
13612 MIB.addReg(LabelReg);
13613 else
13614 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013615 MIB.setMemRefs(MMOBegin, MMOEnd);
13616 // Setup
13617 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13618 .addMBB(restoreMBB);
13619 MIB.addRegMask(RegInfo->getNoPreservedMask());
13620 thisMBB->addSuccessor(mainMBB);
13621 thisMBB->addSuccessor(restoreMBB);
13622
13623 // mainMBB:
13624 // EAX = 0
13625 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13626 mainMBB->addSuccessor(sinkMBB);
13627
13628 // sinkMBB:
13629 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13630 TII->get(X86::PHI), DstReg)
13631 .addReg(mainDstReg).addMBB(mainMBB)
13632 .addReg(restoreDstReg).addMBB(restoreMBB);
13633
13634 // restoreMBB:
13635 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13636 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13637 restoreMBB->addSuccessor(sinkMBB);
13638
13639 MI->eraseFromParent();
13640 return sinkMBB;
13641}
13642
13643MachineBasicBlock *
13644X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13645 MachineBasicBlock *MBB) const {
13646 DebugLoc DL = MI->getDebugLoc();
13647 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13648
13649 MachineFunction *MF = MBB->getParent();
13650 MachineRegisterInfo &MRI = MF->getRegInfo();
13651
13652 // Memory Reference
13653 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13654 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13655
13656 MVT PVT = getPointerTy();
13657 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13658 "Invalid Pointer Size!");
13659
13660 const TargetRegisterClass *RC =
13661 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13662 unsigned Tmp = MRI.createVirtualRegister(RC);
13663 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13664 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13665 unsigned SP = RegInfo->getStackRegister();
13666
13667 MachineInstrBuilder MIB;
13668
Michael Liao281ae5a2012-10-17 02:22:27 +000013669 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13670 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013671
13672 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13673 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13674
13675 // Reload FP
13676 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13677 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13678 MIB.addOperand(MI->getOperand(i));
13679 MIB.setMemRefs(MMOBegin, MMOEnd);
13680 // Reload IP
13681 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13682 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13683 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013684 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013685 else
13686 MIB.addOperand(MI->getOperand(i));
13687 }
13688 MIB.setMemRefs(MMOBegin, MMOEnd);
13689 // Reload SP
13690 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13691 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13692 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013693 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013694 else
13695 MIB.addOperand(MI->getOperand(i));
13696 }
13697 MIB.setMemRefs(MMOBegin, MMOEnd);
13698 // Jump
13699 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13700
13701 MI->eraseFromParent();
13702 return MBB;
13703}
13704
13705MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013706X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013707 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013708 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013709 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013710 case X86::TAILJMPd64:
13711 case X86::TAILJMPr64:
13712 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013713 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013714 case X86::TCRETURNdi64:
13715 case X86::TCRETURNri64:
13716 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013717 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013718 case X86::WIN_ALLOCA:
13719 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013720 case X86::SEG_ALLOCA_32:
13721 return EmitLoweredSegAlloca(MI, BB, false);
13722 case X86::SEG_ALLOCA_64:
13723 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013724 case X86::TLSCall_32:
13725 case X86::TLSCall_64:
13726 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013727 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013728 case X86::CMOV_FR32:
13729 case X86::CMOV_FR64:
13730 case X86::CMOV_V4F32:
13731 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013732 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013733 case X86::CMOV_V8F32:
13734 case X86::CMOV_V4F64:
13735 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013736 case X86::CMOV_GR16:
13737 case X86::CMOV_GR32:
13738 case X86::CMOV_RFP32:
13739 case X86::CMOV_RFP64:
13740 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013741 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013742
Dale Johannesen849f2142007-07-03 00:53:03 +000013743 case X86::FP32_TO_INT16_IN_MEM:
13744 case X86::FP32_TO_INT32_IN_MEM:
13745 case X86::FP32_TO_INT64_IN_MEM:
13746 case X86::FP64_TO_INT16_IN_MEM:
13747 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013748 case X86::FP64_TO_INT64_IN_MEM:
13749 case X86::FP80_TO_INT16_IN_MEM:
13750 case X86::FP80_TO_INT32_IN_MEM:
13751 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013752 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13753 DebugLoc DL = MI->getDebugLoc();
13754
Evan Cheng60c07e12006-07-05 22:17:51 +000013755 // Change the floating point control register to use "round towards zero"
13756 // mode when truncating to an integer value.
13757 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013758 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013759 addFrameReference(BuildMI(*BB, MI, DL,
13760 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013761
13762 // Load the old value of the high byte of the control word...
13763 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013764 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013765 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013766 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013767
13768 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013769 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013770 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013771
13772 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013773 addFrameReference(BuildMI(*BB, MI, DL,
13774 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013775
13776 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013777 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013778 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013779
13780 // Get the X86 opcode to use.
13781 unsigned Opc;
13782 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013783 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013784 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13785 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13786 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13787 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13788 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13789 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013790 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13791 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13792 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013793 }
13794
13795 X86AddressMode AM;
13796 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013797 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013798 AM.BaseType = X86AddressMode::RegBase;
13799 AM.Base.Reg = Op.getReg();
13800 } else {
13801 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013802 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013803 }
13804 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013805 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013806 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013807 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013808 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013809 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013810 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013811 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013812 AM.GV = Op.getGlobal();
13813 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013814 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013815 }
Dan Gohman14152b42010-07-06 20:24:04 +000013816 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013817 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013818
13819 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013820 addFrameReference(BuildMI(*BB, MI, DL,
13821 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013822
Dan Gohman14152b42010-07-06 20:24:04 +000013823 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013824 return BB;
13825 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013826 // String/text processing lowering.
13827 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013828 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013829 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013830 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013831 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013832 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013833 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013834 case X86::VPCMPESTRM128MEM: {
13835 unsigned NumArgs;
13836 bool MemArg;
13837 switch (MI->getOpcode()) {
13838 default: llvm_unreachable("illegal opcode!");
13839 case X86::PCMPISTRM128REG:
13840 case X86::VPCMPISTRM128REG:
13841 NumArgs = 3; MemArg = false; break;
13842 case X86::PCMPISTRM128MEM:
13843 case X86::VPCMPISTRM128MEM:
13844 NumArgs = 3; MemArg = true; break;
13845 case X86::PCMPESTRM128REG:
13846 case X86::VPCMPESTRM128REG:
13847 NumArgs = 5; MemArg = false; break;
13848 case X86::PCMPESTRM128MEM:
13849 case X86::VPCMPESTRM128MEM:
13850 NumArgs = 5; MemArg = true; break;
13851 }
13852 return EmitPCMP(MI, BB, NumArgs, MemArg);
13853 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013854
Eric Christopher228232b2010-11-30 07:20:12 +000013855 // Thread synchronization.
13856 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013857 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013858
Eric Christopherb120ab42009-08-18 22:50:32 +000013859 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013860 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013861 case X86::ATOMAND16:
13862 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013863 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013864 // Fall through
13865 case X86::ATOMOR8:
13866 case X86::ATOMOR16:
13867 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013868 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013869 // Fall through
13870 case X86::ATOMXOR16:
13871 case X86::ATOMXOR8:
13872 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013873 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013874 // Fall through
13875 case X86::ATOMNAND8:
13876 case X86::ATOMNAND16:
13877 case X86::ATOMNAND32:
13878 case X86::ATOMNAND64:
13879 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013880 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013881 case X86::ATOMMAX16:
13882 case X86::ATOMMAX32:
13883 case X86::ATOMMAX64:
13884 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013885 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013886 case X86::ATOMMIN16:
13887 case X86::ATOMMIN32:
13888 case X86::ATOMMIN64:
13889 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013890 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013891 case X86::ATOMUMAX16:
13892 case X86::ATOMUMAX32:
13893 case X86::ATOMUMAX64:
13894 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013895 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013896 case X86::ATOMUMIN16:
13897 case X86::ATOMUMIN32:
13898 case X86::ATOMUMIN64:
13899 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013900
13901 // This group does 64-bit operations on a 32-bit host.
13902 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013903 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013904 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013905 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013906 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013907 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000013908 case X86::ATOMMAX6432:
13909 case X86::ATOMMIN6432:
13910 case X86::ATOMUMAX6432:
13911 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000013912 case X86::ATOMSWAP6432:
13913 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000013914
Dan Gohmand6708ea2009-08-15 01:38:56 +000013915 case X86::VASTART_SAVE_XMM_REGS:
13916 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013917
13918 case X86::VAARG_64:
13919 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013920
13921 case X86::EH_SjLj_SetJmp32:
13922 case X86::EH_SjLj_SetJmp64:
13923 return emitEHSjLjSetJmp(MI, BB);
13924
13925 case X86::EH_SjLj_LongJmp32:
13926 case X86::EH_SjLj_LongJmp64:
13927 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013928 }
13929}
13930
13931//===----------------------------------------------------------------------===//
13932// X86 Optimization Hooks
13933//===----------------------------------------------------------------------===//
13934
Dan Gohman475871a2008-07-27 21:46:04 +000013935void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013936 APInt &KnownZero,
13937 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013938 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013939 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013940 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013941 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013942 assert((Opc >= ISD::BUILTIN_OP_END ||
13943 Opc == ISD::INTRINSIC_WO_CHAIN ||
13944 Opc == ISD::INTRINSIC_W_CHAIN ||
13945 Opc == ISD::INTRINSIC_VOID) &&
13946 "Should use MaskedValueIsZero if you don't know whether Op"
13947 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013948
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013949 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013950 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013951 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013952 case X86ISD::ADD:
13953 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013954 case X86ISD::ADC:
13955 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013956 case X86ISD::SMUL:
13957 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013958 case X86ISD::INC:
13959 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013960 case X86ISD::OR:
13961 case X86ISD::XOR:
13962 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013963 // These nodes' second result is a boolean.
13964 if (Op.getResNo() == 0)
13965 break;
13966 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013967 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013968 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013969 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013970 case ISD::INTRINSIC_WO_CHAIN: {
13971 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13972 unsigned NumLoBits = 0;
13973 switch (IntId) {
13974 default: break;
13975 case Intrinsic::x86_sse_movmsk_ps:
13976 case Intrinsic::x86_avx_movmsk_ps_256:
13977 case Intrinsic::x86_sse2_movmsk_pd:
13978 case Intrinsic::x86_avx_movmsk_pd_256:
13979 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013980 case Intrinsic::x86_sse2_pmovmskb_128:
13981 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013982 // High bits of movmskp{s|d}, pmovmskb are known zero.
13983 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013984 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013985 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13986 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13987 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13988 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13989 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13990 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013991 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013992 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013993 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013994 break;
13995 }
13996 }
13997 break;
13998 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013999 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014000}
Chris Lattner259e97c2006-01-31 19:43:35 +000014001
Owen Andersonbc146b02010-09-21 20:42:50 +000014002unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14003 unsigned Depth) const {
14004 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14005 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14006 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014007
Owen Andersonbc146b02010-09-21 20:42:50 +000014008 // Fallback case.
14009 return 1;
14010}
14011
Evan Cheng206ee9d2006-07-07 08:33:52 +000014012/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014013/// node is a GlobalAddress + offset.
14014bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014015 const GlobalValue* &GA,
14016 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014017 if (N->getOpcode() == X86ISD::Wrapper) {
14018 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014019 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014020 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014021 return true;
14022 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014023 }
Evan Chengad4196b2008-05-12 19:56:52 +000014024 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014025}
14026
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014027/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14028/// same as extracting the high 128-bit part of 256-bit vector and then
14029/// inserting the result into the low part of a new 256-bit vector
14030static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14031 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014032 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014033
14034 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014035 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014036 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14037 SVOp->getMaskElt(j) >= 0)
14038 return false;
14039
14040 return true;
14041}
14042
14043/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14044/// same as extracting the low 128-bit part of 256-bit vector and then
14045/// inserting the result into the high part of a new 256-bit vector
14046static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14047 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014048 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014049
14050 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014051 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014052 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14053 SVOp->getMaskElt(j) >= 0)
14054 return false;
14055
14056 return true;
14057}
14058
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014059/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14060static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014061 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014062 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014063 DebugLoc dl = N->getDebugLoc();
14064 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14065 SDValue V1 = SVOp->getOperand(0);
14066 SDValue V2 = SVOp->getOperand(1);
14067 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014068 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014069
14070 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14071 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14072 //
14073 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014074 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014075 // V UNDEF BUILD_VECTOR UNDEF
14076 // \ / \ /
14077 // CONCAT_VECTOR CONCAT_VECTOR
14078 // \ /
14079 // \ /
14080 // RESULT: V + zero extended
14081 //
14082 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14083 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14084 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14085 return SDValue();
14086
14087 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14088 return SDValue();
14089
14090 // To match the shuffle mask, the first half of the mask should
14091 // be exactly the first vector, and all the rest a splat with the
14092 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014093 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014094 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14095 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14096 return SDValue();
14097
Chad Rosier3d1161e2012-01-03 21:05:52 +000014098 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14099 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014100 if (Ld->hasNUsesOfValue(1, 0)) {
14101 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14102 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14103 SDValue ResNode =
14104 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14105 Ld->getMemoryVT(),
14106 Ld->getPointerInfo(),
14107 Ld->getAlignment(),
14108 false/*isVolatile*/, true/*ReadMem*/,
14109 false/*WriteMem*/);
14110 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14111 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014112 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014113
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014114 // Emit a zeroed vector and insert the desired subvector on its
14115 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014116 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014117 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014118 return DCI.CombineTo(N, InsV);
14119 }
14120
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014121 //===--------------------------------------------------------------------===//
14122 // Combine some shuffles into subvector extracts and inserts:
14123 //
14124
14125 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14126 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014127 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14128 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014129 return DCI.CombineTo(N, InsV);
14130 }
14131
14132 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14133 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014134 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14135 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014136 return DCI.CombineTo(N, InsV);
14137 }
14138
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014139 return SDValue();
14140}
14141
14142/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014143static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014144 TargetLowering::DAGCombinerInfo &DCI,
14145 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014146 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014147 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014148
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014149 // Don't create instructions with illegal types after legalize types has run.
14150 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14151 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14152 return SDValue();
14153
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014154 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000014155 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014156 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014157 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014158
14159 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014160 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014161 return SDValue();
14162
14163 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14164 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14165 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014166 SmallVector<SDValue, 16> Elts;
14167 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014168 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014169
Nate Begemanfdea31a2010-03-24 20:49:50 +000014170 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014171}
Evan Chengd880b972008-05-09 21:53:03 +000014172
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014173
Craig Topper55b24052012-09-11 06:15:32 +000014174/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014175/// a sequence of vector shuffle operations.
14176/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014177static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14178 TargetLowering::DAGCombinerInfo &DCI,
14179 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014180 if (!DCI.isBeforeLegalizeOps())
14181 return SDValue();
14182
Craig Topper3ef43cf2012-04-24 06:36:35 +000014183 if (!Subtarget->hasAVX())
14184 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014185
14186 EVT VT = N->getValueType(0);
14187 SDValue Op = N->getOperand(0);
14188 EVT OpVT = Op.getValueType();
14189 DebugLoc dl = N->getDebugLoc();
14190
14191 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
14192
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014193 if (Subtarget->hasAVX2()) {
14194 // AVX2: v4i64 -> v4i32
14195
14196 // VPERMD
14197 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
14198
14199 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
14200 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
14201 ShufMask);
14202
Craig Topperd63fa652012-04-22 18:51:37 +000014203 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
14204 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014205 }
14206
14207 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014208 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014209 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014210
14211 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014212 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014213
14214 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14215 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14216
14217 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000014218 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014219
Craig Toppercacafd42012-08-14 08:18:43 +000014220 SDValue Undef = DAG.getUNDEF(VT);
14221 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
14222 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014223
14224 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014225 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014226
Elena Demikhovsky73252572012-02-01 10:33:05 +000014227 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014228 }
Craig Topperd63fa652012-04-22 18:51:37 +000014229
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014230 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14231
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014232 if (Subtarget->hasAVX2()) {
14233 // AVX2: v8i32 -> v8i16
14234
14235 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014236
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014237 // PSHUFB
14238 SmallVector<SDValue,32> pshufbMask;
14239 for (unsigned i = 0; i < 2; ++i) {
14240 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14241 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14242 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14243 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14244 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14245 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14246 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14247 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14248 for (unsigned j = 0; j < 8; ++j)
14249 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14250 }
Craig Topperd63fa652012-04-22 18:51:37 +000014251 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14252 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014253 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14254
14255 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14256
14257 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014258 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014259 &ShufMask[0]);
14260
Craig Topperd63fa652012-04-22 18:51:37 +000014261 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14262 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014263
14264 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14265 }
14266
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014267 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014268 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014269
14270 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014271 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014272
14273 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14274 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14275
14276 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014277 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14278 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014279
Craig Toppercacafd42012-08-14 08:18:43 +000014280 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14281 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14282 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014283
14284 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14285 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14286
14287 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014288 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014289
Elena Demikhovsky73252572012-02-01 10:33:05 +000014290 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014291 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014292 }
14293
14294 return SDValue();
14295}
14296
Craig Topper89f4e662012-03-20 07:17:59 +000014297/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14298/// specific shuffle of a load can be folded into a single element load.
14299/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14300/// shuffles have been customed lowered so we need to handle those here.
14301static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14302 TargetLowering::DAGCombinerInfo &DCI) {
14303 if (DCI.isBeforeLegalizeOps())
14304 return SDValue();
14305
14306 SDValue InVec = N->getOperand(0);
14307 SDValue EltNo = N->getOperand(1);
14308
14309 if (!isa<ConstantSDNode>(EltNo))
14310 return SDValue();
14311
14312 EVT VT = InVec.getValueType();
14313
14314 bool HasShuffleIntoBitcast = false;
14315 if (InVec.getOpcode() == ISD::BITCAST) {
14316 // Don't duplicate a load with other uses.
14317 if (!InVec.hasOneUse())
14318 return SDValue();
14319 EVT BCVT = InVec.getOperand(0).getValueType();
14320 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14321 return SDValue();
14322 InVec = InVec.getOperand(0);
14323 HasShuffleIntoBitcast = true;
14324 }
14325
14326 if (!isTargetShuffle(InVec.getOpcode()))
14327 return SDValue();
14328
14329 // Don't duplicate a load with other uses.
14330 if (!InVec.hasOneUse())
14331 return SDValue();
14332
14333 SmallVector<int, 16> ShuffleMask;
14334 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014335 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14336 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014337 return SDValue();
14338
14339 // Select the input vector, guarding against out of range extract vector.
14340 unsigned NumElems = VT.getVectorNumElements();
14341 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14342 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14343 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14344 : InVec.getOperand(1);
14345
14346 // If inputs to shuffle are the same for both ops, then allow 2 uses
14347 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14348
14349 if (LdNode.getOpcode() == ISD::BITCAST) {
14350 // Don't duplicate a load with other uses.
14351 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14352 return SDValue();
14353
14354 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14355 LdNode = LdNode.getOperand(0);
14356 }
14357
14358 if (!ISD::isNormalLoad(LdNode.getNode()))
14359 return SDValue();
14360
14361 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14362
14363 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14364 return SDValue();
14365
14366 if (HasShuffleIntoBitcast) {
14367 // If there's a bitcast before the shuffle, check if the load type and
14368 // alignment is valid.
14369 unsigned Align = LN0->getAlignment();
14370 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014371 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014372 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14373
14374 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14375 return SDValue();
14376 }
14377
14378 // All checks match so transform back to vector_shuffle so that DAG combiner
14379 // can finish the job
14380 DebugLoc dl = N->getDebugLoc();
14381
14382 // Create shuffle node taking into account the case that its a unary shuffle
14383 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14384 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14385 InVec.getOperand(0), Shuffle,
14386 &ShuffleMask[0]);
14387 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14388 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14389 EltNo);
14390}
14391
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014392/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14393/// generation and convert it from being a bunch of shuffles and extracts
14394/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014395static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014396 TargetLowering::DAGCombinerInfo &DCI) {
14397 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14398 if (NewOp.getNode())
14399 return NewOp;
14400
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014401 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014402 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14403 // from mmx to v2i32 has a single usage.
14404 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14405 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14406 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14407 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14408 N->getValueType(0),
14409 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014410
14411 // Only operate on vectors of 4 elements, where the alternative shuffling
14412 // gets to be more expensive.
14413 if (InputVector.getValueType() != MVT::v4i32)
14414 return SDValue();
14415
14416 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14417 // single use which is a sign-extend or zero-extend, and all elements are
14418 // used.
14419 SmallVector<SDNode *, 4> Uses;
14420 unsigned ExtractedElements = 0;
14421 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14422 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14423 if (UI.getUse().getResNo() != InputVector.getResNo())
14424 return SDValue();
14425
14426 SDNode *Extract = *UI;
14427 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14428 return SDValue();
14429
14430 if (Extract->getValueType(0) != MVT::i32)
14431 return SDValue();
14432 if (!Extract->hasOneUse())
14433 return SDValue();
14434 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14435 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14436 return SDValue();
14437 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14438 return SDValue();
14439
14440 // Record which element was extracted.
14441 ExtractedElements |=
14442 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14443
14444 Uses.push_back(Extract);
14445 }
14446
14447 // If not all the elements were used, this may not be worthwhile.
14448 if (ExtractedElements != 15)
14449 return SDValue();
14450
14451 // Ok, we've now decided to do the transformation.
14452 DebugLoc dl = InputVector.getDebugLoc();
14453
14454 // Store the value to a temporary stack slot.
14455 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014456 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14457 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014458
14459 // Replace each use (extract) with a load of the appropriate element.
14460 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14461 UE = Uses.end(); UI != UE; ++UI) {
14462 SDNode *Extract = *UI;
14463
Nadav Rotem86694292011-05-17 08:31:57 +000014464 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014465 SDValue Idx = Extract->getOperand(1);
14466 unsigned EltSize =
14467 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14468 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014469 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014470 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14471
Nadav Rotem86694292011-05-17 08:31:57 +000014472 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014473 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014474
14475 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014476 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014477 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014478 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014479
14480 // Replace the exact with the load.
14481 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14482 }
14483
14484 // The replacement was made in place; don't return anything.
14485 return SDValue();
14486}
14487
Duncan Sands6bcd2192011-09-17 16:49:39 +000014488/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14489/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014490static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014491 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014492 const X86Subtarget *Subtarget) {
14493 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014494 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014495 // Get the LHS/RHS of the select.
14496 SDValue LHS = N->getOperand(1);
14497 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014498 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014499
Dan Gohman670e5392009-09-21 18:03:22 +000014500 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014501 // instructions match the semantics of the common C idiom x<y?x:y but not
14502 // x<=y?x:y, because of how they handle negative zero (which can be
14503 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014504 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14505 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014506 (Subtarget->hasSSE2() ||
14507 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014508 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014509
Chris Lattner47b4ce82009-03-11 05:48:52 +000014510 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014511 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014512 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14513 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014514 switch (CC) {
14515 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014516 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014517 // Converting this to a min would handle NaNs incorrectly, and swapping
14518 // the operands would cause it to handle comparisons between positive
14519 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014520 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014521 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014522 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14523 break;
14524 std::swap(LHS, RHS);
14525 }
Dan Gohman670e5392009-09-21 18:03:22 +000014526 Opcode = X86ISD::FMIN;
14527 break;
14528 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014529 // Converting this to a min would handle comparisons between positive
14530 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014531 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014532 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14533 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014534 Opcode = X86ISD::FMIN;
14535 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014536 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014537 // Converting this to a min would handle both negative zeros and NaNs
14538 // incorrectly, but we can swap the operands to fix both.
14539 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014540 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014541 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014542 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014543 Opcode = X86ISD::FMIN;
14544 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014545
Dan Gohman670e5392009-09-21 18:03:22 +000014546 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014547 // Converting this to a max would handle comparisons between positive
14548 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014549 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014550 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014551 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014552 Opcode = X86ISD::FMAX;
14553 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014554 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014555 // Converting this to a max would handle NaNs incorrectly, and swapping
14556 // the operands would cause it to handle comparisons between positive
14557 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014558 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014559 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014560 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14561 break;
14562 std::swap(LHS, RHS);
14563 }
Dan Gohman670e5392009-09-21 18:03:22 +000014564 Opcode = X86ISD::FMAX;
14565 break;
14566 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014567 // Converting this to a max would handle both negative zeros and NaNs
14568 // incorrectly, but we can swap the operands to fix both.
14569 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014570 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014571 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014572 case ISD::SETGE:
14573 Opcode = X86ISD::FMAX;
14574 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014575 }
Dan Gohman670e5392009-09-21 18:03:22 +000014576 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014577 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14578 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014579 switch (CC) {
14580 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014581 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014582 // Converting this to a min would handle comparisons between positive
14583 // and negative zero incorrectly, and swapping the operands would
14584 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014585 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014586 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014587 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014588 break;
14589 std::swap(LHS, RHS);
14590 }
Dan Gohman670e5392009-09-21 18:03:22 +000014591 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014592 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014593 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014594 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014595 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014596 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14597 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014598 Opcode = X86ISD::FMIN;
14599 break;
14600 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014601 // Converting this to a min would handle both negative zeros and NaNs
14602 // incorrectly, but we can swap the operands to fix both.
14603 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014604 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014605 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014606 case ISD::SETGE:
14607 Opcode = X86ISD::FMIN;
14608 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014609
Dan Gohman670e5392009-09-21 18:03:22 +000014610 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014611 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014612 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014613 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014614 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014615 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014616 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014617 // Converting this to a max would handle comparisons between positive
14618 // and negative zero incorrectly, and swapping the operands would
14619 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014620 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014621 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014622 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014623 break;
14624 std::swap(LHS, RHS);
14625 }
Dan Gohman670e5392009-09-21 18:03:22 +000014626 Opcode = X86ISD::FMAX;
14627 break;
14628 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014629 // Converting this to a max would handle both negative zeros and NaNs
14630 // incorrectly, but we can swap the operands to fix both.
14631 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014632 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014633 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014634 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014635 Opcode = X86ISD::FMAX;
14636 break;
14637 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014638 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014639
Chris Lattner47b4ce82009-03-11 05:48:52 +000014640 if (Opcode)
14641 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014642 }
Eric Christopherfd179292009-08-27 18:07:15 +000014643
Chris Lattnerd1980a52009-03-12 06:52:53 +000014644 // If this is a select between two integer constants, try to do some
14645 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014646 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14647 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014648 // Don't do this for crazy integer types.
14649 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14650 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014651 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014652 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014653
Chris Lattnercee56e72009-03-13 05:53:31 +000014654 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014655 // Efficiently invertible.
14656 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14657 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14658 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14659 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014660 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014661 }
Eric Christopherfd179292009-08-27 18:07:15 +000014662
Chris Lattnerd1980a52009-03-12 06:52:53 +000014663 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014664 if (FalseC->getAPIntValue() == 0 &&
14665 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014666 if (NeedsCondInvert) // Invert the condition if needed.
14667 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14668 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014669
Chris Lattnerd1980a52009-03-12 06:52:53 +000014670 // Zero extend the condition if needed.
14671 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014672
Chris Lattnercee56e72009-03-13 05:53:31 +000014673 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014674 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014675 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014676 }
Eric Christopherfd179292009-08-27 18:07:15 +000014677
Chris Lattner97a29a52009-03-13 05:22:11 +000014678 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014679 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014680 if (NeedsCondInvert) // Invert the condition if needed.
14681 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14682 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014683
Chris Lattner97a29a52009-03-13 05:22:11 +000014684 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014685 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14686 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014687 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014688 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014689 }
Eric Christopherfd179292009-08-27 18:07:15 +000014690
Chris Lattnercee56e72009-03-13 05:53:31 +000014691 // Optimize cases that will turn into an LEA instruction. This requires
14692 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014693 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014694 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014695 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014696
Chris Lattnercee56e72009-03-13 05:53:31 +000014697 bool isFastMultiplier = false;
14698 if (Diff < 10) {
14699 switch ((unsigned char)Diff) {
14700 default: break;
14701 case 1: // result = add base, cond
14702 case 2: // result = lea base( , cond*2)
14703 case 3: // result = lea base(cond, cond*2)
14704 case 4: // result = lea base( , cond*4)
14705 case 5: // result = lea base(cond, cond*4)
14706 case 8: // result = lea base( , cond*8)
14707 case 9: // result = lea base(cond, cond*8)
14708 isFastMultiplier = true;
14709 break;
14710 }
14711 }
Eric Christopherfd179292009-08-27 18:07:15 +000014712
Chris Lattnercee56e72009-03-13 05:53:31 +000014713 if (isFastMultiplier) {
14714 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14715 if (NeedsCondInvert) // Invert the condition if needed.
14716 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14717 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014718
Chris Lattnercee56e72009-03-13 05:53:31 +000014719 // Zero extend the condition if needed.
14720 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14721 Cond);
14722 // Scale the condition by the difference.
14723 if (Diff != 1)
14724 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14725 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014726
Chris Lattnercee56e72009-03-13 05:53:31 +000014727 // Add the base if non-zero.
14728 if (FalseC->getAPIntValue() != 0)
14729 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14730 SDValue(FalseC, 0));
14731 return Cond;
14732 }
Eric Christopherfd179292009-08-27 18:07:15 +000014733 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014734 }
14735 }
Eric Christopherfd179292009-08-27 18:07:15 +000014736
Evan Cheng56f582d2012-01-04 01:41:39 +000014737 // Canonicalize max and min:
14738 // (x > y) ? x : y -> (x >= y) ? x : y
14739 // (x < y) ? x : y -> (x <= y) ? x : y
14740 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14741 // the need for an extra compare
14742 // against zero. e.g.
14743 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14744 // subl %esi, %edi
14745 // testl %edi, %edi
14746 // movl $0, %eax
14747 // cmovgl %edi, %eax
14748 // =>
14749 // xorl %eax, %eax
14750 // subl %esi, $edi
14751 // cmovsl %eax, %edi
14752 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14753 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14754 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14755 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14756 switch (CC) {
14757 default: break;
14758 case ISD::SETLT:
14759 case ISD::SETGT: {
14760 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14761 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14762 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14763 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14764 }
14765 }
14766 }
14767
Nadav Rotemcc616562012-01-15 19:27:55 +000014768 // If we know that this node is legal then we know that it is going to be
14769 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14770 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14771 // to simplify previous instructions.
14772 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14773 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014774 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014775 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014776
14777 // Don't optimize vector selects that map to mask-registers.
14778 if (BitWidth == 1)
14779 return SDValue();
14780
Nadav Rotemcc616562012-01-15 19:27:55 +000014781 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14782 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14783
14784 APInt KnownZero, KnownOne;
14785 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14786 DCI.isBeforeLegalizeOps());
14787 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14788 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14789 DCI.CommitTargetLoweringOpt(TLO);
14790 }
14791
Dan Gohman475871a2008-07-27 21:46:04 +000014792 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014793}
14794
Michael Liao2a33cec2012-08-10 19:58:13 +000014795// Check whether a boolean test is testing a boolean value generated by
14796// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14797// code.
14798//
14799// Simplify the following patterns:
14800// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14801// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14802// to (Op EFLAGS Cond)
14803//
14804// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14805// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14806// to (Op EFLAGS !Cond)
14807//
14808// where Op could be BRCOND or CMOV.
14809//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014810static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014811 // Quit if not CMP and SUB with its value result used.
14812 if (Cmp.getOpcode() != X86ISD::CMP &&
14813 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14814 return SDValue();
14815
14816 // Quit if not used as a boolean value.
14817 if (CC != X86::COND_E && CC != X86::COND_NE)
14818 return SDValue();
14819
14820 // Check CMP operands. One of them should be 0 or 1 and the other should be
14821 // an SetCC or extended from it.
14822 SDValue Op1 = Cmp.getOperand(0);
14823 SDValue Op2 = Cmp.getOperand(1);
14824
14825 SDValue SetCC;
14826 const ConstantSDNode* C = 0;
14827 bool needOppositeCond = (CC == X86::COND_E);
14828
14829 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14830 SetCC = Op2;
14831 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14832 SetCC = Op1;
14833 else // Quit if all operands are not constants.
14834 return SDValue();
14835
14836 if (C->getZExtValue() == 1)
14837 needOppositeCond = !needOppositeCond;
14838 else if (C->getZExtValue() != 0)
14839 // Quit if the constant is neither 0 or 1.
14840 return SDValue();
14841
14842 // Skip 'zext' node.
14843 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14844 SetCC = SetCC.getOperand(0);
14845
Michael Liao7fdc66b2012-09-10 16:36:16 +000014846 switch (SetCC.getOpcode()) {
14847 case X86ISD::SETCC:
14848 // Set the condition code or opposite one if necessary.
14849 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14850 if (needOppositeCond)
14851 CC = X86::GetOppositeBranchCondition(CC);
14852 return SetCC.getOperand(1);
14853 case X86ISD::CMOV: {
14854 // Check whether false/true value has canonical one, i.e. 0 or 1.
14855 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14856 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14857 // Quit if true value is not a constant.
14858 if (!TVal)
14859 return SDValue();
14860 // Quit if false value is not a constant.
14861 if (!FVal) {
14862 // A special case for rdrand, where 0 is set if false cond is found.
14863 SDValue Op = SetCC.getOperand(0);
14864 if (Op.getOpcode() != X86ISD::RDRAND)
14865 return SDValue();
14866 }
14867 // Quit if false value is not the constant 0 or 1.
14868 bool FValIsFalse = true;
14869 if (FVal && FVal->getZExtValue() != 0) {
14870 if (FVal->getZExtValue() != 1)
14871 return SDValue();
14872 // If FVal is 1, opposite cond is needed.
14873 needOppositeCond = !needOppositeCond;
14874 FValIsFalse = false;
14875 }
14876 // Quit if TVal is not the constant opposite of FVal.
14877 if (FValIsFalse && TVal->getZExtValue() != 1)
14878 return SDValue();
14879 if (!FValIsFalse && TVal->getZExtValue() != 0)
14880 return SDValue();
14881 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14882 if (needOppositeCond)
14883 CC = X86::GetOppositeBranchCondition(CC);
14884 return SetCC.getOperand(3);
14885 }
14886 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014887
Michael Liao7fdc66b2012-09-10 16:36:16 +000014888 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014889}
14890
Chris Lattnerd1980a52009-03-12 06:52:53 +000014891/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14892static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014893 TargetLowering::DAGCombinerInfo &DCI,
14894 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014895 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014896
Chris Lattnerd1980a52009-03-12 06:52:53 +000014897 // If the flag operand isn't dead, don't touch this CMOV.
14898 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14899 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014900
Evan Chengb5a55d92011-05-24 01:48:22 +000014901 SDValue FalseOp = N->getOperand(0);
14902 SDValue TrueOp = N->getOperand(1);
14903 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14904 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014905
Evan Chengb5a55d92011-05-24 01:48:22 +000014906 if (CC == X86::COND_E || CC == X86::COND_NE) {
14907 switch (Cond.getOpcode()) {
14908 default: break;
14909 case X86ISD::BSR:
14910 case X86ISD::BSF:
14911 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14912 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14913 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14914 }
14915 }
14916
Michael Liao2a33cec2012-08-10 19:58:13 +000014917 SDValue Flags;
14918
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014919 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014920 if (Flags.getNode() &&
14921 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014922 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014923 SDValue Ops[] = { FalseOp, TrueOp,
14924 DAG.getConstant(CC, MVT::i8), Flags };
14925 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14926 Ops, array_lengthof(Ops));
14927 }
14928
Chris Lattnerd1980a52009-03-12 06:52:53 +000014929 // If this is a select between two integer constants, try to do some
14930 // optimizations. Note that the operands are ordered the opposite of SELECT
14931 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014932 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14933 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014934 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14935 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014936 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14937 CC = X86::GetOppositeBranchCondition(CC);
14938 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000014939 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014940 }
Eric Christopherfd179292009-08-27 18:07:15 +000014941
Chris Lattnerd1980a52009-03-12 06:52:53 +000014942 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014943 // This is efficient for any integer data type (including i8/i16) and
14944 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014945 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014946 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14947 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014948
Chris Lattnerd1980a52009-03-12 06:52:53 +000014949 // Zero extend the condition if needed.
14950 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014951
Chris Lattnerd1980a52009-03-12 06:52:53 +000014952 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14953 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014954 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014955 if (N->getNumValues() == 2) // Dead flag value?
14956 return DCI.CombineTo(N, Cond, SDValue());
14957 return Cond;
14958 }
Eric Christopherfd179292009-08-27 18:07:15 +000014959
Chris Lattnercee56e72009-03-13 05:53:31 +000014960 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14961 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014962 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014963 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14964 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014965
Chris Lattner97a29a52009-03-13 05:22:11 +000014966 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014967 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14968 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014969 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14970 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014971
Chris Lattner97a29a52009-03-13 05:22:11 +000014972 if (N->getNumValues() == 2) // Dead flag value?
14973 return DCI.CombineTo(N, Cond, SDValue());
14974 return Cond;
14975 }
Eric Christopherfd179292009-08-27 18:07:15 +000014976
Chris Lattnercee56e72009-03-13 05:53:31 +000014977 // Optimize cases that will turn into an LEA instruction. This requires
14978 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014979 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014980 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014981 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014982
Chris Lattnercee56e72009-03-13 05:53:31 +000014983 bool isFastMultiplier = false;
14984 if (Diff < 10) {
14985 switch ((unsigned char)Diff) {
14986 default: break;
14987 case 1: // result = add base, cond
14988 case 2: // result = lea base( , cond*2)
14989 case 3: // result = lea base(cond, cond*2)
14990 case 4: // result = lea base( , cond*4)
14991 case 5: // result = lea base(cond, cond*4)
14992 case 8: // result = lea base( , cond*8)
14993 case 9: // result = lea base(cond, cond*8)
14994 isFastMultiplier = true;
14995 break;
14996 }
14997 }
Eric Christopherfd179292009-08-27 18:07:15 +000014998
Chris Lattnercee56e72009-03-13 05:53:31 +000014999 if (isFastMultiplier) {
15000 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015001 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15002 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015003 // Zero extend the condition if needed.
15004 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15005 Cond);
15006 // Scale the condition by the difference.
15007 if (Diff != 1)
15008 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15009 DAG.getConstant(Diff, Cond.getValueType()));
15010
15011 // Add the base if non-zero.
15012 if (FalseC->getAPIntValue() != 0)
15013 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15014 SDValue(FalseC, 0));
15015 if (N->getNumValues() == 2) // Dead flag value?
15016 return DCI.CombineTo(N, Cond, SDValue());
15017 return Cond;
15018 }
Eric Christopherfd179292009-08-27 18:07:15 +000015019 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015020 }
15021 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015022
15023 // Handle these cases:
15024 // (select (x != c), e, c) -> select (x != c), e, x),
15025 // (select (x == c), c, e) -> select (x == c), x, e)
15026 // where the c is an integer constant, and the "select" is the combination
15027 // of CMOV and CMP.
15028 //
15029 // The rationale for this change is that the conditional-move from a constant
15030 // needs two instructions, however, conditional-move from a register needs
15031 // only one instruction.
15032 //
15033 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15034 // some instruction-combining opportunities. This opt needs to be
15035 // postponed as late as possible.
15036 //
15037 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15038 // the DCI.xxxx conditions are provided to postpone the optimization as
15039 // late as possible.
15040
15041 ConstantSDNode *CmpAgainst = 0;
15042 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15043 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15044 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15045
15046 if (CC == X86::COND_NE &&
15047 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15048 CC = X86::GetOppositeBranchCondition(CC);
15049 std::swap(TrueOp, FalseOp);
15050 }
15051
15052 if (CC == X86::COND_E &&
15053 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15054 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15055 DAG.getConstant(CC, MVT::i8), Cond };
15056 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15057 array_lengthof(Ops));
15058 }
15059 }
15060 }
15061
Chris Lattnerd1980a52009-03-12 06:52:53 +000015062 return SDValue();
15063}
15064
15065
Evan Cheng0b0cd912009-03-28 05:57:29 +000015066/// PerformMulCombine - Optimize a single multiply with constant into two
15067/// in order to implement it with two cheaper instructions, e.g.
15068/// LEA + SHL, LEA + LEA.
15069static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15070 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015071 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15072 return SDValue();
15073
Owen Andersone50ed302009-08-10 22:56:29 +000015074 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015075 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015076 return SDValue();
15077
15078 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15079 if (!C)
15080 return SDValue();
15081 uint64_t MulAmt = C->getZExtValue();
15082 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15083 return SDValue();
15084
15085 uint64_t MulAmt1 = 0;
15086 uint64_t MulAmt2 = 0;
15087 if ((MulAmt % 9) == 0) {
15088 MulAmt1 = 9;
15089 MulAmt2 = MulAmt / 9;
15090 } else if ((MulAmt % 5) == 0) {
15091 MulAmt1 = 5;
15092 MulAmt2 = MulAmt / 5;
15093 } else if ((MulAmt % 3) == 0) {
15094 MulAmt1 = 3;
15095 MulAmt2 = MulAmt / 3;
15096 }
15097 if (MulAmt2 &&
15098 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15099 DebugLoc DL = N->getDebugLoc();
15100
15101 if (isPowerOf2_64(MulAmt2) &&
15102 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15103 // If second multiplifer is pow2, issue it first. We want the multiply by
15104 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15105 // is an add.
15106 std::swap(MulAmt1, MulAmt2);
15107
15108 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015109 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015110 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015111 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015112 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015113 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015114 DAG.getConstant(MulAmt1, VT));
15115
Eric Christopherfd179292009-08-27 18:07:15 +000015116 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015117 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015118 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015119 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015120 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015121 DAG.getConstant(MulAmt2, VT));
15122
15123 // Do not add new nodes to DAG combiner worklist.
15124 DCI.CombineTo(N, NewMul, false);
15125 }
15126 return SDValue();
15127}
15128
Evan Chengad9c0a32009-12-15 00:53:42 +000015129static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15130 SDValue N0 = N->getOperand(0);
15131 SDValue N1 = N->getOperand(1);
15132 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15133 EVT VT = N0.getValueType();
15134
15135 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15136 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015137 if (VT.isInteger() && !VT.isVector() &&
15138 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015139 N0.getOperand(1).getOpcode() == ISD::Constant) {
15140 SDValue N00 = N0.getOperand(0);
15141 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15142 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15143 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15144 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15145 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15146 APInt ShAmt = N1C->getAPIntValue();
15147 Mask = Mask.shl(ShAmt);
15148 if (Mask != 0)
15149 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15150 N00, DAG.getConstant(Mask, VT));
15151 }
15152 }
15153
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015154
15155 // Hardware support for vector shifts is sparse which makes us scalarize the
15156 // vector operations in many cases. Also, on sandybridge ADD is faster than
15157 // shl.
15158 // (shl V, 1) -> add V,V
15159 if (isSplatVector(N1.getNode())) {
15160 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15161 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15162 // We shift all of the values by one. In many cases we do not have
15163 // hardware support for this operation. This is better expressed as an ADD
15164 // of two values.
15165 if (N1C && (1 == N1C->getZExtValue())) {
15166 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15167 }
15168 }
15169
Evan Chengad9c0a32009-12-15 00:53:42 +000015170 return SDValue();
15171}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015172
Nate Begeman740ab032009-01-26 00:52:55 +000015173/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15174/// when possible.
15175static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015176 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015177 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015178 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015179 if (N->getOpcode() == ISD::SHL) {
15180 SDValue V = PerformSHLCombine(N, DAG);
15181 if (V.getNode()) return V;
15182 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015183
Nate Begeman740ab032009-01-26 00:52:55 +000015184 // On X86 with SSE2 support, we can transform this to a vector shift if
15185 // all elements are shifted by the same amount. We can't do this in legalize
15186 // because the a constant vector is typically transformed to a constant pool
15187 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015188 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015189 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015190
Craig Topper7be5dfd2011-11-12 09:58:49 +000015191 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
15192 (!Subtarget->hasAVX2() ||
15193 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015194 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015195
Mon P Wang3becd092009-01-28 08:12:05 +000015196 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015197 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015198 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015199 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015200 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15201 unsigned NumElts = VT.getVectorNumElements();
15202 unsigned i = 0;
15203 for (; i != NumElts; ++i) {
15204 SDValue Arg = ShAmtOp.getOperand(i);
15205 if (Arg.getOpcode() == ISD::UNDEF) continue;
15206 BaseShAmt = Arg;
15207 break;
15208 }
Craig Topper37c26772012-01-17 04:44:50 +000015209 // Handle the case where the build_vector is all undef
15210 // FIXME: Should DAG allow this?
15211 if (i == NumElts)
15212 return SDValue();
15213
Mon P Wang3becd092009-01-28 08:12:05 +000015214 for (; i != NumElts; ++i) {
15215 SDValue Arg = ShAmtOp.getOperand(i);
15216 if (Arg.getOpcode() == ISD::UNDEF) continue;
15217 if (Arg != BaseShAmt) {
15218 return SDValue();
15219 }
15220 }
15221 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015222 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015223 SDValue InVec = ShAmtOp.getOperand(0);
15224 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15225 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15226 unsigned i = 0;
15227 for (; i != NumElts; ++i) {
15228 SDValue Arg = InVec.getOperand(i);
15229 if (Arg.getOpcode() == ISD::UNDEF) continue;
15230 BaseShAmt = Arg;
15231 break;
15232 }
15233 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15234 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015235 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015236 if (C->getZExtValue() == SplatIdx)
15237 BaseShAmt = InVec.getOperand(1);
15238 }
15239 }
Mon P Wang845b1892012-02-01 22:15:20 +000015240 if (BaseShAmt.getNode() == 0) {
15241 // Don't create instructions with illegal types after legalize
15242 // types has run.
15243 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15244 !DCI.isBeforeLegalize())
15245 return SDValue();
15246
Mon P Wangefa42202009-09-03 19:56:25 +000015247 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15248 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015249 }
Mon P Wang3becd092009-01-28 08:12:05 +000015250 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015251 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015252
Mon P Wangefa42202009-09-03 19:56:25 +000015253 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015254 if (EltVT.bitsGT(MVT::i32))
15255 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15256 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015257 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015258
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015259 // The shift amount is identical so we can do a vector shift.
15260 SDValue ValOp = N->getOperand(0);
15261 switch (N->getOpcode()) {
15262 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015263 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015264 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015265 switch (VT.getSimpleVT().SimpleTy) {
15266 default: return SDValue();
15267 case MVT::v2i64:
15268 case MVT::v4i32:
15269 case MVT::v8i16:
15270 case MVT::v4i64:
15271 case MVT::v8i32:
15272 case MVT::v16i16:
15273 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15274 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015275 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015276 switch (VT.getSimpleVT().SimpleTy) {
15277 default: return SDValue();
15278 case MVT::v4i32:
15279 case MVT::v8i16:
15280 case MVT::v8i32:
15281 case MVT::v16i16:
15282 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15283 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015284 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015285 switch (VT.getSimpleVT().SimpleTy) {
15286 default: return SDValue();
15287 case MVT::v2i64:
15288 case MVT::v4i32:
15289 case MVT::v8i16:
15290 case MVT::v4i64:
15291 case MVT::v8i32:
15292 case MVT::v16i16:
15293 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15294 }
Nate Begeman740ab032009-01-26 00:52:55 +000015295 }
Nate Begeman740ab032009-01-26 00:52:55 +000015296}
15297
Nate Begemanb65c1752010-12-17 22:55:37 +000015298
Stuart Hastings865f0932011-06-03 23:53:54 +000015299// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15300// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15301// and friends. Likewise for OR -> CMPNEQSS.
15302static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15303 TargetLowering::DAGCombinerInfo &DCI,
15304 const X86Subtarget *Subtarget) {
15305 unsigned opcode;
15306
15307 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15308 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015309 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015310 SDValue N0 = N->getOperand(0);
15311 SDValue N1 = N->getOperand(1);
15312 SDValue CMP0 = N0->getOperand(1);
15313 SDValue CMP1 = N1->getOperand(1);
15314 DebugLoc DL = N->getDebugLoc();
15315
15316 // The SETCCs should both refer to the same CMP.
15317 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15318 return SDValue();
15319
15320 SDValue CMP00 = CMP0->getOperand(0);
15321 SDValue CMP01 = CMP0->getOperand(1);
15322 EVT VT = CMP00.getValueType();
15323
15324 if (VT == MVT::f32 || VT == MVT::f64) {
15325 bool ExpectingFlags = false;
15326 // Check for any users that want flags:
15327 for (SDNode::use_iterator UI = N->use_begin(),
15328 UE = N->use_end();
15329 !ExpectingFlags && UI != UE; ++UI)
15330 switch (UI->getOpcode()) {
15331 default:
15332 case ISD::BR_CC:
15333 case ISD::BRCOND:
15334 case ISD::SELECT:
15335 ExpectingFlags = true;
15336 break;
15337 case ISD::CopyToReg:
15338 case ISD::SIGN_EXTEND:
15339 case ISD::ZERO_EXTEND:
15340 case ISD::ANY_EXTEND:
15341 break;
15342 }
15343
15344 if (!ExpectingFlags) {
15345 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15346 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15347
15348 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15349 X86::CondCode tmp = cc0;
15350 cc0 = cc1;
15351 cc1 = tmp;
15352 }
15353
15354 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15355 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15356 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15357 X86ISD::NodeType NTOperator = is64BitFP ?
15358 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15359 // FIXME: need symbolic constants for these magic numbers.
15360 // See X86ATTInstPrinter.cpp:printSSECC().
15361 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15362 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15363 DAG.getConstant(x86cc, MVT::i8));
15364 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15365 OnesOrZeroesF);
15366 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15367 DAG.getConstant(1, MVT::i32));
15368 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15369 return OneBitOfTruth;
15370 }
15371 }
15372 }
15373 }
15374 return SDValue();
15375}
15376
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015377/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15378/// so it can be folded inside ANDNP.
15379static bool CanFoldXORWithAllOnes(const SDNode *N) {
15380 EVT VT = N->getValueType(0);
15381
15382 // Match direct AllOnes for 128 and 256-bit vectors
15383 if (ISD::isBuildVectorAllOnes(N))
15384 return true;
15385
15386 // Look through a bit convert.
15387 if (N->getOpcode() == ISD::BITCAST)
15388 N = N->getOperand(0).getNode();
15389
15390 // Sometimes the operand may come from a insert_subvector building a 256-bit
15391 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015392 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015393 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15394 SDValue V1 = N->getOperand(0);
15395 SDValue V2 = N->getOperand(1);
15396
15397 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15398 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15399 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15400 ISD::isBuildVectorAllOnes(V2.getNode()))
15401 return true;
15402 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015403
15404 return false;
15405}
15406
Nate Begemanb65c1752010-12-17 22:55:37 +000015407static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15408 TargetLowering::DAGCombinerInfo &DCI,
15409 const X86Subtarget *Subtarget) {
15410 if (DCI.isBeforeLegalizeOps())
15411 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015412
Stuart Hastings865f0932011-06-03 23:53:54 +000015413 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15414 if (R.getNode())
15415 return R;
15416
Craig Topper54a11172011-10-14 07:06:56 +000015417 EVT VT = N->getValueType(0);
15418
Craig Topperb4c94572011-10-21 06:55:01 +000015419 // Create ANDN, BLSI, and BLSR instructions
15420 // BLSI is X & (-X)
15421 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015422 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15423 SDValue N0 = N->getOperand(0);
15424 SDValue N1 = N->getOperand(1);
15425 DebugLoc DL = N->getDebugLoc();
15426
15427 // Check LHS for not
15428 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
15429 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
15430 // Check RHS for not
15431 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
15432 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
15433
Craig Topperb4c94572011-10-21 06:55:01 +000015434 // Check LHS for neg
15435 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15436 isZero(N0.getOperand(0)))
15437 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15438
15439 // Check RHS for neg
15440 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15441 isZero(N1.getOperand(0)))
15442 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15443
15444 // Check LHS for X-1
15445 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15446 isAllOnes(N0.getOperand(1)))
15447 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15448
15449 // Check RHS for X-1
15450 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15451 isAllOnes(N1.getOperand(1)))
15452 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15453
Craig Topper54a11172011-10-14 07:06:56 +000015454 return SDValue();
15455 }
15456
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015457 // Want to form ANDNP nodes:
15458 // 1) In the hopes of then easily combining them with OR and AND nodes
15459 // to form PBLEND/PSIGN.
15460 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015461 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015462 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015463
Nate Begemanb65c1752010-12-17 22:55:37 +000015464 SDValue N0 = N->getOperand(0);
15465 SDValue N1 = N->getOperand(1);
15466 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015467
Nate Begemanb65c1752010-12-17 22:55:37 +000015468 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015469 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015470 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15471 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015472 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015473
15474 // Check RHS for vnot
15475 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015476 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15477 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015478 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015479
Nate Begemanb65c1752010-12-17 22:55:37 +000015480 return SDValue();
15481}
15482
Evan Cheng760d1942010-01-04 21:22:48 +000015483static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015484 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015485 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015486 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015487 return SDValue();
15488
Stuart Hastings865f0932011-06-03 23:53:54 +000015489 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15490 if (R.getNode())
15491 return R;
15492
Evan Cheng760d1942010-01-04 21:22:48 +000015493 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015494
Evan Cheng760d1942010-01-04 21:22:48 +000015495 SDValue N0 = N->getOperand(0);
15496 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015497
Nate Begemanb65c1752010-12-17 22:55:37 +000015498 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015499 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015500 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000015501 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
15502 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015503
Craig Topper1666cb62011-11-19 07:07:26 +000015504 // Canonicalize pandn to RHS
15505 if (N0.getOpcode() == X86ISD::ANDNP)
15506 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015507 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015508 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15509 SDValue Mask = N1.getOperand(0);
15510 SDValue X = N1.getOperand(1);
15511 SDValue Y;
15512 if (N0.getOperand(0) == Mask)
15513 Y = N0.getOperand(1);
15514 if (N0.getOperand(1) == Mask)
15515 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015516
Craig Topper1666cb62011-11-19 07:07:26 +000015517 // Check to see if the mask appeared in both the AND and ANDNP and
15518 if (!Y.getNode())
15519 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015520
Craig Topper1666cb62011-11-19 07:07:26 +000015521 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015522 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015523 if (Mask.getOpcode() == ISD::BITCAST)
15524 Mask = Mask.getOperand(0);
15525 if (X.getOpcode() == ISD::BITCAST)
15526 X = X.getOperand(0);
15527 if (Y.getOpcode() == ISD::BITCAST)
15528 Y = Y.getOperand(0);
15529
Craig Topper1666cb62011-11-19 07:07:26 +000015530 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015531
Craig Toppered2e13d2012-01-22 19:15:14 +000015532 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015533 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15534 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015535 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015536 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015537
15538 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015539 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015540 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15541 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15542 if ((SraAmt + 1) != EltBits)
15543 return SDValue();
15544
15545 DebugLoc DL = N->getDebugLoc();
15546
15547 // Now we know we at least have a plendvb with the mask val. See if
15548 // we can form a psignb/w/d.
15549 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015550 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15551 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015552 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15553 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15554 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015555 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015556 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015557 }
15558 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015559 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015560 return SDValue();
15561
15562 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15563
15564 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15565 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15566 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015567 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015568 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015569 }
15570 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015571
Craig Topper1666cb62011-11-19 07:07:26 +000015572 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15573 return SDValue();
15574
Nate Begemanb65c1752010-12-17 22:55:37 +000015575 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015576 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15577 std::swap(N0, N1);
15578 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15579 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015580 if (!N0.hasOneUse() || !N1.hasOneUse())
15581 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015582
15583 SDValue ShAmt0 = N0.getOperand(1);
15584 if (ShAmt0.getValueType() != MVT::i8)
15585 return SDValue();
15586 SDValue ShAmt1 = N1.getOperand(1);
15587 if (ShAmt1.getValueType() != MVT::i8)
15588 return SDValue();
15589 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15590 ShAmt0 = ShAmt0.getOperand(0);
15591 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15592 ShAmt1 = ShAmt1.getOperand(0);
15593
15594 DebugLoc DL = N->getDebugLoc();
15595 unsigned Opc = X86ISD::SHLD;
15596 SDValue Op0 = N0.getOperand(0);
15597 SDValue Op1 = N1.getOperand(0);
15598 if (ShAmt0.getOpcode() == ISD::SUB) {
15599 Opc = X86ISD::SHRD;
15600 std::swap(Op0, Op1);
15601 std::swap(ShAmt0, ShAmt1);
15602 }
15603
Evan Cheng8b1190a2010-04-28 01:18:01 +000015604 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015605 if (ShAmt1.getOpcode() == ISD::SUB) {
15606 SDValue Sum = ShAmt1.getOperand(0);
15607 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015608 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15609 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15610 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15611 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015612 return DAG.getNode(Opc, DL, VT,
15613 Op0, Op1,
15614 DAG.getNode(ISD::TRUNCATE, DL,
15615 MVT::i8, ShAmt0));
15616 }
15617 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15618 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15619 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015620 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015621 return DAG.getNode(Opc, DL, VT,
15622 N0.getOperand(0), N1.getOperand(0),
15623 DAG.getNode(ISD::TRUNCATE, DL,
15624 MVT::i8, ShAmt0));
15625 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015626
Evan Cheng760d1942010-01-04 21:22:48 +000015627 return SDValue();
15628}
15629
Manman Ren92363622012-06-07 22:39:10 +000015630// Generate NEG and CMOV for integer abs.
15631static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15632 EVT VT = N->getValueType(0);
15633
15634 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15635 // 8-bit integer abs to NEG and CMOV.
15636 if (VT.isInteger() && VT.getSizeInBits() == 8)
15637 return SDValue();
15638
15639 SDValue N0 = N->getOperand(0);
15640 SDValue N1 = N->getOperand(1);
15641 DebugLoc DL = N->getDebugLoc();
15642
15643 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15644 // and change it to SUB and CMOV.
15645 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15646 N0.getOpcode() == ISD::ADD &&
15647 N0.getOperand(1) == N1 &&
15648 N1.getOpcode() == ISD::SRA &&
15649 N1.getOperand(0) == N0.getOperand(0))
15650 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15651 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15652 // Generate SUB & CMOV.
15653 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15654 DAG.getConstant(0, VT), N0.getOperand(0));
15655
15656 SDValue Ops[] = { N0.getOperand(0), Neg,
15657 DAG.getConstant(X86::COND_GE, MVT::i8),
15658 SDValue(Neg.getNode(), 1) };
15659 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15660 Ops, array_lengthof(Ops));
15661 }
15662 return SDValue();
15663}
15664
Craig Topper3738ccd2011-12-27 06:27:23 +000015665// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015666static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15667 TargetLowering::DAGCombinerInfo &DCI,
15668 const X86Subtarget *Subtarget) {
15669 if (DCI.isBeforeLegalizeOps())
15670 return SDValue();
15671
Manman Ren45d53b82012-06-08 18:58:26 +000015672 if (Subtarget->hasCMov()) {
15673 SDValue RV = performIntegerAbsCombine(N, DAG);
15674 if (RV.getNode())
15675 return RV;
15676 }
Manman Ren92363622012-06-07 22:39:10 +000015677
15678 // Try forming BMI if it is available.
15679 if (!Subtarget->hasBMI())
15680 return SDValue();
15681
Craig Topperb4c94572011-10-21 06:55:01 +000015682 EVT VT = N->getValueType(0);
15683
15684 if (VT != MVT::i32 && VT != MVT::i64)
15685 return SDValue();
15686
Craig Topper3738ccd2011-12-27 06:27:23 +000015687 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15688
Craig Topperb4c94572011-10-21 06:55:01 +000015689 // Create BLSMSK instructions by finding X ^ (X-1)
15690 SDValue N0 = N->getOperand(0);
15691 SDValue N1 = N->getOperand(1);
15692 DebugLoc DL = N->getDebugLoc();
15693
15694 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15695 isAllOnes(N0.getOperand(1)))
15696 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15697
15698 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15699 isAllOnes(N1.getOperand(1)))
15700 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15701
15702 return SDValue();
15703}
15704
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015705/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15706static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015707 TargetLowering::DAGCombinerInfo &DCI,
15708 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015709 LoadSDNode *Ld = cast<LoadSDNode>(N);
15710 EVT RegVT = Ld->getValueType(0);
15711 EVT MemVT = Ld->getMemoryVT();
15712 DebugLoc dl = Ld->getDebugLoc();
15713 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15714
15715 ISD::LoadExtType Ext = Ld->getExtensionType();
15716
Nadav Rotemca6f2962011-09-18 19:00:23 +000015717 // If this is a vector EXT Load then attempt to optimize it using a
Michael Liao35a56402012-10-17 03:59:18 +000015718 // shuffle. We need SSSE3 shuffles.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015719 // TODO: It is possible to support ZExt by zeroing the undef values
15720 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015721 if (RegVT.isVector() && RegVT.isInteger() &&
Michael Liao35a56402012-10-17 03:59:18 +000015722 Ext == ISD::EXTLOAD && Subtarget->hasSSSE3()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015723 assert(MemVT != RegVT && "Cannot extend to the same type");
15724 assert(MemVT.isVector() && "Must load a vector from memory");
15725
15726 unsigned NumElems = RegVT.getVectorNumElements();
15727 unsigned RegSz = RegVT.getSizeInBits();
15728 unsigned MemSz = MemVT.getSizeInBits();
15729 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015730
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015731 // All sizes must be a power of two.
15732 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15733 return SDValue();
15734
15735 // Attempt to load the original value using scalar loads.
15736 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015737 MVT SclrLoadTy = MVT::i8;
15738 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15739 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15740 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015741 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015742 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015743 }
15744 }
15745
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015746 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15747 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15748 (64 <= MemSz))
15749 SclrLoadTy = MVT::f64;
15750
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015751 // Calculate the number of scalar loads that we need to perform
15752 // in order to load our vector from memory.
15753 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015754
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015755 // Represent our vector as a sequence of elements which are the
15756 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015757 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15758 RegSz/SclrLoadTy.getSizeInBits());
15759
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015760 // Represent the data using the same element type that is stored in
15761 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015762 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15763 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015764
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015765 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15766 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015767
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015768 // We can't shuffle using an illegal type.
15769 if (!TLI.isTypeLegal(WideVecVT))
15770 return SDValue();
15771
15772 SmallVector<SDValue, 8> Chains;
15773 SDValue Ptr = Ld->getBasePtr();
15774 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15775 TLI.getPointerTy());
15776 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15777
15778 for (unsigned i = 0; i < NumLoads; ++i) {
15779 // Perform a single load.
15780 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15781 Ptr, Ld->getPointerInfo(),
15782 Ld->isVolatile(), Ld->isNonTemporal(),
15783 Ld->isInvariant(), Ld->getAlignment());
15784 Chains.push_back(ScalarLoad.getValue(1));
15785 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15786 // another round of DAGCombining.
15787 if (i == 0)
15788 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15789 else
15790 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15791 ScalarLoad, DAG.getIntPtrConstant(i));
15792
15793 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15794 }
15795
15796 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15797 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015798
15799 // Bitcast the loaded value to a vector of the original element type, in
15800 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015801 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015802 unsigned SizeRatio = RegSz/MemSz;
15803
15804 // Redistribute the loaded elements into the different locations.
15805 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015806 for (unsigned i = 0; i != NumElems; ++i)
15807 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015808
15809 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015810 DAG.getUNDEF(WideVecVT),
15811 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015812
15813 // Bitcast to the requested type.
15814 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15815 // Replace the original load with the new sequence
15816 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015817 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015818 }
15819
15820 return SDValue();
15821}
15822
Chris Lattner149a4e52008-02-22 02:09:43 +000015823/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015824static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015825 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015826 StoreSDNode *St = cast<StoreSDNode>(N);
15827 EVT VT = St->getValue().getValueType();
15828 EVT StVT = St->getMemoryVT();
15829 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015830 SDValue StoredVal = St->getOperand(1);
15831 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15832
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015833 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015834 // On Sandy Bridge, 256-bit memory operations are executed by two
15835 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15836 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015837 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015838 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15839 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015840 SDValue Value0 = StoredVal.getOperand(0);
15841 SDValue Value1 = StoredVal.getOperand(1);
15842
15843 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15844 SDValue Ptr0 = St->getBasePtr();
15845 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15846
15847 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15848 St->getPointerInfo(), St->isVolatile(),
15849 St->isNonTemporal(), St->getAlignment());
15850 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15851 St->getPointerInfo(), St->isVolatile(),
15852 St->isNonTemporal(), St->getAlignment());
15853 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15854 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015855
15856 // Optimize trunc store (of multiple scalars) to shuffle and store.
15857 // First, pack all of the elements in one place. Next, store to memory
15858 // in fewer chunks.
15859 if (St->isTruncatingStore() && VT.isVector()) {
15860 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15861 unsigned NumElems = VT.getVectorNumElements();
15862 assert(StVT != VT && "Cannot truncate to the same type");
15863 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15864 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15865
15866 // From, To sizes and ElemCount must be pow of two
15867 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015868 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015869 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015870 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015871
Nadav Rotem614061b2011-08-10 19:30:14 +000015872 unsigned SizeRatio = FromSz / ToSz;
15873
15874 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15875
15876 // Create a type on which we perform the shuffle
15877 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15878 StVT.getScalarType(), NumElems*SizeRatio);
15879
15880 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15881
15882 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15883 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015884 for (unsigned i = 0; i != NumElems; ++i)
15885 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015886
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015887 // Can't shuffle using an illegal type.
15888 if (!TLI.isTypeLegal(WideVecVT))
15889 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015890
15891 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015892 DAG.getUNDEF(WideVecVT),
15893 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015894 // At this point all of the data is stored at the bottom of the
15895 // register. We now need to save it to mem.
15896
15897 // Find the largest store unit
15898 MVT StoreType = MVT::i8;
15899 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15900 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15901 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015902 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015903 StoreType = Tp;
15904 }
15905
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015906 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15907 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15908 (64 <= NumElems * ToSz))
15909 StoreType = MVT::f64;
15910
Nadav Rotem614061b2011-08-10 19:30:14 +000015911 // Bitcast the original vector into a vector of store-size units
15912 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015913 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015914 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15915 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15916 SmallVector<SDValue, 8> Chains;
15917 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15918 TLI.getPointerTy());
15919 SDValue Ptr = St->getBasePtr();
15920
15921 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015922 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015923 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15924 StoreType, ShuffWide,
15925 DAG.getIntPtrConstant(i));
15926 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15927 St->getPointerInfo(), St->isVolatile(),
15928 St->isNonTemporal(), St->getAlignment());
15929 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15930 Chains.push_back(Ch);
15931 }
15932
15933 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15934 Chains.size());
15935 }
15936
15937
Chris Lattner149a4e52008-02-22 02:09:43 +000015938 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15939 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015940 // A preferable solution to the general problem is to figure out the right
15941 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015942
15943 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015944 if (VT.getSizeInBits() != 64)
15945 return SDValue();
15946
Devang Patel578efa92009-06-05 21:57:13 +000015947 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000015948 bool NoImplicitFloatOps = F->getFnAttributes().
15949 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015950 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015951 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015952 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015953 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015954 isa<LoadSDNode>(St->getValue()) &&
15955 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15956 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015957 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015958 LoadSDNode *Ld = 0;
15959 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015960 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015961 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015962 // Must be a store of a load. We currently handle two cases: the load
15963 // is a direct child, and it's under an intervening TokenFactor. It is
15964 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015965 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015966 Ld = cast<LoadSDNode>(St->getChain());
15967 else if (St->getValue().hasOneUse() &&
15968 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015969 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015970 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015971 TokenFactorIndex = i;
15972 Ld = cast<LoadSDNode>(St->getValue());
15973 } else
15974 Ops.push_back(ChainVal->getOperand(i));
15975 }
15976 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015977
Evan Cheng536e6672009-03-12 05:59:15 +000015978 if (!Ld || !ISD::isNormalLoad(Ld))
15979 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015980
Evan Cheng536e6672009-03-12 05:59:15 +000015981 // If this is not the MMX case, i.e. we are just turning i64 load/store
15982 // into f64 load/store, avoid the transformation if there are multiple
15983 // uses of the loaded value.
15984 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15985 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015986
Evan Cheng536e6672009-03-12 05:59:15 +000015987 DebugLoc LdDL = Ld->getDebugLoc();
15988 DebugLoc StDL = N->getDebugLoc();
15989 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15990 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15991 // pair instead.
15992 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015993 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015994 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15995 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015996 Ld->isNonTemporal(), Ld->isInvariant(),
15997 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015998 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015999 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016000 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016001 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016002 Ops.size());
16003 }
Evan Cheng536e6672009-03-12 05:59:15 +000016004 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016005 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016006 St->isVolatile(), St->isNonTemporal(),
16007 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016008 }
Evan Cheng536e6672009-03-12 05:59:15 +000016009
16010 // Otherwise, lower to two pairs of 32-bit loads / stores.
16011 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016012 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16013 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016014
Owen Anderson825b72b2009-08-11 20:47:22 +000016015 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016016 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016017 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016018 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016019 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016020 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016021 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016022 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016023 MinAlign(Ld->getAlignment(), 4));
16024
16025 SDValue NewChain = LoLd.getValue(1);
16026 if (TokenFactorIndex != -1) {
16027 Ops.push_back(LoLd);
16028 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016029 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016030 Ops.size());
16031 }
16032
16033 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016034 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16035 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016036
16037 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016038 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016039 St->isVolatile(), St->isNonTemporal(),
16040 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016041 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016042 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016043 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016044 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016045 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016046 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016047 }
Dan Gohman475871a2008-07-27 21:46:04 +000016048 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016049}
16050
Duncan Sands17470be2011-09-22 20:15:48 +000016051/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16052/// and return the operands for the horizontal operation in LHS and RHS. A
16053/// horizontal operation performs the binary operation on successive elements
16054/// of its first operand, then on successive elements of its second operand,
16055/// returning the resulting values in a vector. For example, if
16056/// A = < float a0, float a1, float a2, float a3 >
16057/// and
16058/// B = < float b0, float b1, float b2, float b3 >
16059/// then the result of doing a horizontal operation on A and B is
16060/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16061/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16062/// A horizontal-op B, for some already available A and B, and if so then LHS is
16063/// set to A, RHS to B, and the routine returns 'true'.
16064/// Note that the binary operation should have the property that if one of the
16065/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016066static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016067 // Look for the following pattern: if
16068 // A = < float a0, float a1, float a2, float a3 >
16069 // B = < float b0, float b1, float b2, float b3 >
16070 // and
16071 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16072 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16073 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16074 // which is A horizontal-op B.
16075
16076 // At least one of the operands should be a vector shuffle.
16077 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16078 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16079 return false;
16080
16081 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016082
16083 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16084 "Unsupported vector type for horizontal add/sub");
16085
16086 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16087 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016088 unsigned NumElts = VT.getVectorNumElements();
16089 unsigned NumLanes = VT.getSizeInBits()/128;
16090 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016091 assert((NumLaneElts % 2 == 0) &&
16092 "Vector type should have an even number of elements in each lane");
16093 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016094
16095 // View LHS in the form
16096 // LHS = VECTOR_SHUFFLE A, B, LMask
16097 // If LHS is not a shuffle then pretend it is the shuffle
16098 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16099 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16100 // type VT.
16101 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016102 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016103 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16104 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16105 A = LHS.getOperand(0);
16106 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16107 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016108 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16109 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016110 } else {
16111 if (LHS.getOpcode() != ISD::UNDEF)
16112 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016113 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016114 LMask[i] = i;
16115 }
16116
16117 // Likewise, view RHS in the form
16118 // RHS = VECTOR_SHUFFLE C, D, RMask
16119 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016120 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016121 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16122 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16123 C = RHS.getOperand(0);
16124 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16125 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016126 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16127 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016128 } else {
16129 if (RHS.getOpcode() != ISD::UNDEF)
16130 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016131 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016132 RMask[i] = i;
16133 }
16134
16135 // Check that the shuffles are both shuffling the same vectors.
16136 if (!(A == C && B == D) && !(A == D && B == C))
16137 return false;
16138
16139 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16140 if (!A.getNode() && !B.getNode())
16141 return false;
16142
16143 // If A and B occur in reverse order in RHS, then "swap" them (which means
16144 // rewriting the mask).
16145 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016146 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016147
16148 // At this point LHS and RHS are equivalent to
16149 // LHS = VECTOR_SHUFFLE A, B, LMask
16150 // RHS = VECTOR_SHUFFLE A, B, RMask
16151 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016152 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016153 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016154
Craig Topperf8363302011-12-02 08:18:41 +000016155 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016156 if (LIdx < 0 || RIdx < 0 ||
16157 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16158 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016159 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016160
Craig Topperf8363302011-12-02 08:18:41 +000016161 // Check that successive elements are being operated on. If not, this is
16162 // not a horizontal operation.
16163 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16164 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016165 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016166 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016167 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016168 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016169 }
16170
16171 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16172 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16173 return true;
16174}
16175
16176/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16177static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16178 const X86Subtarget *Subtarget) {
16179 EVT VT = N->getValueType(0);
16180 SDValue LHS = N->getOperand(0);
16181 SDValue RHS = N->getOperand(1);
16182
16183 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016184 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016185 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016186 isHorizontalBinOp(LHS, RHS, true))
16187 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16188 return SDValue();
16189}
16190
16191/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16192static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16193 const X86Subtarget *Subtarget) {
16194 EVT VT = N->getValueType(0);
16195 SDValue LHS = N->getOperand(0);
16196 SDValue RHS = N->getOperand(1);
16197
16198 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016199 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016200 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016201 isHorizontalBinOp(LHS, RHS, false))
16202 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16203 return SDValue();
16204}
16205
Chris Lattner6cf73262008-01-25 06:14:17 +000016206/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16207/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016208static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016209 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16210 // F[X]OR(0.0, x) -> x
16211 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016212 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16213 if (C->getValueAPF().isPosZero())
16214 return N->getOperand(1);
16215 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16216 if (C->getValueAPF().isPosZero())
16217 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016218 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016219}
16220
Nadav Rotemd60cb112012-08-19 13:06:16 +000016221/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16222/// X86ISD::FMAX nodes.
16223static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16224 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16225
16226 // Only perform optimizations if UnsafeMath is used.
16227 if (!DAG.getTarget().Options.UnsafeFPMath)
16228 return SDValue();
16229
16230 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016231 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016232 unsigned NewOp = 0;
16233 switch (N->getOpcode()) {
16234 default: llvm_unreachable("unknown opcode");
16235 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16236 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16237 }
16238
16239 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16240 N->getOperand(0), N->getOperand(1));
16241}
16242
16243
Chris Lattneraf723b92008-01-25 05:46:26 +000016244/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016245static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016246 // FAND(0.0, x) -> 0.0
16247 // FAND(x, 0.0) -> 0.0
16248 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16249 if (C->getValueAPF().isPosZero())
16250 return N->getOperand(0);
16251 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16252 if (C->getValueAPF().isPosZero())
16253 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016254 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016255}
16256
Dan Gohmane5af2d32009-01-29 01:59:02 +000016257static SDValue PerformBTCombine(SDNode *N,
16258 SelectionDAG &DAG,
16259 TargetLowering::DAGCombinerInfo &DCI) {
16260 // BT ignores high bits in the bit index operand.
16261 SDValue Op1 = N->getOperand(1);
16262 if (Op1.hasOneUse()) {
16263 unsigned BitWidth = Op1.getValueSizeInBits();
16264 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16265 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016266 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16267 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016268 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016269 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16270 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16271 DCI.CommitTargetLoweringOpt(TLO);
16272 }
16273 return SDValue();
16274}
Chris Lattner83e6c992006-10-04 06:57:07 +000016275
Eli Friedman7a5e5552009-06-07 06:52:44 +000016276static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16277 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016278 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016279 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016280 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016281 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016282 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016283 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016284 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016285 }
16286 return SDValue();
16287}
16288
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016289static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16290 TargetLowering::DAGCombinerInfo &DCI,
16291 const X86Subtarget *Subtarget) {
16292 if (!DCI.isBeforeLegalizeOps())
16293 return SDValue();
16294
Craig Topper3ef43cf2012-04-24 06:36:35 +000016295 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016296 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016297
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016298 EVT VT = N->getValueType(0);
16299 SDValue Op = N->getOperand(0);
16300 EVT OpVT = Op.getValueType();
16301 DebugLoc dl = N->getDebugLoc();
16302
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016303 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16304 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016305
Craig Topper3ef43cf2012-04-24 06:36:35 +000016306 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016307 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016308
16309 // Optimize vectors in AVX mode
16310 // Sign extend v8i16 to v8i32 and
16311 // v4i32 to v4i64
16312 //
16313 // Divide input vector into two parts
16314 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16315 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16316 // concat the vectors to original VT
16317
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016318 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016319 SDValue Undef = DAG.getUNDEF(OpVT);
16320
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016321 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016322 for (unsigned i = 0; i != NumElems/2; ++i)
16323 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016324
Craig Toppercacafd42012-08-14 08:18:43 +000016325 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016326
16327 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016328 for (unsigned i = 0; i != NumElems/2; ++i)
16329 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016330
Craig Toppercacafd42012-08-14 08:18:43 +000016331 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016332
Craig Topper3ef43cf2012-04-24 06:36:35 +000016333 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016334 VT.getVectorNumElements()/2);
16335
Craig Topper3ef43cf2012-04-24 06:36:35 +000016336 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016337 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16338
16339 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16340 }
16341 return SDValue();
16342}
16343
Michael Liaof6c24ee2012-08-10 14:39:24 +000016344static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016345 const X86Subtarget* Subtarget) {
16346 DebugLoc dl = N->getDebugLoc();
16347 EVT VT = N->getValueType(0);
16348
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016349 // Let legalize expand this if it isn't a legal type yet.
16350 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16351 return SDValue();
16352
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016353 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016354 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16355 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016356 return SDValue();
16357
16358 SDValue A = N->getOperand(0);
16359 SDValue B = N->getOperand(1);
16360 SDValue C = N->getOperand(2);
16361
16362 bool NegA = (A.getOpcode() == ISD::FNEG);
16363 bool NegB = (B.getOpcode() == ISD::FNEG);
16364 bool NegC = (C.getOpcode() == ISD::FNEG);
16365
Michael Liaof6c24ee2012-08-10 14:39:24 +000016366 // Negative multiplication when NegA xor NegB
16367 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016368 if (NegA)
16369 A = A.getOperand(0);
16370 if (NegB)
16371 B = B.getOperand(0);
16372 if (NegC)
16373 C = C.getOperand(0);
16374
16375 unsigned Opcode;
16376 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016377 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016378 else
Craig Topperbf404372012-08-31 15:40:30 +000016379 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16380
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016381 return DAG.getNode(Opcode, dl, VT, A, B, C);
16382}
16383
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016384static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016385 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016386 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016387 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16388 // (and (i32 x86isd::setcc_carry), 1)
16389 // This eliminates the zext. This transformation is necessary because
16390 // ISD::SETCC is always legalized to i8.
16391 DebugLoc dl = N->getDebugLoc();
16392 SDValue N0 = N->getOperand(0);
16393 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016394 EVT OpVT = N0.getValueType();
16395
Evan Cheng2e489c42009-12-16 00:53:11 +000016396 if (N0.getOpcode() == ISD::AND &&
16397 N0.hasOneUse() &&
16398 N0.getOperand(0).hasOneUse()) {
16399 SDValue N00 = N0.getOperand(0);
16400 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16401 return SDValue();
16402 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16403 if (!C || C->getZExtValue() != 1)
16404 return SDValue();
16405 return DAG.getNode(ISD::AND, dl, VT,
16406 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16407 N00.getOperand(0), N00.getOperand(1)),
16408 DAG.getConstant(1, VT));
16409 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016410
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016411 // Optimize vectors in AVX mode:
16412 //
16413 // v8i16 -> v8i32
16414 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16415 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16416 // Concat upper and lower parts.
16417 //
16418 // v4i32 -> v4i64
16419 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16420 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16421 // Concat upper and lower parts.
16422 //
Craig Topperc16f8512012-04-25 06:39:39 +000016423 if (!DCI.isBeforeLegalizeOps())
16424 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016425
Craig Topperc16f8512012-04-25 06:39:39 +000016426 if (!Subtarget->hasAVX())
16427 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016428
Craig Topperc16f8512012-04-25 06:39:39 +000016429 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16430 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016431
Craig Topperc16f8512012-04-25 06:39:39 +000016432 if (Subtarget->hasAVX2())
16433 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016434
Craig Topperc16f8512012-04-25 06:39:39 +000016435 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16436 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16437 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016438
Craig Topperc16f8512012-04-25 06:39:39 +000016439 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16440 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016441
Craig Topperc16f8512012-04-25 06:39:39 +000016442 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16443 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16444
16445 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016446 }
16447
Evan Cheng2e489c42009-12-16 00:53:11 +000016448 return SDValue();
16449}
16450
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016451// Optimize x == -y --> x+y == 0
16452// x != -y --> x+y != 0
16453static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16454 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16455 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016456 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016457
16458 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16460 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16461 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16462 LHS.getValueType(), RHS, LHS.getOperand(1));
16463 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16464 addV, DAG.getConstant(0, addV.getValueType()), CC);
16465 }
16466 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16468 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16469 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16470 RHS.getValueType(), LHS, RHS.getOperand(1));
16471 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16472 addV, DAG.getConstant(0, addV.getValueType()), CC);
16473 }
16474 return SDValue();
16475}
16476
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016477// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016478static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16479 TargetLowering::DAGCombinerInfo &DCI,
16480 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016481 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016482 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16483 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016484
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016485 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16486 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16487 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016488 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016489 return DAG.getNode(ISD::AND, DL, MVT::i8,
16490 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000016491 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016492 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016493
Michael Liao2a33cec2012-08-10 19:58:13 +000016494 SDValue Flags;
16495
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016496 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16497 if (Flags.getNode()) {
16498 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16499 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16500 }
16501
Michael Liao2a33cec2012-08-10 19:58:13 +000016502 return SDValue();
16503}
16504
16505// Optimize branch condition evaluation.
16506//
16507static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16508 TargetLowering::DAGCombinerInfo &DCI,
16509 const X86Subtarget *Subtarget) {
16510 DebugLoc DL = N->getDebugLoc();
16511 SDValue Chain = N->getOperand(0);
16512 SDValue Dest = N->getOperand(1);
16513 SDValue EFLAGS = N->getOperand(3);
16514 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16515
16516 SDValue Flags;
16517
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016518 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16519 if (Flags.getNode()) {
16520 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16521 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16522 Flags);
16523 }
16524
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016525 return SDValue();
16526}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016527
Benjamin Kramer1396c402011-06-18 11:09:41 +000016528static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16529 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016530 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016531 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016532
16533 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016534 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016535 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016536 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016537 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16538 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16539 }
16540
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016541 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16542 // a 32-bit target where SSE doesn't support i64->FP operations.
16543 if (Op0.getOpcode() == ISD::LOAD) {
16544 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16545 EVT VT = Ld->getValueType(0);
16546 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16547 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16548 !XTLI->getSubtarget()->is64Bit() &&
16549 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016550 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16551 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016552 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16553 return FILDChain;
16554 }
16555 }
16556 return SDValue();
16557}
16558
Chris Lattner23a01992010-12-20 01:37:09 +000016559// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16560static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16561 X86TargetLowering::DAGCombinerInfo &DCI) {
16562 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16563 // the result is either zero or one (depending on the input carry bit).
16564 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16565 if (X86::isZeroNode(N->getOperand(0)) &&
16566 X86::isZeroNode(N->getOperand(1)) &&
16567 // We don't have a good way to replace an EFLAGS use, so only do this when
16568 // dead right now.
16569 SDValue(N, 1).use_empty()) {
16570 DebugLoc DL = N->getDebugLoc();
16571 EVT VT = N->getValueType(0);
16572 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16573 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16574 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16575 DAG.getConstant(X86::COND_B,MVT::i8),
16576 N->getOperand(2)),
16577 DAG.getConstant(1, VT));
16578 return DCI.CombineTo(N, Res1, CarryOut);
16579 }
16580
16581 return SDValue();
16582}
16583
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016584// fold (add Y, (sete X, 0)) -> adc 0, Y
16585// (add Y, (setne X, 0)) -> sbb -1, Y
16586// (sub (sete X, 0), Y) -> sbb 0, Y
16587// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016588static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016589 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016590
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016591 // Look through ZExts.
16592 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16593 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16594 return SDValue();
16595
16596 SDValue SetCC = Ext.getOperand(0);
16597 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16598 return SDValue();
16599
16600 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16601 if (CC != X86::COND_E && CC != X86::COND_NE)
16602 return SDValue();
16603
16604 SDValue Cmp = SetCC.getOperand(1);
16605 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016606 !X86::isZeroNode(Cmp.getOperand(1)) ||
16607 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016608 return SDValue();
16609
16610 SDValue CmpOp0 = Cmp.getOperand(0);
16611 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16612 DAG.getConstant(1, CmpOp0.getValueType()));
16613
16614 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16615 if (CC == X86::COND_NE)
16616 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16617 DL, OtherVal.getValueType(), OtherVal,
16618 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16619 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16620 DL, OtherVal.getValueType(), OtherVal,
16621 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16622}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016623
Craig Topper54f952a2011-11-19 09:02:40 +000016624/// PerformADDCombine - Do target-specific dag combines on integer adds.
16625static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16626 const X86Subtarget *Subtarget) {
16627 EVT VT = N->getValueType(0);
16628 SDValue Op0 = N->getOperand(0);
16629 SDValue Op1 = N->getOperand(1);
16630
16631 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016632 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016633 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016634 isHorizontalBinOp(Op0, Op1, true))
16635 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16636
16637 return OptimizeConditionalInDecrement(N, DAG);
16638}
16639
16640static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16641 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016642 SDValue Op0 = N->getOperand(0);
16643 SDValue Op1 = N->getOperand(1);
16644
16645 // X86 can't encode an immediate LHS of a sub. See if we can push the
16646 // negation into a preceding instruction.
16647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016648 // If the RHS of the sub is a XOR with one use and a constant, invert the
16649 // immediate. Then add one to the LHS of the sub so we can turn
16650 // X-Y -> X+~Y+1, saving one register.
16651 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16652 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016653 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016654 EVT VT = Op0.getValueType();
16655 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16656 Op1.getOperand(0),
16657 DAG.getConstant(~XorC, VT));
16658 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016659 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016660 }
16661 }
16662
Craig Topper54f952a2011-11-19 09:02:40 +000016663 // Try to synthesize horizontal adds from adds of shuffles.
16664 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016665 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016666 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16667 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016668 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16669
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016670 return OptimizeConditionalInDecrement(N, DAG);
16671}
16672
Michael Liaod9d09602012-10-23 17:34:00 +000016673/// performVZEXTCombine - Performs build vector combines
16674static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
16675 TargetLowering::DAGCombinerInfo &DCI,
16676 const X86Subtarget *Subtarget) {
16677 // (vzext (bitcast (vzext (x)) -> (vzext x)
16678 SDValue In = N->getOperand(0);
16679 while (In.getOpcode() == ISD::BITCAST)
16680 In = In.getOperand(0);
16681
16682 if (In.getOpcode() != X86ISD::VZEXT)
16683 return SDValue();
16684
16685 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
16686}
16687
Dan Gohman475871a2008-07-27 21:46:04 +000016688SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016689 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016690 SelectionDAG &DAG = DCI.DAG;
16691 switch (N->getOpcode()) {
16692 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016693 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016694 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016695 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016696 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016697 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016698 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16699 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016700 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016701 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016702 case ISD::SHL:
16703 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016704 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016705 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016706 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016707 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016708 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016709 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016710 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000016711 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16712 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016713 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016714 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016715 case X86ISD::FMIN:
16716 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016717 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016718 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016719 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016720 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016721 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016722 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016723 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016724 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016725 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016726 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000016727 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016728 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016729 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016730 case X86ISD::UNPCKH:
16731 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016732 case X86ISD::MOVHLPS:
16733 case X86ISD::MOVLHPS:
16734 case X86ISD::PSHUFD:
16735 case X86ISD::PSHUFHW:
16736 case X86ISD::PSHUFLW:
16737 case X86ISD::MOVSS:
16738 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016739 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016740 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016741 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016742 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016743 }
16744
Dan Gohman475871a2008-07-27 21:46:04 +000016745 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016746}
16747
Evan Chenge5b51ac2010-04-17 06:13:15 +000016748/// isTypeDesirableForOp - Return true if the target has native support for
16749/// the specified value type and it is 'desirable' to use the type for the
16750/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16751/// instruction encodings are longer and some i16 instructions are slow.
16752bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16753 if (!isTypeLegal(VT))
16754 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016755 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016756 return true;
16757
16758 switch (Opc) {
16759 default:
16760 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016761 case ISD::LOAD:
16762 case ISD::SIGN_EXTEND:
16763 case ISD::ZERO_EXTEND:
16764 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016765 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016766 case ISD::SRL:
16767 case ISD::SUB:
16768 case ISD::ADD:
16769 case ISD::MUL:
16770 case ISD::AND:
16771 case ISD::OR:
16772 case ISD::XOR:
16773 return false;
16774 }
16775}
16776
16777/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016778/// beneficial for dag combiner to promote the specified node. If true, it
16779/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016780bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016781 EVT VT = Op.getValueType();
16782 if (VT != MVT::i16)
16783 return false;
16784
Evan Cheng4c26e932010-04-19 19:29:22 +000016785 bool Promote = false;
16786 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016787 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016788 default: break;
16789 case ISD::LOAD: {
16790 LoadSDNode *LD = cast<LoadSDNode>(Op);
16791 // If the non-extending load has a single use and it's not live out, then it
16792 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016793 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16794 Op.hasOneUse()*/) {
16795 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16796 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16797 // The only case where we'd want to promote LOAD (rather then it being
16798 // promoted as an operand is when it's only use is liveout.
16799 if (UI->getOpcode() != ISD::CopyToReg)
16800 return false;
16801 }
16802 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016803 Promote = true;
16804 break;
16805 }
16806 case ISD::SIGN_EXTEND:
16807 case ISD::ZERO_EXTEND:
16808 case ISD::ANY_EXTEND:
16809 Promote = true;
16810 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016811 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016812 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016813 SDValue N0 = Op.getOperand(0);
16814 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016815 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016816 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016817 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016818 break;
16819 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016820 case ISD::ADD:
16821 case ISD::MUL:
16822 case ISD::AND:
16823 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016824 case ISD::XOR:
16825 Commute = true;
16826 // fallthrough
16827 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016828 SDValue N0 = Op.getOperand(0);
16829 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016830 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016831 return false;
16832 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016833 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016834 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016835 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016836 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016837 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016838 }
16839 }
16840
16841 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016842 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016843}
16844
Evan Cheng60c07e12006-07-05 22:17:51 +000016845//===----------------------------------------------------------------------===//
16846// X86 Inline Assembly Support
16847//===----------------------------------------------------------------------===//
16848
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016849namespace {
16850 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016851 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016852 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016853
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016854 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016855 StringRef piece(*args[i]);
16856 if (!s.startswith(piece)) // Check if the piece matches.
16857 return false;
16858
16859 s = s.substr(piece.size());
16860 StringRef::size_type pos = s.find_first_not_of(" \t");
16861 if (pos == 0) // We matched a prefix.
16862 return false;
16863
16864 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016865 }
16866
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016867 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016868 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016869 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016870}
16871
Chris Lattnerb8105652009-07-20 17:51:36 +000016872bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16873 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016874
16875 std::string AsmStr = IA->getAsmString();
16876
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016877 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16878 if (!Ty || Ty->getBitWidth() % 16 != 0)
16879 return false;
16880
Chris Lattnerb8105652009-07-20 17:51:36 +000016881 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016882 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016883 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016884
16885 switch (AsmPieces.size()) {
16886 default: return false;
16887 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016888 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016889 // we will turn this bswap into something that will be lowered to logical
16890 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16891 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016892 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016893 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16894 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16895 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16896 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16897 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16898 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016899 // No need to check constraints, nothing other than the equivalent of
16900 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016901 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016902 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016903
Chris Lattnerb8105652009-07-20 17:51:36 +000016904 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016905 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016906 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016907 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16908 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016909 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016910 const std::string &ConstraintsStr = IA->getConstraintString();
16911 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016912 std::sort(AsmPieces.begin(), AsmPieces.end());
16913 if (AsmPieces.size() == 4 &&
16914 AsmPieces[0] == "~{cc}" &&
16915 AsmPieces[1] == "~{dirflag}" &&
16916 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016917 AsmPieces[3] == "~{fpsr}")
16918 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016919 }
16920 break;
16921 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016922 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016923 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016924 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16925 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16926 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016927 AsmPieces.clear();
16928 const std::string &ConstraintsStr = IA->getConstraintString();
16929 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16930 std::sort(AsmPieces.begin(), AsmPieces.end());
16931 if (AsmPieces.size() == 4 &&
16932 AsmPieces[0] == "~{cc}" &&
16933 AsmPieces[1] == "~{dirflag}" &&
16934 AsmPieces[2] == "~{flags}" &&
16935 AsmPieces[3] == "~{fpsr}")
16936 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016937 }
Evan Cheng55d42002011-01-08 01:24:27 +000016938
16939 if (CI->getType()->isIntegerTy(64)) {
16940 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16941 if (Constraints.size() >= 2 &&
16942 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16943 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16944 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016945 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16946 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16947 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016948 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016949 }
16950 }
16951 break;
16952 }
16953 return false;
16954}
16955
16956
16957
Chris Lattnerf4dff842006-07-11 02:54:03 +000016958/// getConstraintType - Given a constraint letter, return the type of
16959/// constraint it is for this target.
16960X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016961X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16962 if (Constraint.size() == 1) {
16963 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016964 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016965 case 'q':
16966 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016967 case 'f':
16968 case 't':
16969 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016970 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016971 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016972 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016973 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016974 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016975 case 'a':
16976 case 'b':
16977 case 'c':
16978 case 'd':
16979 case 'S':
16980 case 'D':
16981 case 'A':
16982 return C_Register;
16983 case 'I':
16984 case 'J':
16985 case 'K':
16986 case 'L':
16987 case 'M':
16988 case 'N':
16989 case 'G':
16990 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016991 case 'e':
16992 case 'Z':
16993 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016994 default:
16995 break;
16996 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016997 }
Chris Lattner4234f572007-03-25 02:14:49 +000016998 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016999}
17000
John Thompson44ab89e2010-10-29 17:29:13 +000017001/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017002/// This object must already have been set up with the operand type
17003/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017004TargetLowering::ConstraintWeight
17005 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017006 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017007 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017008 Value *CallOperandVal = info.CallOperandVal;
17009 // If we don't have a value, we can't do a match,
17010 // but allow it at the lowest weight.
17011 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017012 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017013 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017014 // Look at the constraint type.
17015 switch (*constraint) {
17016 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017017 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17018 case 'R':
17019 case 'q':
17020 case 'Q':
17021 case 'a':
17022 case 'b':
17023 case 'c':
17024 case 'd':
17025 case 'S':
17026 case 'D':
17027 case 'A':
17028 if (CallOperandVal->getType()->isIntegerTy())
17029 weight = CW_SpecificReg;
17030 break;
17031 case 'f':
17032 case 't':
17033 case 'u':
17034 if (type->isFloatingPointTy())
17035 weight = CW_SpecificReg;
17036 break;
17037 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000017038 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000017039 weight = CW_SpecificReg;
17040 break;
17041 case 'x':
17042 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017043 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000017044 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000017045 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017046 break;
17047 case 'I':
17048 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17049 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017050 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017051 }
17052 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017053 case 'J':
17054 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17055 if (C->getZExtValue() <= 63)
17056 weight = CW_Constant;
17057 }
17058 break;
17059 case 'K':
17060 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17061 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17062 weight = CW_Constant;
17063 }
17064 break;
17065 case 'L':
17066 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17067 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17068 weight = CW_Constant;
17069 }
17070 break;
17071 case 'M':
17072 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17073 if (C->getZExtValue() <= 3)
17074 weight = CW_Constant;
17075 }
17076 break;
17077 case 'N':
17078 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17079 if (C->getZExtValue() <= 0xff)
17080 weight = CW_Constant;
17081 }
17082 break;
17083 case 'G':
17084 case 'C':
17085 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17086 weight = CW_Constant;
17087 }
17088 break;
17089 case 'e':
17090 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17091 if ((C->getSExtValue() >= -0x80000000LL) &&
17092 (C->getSExtValue() <= 0x7fffffffLL))
17093 weight = CW_Constant;
17094 }
17095 break;
17096 case 'Z':
17097 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17098 if (C->getZExtValue() <= 0xffffffff)
17099 weight = CW_Constant;
17100 }
17101 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017102 }
17103 return weight;
17104}
17105
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017106/// LowerXConstraint - try to replace an X constraint, which matches anything,
17107/// with another that has more specific requirements based on the type of the
17108/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017109const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017110LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017111 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17112 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017113 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017114 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017115 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017116 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017117 return "x";
17118 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017119
Chris Lattner5e764232008-04-26 23:02:14 +000017120 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017121}
17122
Chris Lattner48884cd2007-08-25 00:47:38 +000017123/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17124/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017125void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017126 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017127 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017128 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017129 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017130
Eric Christopher100c8332011-06-02 23:16:42 +000017131 // Only support length 1 constraints for now.
17132 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017133
Eric Christopher100c8332011-06-02 23:16:42 +000017134 char ConstraintLetter = Constraint[0];
17135 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017136 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017137 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017138 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017139 if (C->getZExtValue() <= 31) {
17140 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017141 break;
17142 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017143 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017144 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017145 case 'J':
17146 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017147 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017148 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17149 break;
17150 }
17151 }
17152 return;
17153 case 'K':
17154 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017155 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000017156 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17157 break;
17158 }
17159 }
17160 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017161 case 'N':
17162 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017163 if (C->getZExtValue() <= 255) {
17164 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017165 break;
17166 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017167 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017168 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017169 case 'e': {
17170 // 32-bit signed value
17171 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017172 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17173 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017174 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017175 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017176 break;
17177 }
17178 // FIXME gcc accepts some relocatable values here too, but only in certain
17179 // memory models; it's complicated.
17180 }
17181 return;
17182 }
17183 case 'Z': {
17184 // 32-bit unsigned value
17185 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017186 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17187 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017188 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17189 break;
17190 }
17191 }
17192 // FIXME gcc accepts some relocatable values here too, but only in certain
17193 // memory models; it's complicated.
17194 return;
17195 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017196 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017197 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017198 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017199 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017200 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017201 break;
17202 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017203
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017204 // In any sort of PIC mode addresses need to be computed at runtime by
17205 // adding in a register or some sort of table lookup. These can't
17206 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017207 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017208 return;
17209
Chris Lattnerdc43a882007-05-03 16:52:29 +000017210 // If we are in non-pic codegen mode, we allow the address of a global (with
17211 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017212 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017213 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017214
Chris Lattner49921962009-05-08 18:23:14 +000017215 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17216 while (1) {
17217 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17218 Offset += GA->getOffset();
17219 break;
17220 } else if (Op.getOpcode() == ISD::ADD) {
17221 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17222 Offset += C->getZExtValue();
17223 Op = Op.getOperand(0);
17224 continue;
17225 }
17226 } else if (Op.getOpcode() == ISD::SUB) {
17227 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17228 Offset += -C->getZExtValue();
17229 Op = Op.getOperand(0);
17230 continue;
17231 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017232 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017233
Chris Lattner49921962009-05-08 18:23:14 +000017234 // Otherwise, this isn't something we can handle, reject it.
17235 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017236 }
Eric Christopherfd179292009-08-27 18:07:15 +000017237
Dan Gohman46510a72010-04-15 01:51:59 +000017238 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017239 // If we require an extra load to get this address, as in PIC mode, we
17240 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017241 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17242 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017243 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017244
Devang Patel0d881da2010-07-06 22:08:15 +000017245 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17246 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017247 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017248 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017249 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017250
Gabor Greifba36cb52008-08-28 21:40:38 +000017251 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017252 Ops.push_back(Result);
17253 return;
17254 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017255 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017256}
17257
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017258std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017259X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017260 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017261 // First, see if this is a constraint that directly corresponds to an LLVM
17262 // register class.
17263 if (Constraint.size() == 1) {
17264 // GCC Constraint Letters
17265 switch (Constraint[0]) {
17266 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017267 // TODO: Slight differences here in allocation order and leaving
17268 // RIP in the class. Do they matter any more here than they do
17269 // in the normal allocation?
17270 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17271 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017272 if (VT == MVT::i32 || VT == MVT::f32)
17273 return std::make_pair(0U, &X86::GR32RegClass);
17274 if (VT == MVT::i16)
17275 return std::make_pair(0U, &X86::GR16RegClass);
17276 if (VT == MVT::i8 || VT == MVT::i1)
17277 return std::make_pair(0U, &X86::GR8RegClass);
17278 if (VT == MVT::i64 || VT == MVT::f64)
17279 return std::make_pair(0U, &X86::GR64RegClass);
17280 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017281 }
17282 // 32-bit fallthrough
17283 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017284 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017285 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17286 if (VT == MVT::i16)
17287 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17288 if (VT == MVT::i8 || VT == MVT::i1)
17289 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17290 if (VT == MVT::i64)
17291 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017292 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017293 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017294 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017295 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017296 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017297 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017298 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017299 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017300 return std::make_pair(0U, &X86::GR32RegClass);
17301 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017302 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017303 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017304 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017305 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017306 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017307 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017308 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17309 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017310 case 'f': // FP Stack registers.
17311 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17312 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017313 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017314 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017315 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017316 return std::make_pair(0U, &X86::RFP64RegClass);
17317 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017318 case 'y': // MMX_REGS if MMX allowed.
17319 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017320 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017321 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017322 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017323 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017324 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017325 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017326
Owen Anderson825b72b2009-08-11 20:47:22 +000017327 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017328 default: break;
17329 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017330 case MVT::f32:
17331 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017332 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017333 case MVT::f64:
17334 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017335 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017336 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017337 case MVT::v16i8:
17338 case MVT::v8i16:
17339 case MVT::v4i32:
17340 case MVT::v2i64:
17341 case MVT::v4f32:
17342 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017343 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017344 // AVX types.
17345 case MVT::v32i8:
17346 case MVT::v16i16:
17347 case MVT::v8i32:
17348 case MVT::v4i64:
17349 case MVT::v8f32:
17350 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017351 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017352 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017353 break;
17354 }
17355 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017356
Chris Lattnerf76d1802006-07-31 23:26:50 +000017357 // Use the default implementation in TargetLowering to convert the register
17358 // constraint into a member of a register class.
17359 std::pair<unsigned, const TargetRegisterClass*> Res;
17360 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017361
17362 // Not found as a standard register?
17363 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017364 // Map st(0) -> st(7) -> ST0
17365 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17366 tolower(Constraint[1]) == 's' &&
17367 tolower(Constraint[2]) == 't' &&
17368 Constraint[3] == '(' &&
17369 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17370 Constraint[5] == ')' &&
17371 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017372
Chris Lattner56d77c72009-09-13 22:41:48 +000017373 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017374 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017375 return Res;
17376 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017377
Chris Lattner56d77c72009-09-13 22:41:48 +000017378 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017379 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017380 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017381 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017382 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017383 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017384
17385 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017386 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017387 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017388 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017389 return Res;
17390 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017391
Dale Johannesen330169f2008-11-13 21:52:36 +000017392 // 'A' means EAX + EDX.
17393 if (Constraint == "A") {
17394 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017395 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017396 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017397 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017398 return Res;
17399 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017400
Chris Lattnerf76d1802006-07-31 23:26:50 +000017401 // Otherwise, check to see if this is a register class of the wrong value
17402 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17403 // turn into {ax},{dx}.
17404 if (Res.second->hasType(VT))
17405 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017406
Chris Lattnerf76d1802006-07-31 23:26:50 +000017407 // All of the single-register GCC register classes map their values onto
17408 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17409 // really want an 8-bit or 32-bit register, map to the appropriate register
17410 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017411 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017412 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017413 unsigned DestReg = 0;
17414 switch (Res.first) {
17415 default: break;
17416 case X86::AX: DestReg = X86::AL; break;
17417 case X86::DX: DestReg = X86::DL; break;
17418 case X86::CX: DestReg = X86::CL; break;
17419 case X86::BX: DestReg = X86::BL; break;
17420 }
17421 if (DestReg) {
17422 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017423 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017424 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017425 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017426 unsigned DestReg = 0;
17427 switch (Res.first) {
17428 default: break;
17429 case X86::AX: DestReg = X86::EAX; break;
17430 case X86::DX: DestReg = X86::EDX; break;
17431 case X86::CX: DestReg = X86::ECX; break;
17432 case X86::BX: DestReg = X86::EBX; break;
17433 case X86::SI: DestReg = X86::ESI; break;
17434 case X86::DI: DestReg = X86::EDI; break;
17435 case X86::BP: DestReg = X86::EBP; break;
17436 case X86::SP: DestReg = X86::ESP; break;
17437 }
17438 if (DestReg) {
17439 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017440 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017441 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017442 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017443 unsigned DestReg = 0;
17444 switch (Res.first) {
17445 default: break;
17446 case X86::AX: DestReg = X86::RAX; break;
17447 case X86::DX: DestReg = X86::RDX; break;
17448 case X86::CX: DestReg = X86::RCX; break;
17449 case X86::BX: DestReg = X86::RBX; break;
17450 case X86::SI: DestReg = X86::RSI; break;
17451 case X86::DI: DestReg = X86::RDI; break;
17452 case X86::BP: DestReg = X86::RBP; break;
17453 case X86::SP: DestReg = X86::RSP; break;
17454 }
17455 if (DestReg) {
17456 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017457 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017458 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017459 }
Craig Topperc9099502012-04-20 06:31:50 +000017460 } else if (Res.second == &X86::FR32RegClass ||
17461 Res.second == &X86::FR64RegClass ||
17462 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017463 // Handle references to XMM physical registers that got mapped into the
17464 // wrong class. This can happen with constraints like {xmm0} where the
17465 // target independent register mapper will just pick the first match it can
17466 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017467
17468 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017469 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017470 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017471 Res.second = &X86::FR64RegClass;
17472 else if (X86::VR128RegClass.hasType(VT))
17473 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017474 else if (X86::VR256RegClass.hasType(VT))
17475 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017476 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017477
Chris Lattnerf76d1802006-07-31 23:26:50 +000017478 return Res;
17479}