blob: a3ec14329e292aa8876ccc1a1d1828a98ace16c6 [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;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004638 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004639 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004640
Craig Topperd978c542012-05-06 19:46:21 +00004641 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004642 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004643
Craig Topper3d092db2012-03-21 02:14:01 +00004644 int Elt = ShuffleMask[Index];
4645 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004646 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004647
Craig Topper3d092db2012-03-21 02:14:01 +00004648 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004649 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004650 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004651 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004652 }
4653
4654 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004655 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004656 V = V.getOperand(0);
4657 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004658 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004659
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004660 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004661 return SDValue();
4662 }
4663
4664 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4665 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004666 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004667
4668 if (V.getOpcode() == ISD::BUILD_VECTOR)
4669 return V.getOperand(Index);
4670
4671 return SDValue();
4672}
4673
4674/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4675/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004676/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004677static
Craig Topper3d092db2012-03-21 02:14:01 +00004678unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004679 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004680 unsigned i;
4681 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004683 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684 if (!(Elt.getNode() &&
4685 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4686 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004687 }
4688
4689 return i;
4690}
4691
Craig Topper3d092db2012-03-21 02:14:01 +00004692/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4693/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004694/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4695static
Craig Topper3d092db2012-03-21 02:14:01 +00004696bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4697 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4698 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004699 bool SeenV1 = false;
4700 bool SeenV2 = false;
4701
Craig Topper3d092db2012-03-21 02:14:01 +00004702 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004703 int Idx = SVOp->getMaskElt(i);
4704 // Ignore undef indicies
4705 if (Idx < 0)
4706 continue;
4707
Craig Topper3d092db2012-03-21 02:14:01 +00004708 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004709 SeenV1 = true;
4710 else
4711 SeenV2 = true;
4712
4713 // Only accept consecutive elements from the same vector
4714 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4715 return false;
4716 }
4717
4718 OpNum = SeenV1 ? 0 : 1;
4719 return true;
4720}
4721
4722/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4723/// logical left shift of a vector.
4724static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4725 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4726 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4727 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4728 false /* check zeros from right */, DAG);
4729 unsigned OpSrc;
4730
4731 if (!NumZeros)
4732 return false;
4733
4734 // Considering the elements in the mask that are not consecutive zeros,
4735 // check if they consecutively come from only one of the source vectors.
4736 //
4737 // V1 = {X, A, B, C} 0
4738 // \ \ \ /
4739 // vector_shuffle V1, V2 <1, 2, 3, X>
4740 //
4741 if (!isShuffleMaskConsecutive(SVOp,
4742 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004743 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004744 NumZeros, // Where to start looking in the src vector
4745 NumElems, // Number of elements in vector
4746 OpSrc)) // Which source operand ?
4747 return false;
4748
4749 isLeft = false;
4750 ShAmt = NumZeros;
4751 ShVal = SVOp->getOperand(OpSrc);
4752 return true;
4753}
4754
4755/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4756/// logical left shift of a vector.
4757static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4758 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4759 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4760 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4761 true /* check zeros from left */, DAG);
4762 unsigned OpSrc;
4763
4764 if (!NumZeros)
4765 return false;
4766
4767 // Considering the elements in the mask that are not consecutive zeros,
4768 // check if they consecutively come from only one of the source vectors.
4769 //
4770 // 0 { A, B, X, X } = V2
4771 // / \ / /
4772 // vector_shuffle V1, V2 <X, X, 4, 5>
4773 //
4774 if (!isShuffleMaskConsecutive(SVOp,
4775 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004776 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004777 0, // Where to start looking in the src vector
4778 NumElems, // Number of elements in vector
4779 OpSrc)) // Which source operand ?
4780 return false;
4781
4782 isLeft = true;
4783 ShAmt = NumZeros;
4784 ShVal = SVOp->getOperand(OpSrc);
4785 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004786}
4787
4788/// isVectorShift - Returns true if the shuffle can be implemented as a
4789/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004790static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004791 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004792 // Although the logic below support any bitwidth size, there are no
4793 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004794 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004795 return false;
4796
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004797 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4798 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4799 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004800
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004801 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004802}
4803
Evan Chengc78d3b42006-04-24 18:01:45 +00004804/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4805///
Dan Gohman475871a2008-07-27 21:46:04 +00004806static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004807 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004808 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004809 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004810 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004812 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004813
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004814 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004815 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004816 bool First = true;
4817 for (unsigned i = 0; i < 16; ++i) {
4818 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4819 if (ThisIsNonZero && First) {
4820 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004821 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004822 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004824 First = false;
4825 }
4826
4827 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004828 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004829 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4830 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004831 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 }
4834 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4836 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4837 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004838 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004839 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004840 } else
4841 ThisElt = LastElt;
4842
Gabor Greifba36cb52008-08-28 21:40:38 +00004843 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004844 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004845 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004846 }
4847 }
4848
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004849 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004850}
4851
Bill Wendlinga348c562007-03-22 18:42:45 +00004852/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004853///
Dan Gohman475871a2008-07-27 21:46:04 +00004854static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004855 unsigned NumNonZero, unsigned NumZero,
4856 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004857 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004858 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004859 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004860 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004861
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004862 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004863 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004864 bool First = true;
4865 for (unsigned i = 0; i < 8; ++i) {
4866 bool isNonZero = (NonZeros & (1 << i)) != 0;
4867 if (isNonZero) {
4868 if (First) {
4869 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004870 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004871 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004873 First = false;
4874 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004875 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004877 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004878 }
4879 }
4880
4881 return V;
4882}
4883
Evan Chengf26ffe92008-05-29 08:22:04 +00004884/// getVShift - Return a vector logical shift node.
4885///
Owen Andersone50ed302009-08-10 22:56:29 +00004886static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004887 unsigned NumBits, SelectionDAG &DAG,
4888 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004889 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004890 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004891 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004892 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4893 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004894 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004895 DAG.getConstant(NumBits,
4896 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004897}
4898
Dan Gohman475871a2008-07-27 21:46:04 +00004899SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004900X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004901 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004902
Evan Chengc3630942009-12-09 21:00:30 +00004903 // Check if the scalar load can be widened into a vector load. And if
4904 // the address is "base + cst" see if the cst can be "absorbed" into
4905 // the shuffle mask.
4906 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4907 SDValue Ptr = LD->getBasePtr();
4908 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4909 return SDValue();
4910 EVT PVT = LD->getValueType(0);
4911 if (PVT != MVT::i32 && PVT != MVT::f32)
4912 return SDValue();
4913
4914 int FI = -1;
4915 int64_t Offset = 0;
4916 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4917 FI = FINode->getIndex();
4918 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004919 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004920 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4921 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4922 Offset = Ptr.getConstantOperandVal(1);
4923 Ptr = Ptr.getOperand(0);
4924 } else {
4925 return SDValue();
4926 }
4927
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004928 // FIXME: 256-bit vector instructions don't require a strict alignment,
4929 // improve this code to support it better.
4930 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004931 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004932 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004933 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004934 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004935 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004936 // Can't change the alignment. FIXME: It's possible to compute
4937 // the exact stack offset and reference FI + adjust offset instead.
4938 // If someone *really* cares about this. That's the way to implement it.
4939 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004940 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004941 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004942 }
4943 }
4944
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004945 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004946 // Ptr + (Offset & ~15).
4947 if (Offset < 0)
4948 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004949 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004950 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004951 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004952 if (StartOffset)
4953 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4954 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4955
4956 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004957 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004958
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004959 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4960 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004961 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004962 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004963
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004964 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004965 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004966 Mask.push_back(EltNo);
4967
Craig Toppercc3000632012-01-30 07:50:31 +00004968 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004969 }
4970
4971 return SDValue();
4972}
4973
Michael J. Spencerec38de22010-10-10 22:04:20 +00004974/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4975/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004976/// load which has the same value as a build_vector whose operands are 'elts'.
4977///
4978/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004979///
Nate Begeman1449f292010-03-24 22:19:06 +00004980/// FIXME: we'd also like to handle the case where the last elements are zero
4981/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4982/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004983static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004984 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004985 EVT EltVT = VT.getVectorElementType();
4986 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004987
Nate Begemanfdea31a2010-03-24 20:49:50 +00004988 LoadSDNode *LDBase = NULL;
4989 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004990
Nate Begeman1449f292010-03-24 22:19:06 +00004991 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004992 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004993 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004994 for (unsigned i = 0; i < NumElems; ++i) {
4995 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004996
Nate Begemanfdea31a2010-03-24 20:49:50 +00004997 if (!Elt.getNode() ||
4998 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4999 return SDValue();
5000 if (!LDBase) {
5001 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5002 return SDValue();
5003 LDBase = cast<LoadSDNode>(Elt.getNode());
5004 LastLoadedElt = i;
5005 continue;
5006 }
5007 if (Elt.getOpcode() == ISD::UNDEF)
5008 continue;
5009
5010 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5011 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5012 return SDValue();
5013 LastLoadedElt = i;
5014 }
Nate Begeman1449f292010-03-24 22:19:06 +00005015
5016 // If we have found an entire vector of loads and undefs, then return a large
5017 // load of the entire vector width starting at the base pointer. If we found
5018 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005019 if (LastLoadedElt == NumElems - 1) {
5020 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005021 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005022 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005023 LDBase->isVolatile(), LDBase->isNonTemporal(),
5024 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005025 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005026 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005027 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005028 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005029 }
5030 if (NumElems == 4 && LastLoadedElt == 1 &&
5031 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005032 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5033 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005034 SDValue ResNode =
5035 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5036 LDBase->getPointerInfo(),
5037 LDBase->getAlignment(),
5038 false/*isVolatile*/, true/*ReadMem*/,
5039 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005040
5041 // Make sure the newly-created LOAD is in the same position as LDBase in
5042 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5043 // update uses of LDBase's output chain to use the TokenFactor.
5044 if (LDBase->hasAnyUseOfValue(1)) {
5045 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5046 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5047 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5048 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5049 SDValue(ResNode.getNode(), 1));
5050 }
5051
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005052 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005053 }
5054 return SDValue();
5055}
5056
Nadav Rotem9d68b062012-04-08 12:54:54 +00005057/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5058/// to generate a splat value for the following cases:
5059/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005060/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005061/// a scalar load, or a constant.
5062/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005063/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005064SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005065X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005066 if (!Subtarget->hasAVX())
5067 return SDValue();
5068
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005069 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005070 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005071
Craig Topper5da8a802012-05-04 05:49:51 +00005072 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5073 "Unsupported vector type for broadcast.");
5074
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005075 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005076 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005077
Nadav Rotem9d68b062012-04-08 12:54:54 +00005078 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005079 default:
5080 // Unknown pattern found.
5081 return SDValue();
5082
5083 case ISD::BUILD_VECTOR: {
5084 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005085 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005086 return SDValue();
5087
Nadav Rotem9d68b062012-04-08 12:54:54 +00005088 Ld = Op.getOperand(0);
5089 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5090 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005091
5092 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005093 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005094 // Constants may have multiple users.
5095 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005096 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005097 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005098 }
5099
5100 case ISD::VECTOR_SHUFFLE: {
5101 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5102
5103 // Shuffles must have a splat mask where the first element is
5104 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005105 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005106 return SDValue();
5107
5108 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005109 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005110 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5111
5112 if (!Subtarget->hasAVX2())
5113 return SDValue();
5114
5115 // Use the register form of the broadcast instruction available on AVX2.
5116 if (VT.is256BitVector())
5117 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5118 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5119 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005120
5121 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005122 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005123 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005124
5125 // The scalar_to_vector node and the suspected
5126 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005127 // Constants may have multiple users.
5128 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005129 return SDValue();
5130 break;
5131 }
5132 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005133
Craig Topper7a9a28b2012-08-12 02:23:29 +00005134 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005135
5136 // Handle the broadcasting a single constant scalar from the constant pool
5137 // into a vector. On Sandybridge it is still better to load a constant vector
5138 // from the constant pool and not to broadcast it from a scalar.
5139 if (ConstSplatVal && Subtarget->hasAVX2()) {
5140 EVT CVT = Ld.getValueType();
5141 assert(!CVT.isVector() && "Must not broadcast a vector type");
5142 unsigned ScalarSize = CVT.getSizeInBits();
5143
Craig Topper5da8a802012-05-04 05:49:51 +00005144 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005145 const Constant *C = 0;
5146 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5147 C = CI->getConstantIntValue();
5148 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5149 C = CF->getConstantFPValue();
5150
5151 assert(C && "Invalid constant type");
5152
Nadav Rotem154819d2012-04-09 07:45:58 +00005153 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005154 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005155 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005156 MachinePointerInfo::getConstantPool(),
5157 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005158
Nadav Rotem9d68b062012-04-08 12:54:54 +00005159 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5160 }
5161 }
5162
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005163 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005164 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5165
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005166 // Handle AVX2 in-register broadcasts.
5167 if (!IsLoad && Subtarget->hasAVX2() &&
5168 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5169 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5170
5171 // The scalar source must be a normal load.
5172 if (!IsLoad)
5173 return SDValue();
5174
Craig Topper5da8a802012-05-04 05:49:51 +00005175 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005176 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005177
Craig Toppera9376332012-01-10 08:23:59 +00005178 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005179 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005180 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005181 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005182 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005183 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005184
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005185 // Unsupported broadcast.
5186 return SDValue();
5187}
5188
Evan Chengc3630942009-12-09 21:00:30 +00005189SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005190X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5191 EVT VT = Op.getValueType();
5192
5193 // Skip if insert_vec_elt is not supported.
5194 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5195 return SDValue();
5196
5197 DebugLoc DL = Op.getDebugLoc();
5198 unsigned NumElems = Op.getNumOperands();
5199
5200 SDValue VecIn1;
5201 SDValue VecIn2;
5202 SmallVector<unsigned, 4> InsertIndices;
5203 SmallVector<int, 8> Mask(NumElems, -1);
5204
5205 for (unsigned i = 0; i != NumElems; ++i) {
5206 unsigned Opc = Op.getOperand(i).getOpcode();
5207
5208 if (Opc == ISD::UNDEF)
5209 continue;
5210
5211 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5212 // Quit if more than 1 elements need inserting.
5213 if (InsertIndices.size() > 1)
5214 return SDValue();
5215
5216 InsertIndices.push_back(i);
5217 continue;
5218 }
5219
5220 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5221 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5222
5223 // Quit if extracted from vector of different type.
5224 if (ExtractedFromVec.getValueType() != VT)
5225 return SDValue();
5226
5227 // Quit if non-constant index.
5228 if (!isa<ConstantSDNode>(ExtIdx))
5229 return SDValue();
5230
5231 if (VecIn1.getNode() == 0)
5232 VecIn1 = ExtractedFromVec;
5233 else if (VecIn1 != ExtractedFromVec) {
5234 if (VecIn2.getNode() == 0)
5235 VecIn2 = ExtractedFromVec;
5236 else if (VecIn2 != ExtractedFromVec)
5237 // Quit if more than 2 vectors to shuffle
5238 return SDValue();
5239 }
5240
5241 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5242
5243 if (ExtractedFromVec == VecIn1)
5244 Mask[i] = Idx;
5245 else if (ExtractedFromVec == VecIn2)
5246 Mask[i] = Idx + NumElems;
5247 }
5248
5249 if (VecIn1.getNode() == 0)
5250 return SDValue();
5251
5252 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5253 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5254 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5255 unsigned Idx = InsertIndices[i];
5256 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5257 DAG.getIntPtrConstant(Idx));
5258 }
5259
5260 return NV;
5261}
5262
5263SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005264X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005265 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005266
David Greenef125a292011-02-08 19:04:41 +00005267 EVT VT = Op.getValueType();
5268 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005269 unsigned NumElems = Op.getNumOperands();
5270
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005271 // Vectors containing all zeros can be matched by pxor and xorps later
5272 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5273 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5274 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005275 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005276 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005278 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005279 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005281 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005282 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5283 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005284 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005285 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005286 return Op;
5287
Craig Topper07a27622012-01-22 03:07:48 +00005288 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005289 }
5290
Nadav Rotem154819d2012-04-09 07:45:58 +00005291 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005292 if (Broadcast.getNode())
5293 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005294
Owen Andersone50ed302009-08-10 22:56:29 +00005295 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296
Evan Cheng0db9fe62006-04-25 20:13:52 +00005297 unsigned NumZero = 0;
5298 unsigned NumNonZero = 0;
5299 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005300 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005301 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005302 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005304 if (Elt.getOpcode() == ISD::UNDEF)
5305 continue;
5306 Values.insert(Elt);
5307 if (Elt.getOpcode() != ISD::Constant &&
5308 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005309 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005310 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005311 NumZero++;
5312 else {
5313 NonZeros |= (1 << i);
5314 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005315 }
5316 }
5317
Chris Lattner97a2a562010-08-26 05:24:29 +00005318 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5319 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005320 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005321
Chris Lattner67f453a2008-03-09 05:42:06 +00005322 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005323 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005324 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005325 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005326
Chris Lattner62098042008-03-09 01:05:04 +00005327 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5328 // the value are obviously zero, truncate the value to i32 and do the
5329 // insertion that way. Only do this if the value is non-constant or if the
5330 // value is a constant being inserted into element 0. It is cheaper to do
5331 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005332 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005333 (!IsAllConstants || Idx == 0)) {
5334 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005335 // Handle SSE only.
5336 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5337 EVT VecVT = MVT::v4i32;
5338 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005339
Chris Lattner62098042008-03-09 01:05:04 +00005340 // Truncate the value (which may itself be a constant) to i32, and
5341 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005343 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005344 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005345
Chris Lattner62098042008-03-09 01:05:04 +00005346 // Now we have our 32-bit value zero extended in the low element of
5347 // a vector. If Idx != 0, swizzle it into place.
5348 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005349 SmallVector<int, 4> Mask;
5350 Mask.push_back(Idx);
5351 for (unsigned i = 1; i != VecElts; ++i)
5352 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005353 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005354 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005355 }
Craig Topper07a27622012-01-22 03:07:48 +00005356 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005357 }
5358 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005359
Chris Lattner19f79692008-03-08 22:59:52 +00005360 // If we have a constant or non-constant insertion into the low element of
5361 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5362 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005363 // depending on what the source datatype is.
5364 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005365 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005366 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005367
5368 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005369 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005370 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005371 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005372 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5373 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005374 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005375 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005376 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5377 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005378 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005379 }
5380
5381 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005382 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005383 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005384 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005385 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005386 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005387 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005388 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005389 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005390 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005391 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005392 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005393 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005394
5395 // Is it a vector logical left shift?
5396 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005397 X86::isZeroNode(Op.getOperand(0)) &&
5398 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005399 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005400 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005401 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005402 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005403 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005404 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005405
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005406 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005407 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005408
Chris Lattner19f79692008-03-08 22:59:52 +00005409 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5410 // is a non-constant being inserted into an element other than the low one,
5411 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5412 // movd/movss) to move this into the low element, then shuffle it into
5413 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005414 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005415 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005416
Evan Cheng0db9fe62006-04-25 20:13:52 +00005417 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005418 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005419 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005420 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005421 MaskVec.push_back(i == Idx ? 0 : 1);
5422 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005423 }
5424 }
5425
Chris Lattner67f453a2008-03-09 05:42:06 +00005426 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005427 if (Values.size() == 1) {
5428 if (EVTBits == 32) {
5429 // Instead of a shuffle like this:
5430 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5431 // Check if it's possible to issue this instead.
5432 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5433 unsigned Idx = CountTrailingZeros_32(NonZeros);
5434 SDValue Item = Op.getOperand(Idx);
5435 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5436 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5437 }
Dan Gohman475871a2008-07-27 21:46:04 +00005438 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005439 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005440
Dan Gohmana3941172007-07-24 22:55:08 +00005441 // A vector full of immediates; various special cases are already
5442 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005443 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005444 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005445
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005446 // For AVX-length vectors, build the individual 128-bit pieces and use
5447 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005448 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005449 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005450 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005451 V.push_back(Op.getOperand(i));
5452
5453 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5454
5455 // Build both the lower and upper subvector.
5456 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5457 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5458 NumElems/2);
5459
5460 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005461 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005462 }
5463
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005464 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005465 if (EVTBits == 64) {
5466 if (NumNonZero == 1) {
5467 // One half is zero or undef.
5468 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005469 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005470 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005471 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005472 }
Dan Gohman475871a2008-07-27 21:46:04 +00005473 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005474 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005475
5476 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005477 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005478 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005479 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005480 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 }
5482
Bill Wendling826f36f2007-03-28 00:57:11 +00005483 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005484 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005485 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005486 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487 }
5488
5489 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005490 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005491 if (NumElems == 4 && NumZero > 0) {
5492 for (unsigned i = 0; i < 4; ++i) {
5493 bool isZero = !(NonZeros & (1 << i));
5494 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005495 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005496 else
Dale Johannesenace16102009-02-03 19:33:06 +00005497 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498 }
5499
5500 for (unsigned i = 0; i < 2; ++i) {
5501 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5502 default: break;
5503 case 0:
5504 V[i] = V[i*2]; // Must be a zero vector.
5505 break;
5506 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005507 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005508 break;
5509 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005510 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005511 break;
5512 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005513 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005514 break;
5515 }
5516 }
5517
Benjamin Kramer9c683542012-01-30 15:16:21 +00005518 bool Reverse1 = (NonZeros & 0x3) == 2;
5519 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5520 int MaskVec[] = {
5521 Reverse1 ? 1 : 0,
5522 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005523 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5524 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005525 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005527 }
5528
Craig Topper7a9a28b2012-08-12 02:23:29 +00005529 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005530 // Check for a build vector of consecutive loads.
5531 for (unsigned i = 0; i < NumElems; ++i)
5532 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005533
Nate Begemanfdea31a2010-03-24 20:49:50 +00005534 // Check for elements which are consecutive loads.
5535 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5536 if (LD.getNode())
5537 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005538
Michael Liaofacace82012-10-19 17:15:18 +00005539 // Check for a build vector from mostly shuffle plus few inserting.
5540 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5541 if (Sh.getNode())
5542 return Sh;
5543
Michael J. Spencerec38de22010-10-10 22:04:20 +00005544 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005545 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005546 SDValue Result;
5547 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5548 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5549 else
5550 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005551
Chris Lattner24faf612010-08-28 17:59:08 +00005552 for (unsigned i = 1; i < NumElems; ++i) {
5553 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5554 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005555 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005556 }
5557 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005558 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005559
Chris Lattner6e80e442010-08-28 17:15:43 +00005560 // Otherwise, expand into a number of unpckl*, start by extending each of
5561 // our (non-undef) elements to the full vector width with the element in the
5562 // bottom slot of the vector (which generates no code for SSE).
5563 for (unsigned i = 0; i < NumElems; ++i) {
5564 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5565 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5566 else
5567 V[i] = DAG.getUNDEF(VT);
5568 }
5569
5570 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005571 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5572 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5573 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005574 unsigned EltStride = NumElems >> 1;
5575 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005576 for (unsigned i = 0; i < EltStride; ++i) {
5577 // If V[i+EltStride] is undef and this is the first round of mixing,
5578 // then it is safe to just drop this shuffle: V[i] is already in the
5579 // right place, the one element (since it's the first round) being
5580 // inserted as undef can be dropped. This isn't safe for successive
5581 // rounds because they will permute elements within both vectors.
5582 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5583 EltStride == NumElems/2)
5584 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005585
Chris Lattner6e80e442010-08-28 17:15:43 +00005586 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005587 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005588 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005589 }
5590 return V[0];
5591 }
Dan Gohman475871a2008-07-27 21:46:04 +00005592 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005593}
5594
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005595// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5596// to create 256-bit vectors from two other 128-bit ones.
5597static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5598 DebugLoc dl = Op.getDebugLoc();
5599 EVT ResVT = Op.getValueType();
5600
Craig Topper7a9a28b2012-08-12 02:23:29 +00005601 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005602
5603 SDValue V1 = Op.getOperand(0);
5604 SDValue V2 = Op.getOperand(1);
5605 unsigned NumElems = ResVT.getVectorNumElements();
5606
Craig Topper4c7972d2012-04-22 18:15:59 +00005607 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005608}
5609
Craig Topper55b24052012-09-11 06:15:32 +00005610static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005611 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005612
5613 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5614 // from two other 128-bit ones.
5615 return LowerAVXCONCAT_VECTORS(Op, DAG);
5616}
5617
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005618// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005619static SDValue
5620LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5621 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005622 SDValue V1 = SVOp->getOperand(0);
5623 SDValue V2 = SVOp->getOperand(1);
5624 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005625 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005626 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005627
Nadav Roteme6113782012-04-11 06:40:27 +00005628 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005629 return SDValue();
5630
Craig Topper1842ba02012-04-23 06:38:28 +00005631 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005632 MVT OpTy;
5633
Craig Topper708e44f2012-04-23 07:36:33 +00005634 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005635 default: return SDValue();
5636 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005637 ISDNo = X86ISD::BLENDPW;
5638 OpTy = MVT::v8i16;
5639 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005640 case MVT::v4i32:
5641 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005642 ISDNo = X86ISD::BLENDPS;
5643 OpTy = MVT::v4f32;
5644 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005645 case MVT::v2i64:
5646 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005647 ISDNo = X86ISD::BLENDPD;
5648 OpTy = MVT::v2f64;
5649 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005650 case MVT::v8i32:
5651 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005652 if (!Subtarget->hasAVX())
5653 return SDValue();
5654 ISDNo = X86ISD::BLENDPS;
5655 OpTy = MVT::v8f32;
5656 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005657 case MVT::v4i64:
5658 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005659 if (!Subtarget->hasAVX())
5660 return SDValue();
5661 ISDNo = X86ISD::BLENDPD;
5662 OpTy = MVT::v4f64;
5663 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005664 }
5665 assert(ISDNo && "Invalid Op Number");
5666
5667 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005668
Craig Topper1842ba02012-04-23 06:38:28 +00005669 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005670 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005671 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005672 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005673 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005674 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005675 else
5676 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005677 }
5678
Nadav Roteme6113782012-04-11 06:40:27 +00005679 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5680 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5681 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5682 DAG.getConstant(MaskVals, MVT::i32));
5683 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005684}
5685
Nate Begemanb9a47b82009-02-23 08:49:38 +00005686// v8i16 shuffles - Prefer shuffles in the following order:
5687// 1. [all] pshuflw, pshufhw, optional move
5688// 2. [ssse3] 1 x pshufb
5689// 3. [ssse3] 2 x pshufb + 1 x por
5690// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005691static SDValue
5692LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5693 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005694 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005695 SDValue V1 = SVOp->getOperand(0);
5696 SDValue V2 = SVOp->getOperand(1);
5697 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005699
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 // Determine if more than 1 of the words in each of the low and high quadwords
5701 // of the result come from the same quadword of one of the two inputs. Undef
5702 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005703 unsigned LoQuad[] = { 0, 0, 0, 0 };
5704 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005705 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005707 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005708 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005709 MaskVals.push_back(EltIdx);
5710 if (EltIdx < 0) {
5711 ++Quad[0];
5712 ++Quad[1];
5713 ++Quad[2];
5714 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005715 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 }
5717 ++Quad[EltIdx / 4];
5718 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005719 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005720
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005722 unsigned MaxQuad = 1;
5723 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 if (LoQuad[i] > MaxQuad) {
5725 BestLoQuad = i;
5726 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005727 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005728 }
5729
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005731 MaxQuad = 1;
5732 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 if (HiQuad[i] > MaxQuad) {
5734 BestHiQuad = i;
5735 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005736 }
5737 }
5738
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005740 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 // single pshufb instruction is necessary. If There are more than 2 input
5742 // quads, disable the next transformation since it does not help SSSE3.
5743 bool V1Used = InputQuads[0] || InputQuads[1];
5744 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005745 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005747 BestLoQuad = InputQuads[0] ? 0 : 1;
5748 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 }
5750 if (InputQuads.count() > 2) {
5751 BestLoQuad = -1;
5752 BestHiQuad = -1;
5753 }
5754 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005755
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5757 // the shuffle mask. If a quad is scored as -1, that means that it contains
5758 // words from all 4 input quadwords.
5759 SDValue NewV;
5760 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005761 int MaskV[] = {
5762 BestLoQuad < 0 ? 0 : BestLoQuad,
5763 BestHiQuad < 0 ? 1 : BestHiQuad
5764 };
Eric Christopherfd179292009-08-27 18:07:15 +00005765 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005766 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5767 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5768 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005769
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5771 // source words for the shuffle, to aid later transformations.
5772 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005773 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005774 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005776 if (idx != (int)i)
5777 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005779 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 AllWordsInNewV = false;
5781 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005782 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005783
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5785 if (AllWordsInNewV) {
5786 for (int i = 0; i != 8; ++i) {
5787 int idx = MaskVals[i];
5788 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005789 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005790 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 if ((idx != i) && idx < 4)
5792 pshufhw = false;
5793 if ((idx != i) && idx > 3)
5794 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005795 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005796 V1 = NewV;
5797 V2Used = false;
5798 BestLoQuad = 0;
5799 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005800 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005801
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5803 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005804 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005805 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5806 unsigned TargetMask = 0;
5807 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005808 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005809 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5810 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5811 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005812 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005813 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005814 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005815 }
Eric Christopherfd179292009-08-27 18:07:15 +00005816
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 // If we have SSSE3, and all words of the result are from 1 input vector,
5818 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5819 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005820 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005821 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005822
Nate Begemanb9a47b82009-02-23 08:49:38 +00005823 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005824 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005825 // mask, and elements that come from V1 in the V2 mask, so that the two
5826 // results can be OR'd together.
5827 bool TwoInputs = V1Used && V2Used;
5828 for (unsigned i = 0; i != 8; ++i) {
5829 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005830 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5831 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5832 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5833 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005835 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005836 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005837 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005838 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005840 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005841
Nate Begemanb9a47b82009-02-23 08:49:38 +00005842 // Calculate the shuffle mask for the second input, shuffle it, and
5843 // OR it with the first shuffled input.
5844 pshufbMask.clear();
5845 for (unsigned i = 0; i != 8; ++i) {
5846 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005847 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5848 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5849 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5850 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005852 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005853 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005854 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005855 MVT::v16i8, &pshufbMask[0], 16));
5856 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005857 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 }
5859
5860 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5861 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005862 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005863 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005864 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005865 for (int i = 0; i != 4; ++i) {
5866 int idx = MaskVals[i];
5867 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005868 InOrder.set(i);
5869 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005870 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 }
5873 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005875 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005876
Craig Topperdd637ae2012-02-19 05:41:45 +00005877 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5878 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005879 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005880 NewV.getOperand(0),
5881 getShufflePSHUFLWImmediate(SVOp), DAG);
5882 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 }
Eric Christopherfd179292009-08-27 18:07:15 +00005884
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5886 // and update MaskVals with the new element order.
5887 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005888 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005889 for (unsigned i = 4; i != 8; ++i) {
5890 int idx = MaskVals[i];
5891 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005892 InOrder.set(i);
5893 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005894 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 }
5897 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005899 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005900
Craig Topperdd637ae2012-02-19 05:41:45 +00005901 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5902 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005903 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005904 NewV.getOperand(0),
5905 getShufflePSHUFHWImmediate(SVOp), DAG);
5906 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005907 }
Eric Christopherfd179292009-08-27 18:07:15 +00005908
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 // In case BestHi & BestLo were both -1, which means each quadword has a word
5910 // from each of the four input quadwords, calculate the InOrder bitvector now
5911 // before falling through to the insert/extract cleanup.
5912 if (BestLoQuad == -1 && BestHiQuad == -1) {
5913 NewV = V1;
5914 for (int i = 0; i != 8; ++i)
5915 if (MaskVals[i] < 0 || MaskVals[i] == i)
5916 InOrder.set(i);
5917 }
Eric Christopherfd179292009-08-27 18:07:15 +00005918
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 // The other elements are put in the right place using pextrw and pinsrw.
5920 for (unsigned i = 0; i != 8; ++i) {
5921 if (InOrder[i])
5922 continue;
5923 int EltIdx = MaskVals[i];
5924 if (EltIdx < 0)
5925 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005926 SDValue ExtOp = (EltIdx < 8) ?
5927 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5928 DAG.getIntPtrConstant(EltIdx)) :
5929 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 DAG.getIntPtrConstant(i));
5933 }
5934 return NewV;
5935}
5936
5937// v16i8 shuffles - Prefer shuffles in the following order:
5938// 1. [ssse3] 1 x pshufb
5939// 2. [ssse3] 2 x pshufb + 1 x por
5940// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5941static
Nate Begeman9008ca62009-04-27 18:41:29 +00005942SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005943 SelectionDAG &DAG,
5944 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005945 SDValue V1 = SVOp->getOperand(0);
5946 SDValue V2 = SVOp->getOperand(1);
5947 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005948 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005949
Nate Begemanb9a47b82009-02-23 08:49:38 +00005950 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005951 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005953
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005955 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005957
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005959 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005960 //
5961 // Otherwise, we have elements from both input vectors, and must zero out
5962 // elements that come from V2 in the first mask, and V1 in the second mask
5963 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 for (unsigned i = 0; i != 16; ++i) {
5965 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005966 if (EltIdx < 0 || EltIdx >= 16)
5967 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005968 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005970 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005971 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005972 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005973
5974 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5975 // the 2nd operand if it's undefined or zero.
5976 if (V2.getOpcode() == ISD::UNDEF ||
5977 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005979
Nate Begemanb9a47b82009-02-23 08:49:38 +00005980 // Calculate the shuffle mask for the second input, shuffle it, and
5981 // OR it with the first shuffled input.
5982 pshufbMask.clear();
5983 for (unsigned i = 0; i != 16; ++i) {
5984 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005985 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005986 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005989 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005990 MVT::v16i8, &pshufbMask[0], 16));
5991 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005992 }
Eric Christopherfd179292009-08-27 18:07:15 +00005993
Nate Begemanb9a47b82009-02-23 08:49:38 +00005994 // No SSSE3 - Calculate in place words and then fix all out of place words
5995 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5996 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005997 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5998 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005999 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 for (int i = 0; i != 8; ++i) {
6001 int Elt0 = MaskVals[i*2];
6002 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006003
Nate Begemanb9a47b82009-02-23 08:49:38 +00006004 // This word of the result is all undef, skip it.
6005 if (Elt0 < 0 && Elt1 < 0)
6006 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006007
Nate Begemanb9a47b82009-02-23 08:49:38 +00006008 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006009 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006011
Nate Begemanb9a47b82009-02-23 08:49:38 +00006012 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6013 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6014 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006015
6016 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6017 // using a single extract together, load it and store it.
6018 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006019 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006020 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006021 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006022 DAG.getIntPtrConstant(i));
6023 continue;
6024 }
6025
Nate Begemanb9a47b82009-02-23 08:49:38 +00006026 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006027 // source byte is not also odd, shift the extracted word left 8 bits
6028 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006030 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 DAG.getIntPtrConstant(Elt1 / 2));
6032 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006033 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006034 DAG.getConstant(8,
6035 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006036 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006037 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6038 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006039 }
6040 // If Elt0 is defined, extract it from the appropriate source. If the
6041 // source byte is not also even, shift the extracted word right 8 bits. If
6042 // Elt1 was also defined, OR the extracted values together before
6043 // inserting them in the result.
6044 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006045 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006046 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6047 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006048 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006049 DAG.getConstant(8,
6050 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006051 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006052 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6053 DAG.getConstant(0x00FF, MVT::i16));
6054 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006055 : InsElt0;
6056 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006058 DAG.getIntPtrConstant(i));
6059 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006060 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006061}
6062
Elena Demikhovsky41789462012-09-06 12:42:01 +00006063// v32i8 shuffles - Translate to VPSHUFB if possible.
6064static
6065SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006066 const X86Subtarget *Subtarget,
6067 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006068 EVT VT = SVOp->getValueType(0);
6069 SDValue V1 = SVOp->getOperand(0);
6070 SDValue V2 = SVOp->getOperand(1);
6071 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006072 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006073
6074 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006075 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6076 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006077
Michael Liao471b9172012-10-03 23:43:52 +00006078 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006079 // (1) one of input vector is undefined or zeroinitializer.
6080 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6081 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006082 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006083 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006084 return SDValue();
6085
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006086 if (V1IsAllZero && !V2IsAllZero) {
6087 CommuteVectorShuffleMask(MaskVals, 32);
6088 V1 = V2;
6089 }
6090 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006091 for (unsigned i = 0; i != 32; i++) {
6092 int EltIdx = MaskVals[i];
6093 if (EltIdx < 0 || EltIdx >= 32)
6094 EltIdx = 0x80;
6095 else {
6096 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6097 // Cross lane is not allowed.
6098 return SDValue();
6099 EltIdx &= 0xf;
6100 }
6101 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6102 }
6103 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6104 DAG.getNode(ISD::BUILD_VECTOR, dl,
6105 MVT::v32i8, &pshufbMask[0], 32));
6106}
6107
Evan Cheng7a831ce2007-12-15 03:00:47 +00006108/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006109/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006110/// done when every pair / quad of shuffle mask elements point to elements in
6111/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006112/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006113static
Nate Begeman9008ca62009-04-27 18:41:29 +00006114SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006115 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006116 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006117 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006118 MVT NewVT;
6119 unsigned Scale;
6120 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006121 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006122 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6123 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6124 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6125 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6126 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6127 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006128 }
6129
Nate Begeman9008ca62009-04-27 18:41:29 +00006130 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006131 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006132 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006133 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006134 int EltIdx = SVOp->getMaskElt(i+j);
6135 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006136 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006137 if (StartIdx < 0)
6138 StartIdx = (EltIdx / Scale);
6139 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006140 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006141 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006142 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006143 }
6144
Craig Topper11ac1f82012-05-04 04:08:44 +00006145 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6146 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006147 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006148}
6149
Evan Chengd880b972008-05-09 21:53:03 +00006150/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006151///
Owen Andersone50ed302009-08-10 22:56:29 +00006152static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 SDValue SrcOp, SelectionDAG &DAG,
6154 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006155 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006156 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006157 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006158 LD = dyn_cast<LoadSDNode>(SrcOp);
6159 if (!LD) {
6160 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6161 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006162 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006163 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006164 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006165 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006166 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006167 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006168 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006169 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006170 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6171 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6172 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006173 SrcOp.getOperand(0)
6174 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006175 }
6176 }
6177 }
6178
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006179 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006180 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006181 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006182 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006183}
6184
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006185/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6186/// which could not be matched by any known target speficic shuffle
6187static SDValue
6188LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006189
6190 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6191 if (NewOp.getNode())
6192 return NewOp;
6193
Craig Topper8f35c132012-01-20 09:29:03 +00006194 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006195
Craig Topper8f35c132012-01-20 09:29:03 +00006196 unsigned NumElems = VT.getVectorNumElements();
6197 unsigned NumLaneElems = NumElems / 2;
6198
Craig Topper8f35c132012-01-20 09:29:03 +00006199 DebugLoc dl = SVOp->getDebugLoc();
6200 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006201 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006202 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006203
Craig Topper9a2b6e12012-04-06 07:45:23 +00006204 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006205 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006206 // Build a shuffle mask for the output, discovering on the fly which
6207 // input vectors to use as shuffle operands (recorded in InputUsed).
6208 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006209 // out with UseBuildVector set.
6210 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006211 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006212 unsigned LaneStart = l * NumLaneElems;
6213 for (unsigned i = 0; i != NumLaneElems; ++i) {
6214 // The mask element. This indexes into the input.
6215 int Idx = SVOp->getMaskElt(i+LaneStart);
6216 if (Idx < 0) {
6217 // the mask element does not index into any input vector.
6218 Mask.push_back(-1);
6219 continue;
6220 }
Craig Topper8f35c132012-01-20 09:29:03 +00006221
Craig Topper9a2b6e12012-04-06 07:45:23 +00006222 // The input vector this mask element indexes into.
6223 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006224
Craig Topper9a2b6e12012-04-06 07:45:23 +00006225 // Turn the index into an offset from the start of the input vector.
6226 Idx -= Input * NumLaneElems;
6227
6228 // Find or create a shuffle vector operand to hold this input.
6229 unsigned OpNo;
6230 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6231 if (InputUsed[OpNo] == Input)
6232 // This input vector is already an operand.
6233 break;
6234 if (InputUsed[OpNo] < 0) {
6235 // Create a new operand for this input vector.
6236 InputUsed[OpNo] = Input;
6237 break;
6238 }
6239 }
6240
6241 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006242 // More than two input vectors used! Give up on trying to create a
6243 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6244 UseBuildVector = true;
6245 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006246 }
6247
6248 // Add the mask index for the new shuffle vector.
6249 Mask.push_back(Idx + OpNo * NumLaneElems);
6250 }
6251
Craig Topper8ae97ba2012-05-21 06:40:16 +00006252 if (UseBuildVector) {
6253 SmallVector<SDValue, 16> SVOps;
6254 for (unsigned i = 0; i != NumLaneElems; ++i) {
6255 // The mask element. This indexes into the input.
6256 int Idx = SVOp->getMaskElt(i+LaneStart);
6257 if (Idx < 0) {
6258 SVOps.push_back(DAG.getUNDEF(EltVT));
6259 continue;
6260 }
6261
6262 // The input vector this mask element indexes into.
6263 int Input = Idx / NumElems;
6264
6265 // Turn the index into an offset from the start of the input vector.
6266 Idx -= Input * NumElems;
6267
6268 // Extract the vector element by hand.
6269 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6270 SVOp->getOperand(Input),
6271 DAG.getIntPtrConstant(Idx)));
6272 }
6273
6274 // Construct the output using a BUILD_VECTOR.
6275 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6276 SVOps.size());
6277 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006278 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006279 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006280 } else {
6281 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006282 (InputUsed[0] % 2) * NumLaneElems,
6283 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006284 // If only one input was used, use an undefined vector for the other.
6285 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6286 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006287 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006288 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006289 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006290 }
6291
6292 Mask.clear();
6293 }
Craig Topper8f35c132012-01-20 09:29:03 +00006294
6295 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006296 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006297}
6298
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006299/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6300/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006301static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006302LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006303 SDValue V1 = SVOp->getOperand(0);
6304 SDValue V2 = SVOp->getOperand(1);
6305 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006306 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006307
Craig Topper7a9a28b2012-08-12 02:23:29 +00006308 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006309
Benjamin Kramer9c683542012-01-30 15:16:21 +00006310 std::pair<int, int> Locs[4];
6311 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006312 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006313
Evan Chengace3c172008-07-22 21:13:36 +00006314 unsigned NumHi = 0;
6315 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006316 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006317 int Idx = PermMask[i];
6318 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006319 Locs[i] = std::make_pair(-1, -1);
6320 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006321 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6322 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006323 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006324 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006325 NumLo++;
6326 } else {
6327 Locs[i] = std::make_pair(1, NumHi);
6328 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006329 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006330 NumHi++;
6331 }
6332 }
6333 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006334
Evan Chengace3c172008-07-22 21:13:36 +00006335 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006336 // If no more than two elements come from either vector. This can be
6337 // implemented with two shuffles. First shuffle gather the elements.
6338 // The second shuffle, which takes the first shuffle as both of its
6339 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006340 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006341
Benjamin Kramer9c683542012-01-30 15:16:21 +00006342 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006343
Benjamin Kramer9c683542012-01-30 15:16:21 +00006344 for (unsigned i = 0; i != 4; ++i)
6345 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006346 unsigned Idx = (i < 2) ? 0 : 4;
6347 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006348 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006349 }
Evan Chengace3c172008-07-22 21:13:36 +00006350
Nate Begeman9008ca62009-04-27 18:41:29 +00006351 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006352 }
6353
6354 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006355 // Otherwise, we must have three elements from one vector, call it X, and
6356 // one element from the other, call it Y. First, use a shufps to build an
6357 // intermediate vector with the one element from Y and the element from X
6358 // that will be in the same half in the final destination (the indexes don't
6359 // matter). Then, use a shufps to build the final vector, taking the half
6360 // containing the element from Y from the intermediate, and the other half
6361 // from X.
6362 if (NumHi == 3) {
6363 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006364 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006365 std::swap(V1, V2);
6366 }
6367
6368 // Find the element from V2.
6369 unsigned HiIndex;
6370 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006371 int Val = PermMask[HiIndex];
6372 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006373 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006374 if (Val >= 4)
6375 break;
6376 }
6377
Nate Begeman9008ca62009-04-27 18:41:29 +00006378 Mask1[0] = PermMask[HiIndex];
6379 Mask1[1] = -1;
6380 Mask1[2] = PermMask[HiIndex^1];
6381 Mask1[3] = -1;
6382 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006383
6384 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006385 Mask1[0] = PermMask[0];
6386 Mask1[1] = PermMask[1];
6387 Mask1[2] = HiIndex & 1 ? 6 : 4;
6388 Mask1[3] = HiIndex & 1 ? 4 : 6;
6389 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006390 }
Craig Topper69947b92012-04-23 06:57:04 +00006391
6392 Mask1[0] = HiIndex & 1 ? 2 : 0;
6393 Mask1[1] = HiIndex & 1 ? 0 : 2;
6394 Mask1[2] = PermMask[2];
6395 Mask1[3] = PermMask[3];
6396 if (Mask1[2] >= 0)
6397 Mask1[2] += 4;
6398 if (Mask1[3] >= 0)
6399 Mask1[3] += 4;
6400 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006401 }
6402
6403 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006404 int LoMask[] = { -1, -1, -1, -1 };
6405 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006406
Benjamin Kramer9c683542012-01-30 15:16:21 +00006407 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006408 unsigned MaskIdx = 0;
6409 unsigned LoIdx = 0;
6410 unsigned HiIdx = 2;
6411 for (unsigned i = 0; i != 4; ++i) {
6412 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006413 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006414 MaskIdx = 1;
6415 LoIdx = 0;
6416 HiIdx = 2;
6417 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006418 int Idx = PermMask[i];
6419 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006420 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006421 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006422 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006423 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006424 LoIdx++;
6425 } else {
6426 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006427 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006428 HiIdx++;
6429 }
6430 }
6431
Nate Begeman9008ca62009-04-27 18:41:29 +00006432 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6433 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006434 int MaskOps[] = { -1, -1, -1, -1 };
6435 for (unsigned i = 0; i != 4; ++i)
6436 if (Locs[i].first != -1)
6437 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006438 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006439}
6440
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006441static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszak6d317822012-10-29 21:56:35 +00006442 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006443 V = V.getOperand(0);
Jakub Staszak6d317822012-10-29 21:56:35 +00006444
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006445 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6446 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006447 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6448 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6449 // BUILD_VECTOR (load), undef
6450 V = V.getOperand(0);
Jakub Staszak6d317822012-10-29 21:56:35 +00006451
6452 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006453}
6454
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006455// FIXME: the version above should always be used. Since there's
6456// a bug where several vector shuffles can't be folded because the
6457// DAG is not updated during lowering and a node claims to have two
6458// uses while it only has one, use this version, and let isel match
6459// another instruction if the load really happens to have more than
6460// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006461// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006462static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006463 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006464 V = V.getOperand(0);
6465 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6466 V = V.getOperand(0);
6467 if (ISD::isNormalLoad(V.getNode()))
6468 return true;
6469 return false;
6470}
6471
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006472static
Evan Cheng835580f2010-10-07 20:50:20 +00006473SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6474 EVT VT = Op.getValueType();
6475
6476 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006477 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6478 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006479 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6480 V1, DAG));
6481}
6482
6483static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006484SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006485 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006486 SDValue V1 = Op.getOperand(0);
6487 SDValue V2 = Op.getOperand(1);
6488 EVT VT = Op.getValueType();
6489
6490 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6491
Craig Topper1accb7e2012-01-10 06:54:16 +00006492 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006493 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6494
Evan Cheng0899f5c2011-08-31 02:05:24 +00006495 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6496 return DAG.getNode(ISD::BITCAST, dl, VT,
6497 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6498 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6499 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006500}
6501
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006502static
6503SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6504 SDValue V1 = Op.getOperand(0);
6505 SDValue V2 = Op.getOperand(1);
6506 EVT VT = Op.getValueType();
6507
6508 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6509 "unsupported shuffle type");
6510
6511 if (V2.getOpcode() == ISD::UNDEF)
6512 V2 = V1;
6513
6514 // v4i32 or v4f32
6515 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6516}
6517
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006518static
Craig Topper1accb7e2012-01-10 06:54:16 +00006519SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006520 SDValue V1 = Op.getOperand(0);
6521 SDValue V2 = Op.getOperand(1);
6522 EVT VT = Op.getValueType();
6523 unsigned NumElems = VT.getVectorNumElements();
6524
6525 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6526 // operand of these instructions is only memory, so check if there's a
6527 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6528 // same masks.
6529 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006530
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006531 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006532 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006533 CanFoldLoad = true;
6534
6535 // When V1 is a load, it can be folded later into a store in isel, example:
6536 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6537 // turns into:
6538 // (MOVLPSmr addr:$src1, VR128:$src2)
6539 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006540 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006541 CanFoldLoad = true;
6542
Dan Gohman65fd6562011-11-03 21:49:52 +00006543 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006544 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006545 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006546 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6547
6548 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006549 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006550 if (SVOp->getMaskElt(1) != -1)
6551 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006552 }
6553
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006554 // movl and movlp will both match v2i64, but v2i64 is never matched by
6555 // movl earlier because we make it strict to avoid messing with the movlp load
6556 // folding logic (see the code above getMOVLP call). Match it here then,
6557 // this is horrible, but will stay like this until we move all shuffle
6558 // matching to x86 specific nodes. Note that for the 1st condition all
6559 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006560 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006561 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6562 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006563 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006564 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006565 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006566 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006567
6568 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6569
6570 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006571 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006572 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006573}
6574
Michael Liaod9d09602012-10-23 17:34:00 +00006575// Reduce a vector shuffle to zext.
6576SDValue
6577X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6578 // PMOVZX is only available from SSE41.
6579 if (!Subtarget->hasSSE41())
6580 return SDValue();
6581
6582 EVT VT = Op.getValueType();
6583
6584 // Only AVX2 support 256-bit vector integer extending.
6585 if (!Subtarget->hasAVX2() && VT.is256BitVector())
6586 return SDValue();
6587
6588 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6589 DebugLoc DL = Op.getDebugLoc();
6590 SDValue V1 = Op.getOperand(0);
6591 SDValue V2 = Op.getOperand(1);
6592 unsigned NumElems = VT.getVectorNumElements();
6593
6594 // Extending is an unary operation and the element type of the source vector
6595 // won't be equal to or larger than i64.
6596 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6597 VT.getVectorElementType() == MVT::i64)
6598 return SDValue();
6599
6600 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6601 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006602 while ((1U << Shift) < NumElems) {
6603 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006604 break;
6605 Shift += 1;
6606 // The maximal ratio is 8, i.e. from i8 to i64.
6607 if (Shift > 3)
6608 return SDValue();
6609 }
6610
6611 // Check the shuffle mask.
6612 unsigned Mask = (1U << Shift) - 1;
6613 for (unsigned i = 0; i != NumElems; ++i) {
6614 int EltIdx = SVOp->getMaskElt(i);
6615 if ((i & Mask) != 0 && EltIdx != -1)
6616 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006617 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006618 return SDValue();
6619 }
6620
6621 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6622 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6623 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6624
6625 if (!isTypeLegal(NVT))
6626 return SDValue();
6627
6628 // Simplify the operand as it's prepared to be fed into shuffle.
6629 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6630 if (V1.getOpcode() == ISD::BITCAST &&
6631 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6632 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6633 V1.getOperand(0)
6634 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6635 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6636 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006637 ConstantSDNode *CIdx =
6638 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006639 // If it's foldable, i.e. normal load with single use, we will let code
6640 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006641 if (CIdx && CIdx->getZExtValue() == 0 &&
6642 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006643 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6644 }
6645
6646 return DAG.getNode(ISD::BITCAST, DL, VT,
6647 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6648}
6649
Nadav Rotem154819d2012-04-09 07:45:58 +00006650SDValue
6651X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006652 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6653 EVT VT = Op.getValueType();
6654 DebugLoc dl = Op.getDebugLoc();
6655 SDValue V1 = Op.getOperand(0);
6656 SDValue V2 = Op.getOperand(1);
6657
6658 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006659 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006660
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006661 // Handle splat operations
6662 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006663 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006664 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006665
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006666 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006667 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006668 if (Broadcast.getNode())
6669 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006670
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006671 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006672 if ((Size == 128 && NumElem <= 4) ||
6673 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006674 return SDValue();
6675
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006676 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006677 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006678 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006679
Michael Liaod9d09602012-10-23 17:34:00 +00006680 // Check integer expanding shuffles.
6681 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6682 if (NewOp.getNode())
6683 return NewOp;
6684
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006685 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6686 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006687 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6688 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006689 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6690 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006691 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006692 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006693 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006694 // FIXME: Figure out a cleaner way to do this.
6695 // Try to make use of movq to zero out the top part.
6696 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6697 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6698 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006699 EVT NewVT = NewOp.getValueType();
6700 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6701 NewVT, true, false))
6702 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006703 DAG, Subtarget, dl);
6704 }
6705 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6706 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006707 if (NewOp.getNode()) {
6708 EVT NewVT = NewOp.getValueType();
6709 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6710 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6711 DAG, Subtarget, dl);
6712 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006713 }
6714 }
6715 return SDValue();
6716}
6717
Dan Gohman475871a2008-07-27 21:46:04 +00006718SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006719X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006720 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006721 SDValue V1 = Op.getOperand(0);
6722 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006723 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006724 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006725 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006726 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006727 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006728 bool V1IsSplat = false;
6729 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006730 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006731 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006732 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006733 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006734 bool OptForSize = MF.getFunction()->getFnAttributes().
6735 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006736
Craig Topper3426a3e2011-11-14 06:46:21 +00006737 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006738
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006739 if (V1IsUndef && V2IsUndef)
6740 return DAG.getUNDEF(VT);
6741
6742 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006743
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006744 // Vector shuffle lowering takes 3 steps:
6745 //
6746 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6747 // narrowing and commutation of operands should be handled.
6748 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6749 // shuffle nodes.
6750 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6751 // so the shuffle can be broken into other shuffles and the legalizer can
6752 // try the lowering again.
6753 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006754 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006755 // be matched during isel, all of them must be converted to a target specific
6756 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006757
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006758 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6759 // narrowing and commutation of operands should be handled. The actual code
6760 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006761 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006762 if (NewOp.getNode())
6763 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006764
Craig Topper5aaffa82012-02-19 02:53:47 +00006765 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6766
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006767 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6768 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006769 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006770 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006771 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006772 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006773
Craig Topperdd637ae2012-02-19 05:41:45 +00006774 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006775 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006776 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006777
Craig Topperdd637ae2012-02-19 05:41:45 +00006778 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006779 return getMOVHighToLow(Op, dl, DAG);
6780
6781 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006782 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006783 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006784 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006785
Craig Topper5aaffa82012-02-19 02:53:47 +00006786 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006787 // The actual implementation will match the mask in the if above and then
6788 // during isel it can match several different instructions, not only pshufd
6789 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006790 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6791 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006792
Craig Topper5aaffa82012-02-19 02:53:47 +00006793 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006794
Craig Topperdbd98a42012-02-07 06:28:42 +00006795 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6796 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6797
Craig Topper1accb7e2012-01-10 06:54:16 +00006798 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006799 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6800
Craig Topperb3982da2011-12-31 23:50:21 +00006801 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006802 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006803 }
Eric Christopherfd179292009-08-27 18:07:15 +00006804
Evan Chengf26ffe92008-05-29 08:22:04 +00006805 // Check if this can be converted into a logical shift.
6806 bool isLeft = false;
6807 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006808 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006809 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006810 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006811 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006812 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006813 EVT EltVT = VT.getVectorElementType();
6814 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006815 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006816 }
Eric Christopherfd179292009-08-27 18:07:15 +00006817
Craig Topper5aaffa82012-02-19 02:53:47 +00006818 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006819 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006820 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006821 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006822 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006823 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6824
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006825 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006826 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6827 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006828 }
Eric Christopherfd179292009-08-27 18:07:15 +00006829
Nate Begeman9008ca62009-04-27 18:41:29 +00006830 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006831 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006832 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006833
Craig Topperdd637ae2012-02-19 05:41:45 +00006834 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006835 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006836
Craig Topperdd637ae2012-02-19 05:41:45 +00006837 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006838 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006839
Craig Topperdd637ae2012-02-19 05:41:45 +00006840 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006841 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006842
Craig Topperdd637ae2012-02-19 05:41:45 +00006843 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006844 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006845
Craig Topperdd637ae2012-02-19 05:41:45 +00006846 if (ShouldXformToMOVHLPS(M, VT) ||
6847 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006848 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006849
Evan Chengf26ffe92008-05-29 08:22:04 +00006850 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006851 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006852 EVT EltVT = VT.getVectorElementType();
6853 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006854 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006855 }
Eric Christopherfd179292009-08-27 18:07:15 +00006856
Evan Cheng9eca5e82006-10-25 21:49:50 +00006857 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006858 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6859 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006860 V1IsSplat = isSplatVector(V1.getNode());
6861 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006862
Chris Lattner8a594482007-11-25 00:24:49 +00006863 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006864 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6865 CommuteVectorShuffleMask(M, NumElems);
6866 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006867 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006868 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006869 }
6870
Craig Topperbeabc6c2011-12-05 06:56:46 +00006871 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006872 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006873 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006874 return V1;
6875 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6876 // the instruction selector will not match, so get a canonical MOVL with
6877 // swapped operands to undo the commute.
6878 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006879 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006880
Craig Topperbeabc6c2011-12-05 06:56:46 +00006881 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006882 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006883
Craig Topperbeabc6c2011-12-05 06:56:46 +00006884 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006885 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006886
Evan Cheng9bbbb982006-10-25 20:48:19 +00006887 if (V2IsSplat) {
6888 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006889 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006890 // new vector_shuffle with the corrected mask.p
6891 SmallVector<int, 8> NewMask(M.begin(), M.end());
6892 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006893 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006894 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006895 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006896 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006897 }
6898
Evan Cheng9eca5e82006-10-25 21:49:50 +00006899 if (Commuted) {
6900 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006901 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006902 CommuteVectorShuffleMask(M, NumElems);
6903 std::swap(V1, V2);
6904 std::swap(V1IsSplat, V2IsSplat);
6905 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006906
Craig Topper39a9e482012-02-11 06:24:48 +00006907 if (isUNPCKLMask(M, VT, HasAVX2))
6908 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006909
Craig Topper39a9e482012-02-11 06:24:48 +00006910 if (isUNPCKHMask(M, VT, HasAVX2))
6911 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006912 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006913
Nate Begeman9008ca62009-04-27 18:41:29 +00006914 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006915 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006916 return CommuteVectorShuffle(SVOp, DAG);
6917
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006918 // The checks below are all present in isShuffleMaskLegal, but they are
6919 // inlined here right now to enable us to directly emit target specific
6920 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006921
Craig Topper0e2037b2012-01-20 05:53:00 +00006922 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006923 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006924 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006925 DAG);
6926
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006927 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6928 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006929 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006930 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006931 }
6932
Craig Toppera9a568a2012-05-02 08:03:44 +00006933 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006934 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006935 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006936 DAG);
6937
Craig Toppera9a568a2012-05-02 08:03:44 +00006938 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006939 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006940 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006941 DAG);
6942
Craig Topper1a7700a2012-01-19 08:19:12 +00006943 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006944 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006945 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006946
Craig Topper94438ba2011-12-16 08:06:31 +00006947 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006948 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006949 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006950 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006951
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006952 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006953 // Generate target specific nodes for 128 or 256-bit shuffles only
6954 // supported in the AVX instruction set.
6955 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006956
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006957 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006958 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006959 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6960
Craig Topper70b883b2011-11-28 10:14:51 +00006961 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006962 if (isVPERMILPMask(M, VT, HasAVX)) {
6963 if (HasAVX2 && VT == MVT::v8i32)
6964 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006965 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006966 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006967 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006968 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006969
Craig Topper70b883b2011-11-28 10:14:51 +00006970 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006971 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006972 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006973 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006974
Craig Topper1842ba02012-04-23 06:38:28 +00006975 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006976 if (BlendOp.getNode())
6977 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006978
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006979 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006980 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006981 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006982 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006983 }
Craig Topper92040742012-04-16 06:43:40 +00006984 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6985 &permclMask[0], 8);
6986 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006987 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006988 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006989 }
Craig Topper095c5282012-04-15 23:48:57 +00006990
Craig Topper8325c112012-04-16 00:41:45 +00006991 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6992 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006993 getShuffleCLImmediate(SVOp), DAG);
6994
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006995
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006996 //===--------------------------------------------------------------------===//
6997 // Since no target specific shuffle was selected for this generic one,
6998 // lower it into other known shuffles. FIXME: this isn't true yet, but
6999 // this is the plan.
7000 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007001
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007002 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7003 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007004 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007005 if (NewOp.getNode())
7006 return NewOp;
7007 }
7008
7009 if (VT == MVT::v16i8) {
7010 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7011 if (NewOp.getNode())
7012 return NewOp;
7013 }
7014
Elena Demikhovsky41789462012-09-06 12:42:01 +00007015 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007016 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007017 if (NewOp.getNode())
7018 return NewOp;
7019 }
7020
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007021 // Handle all 128-bit wide vectors with 4 elements, and match them with
7022 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007023 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007024 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7025
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007026 // Handle general 256-bit shuffles
7027 if (VT.is256BitVector())
7028 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7029
Dan Gohman475871a2008-07-27 21:46:04 +00007030 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007031}
7032
Dan Gohman475871a2008-07-27 21:46:04 +00007033SDValue
7034X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007035 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007036 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007037 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007038
Craig Topper7a9a28b2012-08-12 02:23:29 +00007039 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007040 return SDValue();
7041
Duncan Sands83ec4b62008-06-06 12:08:01 +00007042 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007043 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007044 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007046 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007047 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007048 }
7049
7050 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007051 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7052 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7053 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007054 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7055 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007056 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007058 Op.getOperand(0)),
7059 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007060 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007061 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007063 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007064 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007065 }
7066
7067 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007068 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7069 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007070 // result has a single use which is a store or a bitcast to i32. And in
7071 // the case of a store, it's not worth it if the index is a constant 0,
7072 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007073 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007074 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007075 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007076 if ((User->getOpcode() != ISD::STORE ||
7077 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7078 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007079 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007081 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007083 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007084 Op.getOperand(0)),
7085 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007086 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007087 }
7088
7089 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007090 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007091 if (isa<ConstantSDNode>(Op.getOperand(1)))
7092 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007093 }
Dan Gohman475871a2008-07-27 21:46:04 +00007094 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007095}
7096
7097
Dan Gohman475871a2008-07-27 21:46:04 +00007098SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007099X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7100 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007101 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007102 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007103
David Greene74a579d2011-02-10 16:57:36 +00007104 SDValue Vec = Op.getOperand(0);
7105 EVT VecVT = Vec.getValueType();
7106
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007107 // If this is a 256-bit vector result, first extract the 128-bit vector and
7108 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007109 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007110 DebugLoc dl = Op.getNode()->getDebugLoc();
7111 unsigned NumElems = VecVT.getVectorNumElements();
7112 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007113 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7114
7115 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007116 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007117
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007118 if (IdxVal >= NumElems/2)
7119 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007120 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007121 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007122 }
7123
Craig Topper7a9a28b2012-08-12 02:23:29 +00007124 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007125
Craig Topperd0a31172012-01-10 06:37:29 +00007126 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007127 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007128 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007129 return Res;
7130 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007131
Owen Andersone50ed302009-08-10 22:56:29 +00007132 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007133 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007134 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007135 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007136 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007137 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007138 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007139 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7140 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007141 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007142 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007143 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007144 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007145 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007146 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007147 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007148 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007149 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007150 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007151 }
7152
7153 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007154 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007155 if (Idx == 0)
7156 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007157
Evan Cheng0db9fe62006-04-25 20:13:52 +00007158 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007159 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007160 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007161 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007162 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007163 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007164 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007165 }
7166
7167 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007168 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7169 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7170 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007171 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007172 if (Idx == 0)
7173 return Op;
7174
7175 // UNPCKHPD the element to the lowest double word, then movsd.
7176 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7177 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007178 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007179 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007180 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007181 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007182 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007183 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007184 }
7185
Dan Gohman475871a2008-07-27 21:46:04 +00007186 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007187}
7188
Dan Gohman475871a2008-07-27 21:46:04 +00007189SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007190X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7191 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007192 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007193 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007194 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007195
Dan Gohman475871a2008-07-27 21:46:04 +00007196 SDValue N0 = Op.getOperand(0);
7197 SDValue N1 = Op.getOperand(1);
7198 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007199
Craig Topper7a9a28b2012-08-12 02:23:29 +00007200 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007201 return SDValue();
7202
Dan Gohman8a55ce42009-09-23 21:02:20 +00007203 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007204 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007205 unsigned Opc;
7206 if (VT == MVT::v8i16)
7207 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007208 else if (VT == MVT::v16i8)
7209 Opc = X86ISD::PINSRB;
7210 else
7211 Opc = X86ISD::PINSRB;
7212
Nate Begeman14d12ca2008-02-11 04:19:36 +00007213 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7214 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007215 if (N1.getValueType() != MVT::i32)
7216 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7217 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007218 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007219 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007220 }
7221
7222 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007223 // Bits [7:6] of the constant are the source select. This will always be
7224 // zero here. The DAG Combiner may combine an extract_elt index into these
7225 // bits. For example (insert (extract, 3), 2) could be matched by putting
7226 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007227 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007228 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007229 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007230 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007231 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007232 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007233 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007234 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007235 }
7236
7237 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007238 // PINSR* works with constant index.
7239 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007240 }
Dan Gohman475871a2008-07-27 21:46:04 +00007241 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007242}
7243
Dan Gohman475871a2008-07-27 21:46:04 +00007244SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007245X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007246 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007247 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007248
David Greene6b381262011-02-09 15:32:06 +00007249 DebugLoc dl = Op.getDebugLoc();
7250 SDValue N0 = Op.getOperand(0);
7251 SDValue N1 = Op.getOperand(1);
7252 SDValue N2 = Op.getOperand(2);
7253
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007254 // If this is a 256-bit vector result, first extract the 128-bit vector,
7255 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007256 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007257 if (!isa<ConstantSDNode>(N2))
7258 return SDValue();
7259
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007260 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007261 unsigned NumElems = VT.getVectorNumElements();
7262 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007263 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007264
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007265 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007266 bool Upper = IdxVal >= NumElems/2;
7267 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7268 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007269
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007270 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007271 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007272 }
7273
Craig Topperd0a31172012-01-10 06:37:29 +00007274 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007275 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7276
Dan Gohman8a55ce42009-09-23 21:02:20 +00007277 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007278 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007279
Dan Gohman8a55ce42009-09-23 21:02:20 +00007280 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007281 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7282 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007283 if (N1.getValueType() != MVT::i32)
7284 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7285 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007286 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007287 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007288 }
Dan Gohman475871a2008-07-27 21:46:04 +00007289 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007290}
7291
Craig Topper55b24052012-09-11 06:15:32 +00007292static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007293 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007294 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007295 EVT OpVT = Op.getValueType();
7296
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007297 // If this is a 256-bit vector result, first insert into a 128-bit
7298 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007299 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007300 // Insert into a 128-bit vector.
7301 EVT VT128 = EVT::getVectorVT(*Context,
7302 OpVT.getVectorElementType(),
7303 OpVT.getVectorNumElements() / 2);
7304
7305 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7306
7307 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007308 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007309 }
7310
Craig Topperd77d2fe2012-04-29 20:22:05 +00007311 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007312 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007313 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007314
Owen Anderson825b72b2009-08-11 20:47:22 +00007315 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007316 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007317 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007318 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007319}
7320
David Greene91585092011-01-26 15:38:49 +00007321// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7322// a simple subregister reference or explicit instructions to grab
7323// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007324static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7325 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007326 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007327 DebugLoc dl = Op.getNode()->getDebugLoc();
7328 SDValue Vec = Op.getNode()->getOperand(0);
7329 SDValue Idx = Op.getNode()->getOperand(1);
7330
Craig Topper7a9a28b2012-08-12 02:23:29 +00007331 if (Op.getNode()->getValueType(0).is128BitVector() &&
7332 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007333 isa<ConstantSDNode>(Idx)) {
7334 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7335 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007336 }
David Greene91585092011-01-26 15:38:49 +00007337 }
7338 return SDValue();
7339}
7340
David Greenecfe33c42011-01-26 19:13:22 +00007341// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7342// simple superregister reference or explicit instructions to insert
7343// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007344static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7345 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007346 if (Subtarget->hasAVX()) {
7347 DebugLoc dl = Op.getNode()->getDebugLoc();
7348 SDValue Vec = Op.getNode()->getOperand(0);
7349 SDValue SubVec = Op.getNode()->getOperand(1);
7350 SDValue Idx = Op.getNode()->getOperand(2);
7351
Craig Topper7a9a28b2012-08-12 02:23:29 +00007352 if (Op.getNode()->getValueType(0).is256BitVector() &&
7353 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007354 isa<ConstantSDNode>(Idx)) {
7355 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7356 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007357 }
7358 }
7359 return SDValue();
7360}
7361
Bill Wendling056292f2008-09-16 21:48:12 +00007362// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7363// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7364// one of the above mentioned nodes. It has to be wrapped because otherwise
7365// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7366// be used to form addressing mode. These wrapped nodes will be selected
7367// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007368SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007369X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007370 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007371
Chris Lattner41621a22009-06-26 19:22:52 +00007372 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7373 // global base reg.
7374 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007375 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007376 CodeModel::Model M = getTargetMachine().getCodeModel();
7377
Chris Lattner4f066492009-07-11 20:29:19 +00007378 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007379 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007380 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007381 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007382 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007383 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007384 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007385
Evan Cheng1606e8e2009-03-13 07:51:59 +00007386 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007387 CP->getAlignment(),
7388 CP->getOffset(), OpFlag);
7389 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007390 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007391 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007392 if (OpFlag) {
7393 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007394 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007395 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007396 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007397 }
7398
7399 return Result;
7400}
7401
Dan Gohmand858e902010-04-17 15:26:15 +00007402SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007403 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007404
Chris Lattner18c59872009-06-27 04:16:01 +00007405 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7406 // global base reg.
7407 unsigned char OpFlag = 0;
7408 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007409 CodeModel::Model M = getTargetMachine().getCodeModel();
7410
Chris Lattner4f066492009-07-11 20:29:19 +00007411 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007412 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007413 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007414 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007415 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007416 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007417 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007418
Chris Lattner18c59872009-06-27 04:16:01 +00007419 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7420 OpFlag);
7421 DebugLoc DL = JT->getDebugLoc();
7422 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007423
Chris Lattner18c59872009-06-27 04:16:01 +00007424 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007425 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007426 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7427 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007428 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007429 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007430
Chris Lattner18c59872009-06-27 04:16:01 +00007431 return Result;
7432}
7433
7434SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007435X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007436 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007437
Chris Lattner18c59872009-06-27 04:16:01 +00007438 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7439 // global base reg.
7440 unsigned char OpFlag = 0;
7441 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007442 CodeModel::Model M = getTargetMachine().getCodeModel();
7443
Chris Lattner4f066492009-07-11 20:29:19 +00007444 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007445 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7446 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7447 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007448 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007449 } else if (Subtarget->isPICStyleGOT()) {
7450 OpFlag = X86II::MO_GOT;
7451 } else if (Subtarget->isPICStyleStubPIC()) {
7452 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7453 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7454 OpFlag = X86II::MO_DARWIN_NONLAZY;
7455 }
Eric Christopherfd179292009-08-27 18:07:15 +00007456
Chris Lattner18c59872009-06-27 04:16:01 +00007457 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007458
Chris Lattner18c59872009-06-27 04:16:01 +00007459 DebugLoc DL = Op.getDebugLoc();
7460 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007461
7462
Chris Lattner18c59872009-06-27 04:16:01 +00007463 // With PIC, the address is actually $g + Offset.
7464 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007465 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007466 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7467 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007468 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007469 Result);
7470 }
Eric Christopherfd179292009-08-27 18:07:15 +00007471
Eli Friedman586272d2011-08-11 01:48:05 +00007472 // For symbols that require a load from a stub to get the address, emit the
7473 // load.
7474 if (isGlobalStubReference(OpFlag))
7475 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007476 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007477
Chris Lattner18c59872009-06-27 04:16:01 +00007478 return Result;
7479}
7480
Dan Gohman475871a2008-07-27 21:46:04 +00007481SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007482X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007483 // Create the TargetBlockAddressAddress node.
7484 unsigned char OpFlags =
7485 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007486 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007487 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007488 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007489 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007490 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7491 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007492
Dan Gohmanf705adb2009-10-30 01:28:02 +00007493 if (Subtarget->isPICStyleRIPRel() &&
7494 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007495 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7496 else
7497 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007498
Dan Gohman29cbade2009-11-20 23:18:13 +00007499 // With PIC, the address is actually $g + Offset.
7500 if (isGlobalRelativeToPICBase(OpFlags)) {
7501 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7502 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7503 Result);
7504 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007505
7506 return Result;
7507}
7508
7509SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007510X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007511 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007512 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007513 // Create the TargetGlobalAddress node, folding in the constant
7514 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007515 unsigned char OpFlags =
7516 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007517 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007518 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007519 if (OpFlags == X86II::MO_NO_FLAG &&
7520 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007521 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007522 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007523 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007524 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007525 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007526 }
Eric Christopherfd179292009-08-27 18:07:15 +00007527
Chris Lattner4f066492009-07-11 20:29:19 +00007528 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007529 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007530 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7531 else
7532 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007533
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007534 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007535 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007536 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7537 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007538 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007539 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007540
Chris Lattner36c25012009-07-10 07:34:39 +00007541 // For globals that require a load from a stub to get the address, emit the
7542 // load.
7543 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007544 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007545 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007546
Dan Gohman6520e202008-10-18 02:06:02 +00007547 // If there was a non-zero offset that we didn't fold, create an explicit
7548 // addition for it.
7549 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007550 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007551 DAG.getConstant(Offset, getPointerTy()));
7552
Evan Cheng0db9fe62006-04-25 20:13:52 +00007553 return Result;
7554}
7555
Evan Chengda43bcf2008-09-24 00:05:32 +00007556SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007557X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007558 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007559 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007560 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007561}
7562
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007563static SDValue
7564GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007565 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007566 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007567 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007568 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007569 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007570 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007571 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007572 GA->getOffset(),
7573 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007574
7575 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7576 : X86ISD::TLSADDR;
7577
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007578 if (InFlag) {
7579 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007580 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007581 } else {
7582 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007583 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007584 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007585
7586 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007587 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007588
Rafael Espindola15f1b662009-04-24 12:59:40 +00007589 SDValue Flag = Chain.getValue(1);
7590 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007591}
7592
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007593// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007594static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007595LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007596 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007597 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007598 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7599 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007600 DAG.getNode(X86ISD::GlobalBaseReg,
7601 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007602 InFlag = Chain.getValue(1);
7603
Chris Lattnerb903bed2009-06-26 21:20:29 +00007604 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007605}
7606
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007607// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007608static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007609LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007610 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007611 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7612 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007613}
7614
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007615static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7616 SelectionDAG &DAG,
7617 const EVT PtrVT,
7618 bool is64Bit) {
7619 DebugLoc dl = GA->getDebugLoc();
7620
7621 // Get the start address of the TLS block for this module.
7622 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7623 .getInfo<X86MachineFunctionInfo>();
7624 MFI->incNumLocalDynamicTLSAccesses();
7625
7626 SDValue Base;
7627 if (is64Bit) {
7628 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7629 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7630 } else {
7631 SDValue InFlag;
7632 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7633 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7634 InFlag = Chain.getValue(1);
7635 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7636 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7637 }
7638
7639 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7640 // of Base.
7641
7642 // Build x@dtpoff.
7643 unsigned char OperandFlags = X86II::MO_DTPOFF;
7644 unsigned WrapperKind = X86ISD::Wrapper;
7645 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7646 GA->getValueType(0),
7647 GA->getOffset(), OperandFlags);
7648 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7649
7650 // Add x@dtpoff with the base.
7651 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7652}
7653
Hans Wennborg228756c2012-05-11 10:11:01 +00007654// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007655static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007656 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007657 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007658 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007659
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007660 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7661 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7662 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007663
Michael J. Spencerec38de22010-10-10 22:04:20 +00007664 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007665 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007666 MachinePointerInfo(Ptr),
7667 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007668
Chris Lattnerb903bed2009-06-26 21:20:29 +00007669 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007670 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7671 // initialexec.
7672 unsigned WrapperKind = X86ISD::Wrapper;
7673 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007674 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007675 } else if (model == TLSModel::InitialExec) {
7676 if (is64Bit) {
7677 OperandFlags = X86II::MO_GOTTPOFF;
7678 WrapperKind = X86ISD::WrapperRIP;
7679 } else {
7680 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7681 }
Chris Lattner18c59872009-06-27 04:16:01 +00007682 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007683 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007684 }
Eric Christopherfd179292009-08-27 18:07:15 +00007685
Hans Wennborg228756c2012-05-11 10:11:01 +00007686 // emit "addl x@ntpoff,%eax" (local exec)
7687 // or "addl x@indntpoff,%eax" (initial exec)
7688 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007689 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007690 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007691 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007692 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007693
Hans Wennborg228756c2012-05-11 10:11:01 +00007694 if (model == TLSModel::InitialExec) {
7695 if (isPIC && !is64Bit) {
7696 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7697 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7698 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007699 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007700
7701 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7702 MachinePointerInfo::getGOT(), false, false, false,
7703 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007704 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007705
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007706 // The address of the thread local variable is the add of the thread
7707 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007708 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007709}
7710
Dan Gohman475871a2008-07-27 21:46:04 +00007711SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007712X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007713
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007714 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007715 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007716
Eric Christopher30ef0e52010-06-03 04:07:48 +00007717 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007718 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007719
Eric Christopher30ef0e52010-06-03 04:07:48 +00007720 switch (model) {
7721 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007722 if (Subtarget->is64Bit())
7723 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7724 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007725 case TLSModel::LocalDynamic:
7726 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7727 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007728 case TLSModel::InitialExec:
7729 case TLSModel::LocalExec:
7730 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007731 Subtarget->is64Bit(),
7732 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007733 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007734 llvm_unreachable("Unknown TLS model.");
7735 }
7736
7737 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007738 // Darwin only has one model of TLS. Lower to that.
7739 unsigned char OpFlag = 0;
7740 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7741 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007742
Eric Christopher30ef0e52010-06-03 04:07:48 +00007743 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7744 // global base reg.
7745 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7746 !Subtarget->is64Bit();
7747 if (PIC32)
7748 OpFlag = X86II::MO_TLVP_PIC_BASE;
7749 else
7750 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007751 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007752 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007753 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007754 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007755 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007756
Eric Christopher30ef0e52010-06-03 04:07:48 +00007757 // With PIC32, the address is actually $g + Offset.
7758 if (PIC32)
7759 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7760 DAG.getNode(X86ISD::GlobalBaseReg,
7761 DebugLoc(), getPointerTy()),
7762 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007763
Eric Christopher30ef0e52010-06-03 04:07:48 +00007764 // Lowering the machine isd will make sure everything is in the right
7765 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007766 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007767 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007768 SDValue Args[] = { Chain, Offset };
7769 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007770
Eric Christopher30ef0e52010-06-03 04:07:48 +00007771 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7772 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7773 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007774
Eric Christopher30ef0e52010-06-03 04:07:48 +00007775 // And our return value (tls address) is in the standard call return value
7776 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007777 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007778 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7779 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007780 }
7781
7782 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007783 // Just use the implicit TLS architecture
7784 // Need to generate someting similar to:
7785 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7786 // ; from TEB
7787 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7788 // mov rcx, qword [rdx+rcx*8]
7789 // mov eax, .tls$:tlsvar
7790 // [rax+rcx] contains the address
7791 // Windows 64bit: gs:0x58
7792 // Windows 32bit: fs:__tls_array
7793
7794 // If GV is an alias then use the aliasee for determining
7795 // thread-localness.
7796 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7797 GV = GA->resolveAliasedGlobal(false);
7798 DebugLoc dl = GA->getDebugLoc();
7799 SDValue Chain = DAG.getEntryNode();
7800
7801 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7802 // %gs:0x58 (64-bit).
7803 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7804 ? Type::getInt8PtrTy(*DAG.getContext(),
7805 256)
7806 : Type::getInt32PtrTy(*DAG.getContext(),
7807 257));
7808
7809 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7810 Subtarget->is64Bit()
7811 ? DAG.getIntPtrConstant(0x58)
7812 : DAG.getExternalSymbol("_tls_array",
7813 getPointerTy()),
7814 MachinePointerInfo(Ptr),
7815 false, false, false, 0);
7816
7817 // Load the _tls_index variable
7818 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7819 if (Subtarget->is64Bit())
7820 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7821 IDX, MachinePointerInfo(), MVT::i32,
7822 false, false, 0);
7823 else
7824 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7825 false, false, false, 0);
7826
Micah Villmow2c39b152012-10-15 16:24:29 +00007827 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize(0)),
Craig Topper0fbf3642012-04-23 03:28:34 +00007828 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007829 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7830
7831 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7832 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7833 false, false, false, 0);
7834
7835 // Get the offset of start of .tls section
7836 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7837 GA->getValueType(0),
7838 GA->getOffset(), X86II::MO_SECREL);
7839 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7840
7841 // The address of the thread local variable is the add of the thread
7842 // pointer with the offset of the variable.
7843 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007844 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007845
David Blaikie4d6ccb52012-01-20 21:51:11 +00007846 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007847}
7848
Evan Cheng0db9fe62006-04-25 20:13:52 +00007849
Chad Rosierb90d2a92012-01-03 23:19:12 +00007850/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7851/// and take a 2 x i32 value to shift plus a shift amount.
7852SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007853 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007854 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007855 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007856 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007857 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007858 SDValue ShOpLo = Op.getOperand(0);
7859 SDValue ShOpHi = Op.getOperand(1);
7860 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007861 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007863 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007864
Dan Gohman475871a2008-07-27 21:46:04 +00007865 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007866 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007867 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7868 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007869 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007870 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7871 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007872 }
Evan Chenge3413162006-01-09 18:33:28 +00007873
Owen Anderson825b72b2009-08-11 20:47:22 +00007874 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7875 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007876 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007877 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007878
Dan Gohman475871a2008-07-27 21:46:04 +00007879 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007881 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7882 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007883
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007884 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007885 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7886 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007887 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007888 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7889 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007890 }
7891
Dan Gohman475871a2008-07-27 21:46:04 +00007892 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007893 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007894}
Evan Chenga3195e82006-01-12 22:54:21 +00007895
Dan Gohmand858e902010-04-17 15:26:15 +00007896SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7897 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007898 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007899
Dale Johannesen0488fb62010-09-30 23:57:10 +00007900 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007901 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007902
Owen Anderson825b72b2009-08-11 20:47:22 +00007903 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007904 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007905
Eli Friedman36df4992009-05-27 00:47:34 +00007906 // These are really Legal; return the operand so the caller accepts it as
7907 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007909 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007911 Subtarget->is64Bit()) {
7912 return Op;
7913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007914
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007915 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007916 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007917 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007918 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007919 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007920 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007921 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007922 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007923 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007924 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7925}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007926
Owen Andersone50ed302009-08-10 22:56:29 +00007927SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007928 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007929 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007930 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007931 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007932 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007933 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007934 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007935 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007936 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007938
Chris Lattner492a43e2010-09-22 01:28:21 +00007939 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007940
Stuart Hastings84be9582011-06-02 15:57:11 +00007941 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7942 MachineMemOperand *MMO;
7943 if (FI) {
7944 int SSFI = FI->getIndex();
7945 MMO =
7946 DAG.getMachineFunction()
7947 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7948 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7949 } else {
7950 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7951 StackSlot = StackSlot.getOperand(1);
7952 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007953 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007954 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7955 X86ISD::FILD, DL,
7956 Tys, Ops, array_lengthof(Ops),
7957 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007958
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007959 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007961 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962
7963 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7964 // shouldn't be necessary except that RFP cannot be live across
7965 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007966 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007967 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7968 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007969 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007970 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007971 SDValue Ops[] = {
7972 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7973 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007974 MachineMemOperand *MMO =
7975 DAG.getMachineFunction()
7976 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007977 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007978
Chris Lattner492a43e2010-09-22 01:28:21 +00007979 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7980 Ops, array_lengthof(Ops),
7981 Op.getValueType(), MMO);
7982 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007983 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007984 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007985 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007986
Evan Cheng0db9fe62006-04-25 20:13:52 +00007987 return Result;
7988}
7989
Bill Wendling8b8a6362009-01-17 03:56:04 +00007990// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007991SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7992 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007993 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007994 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007995 movq %rax, %xmm0
7996 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7997 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7998 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007999 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008000 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008001 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008002 addpd %xmm1, %xmm0
8003 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008004 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008005
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008006 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008007 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008008
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008009 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008010 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8011 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008012 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008013
Chris Lattner97484792012-01-25 09:56:22 +00008014 SmallVector<Constant*,2> CV1;
8015 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008016 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008017 CV1.push_back(
8018 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8019 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008020 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008021
Bill Wendling397ae212012-01-05 02:13:20 +00008022 // Load the 64-bit value into an XMM register.
8023 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8024 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008025 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008026 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008027 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008028 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8029 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8030 CLod0);
8031
Owen Anderson825b72b2009-08-11 20:47:22 +00008032 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008033 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008034 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008035 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008036 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008037 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008038
Craig Topperd0a31172012-01-10 06:37:29 +00008039 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008040 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8041 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8042 } else {
8043 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8044 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8045 S2F, 0x4E, DAG);
8046 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8047 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8048 Sub);
8049 }
8050
8051 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008052 DAG.getIntPtrConstant(0));
8053}
8054
Bill Wendling8b8a6362009-01-17 03:56:04 +00008055// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008056SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8057 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008058 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008059 // FP constant to bias correct the final result.
8060 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008061 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008062
8063 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008064 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008065 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008066
Eli Friedmanf3704762011-08-29 21:15:46 +00008067 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008068 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008069
Owen Anderson825b72b2009-08-11 20:47:22 +00008070 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008071 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008072 DAG.getIntPtrConstant(0));
8073
8074 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008075 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008076 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008077 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008078 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008079 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008080 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 MVT::v2f64, Bias)));
8082 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008083 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008084 DAG.getIntPtrConstant(0));
8085
8086 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008087 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008088
8089 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008090 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008091
Craig Topper69947b92012-04-23 06:57:04 +00008092 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008093 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008094 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008095 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008096 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008097
8098 // Handle final rounding.
8099 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008100}
8101
Michael Liaoa7554632012-10-23 17:36:08 +00008102SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8103 SelectionDAG &DAG) const {
8104 SDValue N0 = Op.getOperand(0);
8105 EVT SVT = N0.getValueType();
8106 DebugLoc dl = Op.getDebugLoc();
8107
8108 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8109 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8110 "Custom UINT_TO_FP is not supported!");
8111
8112 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8113 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8114 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8115}
8116
Dan Gohmand858e902010-04-17 15:26:15 +00008117SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8118 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008119 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008120 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008121
Michael Liaoa7554632012-10-23 17:36:08 +00008122 if (Op.getValueType().isVector())
8123 return lowerUINT_TO_FP_vec(Op, DAG);
8124
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008125 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008126 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8127 // the optimization here.
8128 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008129 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008130
Owen Andersone50ed302009-08-10 22:56:29 +00008131 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008132 EVT DstVT = Op.getValueType();
8133 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008134 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008135 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008136 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008137 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008138 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008139
8140 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008141 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008142 if (SrcVT == MVT::i32) {
8143 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8144 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8145 getPointerTy(), StackSlot, WordOff);
8146 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008147 StackSlot, MachinePointerInfo(),
8148 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008149 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008150 OffsetSlot, MachinePointerInfo(),
8151 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008152 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8153 return Fild;
8154 }
8155
8156 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8157 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008158 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008159 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008160 // For i64 source, we need to add the appropriate power of 2 if the input
8161 // was negative. This is the same as the optimization in
8162 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8163 // we must be careful to do the computation in x87 extended precision, not
8164 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008165 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8166 MachineMemOperand *MMO =
8167 DAG.getMachineFunction()
8168 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8169 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008170
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008171 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8172 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008173 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8174 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008175
8176 APInt FF(32, 0x5F800000ULL);
8177
8178 // Check whether the sign bit is set.
8179 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8180 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8181 ISD::SETLT);
8182
8183 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8184 SDValue FudgePtr = DAG.getConstantPool(
8185 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8186 getPointerTy());
8187
8188 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8189 SDValue Zero = DAG.getIntPtrConstant(0);
8190 SDValue Four = DAG.getIntPtrConstant(4);
8191 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8192 Zero, Four);
8193 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8194
8195 // Load the value out, extending it from f32 to f80.
8196 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008197 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008198 FudgePtr, MachinePointerInfo::getConstantPool(),
8199 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008200 // Extend everything to 80 bits to force it to be done on x87.
8201 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8202 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008203}
8204
Dan Gohman475871a2008-07-27 21:46:04 +00008205std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008206FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008207 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008208
Owen Andersone50ed302009-08-10 22:56:29 +00008209 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008210
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008211 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008212 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8213 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008214 }
8215
Owen Anderson825b72b2009-08-11 20:47:22 +00008216 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8217 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008218 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008219
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008220 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008221 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008222 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008223 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008224 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008225 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008226 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008227 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008228
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008229 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8230 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008231 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008232 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008233 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008234 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008235
Evan Cheng0db9fe62006-04-25 20:13:52 +00008236 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008237 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8238 Opc = X86ISD::WIN_FTOL;
8239 else
8240 switch (DstTy.getSimpleVT().SimpleTy) {
8241 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8242 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8243 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8244 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8245 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008246
Dan Gohman475871a2008-07-27 21:46:04 +00008247 SDValue Chain = DAG.getEntryNode();
8248 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008249 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008250 // FIXME This causes a redundant load/store if the SSE-class value is already
8251 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008252 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008253 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008254 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008255 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008256 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008258 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008259 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008260 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008261
Chris Lattner492a43e2010-09-22 01:28:21 +00008262 MachineMemOperand *MMO =
8263 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8264 MachineMemOperand::MOLoad, MemSize, MemSize);
8265 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8266 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008267 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008268 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008269 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8270 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008271
Chris Lattner07290932010-09-22 01:05:16 +00008272 MachineMemOperand *MMO =
8273 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8274 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008275
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008276 if (Opc != X86ISD::WIN_FTOL) {
8277 // Build the FP_TO_INT*_IN_MEM
8278 SDValue Ops[] = { Chain, Value, StackSlot };
8279 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8280 Ops, 3, DstTy, MMO);
8281 return std::make_pair(FIST, StackSlot);
8282 } else {
8283 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8284 DAG.getVTList(MVT::Other, MVT::Glue),
8285 Chain, Value);
8286 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8287 MVT::i32, ftol.getValue(1));
8288 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8289 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008290 SDValue Ops[] = { eax, edx };
8291 SDValue pair = IsReplace
8292 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8293 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008294 return std::make_pair(pair, SDValue());
8295 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008296}
8297
Michael Liaoa7554632012-10-23 17:36:08 +00008298SDValue X86TargetLowering::lowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const {
8299 DebugLoc DL = Op.getDebugLoc();
8300 EVT VT = Op.getValueType();
8301 SDValue In = Op.getOperand(0);
8302 EVT SVT = In.getValueType();
8303
8304 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8305 VT.getVectorNumElements() != SVT.getVectorNumElements())
8306 return SDValue();
8307
8308 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8309
8310 // AVX2 has better support of integer extending.
8311 if (Subtarget->hasAVX2())
8312 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8313
8314 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8315 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8316 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
8317 DAG.getVectorShuffle(MVT::v8i16, DL, In, DAG.getUNDEF(MVT::v8i16), &Mask[0]));
8318
8319 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8320}
8321
Michael Liaobedcbd42012-10-16 18:14:11 +00008322SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8323 DebugLoc DL = Op.getDebugLoc();
8324 EVT VT = Op.getValueType();
8325 EVT SVT = Op.getOperand(0).getValueType();
8326
8327 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8328 VT.getVectorNumElements() != SVT.getVectorNumElements())
8329 return SDValue();
8330
8331 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8332
8333 unsigned NumElems = VT.getVectorNumElements();
8334 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8335 NumElems * 2);
8336
8337 SDValue In = Op.getOperand(0);
8338 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8339 // Prepare truncation shuffle mask
8340 for (unsigned i = 0; i != NumElems; ++i)
8341 MaskVec[i] = i * 2;
8342 SDValue V = DAG.getVectorShuffle(NVT, DL,
8343 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8344 DAG.getUNDEF(NVT), &MaskVec[0]);
8345 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8346 DAG.getIntPtrConstant(0));
8347}
8348
Dan Gohmand858e902010-04-17 15:26:15 +00008349SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8350 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008351 if (Op.getValueType().isVector()) {
8352 if (Op.getValueType() == MVT::v8i16)
8353 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8354 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8355 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008356 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008357 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008358
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008359 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8360 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008361 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008362 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8363 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008364
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008365 if (StackSlot.getNode())
8366 // Load the result.
8367 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8368 FIST, StackSlot, MachinePointerInfo(),
8369 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008370
8371 // The node is the result.
8372 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008373}
8374
Dan Gohmand858e902010-04-17 15:26:15 +00008375SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8376 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008377 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8378 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008379 SDValue FIST = Vals.first, StackSlot = Vals.second;
8380 assert(FIST.getNode() && "Unexpected failure");
8381
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008382 if (StackSlot.getNode())
8383 // Load the result.
8384 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8385 FIST, StackSlot, MachinePointerInfo(),
8386 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008387
8388 // The node is the result.
8389 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008390}
8391
Michael Liao9d796db2012-10-10 16:32:15 +00008392SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8393 SelectionDAG &DAG) const {
8394 DebugLoc DL = Op.getDebugLoc();
8395 EVT VT = Op.getValueType();
8396 SDValue In = Op.getOperand(0);
8397 EVT SVT = In.getValueType();
8398
8399 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8400
8401 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8402 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8403 In, DAG.getUNDEF(SVT)));
8404}
8405
Craig Topper43620672012-09-08 07:31:51 +00008406SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008407 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008408 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008409 EVT VT = Op.getValueType();
8410 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008411 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8412 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008413 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008414 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008415 }
Craig Topper43620672012-09-08 07:31:51 +00008416 Constant *C;
8417 if (EltVT == MVT::f64)
8418 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8419 else
8420 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8421 C = ConstantVector::getSplat(NumElts, C);
8422 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8423 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008424 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008425 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008426 false, false, false, Alignment);
8427 if (VT.isVector()) {
8428 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8429 return DAG.getNode(ISD::BITCAST, dl, VT,
8430 DAG.getNode(ISD::AND, dl, ANDVT,
8431 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8432 Op.getOperand(0)),
8433 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8434 }
Dale Johannesenace16102009-02-03 19:33:06 +00008435 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008436}
8437
Dan Gohmand858e902010-04-17 15:26:15 +00008438SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008439 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008440 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008441 EVT VT = Op.getValueType();
8442 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008443 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8444 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008445 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008446 NumElts = VT.getVectorNumElements();
8447 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008448 Constant *C;
8449 if (EltVT == MVT::f64)
8450 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8451 else
8452 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8453 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008454 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8455 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008456 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008457 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008458 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008459 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008460 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008461 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008462 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008463 DAG.getNode(ISD::BITCAST, dl, XORVT,
8464 Op.getOperand(0)),
8465 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008466 }
Craig Topper69947b92012-04-23 06:57:04 +00008467
8468 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008469}
8470
Dan Gohmand858e902010-04-17 15:26:15 +00008471SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008472 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008473 SDValue Op0 = Op.getOperand(0);
8474 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008475 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008476 EVT VT = Op.getValueType();
8477 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008478
8479 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008480 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008481 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008482 SrcVT = VT;
8483 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008484 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008485 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008486 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008487 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008488 }
8489
8490 // At this point the operands and the result should have the same
8491 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008492
Evan Cheng68c47cb2007-01-05 07:55:56 +00008493 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008494 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008495 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008496 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8497 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008498 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008499 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8500 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8501 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8502 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008503 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008504 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008505 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008506 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008507 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008508 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008509 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008510
8511 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008512 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008513 // Op0 is MVT::f32, Op1 is MVT::f64.
8514 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8515 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8516 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008517 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008518 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008519 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008520 }
8521
Evan Cheng73d6cf12007-01-05 21:37:56 +00008522 // Clear first operand sign bit.
8523 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008524 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008525 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8526 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008527 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008528 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8529 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8530 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8531 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008532 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008533 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008534 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008535 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008536 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008537 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008538 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008539
8540 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008541 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008542}
8543
Craig Topper55b24052012-09-11 06:15:32 +00008544static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008545 SDValue N0 = Op.getOperand(0);
8546 DebugLoc dl = Op.getDebugLoc();
8547 EVT VT = Op.getValueType();
8548
8549 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8550 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8551 DAG.getConstant(1, VT));
8552 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8553}
8554
Michael Liaof966e4e2012-09-13 20:24:54 +00008555// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8556//
8557SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8558 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8559
8560 if (!Subtarget->hasSSE41())
8561 return SDValue();
8562
8563 if (!Op->hasOneUse())
8564 return SDValue();
8565
8566 SDNode *N = Op.getNode();
8567 DebugLoc DL = N->getDebugLoc();
8568
8569 SmallVector<SDValue, 8> Opnds;
8570 DenseMap<SDValue, unsigned> VecInMap;
8571 EVT VT = MVT::Other;
8572
8573 // Recognize a special case where a vector is casted into wide integer to
8574 // test all 0s.
8575 Opnds.push_back(N->getOperand(0));
8576 Opnds.push_back(N->getOperand(1));
8577
8578 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8579 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8580 // BFS traverse all OR'd operands.
8581 if (I->getOpcode() == ISD::OR) {
8582 Opnds.push_back(I->getOperand(0));
8583 Opnds.push_back(I->getOperand(1));
8584 // Re-evaluate the number of nodes to be traversed.
8585 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8586 continue;
8587 }
8588
8589 // Quit if a non-EXTRACT_VECTOR_ELT
8590 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8591 return SDValue();
8592
8593 // Quit if without a constant index.
8594 SDValue Idx = I->getOperand(1);
8595 if (!isa<ConstantSDNode>(Idx))
8596 return SDValue();
8597
8598 SDValue ExtractedFromVec = I->getOperand(0);
8599 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8600 if (M == VecInMap.end()) {
8601 VT = ExtractedFromVec.getValueType();
8602 // Quit if not 128/256-bit vector.
8603 if (!VT.is128BitVector() && !VT.is256BitVector())
8604 return SDValue();
8605 // Quit if not the same type.
8606 if (VecInMap.begin() != VecInMap.end() &&
8607 VT != VecInMap.begin()->first.getValueType())
8608 return SDValue();
8609 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8610 }
8611 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8612 }
8613
8614 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008615 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008616
8617 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8618 SmallVector<SDValue, 8> VecIns;
8619
8620 for (DenseMap<SDValue, unsigned>::const_iterator
8621 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8622 // Quit if not all elements are used.
8623 if (I->second != FullMask)
8624 return SDValue();
8625 VecIns.push_back(I->first);
8626 }
8627
8628 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8629
8630 // Cast all vectors into TestVT for PTEST.
8631 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8632 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8633
8634 // If more than one full vectors are evaluated, OR them first before PTEST.
8635 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8636 // Each iteration will OR 2 nodes and append the result until there is only
8637 // 1 node left, i.e. the final OR'd value of all vectors.
8638 SDValue LHS = VecIns[Slot];
8639 SDValue RHS = VecIns[Slot + 1];
8640 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8641 }
8642
8643 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8644 VecIns.back(), VecIns.back());
8645}
8646
Dan Gohman076aee32009-03-04 19:44:21 +00008647/// Emit nodes that will be selected as "test Op0,Op0", or something
8648/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008649SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008650 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008651 DebugLoc dl = Op.getDebugLoc();
8652
Dan Gohman31125812009-03-07 01:58:32 +00008653 // CF and OF aren't always set the way we want. Determine which
8654 // of these we need.
8655 bool NeedCF = false;
8656 bool NeedOF = false;
8657 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008658 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008659 case X86::COND_A: case X86::COND_AE:
8660 case X86::COND_B: case X86::COND_BE:
8661 NeedCF = true;
8662 break;
8663 case X86::COND_G: case X86::COND_GE:
8664 case X86::COND_L: case X86::COND_LE:
8665 case X86::COND_O: case X86::COND_NO:
8666 NeedOF = true;
8667 break;
Dan Gohman31125812009-03-07 01:58:32 +00008668 }
8669
Dan Gohman076aee32009-03-04 19:44:21 +00008670 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008671 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8672 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008673 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8674 // Emit a CMP with 0, which is the TEST pattern.
8675 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8676 DAG.getConstant(0, Op.getValueType()));
8677
8678 unsigned Opcode = 0;
8679 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008680
8681 // Truncate operations may prevent the merge of the SETCC instruction
8682 // and the arithmetic intruction before it. Attempt to truncate the operands
8683 // of the arithmetic instruction and use a reduced bit-width instruction.
8684 bool NeedTruncation = false;
8685 SDValue ArithOp = Op;
8686 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8687 SDValue Arith = Op->getOperand(0);
8688 // Both the trunc and the arithmetic op need to have one user each.
8689 if (Arith->hasOneUse())
8690 switch (Arith.getOpcode()) {
8691 default: break;
8692 case ISD::ADD:
8693 case ISD::SUB:
8694 case ISD::AND:
8695 case ISD::OR:
8696 case ISD::XOR: {
8697 NeedTruncation = true;
8698 ArithOp = Arith;
8699 }
8700 }
8701 }
8702
8703 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8704 // which may be the result of a CAST. We use the variable 'Op', which is the
8705 // non-casted variable when we check for possible users.
8706 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008707 case ISD::ADD:
8708 // Due to an isel shortcoming, be conservative if this add is likely to be
8709 // selected as part of a load-modify-store instruction. When the root node
8710 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8711 // uses of other nodes in the match, such as the ADD in this case. This
8712 // leads to the ADD being left around and reselected, with the result being
8713 // two adds in the output. Alas, even if none our users are stores, that
8714 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8715 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8716 // climbing the DAG back to the root, and it doesn't seem to be worth the
8717 // effort.
8718 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008719 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8720 if (UI->getOpcode() != ISD::CopyToReg &&
8721 UI->getOpcode() != ISD::SETCC &&
8722 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008723 goto default_case;
8724
8725 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008726 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008727 // An add of one will be selected as an INC.
8728 if (C->getAPIntValue() == 1) {
8729 Opcode = X86ISD::INC;
8730 NumOperands = 1;
8731 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008732 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008733
8734 // An add of negative one (subtract of one) will be selected as a DEC.
8735 if (C->getAPIntValue().isAllOnesValue()) {
8736 Opcode = X86ISD::DEC;
8737 NumOperands = 1;
8738 break;
8739 }
Dan Gohman076aee32009-03-04 19:44:21 +00008740 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008741
8742 // Otherwise use a regular EFLAGS-setting add.
8743 Opcode = X86ISD::ADD;
8744 NumOperands = 2;
8745 break;
8746 case ISD::AND: {
8747 // If the primary and result isn't used, don't bother using X86ISD::AND,
8748 // because a TEST instruction will be better.
8749 bool NonFlagUse = false;
8750 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8751 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8752 SDNode *User = *UI;
8753 unsigned UOpNo = UI.getOperandNo();
8754 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8755 // Look pass truncate.
8756 UOpNo = User->use_begin().getOperandNo();
8757 User = *User->use_begin();
8758 }
8759
8760 if (User->getOpcode() != ISD::BRCOND &&
8761 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008762 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008763 NonFlagUse = true;
8764 break;
8765 }
Dan Gohman076aee32009-03-04 19:44:21 +00008766 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008767
8768 if (!NonFlagUse)
8769 break;
8770 }
8771 // FALL THROUGH
8772 case ISD::SUB:
8773 case ISD::OR:
8774 case ISD::XOR:
8775 // Due to the ISEL shortcoming noted above, be conservative if this op is
8776 // likely to be selected as part of a load-modify-store instruction.
8777 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8778 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8779 if (UI->getOpcode() == ISD::STORE)
8780 goto default_case;
8781
8782 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008783 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008784 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008785 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008786 case ISD::XOR: Opcode = X86ISD::XOR; break;
8787 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008788 case ISD::OR: {
8789 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8790 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8791 if (EFLAGS.getNode())
8792 return EFLAGS;
8793 }
8794 Opcode = X86ISD::OR;
8795 break;
8796 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008797 }
8798
8799 NumOperands = 2;
8800 break;
8801 case X86ISD::ADD:
8802 case X86ISD::SUB:
8803 case X86ISD::INC:
8804 case X86ISD::DEC:
8805 case X86ISD::OR:
8806 case X86ISD::XOR:
8807 case X86ISD::AND:
8808 return SDValue(Op.getNode(), 1);
8809 default:
8810 default_case:
8811 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008812 }
8813
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008814 // If we found that truncation is beneficial, perform the truncation and
8815 // update 'Op'.
8816 if (NeedTruncation) {
8817 EVT VT = Op.getValueType();
8818 SDValue WideVal = Op->getOperand(0);
8819 EVT WideVT = WideVal.getValueType();
8820 unsigned ConvertedOp = 0;
8821 // Use a target machine opcode to prevent further DAGCombine
8822 // optimizations that may separate the arithmetic operations
8823 // from the setcc node.
8824 switch (WideVal.getOpcode()) {
8825 default: break;
8826 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8827 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8828 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8829 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8830 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8831 }
8832
8833 if (ConvertedOp) {
8834 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8835 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8836 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8837 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8838 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8839 }
8840 }
8841 }
8842
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008843 if (Opcode == 0)
8844 // Emit a CMP with 0, which is the TEST pattern.
8845 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8846 DAG.getConstant(0, Op.getValueType()));
8847
8848 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8849 SmallVector<SDValue, 4> Ops;
8850 for (unsigned i = 0; i != NumOperands; ++i)
8851 Ops.push_back(Op.getOperand(i));
8852
8853 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8854 DAG.ReplaceAllUsesWith(Op, New);
8855 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008856}
8857
8858/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8859/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008860SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008861 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008862 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8863 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008864 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008865
8866 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008867 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8868 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8869 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8870 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8871 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8872 Op0, Op1);
8873 return SDValue(Sub.getNode(), 1);
8874 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008875 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008876}
8877
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008878/// Convert a comparison if required by the subtarget.
8879SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8880 SelectionDAG &DAG) const {
8881 // If the subtarget does not support the FUCOMI instruction, floating-point
8882 // comparisons have to be converted.
8883 if (Subtarget->hasCMov() ||
8884 Cmp.getOpcode() != X86ISD::CMP ||
8885 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8886 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8887 return Cmp;
8888
8889 // The instruction selector will select an FUCOM instruction instead of
8890 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8891 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8892 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8893 DebugLoc dl = Cmp.getDebugLoc();
8894 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8895 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8896 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8897 DAG.getConstant(8, MVT::i8));
8898 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8899 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8900}
8901
Evan Chengd40d03e2010-01-06 19:38:29 +00008902/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8903/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008904SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8905 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008906 SDValue Op0 = And.getOperand(0);
8907 SDValue Op1 = And.getOperand(1);
8908 if (Op0.getOpcode() == ISD::TRUNCATE)
8909 Op0 = Op0.getOperand(0);
8910 if (Op1.getOpcode() == ISD::TRUNCATE)
8911 Op1 = Op1.getOperand(0);
8912
Evan Chengd40d03e2010-01-06 19:38:29 +00008913 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008914 if (Op1.getOpcode() == ISD::SHL)
8915 std::swap(Op0, Op1);
8916 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008917 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8918 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008919 // If we looked past a truncate, check that it's only truncating away
8920 // known zeros.
8921 unsigned BitWidth = Op0.getValueSizeInBits();
8922 unsigned AndBitWidth = And.getValueSizeInBits();
8923 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008924 APInt Zeros, Ones;
8925 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008926 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8927 return SDValue();
8928 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008929 LHS = Op1;
8930 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008931 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008932 } else if (Op1.getOpcode() == ISD::Constant) {
8933 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008934 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008935 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008936
8937 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008938 LHS = AndLHS.getOperand(0);
8939 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008940 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008941
8942 // Use BT if the immediate can't be encoded in a TEST instruction.
8943 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8944 LHS = AndLHS;
8945 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8946 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008947 }
Evan Cheng0488db92007-09-25 01:57:46 +00008948
Evan Chengd40d03e2010-01-06 19:38:29 +00008949 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008950 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008951 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008952 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008953 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008954 // Also promote i16 to i32 for performance / code size reason.
8955 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008956 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008957 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008958
Evan Chengd40d03e2010-01-06 19:38:29 +00008959 // If the operand types disagree, extend the shift amount to match. Since
8960 // BT ignores high bits (like shifts) we can use anyextend.
8961 if (LHS.getValueType() != RHS.getValueType())
8962 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008963
Evan Chengd40d03e2010-01-06 19:38:29 +00008964 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8965 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8966 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8967 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008968 }
8969
Evan Cheng54de3ea2010-01-05 06:52:31 +00008970 return SDValue();
8971}
8972
Dan Gohmand858e902010-04-17 15:26:15 +00008973SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008974
8975 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8976
Evan Cheng54de3ea2010-01-05 06:52:31 +00008977 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8978 SDValue Op0 = Op.getOperand(0);
8979 SDValue Op1 = Op.getOperand(1);
8980 DebugLoc dl = Op.getDebugLoc();
8981 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8982
8983 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008984 // Lower (X & (1 << N)) == 0 to BT(X, N).
8985 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8986 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008987 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008988 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008989 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008990 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8991 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8992 if (NewSetCC.getNode())
8993 return NewSetCC;
8994 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008995
Chris Lattner481eebc2010-12-19 21:23:48 +00008996 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8997 // these.
8998 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008999 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009000 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9001 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009002
Chris Lattner481eebc2010-12-19 21:23:48 +00009003 // If the input is a setcc, then reuse the input setcc or use a new one with
9004 // the inverted condition.
9005 if (Op0.getOpcode() == X86ISD::SETCC) {
9006 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9007 bool Invert = (CC == ISD::SETNE) ^
9008 cast<ConstantSDNode>(Op1)->isNullValue();
9009 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009010
Evan Cheng2c755ba2010-02-27 07:36:59 +00009011 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009012 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9013 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9014 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009015 }
9016
Evan Chenge5b51ac2010-04-17 06:13:15 +00009017 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009018 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009019 if (X86CC == X86::COND_INVALID)
9020 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009021
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009022 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009023 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009024 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009025 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009026}
9027
Craig Topper89af15e2011-09-18 08:03:58 +00009028// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009029// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009030static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009031 EVT VT = Op.getValueType();
9032
Craig Topper7a9a28b2012-08-12 02:23:29 +00009033 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009034 "Unsupported value type for operation");
9035
Craig Topper66ddd152012-04-27 22:54:43 +00009036 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009037 DebugLoc dl = Op.getDebugLoc();
9038 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009039
9040 // Extract the LHS vectors
9041 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009042 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9043 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009044
9045 // Extract the RHS vectors
9046 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009047 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9048 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009049
9050 // Issue the operation on the smaller types and concatenate the result back
9051 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9052 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9053 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9054 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9055 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9056}
9057
9058
Dan Gohmand858e902010-04-17 15:26:15 +00009059SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009060 SDValue Cond;
9061 SDValue Op0 = Op.getOperand(0);
9062 SDValue Op1 = Op.getOperand(1);
9063 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009064 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009065 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9066 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009067 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009068
9069 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009070#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009071 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009072 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9073#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009074
Craig Topper523908d2012-08-13 02:34:03 +00009075 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009076 bool Swap = false;
9077
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009078 // SSE Condition code mapping:
9079 // 0 - EQ
9080 // 1 - LT
9081 // 2 - LE
9082 // 3 - UNORD
9083 // 4 - NEQ
9084 // 5 - NLT
9085 // 6 - NLE
9086 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009087 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009088 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009089 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009090 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009091 case ISD::SETOGT:
9092 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009093 case ISD::SETLT:
9094 case ISD::SETOLT: SSECC = 1; break;
9095 case ISD::SETOGE:
9096 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009097 case ISD::SETLE:
9098 case ISD::SETOLE: SSECC = 2; break;
9099 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009100 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009101 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009102 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009103 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009104 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009105 case ISD::SETUGT: SSECC = 6; break;
9106 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009107 case ISD::SETUEQ:
9108 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009109 }
9110 if (Swap)
9111 std::swap(Op0, Op1);
9112
Nate Begemanfb8ead02008-07-25 19:05:58 +00009113 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009114 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009115 unsigned CC0, CC1;
9116 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009117 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009118 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9119 } else {
9120 assert(SetCCOpcode == ISD::SETONE);
9121 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009122 }
Craig Topper523908d2012-08-13 02:34:03 +00009123
9124 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9125 DAG.getConstant(CC0, MVT::i8));
9126 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9127 DAG.getConstant(CC1, MVT::i8));
9128 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009129 }
9130 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009131 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9132 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009134
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009135 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00009136 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00009137 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009138
Nate Begeman30a0de92008-07-17 16:51:19 +00009139 // We are handling one of the integer comparisons here. Since SSE only has
9140 // GT and EQ comparisons for integer, swapping operands and multiple
9141 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009142 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009143 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009144
Nate Begeman30a0de92008-07-17 16:51:19 +00009145 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009146 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009147 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009148 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009149 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009150 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009151 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009152 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009153 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009154 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009155 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009156 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009157 }
9158 if (Swap)
9159 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009160
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009161 // Check that the operation in question is available (most are plain SSE2,
9162 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009163 if (VT == MVT::v2i64) {
9164 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9165 return SDValue();
9166 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
9167 return SDValue();
9168 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009169
Nate Begeman30a0de92008-07-17 16:51:19 +00009170 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9171 // bits of the inputs before performing those operations.
9172 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009173 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009174 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9175 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009176 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009177 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9178 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009179 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9180 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009181 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009182
Dale Johannesenace16102009-02-03 19:33:06 +00009183 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009184
9185 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009186 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009187 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009188
Nate Begeman30a0de92008-07-17 16:51:19 +00009189 return Result;
9190}
Evan Cheng0488db92007-09-25 01:57:46 +00009191
Evan Cheng370e5342008-12-03 08:38:43 +00009192// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009193static bool isX86LogicalCmp(SDValue Op) {
9194 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009195 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9196 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009197 return true;
9198 if (Op.getResNo() == 1 &&
9199 (Opc == X86ISD::ADD ||
9200 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009201 Opc == X86ISD::ADC ||
9202 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009203 Opc == X86ISD::SMUL ||
9204 Opc == X86ISD::UMUL ||
9205 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009206 Opc == X86ISD::DEC ||
9207 Opc == X86ISD::OR ||
9208 Opc == X86ISD::XOR ||
9209 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009210 return true;
9211
Chris Lattner9637d5b2010-12-05 07:49:54 +00009212 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9213 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009214
Dan Gohman076aee32009-03-04 19:44:21 +00009215 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009216}
9217
Chris Lattnera2b56002010-12-05 01:23:24 +00009218static bool isZero(SDValue V) {
9219 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9220 return C && C->isNullValue();
9221}
9222
Chris Lattner96908b12010-12-05 02:00:51 +00009223static bool isAllOnes(SDValue V) {
9224 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9225 return C && C->isAllOnesValue();
9226}
9227
Evan Chengb64dd5f2012-08-07 22:21:00 +00009228static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9229 if (V.getOpcode() != ISD::TRUNCATE)
9230 return false;
9231
9232 SDValue VOp0 = V.getOperand(0);
9233 unsigned InBits = VOp0.getValueSizeInBits();
9234 unsigned Bits = V.getValueSizeInBits();
9235 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9236}
9237
Dan Gohmand858e902010-04-17 15:26:15 +00009238SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009239 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009240 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009241 SDValue Op1 = Op.getOperand(1);
9242 SDValue Op2 = Op.getOperand(2);
9243 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009244 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009245
Dan Gohman1a492952009-10-20 16:22:37 +00009246 if (Cond.getOpcode() == ISD::SETCC) {
9247 SDValue NewCond = LowerSETCC(Cond, DAG);
9248 if (NewCond.getNode())
9249 Cond = NewCond;
9250 }
Evan Cheng734503b2006-09-11 02:19:56 +00009251
Chris Lattnera2b56002010-12-05 01:23:24 +00009252 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009253 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009254 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009255 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009256 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009257 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9258 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009259 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009260
Chris Lattnera2b56002010-12-05 01:23:24 +00009261 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009262
9263 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009264 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9265 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009266
9267 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009268 // Apply further optimizations for special cases
9269 // (select (x != 0), -1, 0) -> neg & sbb
9270 // (select (x == 0), 0, -1) -> neg & sbb
9271 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009272 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009273 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9274 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009275 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9276 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009277 CmpOp0);
9278 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9279 DAG.getConstant(X86::COND_B, MVT::i8),
9280 SDValue(Neg.getNode(), 1));
9281 return Res;
9282 }
9283
Chris Lattnera2b56002010-12-05 01:23:24 +00009284 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9285 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009286 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009287
Chris Lattner96908b12010-12-05 02:00:51 +00009288 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009289 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9290 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009291
Chris Lattner96908b12010-12-05 02:00:51 +00009292 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9293 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009294
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009295 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009296 if (N2C == 0 || !N2C->isNullValue())
9297 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9298 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009299 }
9300 }
9301
Chris Lattnera2b56002010-12-05 01:23:24 +00009302 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009303 if (Cond.getOpcode() == ISD::AND &&
9304 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9305 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009306 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009307 Cond = Cond.getOperand(0);
9308 }
9309
Evan Cheng3f41d662007-10-08 22:16:29 +00009310 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9311 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009312 unsigned CondOpcode = Cond.getOpcode();
9313 if (CondOpcode == X86ISD::SETCC ||
9314 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009315 CC = Cond.getOperand(0);
9316
Dan Gohman475871a2008-07-27 21:46:04 +00009317 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009318 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009319 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009320
Evan Cheng3f41d662007-10-08 22:16:29 +00009321 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009322 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009323 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009324 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009325
Chris Lattnerd1980a52009-03-12 06:52:53 +00009326 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9327 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009328 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009329 addTest = false;
9330 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009331 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9332 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9333 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9334 Cond.getOperand(0).getValueType() != MVT::i8)) {
9335 SDValue LHS = Cond.getOperand(0);
9336 SDValue RHS = Cond.getOperand(1);
9337 unsigned X86Opcode;
9338 unsigned X86Cond;
9339 SDVTList VTs;
9340 switch (CondOpcode) {
9341 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9342 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9343 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9344 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9345 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9346 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9347 default: llvm_unreachable("unexpected overflowing operator");
9348 }
9349 if (CondOpcode == ISD::UMULO)
9350 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9351 MVT::i32);
9352 else
9353 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9354
9355 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9356
9357 if (CondOpcode == ISD::UMULO)
9358 Cond = X86Op.getValue(2);
9359 else
9360 Cond = X86Op.getValue(1);
9361
9362 CC = DAG.getConstant(X86Cond, MVT::i8);
9363 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009364 }
9365
9366 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009367 // Look pass the truncate if the high bits are known zero.
9368 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9369 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009370
9371 // We know the result of AND is compared against zero. Try to match
9372 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009373 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009374 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009375 if (NewSetCC.getNode()) {
9376 CC = NewSetCC.getOperand(0);
9377 Cond = NewSetCC.getOperand(1);
9378 addTest = false;
9379 }
9380 }
9381 }
9382
9383 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009384 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009385 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009386 }
9387
Benjamin Kramere915ff32010-12-22 23:09:28 +00009388 // a < b ? -1 : 0 -> RES = ~setcc_carry
9389 // a < b ? 0 : -1 -> RES = setcc_carry
9390 // a >= b ? -1 : 0 -> RES = setcc_carry
9391 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009392 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009393 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009394 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9395
9396 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9397 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9398 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9399 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9400 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9401 return DAG.getNOT(DL, Res, Res.getValueType());
9402 return Res;
9403 }
9404 }
9405
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009406 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9407 // widen the cmov and push the truncate through. This avoids introducing a new
9408 // branch during isel and doesn't add any extensions.
9409 if (Op.getValueType() == MVT::i8 &&
9410 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9411 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9412 if (T1.getValueType() == T2.getValueType() &&
9413 // Blacklist CopyFromReg to avoid partial register stalls.
9414 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9415 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009416 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009417 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9418 }
9419 }
9420
Evan Cheng0488db92007-09-25 01:57:46 +00009421 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9422 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009423 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009424 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009425 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009426}
9427
Evan Cheng370e5342008-12-03 08:38:43 +00009428// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9429// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9430// from the AND / OR.
9431static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9432 Opc = Op.getOpcode();
9433 if (Opc != ISD::OR && Opc != ISD::AND)
9434 return false;
9435 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9436 Op.getOperand(0).hasOneUse() &&
9437 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9438 Op.getOperand(1).hasOneUse());
9439}
9440
Evan Cheng961d6d42009-02-02 08:19:07 +00009441// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9442// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009443static bool isXor1OfSetCC(SDValue Op) {
9444 if (Op.getOpcode() != ISD::XOR)
9445 return false;
9446 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9447 if (N1C && N1C->getAPIntValue() == 1) {
9448 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9449 Op.getOperand(0).hasOneUse();
9450 }
9451 return false;
9452}
9453
Dan Gohmand858e902010-04-17 15:26:15 +00009454SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009455 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009456 SDValue Chain = Op.getOperand(0);
9457 SDValue Cond = Op.getOperand(1);
9458 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009459 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009460 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009461 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009462
Dan Gohman1a492952009-10-20 16:22:37 +00009463 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009464 // Check for setcc([su]{add,sub,mul}o == 0).
9465 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9466 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9467 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9468 Cond.getOperand(0).getResNo() == 1 &&
9469 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9470 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9471 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9472 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9473 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9474 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9475 Inverted = true;
9476 Cond = Cond.getOperand(0);
9477 } else {
9478 SDValue NewCond = LowerSETCC(Cond, DAG);
9479 if (NewCond.getNode())
9480 Cond = NewCond;
9481 }
Dan Gohman1a492952009-10-20 16:22:37 +00009482 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009483#if 0
9484 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009485 else if (Cond.getOpcode() == X86ISD::ADD ||
9486 Cond.getOpcode() == X86ISD::SUB ||
9487 Cond.getOpcode() == X86ISD::SMUL ||
9488 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009489 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009490#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009491
Evan Chengad9c0a32009-12-15 00:53:42 +00009492 // Look pass (and (setcc_carry (cmp ...)), 1).
9493 if (Cond.getOpcode() == ISD::AND &&
9494 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9495 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009496 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009497 Cond = Cond.getOperand(0);
9498 }
9499
Evan Cheng3f41d662007-10-08 22:16:29 +00009500 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9501 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009502 unsigned CondOpcode = Cond.getOpcode();
9503 if (CondOpcode == X86ISD::SETCC ||
9504 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009505 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009506
Dan Gohman475871a2008-07-27 21:46:04 +00009507 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009508 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009509 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009510 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009511 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009512 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009513 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009514 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009515 default: break;
9516 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009517 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009518 // These can only come from an arithmetic instruction with overflow,
9519 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009520 Cond = Cond.getNode()->getOperand(1);
9521 addTest = false;
9522 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009523 }
Evan Cheng0488db92007-09-25 01:57:46 +00009524 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009525 }
9526 CondOpcode = Cond.getOpcode();
9527 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9528 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9529 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9530 Cond.getOperand(0).getValueType() != MVT::i8)) {
9531 SDValue LHS = Cond.getOperand(0);
9532 SDValue RHS = Cond.getOperand(1);
9533 unsigned X86Opcode;
9534 unsigned X86Cond;
9535 SDVTList VTs;
9536 switch (CondOpcode) {
9537 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9538 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9539 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9540 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9541 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9542 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9543 default: llvm_unreachable("unexpected overflowing operator");
9544 }
9545 if (Inverted)
9546 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9547 if (CondOpcode == ISD::UMULO)
9548 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9549 MVT::i32);
9550 else
9551 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9552
9553 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9554
9555 if (CondOpcode == ISD::UMULO)
9556 Cond = X86Op.getValue(2);
9557 else
9558 Cond = X86Op.getValue(1);
9559
9560 CC = DAG.getConstant(X86Cond, MVT::i8);
9561 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009562 } else {
9563 unsigned CondOpc;
9564 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9565 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009566 if (CondOpc == ISD::OR) {
9567 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9568 // two branches instead of an explicit OR instruction with a
9569 // separate test.
9570 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009571 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009572 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009573 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009574 Chain, Dest, CC, Cmp);
9575 CC = Cond.getOperand(1).getOperand(0);
9576 Cond = Cmp;
9577 addTest = false;
9578 }
9579 } else { // ISD::AND
9580 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9581 // two branches instead of an explicit AND instruction with a
9582 // separate test. However, we only do this if this block doesn't
9583 // have a fall-through edge, because this requires an explicit
9584 // jmp when the condition is false.
9585 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009586 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009587 Op.getNode()->hasOneUse()) {
9588 X86::CondCode CCode =
9589 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9590 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009591 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009592 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009593 // Look for an unconditional branch following this conditional branch.
9594 // We need this because we need to reverse the successors in order
9595 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009596 if (User->getOpcode() == ISD::BR) {
9597 SDValue FalseBB = User->getOperand(1);
9598 SDNode *NewBR =
9599 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009600 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009601 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009602 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009603
Dale Johannesene4d209d2009-02-03 20:21:25 +00009604 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009605 Chain, Dest, CC, Cmp);
9606 X86::CondCode CCode =
9607 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9608 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009609 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009610 Cond = Cmp;
9611 addTest = false;
9612 }
9613 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009614 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009615 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9616 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9617 // It should be transformed during dag combiner except when the condition
9618 // is set by a arithmetics with overflow node.
9619 X86::CondCode CCode =
9620 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9621 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009622 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009623 Cond = Cond.getOperand(0).getOperand(1);
9624 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009625 } else if (Cond.getOpcode() == ISD::SETCC &&
9626 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9627 // For FCMP_OEQ, we can emit
9628 // two branches instead of an explicit AND instruction with a
9629 // separate test. However, we only do this if this block doesn't
9630 // have a fall-through edge, because this requires an explicit
9631 // jmp when the condition is false.
9632 if (Op.getNode()->hasOneUse()) {
9633 SDNode *User = *Op.getNode()->use_begin();
9634 // Look for an unconditional branch following this conditional branch.
9635 // We need this because we need to reverse the successors in order
9636 // to implement FCMP_OEQ.
9637 if (User->getOpcode() == ISD::BR) {
9638 SDValue FalseBB = User->getOperand(1);
9639 SDNode *NewBR =
9640 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9641 assert(NewBR == User);
9642 (void)NewBR;
9643 Dest = FalseBB;
9644
9645 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9646 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009647 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009648 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9649 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9650 Chain, Dest, CC, Cmp);
9651 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9652 Cond = Cmp;
9653 addTest = false;
9654 }
9655 }
9656 } else if (Cond.getOpcode() == ISD::SETCC &&
9657 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9658 // For FCMP_UNE, we can emit
9659 // two branches instead of an explicit AND instruction with a
9660 // separate test. However, we only do this if this block doesn't
9661 // have a fall-through edge, because this requires an explicit
9662 // jmp when the condition is false.
9663 if (Op.getNode()->hasOneUse()) {
9664 SDNode *User = *Op.getNode()->use_begin();
9665 // Look for an unconditional branch following this conditional branch.
9666 // We need this because we need to reverse the successors in order
9667 // to implement FCMP_UNE.
9668 if (User->getOpcode() == ISD::BR) {
9669 SDValue FalseBB = User->getOperand(1);
9670 SDNode *NewBR =
9671 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9672 assert(NewBR == User);
9673 (void)NewBR;
9674
9675 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9676 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009677 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009678 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9679 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9680 Chain, Dest, CC, Cmp);
9681 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9682 Cond = Cmp;
9683 addTest = false;
9684 Dest = FalseBB;
9685 }
9686 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009687 }
Evan Cheng0488db92007-09-25 01:57:46 +00009688 }
9689
9690 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009691 // Look pass the truncate if the high bits are known zero.
9692 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9693 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009694
9695 // We know the result of AND is compared against zero. Try to match
9696 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009697 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009698 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9699 if (NewSetCC.getNode()) {
9700 CC = NewSetCC.getOperand(0);
9701 Cond = NewSetCC.getOperand(1);
9702 addTest = false;
9703 }
9704 }
9705 }
9706
9707 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009708 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009709 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009710 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009711 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009712 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009713 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009714}
9715
Anton Korobeynikove060b532007-04-17 19:34:00 +00009716
9717// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9718// Calls to _alloca is needed to probe the stack when allocating more than 4k
9719// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9720// that the guard pages used by the OS virtual memory manager are allocated in
9721// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009722SDValue
9723X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009724 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009725 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009726 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009727 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009728 "are being used");
9729 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009730 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009731
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009732 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009733 SDValue Chain = Op.getOperand(0);
9734 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009735 // FIXME: Ensure alignment here
9736
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009737 bool Is64Bit = Subtarget->is64Bit();
9738 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009739
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009740 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009741 MachineFunction &MF = DAG.getMachineFunction();
9742 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009743
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009744 if (Is64Bit) {
9745 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009746 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009747 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009748
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009749 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009750 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009751 if (I->hasNestAttr())
9752 report_fatal_error("Cannot use segmented stacks with functions that "
9753 "have nested arguments.");
9754 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009755
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009756 const TargetRegisterClass *AddrRegClass =
9757 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9758 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9759 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9760 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9761 DAG.getRegister(Vreg, SPTy));
9762 SDValue Ops1[2] = { Value, Chain };
9763 return DAG.getMergeValues(Ops1, 2, dl);
9764 } else {
9765 SDValue Flag;
9766 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009767
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009768 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9769 Flag = Chain.getValue(1);
9770 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009771
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009772 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9773 Flag = Chain.getValue(1);
9774
9775 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9776
9777 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9778 return DAG.getMergeValues(Ops1, 2, dl);
9779 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009780}
9781
Dan Gohmand858e902010-04-17 15:26:15 +00009782SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009783 MachineFunction &MF = DAG.getMachineFunction();
9784 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9785
Dan Gohman69de1932008-02-06 22:27:42 +00009786 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009787 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009788
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009789 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009790 // vastart just stores the address of the VarArgsFrameIndex slot into the
9791 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009792 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9793 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009794 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9795 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009796 }
9797
9798 // __va_list_tag:
9799 // gp_offset (0 - 6 * 8)
9800 // fp_offset (48 - 48 + 8 * 16)
9801 // overflow_arg_area (point to parameters coming in memory).
9802 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009803 SmallVector<SDValue, 8> MemOps;
9804 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009805 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009806 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009807 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9808 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009809 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009810 MemOps.push_back(Store);
9811
9812 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009813 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009814 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009815 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009816 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9817 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009818 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009819 MemOps.push_back(Store);
9820
9821 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009822 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009823 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009824 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9825 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009826 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9827 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009828 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009829 MemOps.push_back(Store);
9830
9831 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009832 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009833 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009834 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9835 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009836 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9837 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009838 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009839 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009840 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009841}
9842
Dan Gohmand858e902010-04-17 15:26:15 +00009843SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009844 assert(Subtarget->is64Bit() &&
9845 "LowerVAARG only handles 64-bit va_arg!");
9846 assert((Subtarget->isTargetLinux() ||
9847 Subtarget->isTargetDarwin()) &&
9848 "Unhandled target in LowerVAARG");
9849 assert(Op.getNode()->getNumOperands() == 4);
9850 SDValue Chain = Op.getOperand(0);
9851 SDValue SrcPtr = Op.getOperand(1);
9852 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9853 unsigned Align = Op.getConstantOperandVal(3);
9854 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009855
Dan Gohman320afb82010-10-12 18:00:49 +00009856 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009857 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009858 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009859 uint8_t ArgMode;
9860
9861 // Decide which area this value should be read from.
9862 // TODO: Implement the AMD64 ABI in its entirety. This simple
9863 // selection mechanism works only for the basic types.
9864 if (ArgVT == MVT::f80) {
9865 llvm_unreachable("va_arg for f80 not yet implemented");
9866 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9867 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9868 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9869 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9870 } else {
9871 llvm_unreachable("Unhandled argument type in LowerVAARG");
9872 }
9873
9874 if (ArgMode == 2) {
9875 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009876 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009877 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009878 .getFunction()->getFnAttributes()
9879 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009880 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009881 }
9882
9883 // Insert VAARG_64 node into the DAG
9884 // VAARG_64 returns two values: Variable Argument Address, Chain
9885 SmallVector<SDValue, 11> InstOps;
9886 InstOps.push_back(Chain);
9887 InstOps.push_back(SrcPtr);
9888 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9889 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9890 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9891 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9892 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9893 VTs, &InstOps[0], InstOps.size(),
9894 MVT::i64,
9895 MachinePointerInfo(SV),
9896 /*Align=*/0,
9897 /*Volatile=*/false,
9898 /*ReadMem=*/true,
9899 /*WriteMem=*/true);
9900 Chain = VAARG.getValue(1);
9901
9902 // Load the next argument and return it
9903 return DAG.getLoad(ArgVT, dl,
9904 Chain,
9905 VAARG,
9906 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009907 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009908}
9909
Craig Topper55b24052012-09-11 06:15:32 +00009910static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9911 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009912 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009913 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009914 SDValue Chain = Op.getOperand(0);
9915 SDValue DstPtr = Op.getOperand(1);
9916 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009917 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9918 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009919 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009920
Chris Lattnere72f2022010-09-21 05:40:29 +00009921 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009922 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009923 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009924 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009925}
9926
Craig Topper80e46362012-01-23 06:16:53 +00009927// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9928// may or may not be a constant. Takes immediate version of shift as input.
9929static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9930 SDValue SrcOp, SDValue ShAmt,
9931 SelectionDAG &DAG) {
9932 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9933
9934 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009935 // Constant may be a TargetConstant. Use a regular constant.
9936 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009937 switch (Opc) {
9938 default: llvm_unreachable("Unknown target vector shift node");
9939 case X86ISD::VSHLI:
9940 case X86ISD::VSRLI:
9941 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009942 return DAG.getNode(Opc, dl, VT, SrcOp,
9943 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009944 }
9945 }
9946
9947 // Change opcode to non-immediate version
9948 switch (Opc) {
9949 default: llvm_unreachable("Unknown target vector shift node");
9950 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9951 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9952 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9953 }
9954
9955 // Need to build a vector containing shift amount
9956 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9957 SDValue ShOps[4];
9958 ShOps[0] = ShAmt;
9959 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009960 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009961 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009962
9963 // The return type has to be a 128-bit type with the same element
9964 // type as the input type.
9965 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9966 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9967
9968 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009969 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9970}
9971
Craig Topper55b24052012-09-11 06:15:32 +00009972static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009973 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009974 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009975 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009976 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009977 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009978 case Intrinsic::x86_sse_comieq_ss:
9979 case Intrinsic::x86_sse_comilt_ss:
9980 case Intrinsic::x86_sse_comile_ss:
9981 case Intrinsic::x86_sse_comigt_ss:
9982 case Intrinsic::x86_sse_comige_ss:
9983 case Intrinsic::x86_sse_comineq_ss:
9984 case Intrinsic::x86_sse_ucomieq_ss:
9985 case Intrinsic::x86_sse_ucomilt_ss:
9986 case Intrinsic::x86_sse_ucomile_ss:
9987 case Intrinsic::x86_sse_ucomigt_ss:
9988 case Intrinsic::x86_sse_ucomige_ss:
9989 case Intrinsic::x86_sse_ucomineq_ss:
9990 case Intrinsic::x86_sse2_comieq_sd:
9991 case Intrinsic::x86_sse2_comilt_sd:
9992 case Intrinsic::x86_sse2_comile_sd:
9993 case Intrinsic::x86_sse2_comigt_sd:
9994 case Intrinsic::x86_sse2_comige_sd:
9995 case Intrinsic::x86_sse2_comineq_sd:
9996 case Intrinsic::x86_sse2_ucomieq_sd:
9997 case Intrinsic::x86_sse2_ucomilt_sd:
9998 case Intrinsic::x86_sse2_ucomile_sd:
9999 case Intrinsic::x86_sse2_ucomigt_sd:
10000 case Intrinsic::x86_sse2_ucomige_sd:
10001 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010002 unsigned Opc;
10003 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010004 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010005 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010006 case Intrinsic::x86_sse_comieq_ss:
10007 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010008 Opc = X86ISD::COMI;
10009 CC = ISD::SETEQ;
10010 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010011 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010012 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010013 Opc = X86ISD::COMI;
10014 CC = ISD::SETLT;
10015 break;
10016 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010017 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010018 Opc = X86ISD::COMI;
10019 CC = ISD::SETLE;
10020 break;
10021 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010022 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010023 Opc = X86ISD::COMI;
10024 CC = ISD::SETGT;
10025 break;
10026 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010027 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010028 Opc = X86ISD::COMI;
10029 CC = ISD::SETGE;
10030 break;
10031 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010032 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010033 Opc = X86ISD::COMI;
10034 CC = ISD::SETNE;
10035 break;
10036 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010037 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010038 Opc = X86ISD::UCOMI;
10039 CC = ISD::SETEQ;
10040 break;
10041 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010042 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010043 Opc = X86ISD::UCOMI;
10044 CC = ISD::SETLT;
10045 break;
10046 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010047 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010048 Opc = X86ISD::UCOMI;
10049 CC = ISD::SETLE;
10050 break;
10051 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010052 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010053 Opc = X86ISD::UCOMI;
10054 CC = ISD::SETGT;
10055 break;
10056 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010057 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010058 Opc = X86ISD::UCOMI;
10059 CC = ISD::SETGE;
10060 break;
10061 case Intrinsic::x86_sse_ucomineq_ss:
10062 case Intrinsic::x86_sse2_ucomineq_sd:
10063 Opc = X86ISD::UCOMI;
10064 CC = ISD::SETNE;
10065 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010066 }
Evan Cheng734503b2006-09-11 02:19:56 +000010067
Dan Gohman475871a2008-07-27 21:46:04 +000010068 SDValue LHS = Op.getOperand(1);
10069 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010070 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010071 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010072 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10073 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10074 DAG.getConstant(X86CC, MVT::i8), Cond);
10075 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010076 }
Craig Topper6d688152012-08-14 07:43:25 +000010077
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010078 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010079 case Intrinsic::x86_sse2_pmulu_dq:
10080 case Intrinsic::x86_avx2_pmulu_dq:
10081 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10082 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010083
10084 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010085 case Intrinsic::x86_sse3_hadd_ps:
10086 case Intrinsic::x86_sse3_hadd_pd:
10087 case Intrinsic::x86_avx_hadd_ps_256:
10088 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010089 case Intrinsic::x86_sse3_hsub_ps:
10090 case Intrinsic::x86_sse3_hsub_pd:
10091 case Intrinsic::x86_avx_hsub_ps_256:
10092 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010093 case Intrinsic::x86_ssse3_phadd_w_128:
10094 case Intrinsic::x86_ssse3_phadd_d_128:
10095 case Intrinsic::x86_avx2_phadd_w:
10096 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010097 case Intrinsic::x86_ssse3_phsub_w_128:
10098 case Intrinsic::x86_ssse3_phsub_d_128:
10099 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010100 case Intrinsic::x86_avx2_phsub_d: {
10101 unsigned Opcode;
10102 switch (IntNo) {
10103 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10104 case Intrinsic::x86_sse3_hadd_ps:
10105 case Intrinsic::x86_sse3_hadd_pd:
10106 case Intrinsic::x86_avx_hadd_ps_256:
10107 case Intrinsic::x86_avx_hadd_pd_256:
10108 Opcode = X86ISD::FHADD;
10109 break;
10110 case Intrinsic::x86_sse3_hsub_ps:
10111 case Intrinsic::x86_sse3_hsub_pd:
10112 case Intrinsic::x86_avx_hsub_ps_256:
10113 case Intrinsic::x86_avx_hsub_pd_256:
10114 Opcode = X86ISD::FHSUB;
10115 break;
10116 case Intrinsic::x86_ssse3_phadd_w_128:
10117 case Intrinsic::x86_ssse3_phadd_d_128:
10118 case Intrinsic::x86_avx2_phadd_w:
10119 case Intrinsic::x86_avx2_phadd_d:
10120 Opcode = X86ISD::HADD;
10121 break;
10122 case Intrinsic::x86_ssse3_phsub_w_128:
10123 case Intrinsic::x86_ssse3_phsub_d_128:
10124 case Intrinsic::x86_avx2_phsub_w:
10125 case Intrinsic::x86_avx2_phsub_d:
10126 Opcode = X86ISD::HSUB;
10127 break;
10128 }
10129 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010130 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010131 }
10132
10133 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010134 case Intrinsic::x86_avx2_psllv_d:
10135 case Intrinsic::x86_avx2_psllv_q:
10136 case Intrinsic::x86_avx2_psllv_d_256:
10137 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010138 case Intrinsic::x86_avx2_psrlv_d:
10139 case Intrinsic::x86_avx2_psrlv_q:
10140 case Intrinsic::x86_avx2_psrlv_d_256:
10141 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010142 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010143 case Intrinsic::x86_avx2_psrav_d_256: {
10144 unsigned Opcode;
10145 switch (IntNo) {
10146 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10147 case Intrinsic::x86_avx2_psllv_d:
10148 case Intrinsic::x86_avx2_psllv_q:
10149 case Intrinsic::x86_avx2_psllv_d_256:
10150 case Intrinsic::x86_avx2_psllv_q_256:
10151 Opcode = ISD::SHL;
10152 break;
10153 case Intrinsic::x86_avx2_psrlv_d:
10154 case Intrinsic::x86_avx2_psrlv_q:
10155 case Intrinsic::x86_avx2_psrlv_d_256:
10156 case Intrinsic::x86_avx2_psrlv_q_256:
10157 Opcode = ISD::SRL;
10158 break;
10159 case Intrinsic::x86_avx2_psrav_d:
10160 case Intrinsic::x86_avx2_psrav_d_256:
10161 Opcode = ISD::SRA;
10162 break;
10163 }
10164 return DAG.getNode(Opcode, dl, Op.getValueType(),
10165 Op.getOperand(1), Op.getOperand(2));
10166 }
10167
Craig Topper969ba282012-01-25 06:43:11 +000010168 case Intrinsic::x86_ssse3_pshuf_b_128:
10169 case Intrinsic::x86_avx2_pshuf_b:
10170 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10171 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010172
Craig Topper969ba282012-01-25 06:43:11 +000010173 case Intrinsic::x86_ssse3_psign_b_128:
10174 case Intrinsic::x86_ssse3_psign_w_128:
10175 case Intrinsic::x86_ssse3_psign_d_128:
10176 case Intrinsic::x86_avx2_psign_b:
10177 case Intrinsic::x86_avx2_psign_w:
10178 case Intrinsic::x86_avx2_psign_d:
10179 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10180 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010181
Craig Toppere566cd02012-01-26 07:18:03 +000010182 case Intrinsic::x86_sse41_insertps:
10183 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10184 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010185
Craig Toppere566cd02012-01-26 07:18:03 +000010186 case Intrinsic::x86_avx_vperm2f128_ps_256:
10187 case Intrinsic::x86_avx_vperm2f128_pd_256:
10188 case Intrinsic::x86_avx_vperm2f128_si_256:
10189 case Intrinsic::x86_avx2_vperm2i128:
10190 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10191 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010192
Craig Topperffa6c402012-04-16 07:13:00 +000010193 case Intrinsic::x86_avx2_permd:
10194 case Intrinsic::x86_avx2_permps:
10195 // Operands intentionally swapped. Mask is last operand to intrinsic,
10196 // but second operand for node/intruction.
10197 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10198 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010199
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010200 // ptest and testp intrinsics. The intrinsic these come from are designed to
10201 // return an integer value, not just an instruction so lower it to the ptest
10202 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010203 case Intrinsic::x86_sse41_ptestz:
10204 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010205 case Intrinsic::x86_sse41_ptestnzc:
10206 case Intrinsic::x86_avx_ptestz_256:
10207 case Intrinsic::x86_avx_ptestc_256:
10208 case Intrinsic::x86_avx_ptestnzc_256:
10209 case Intrinsic::x86_avx_vtestz_ps:
10210 case Intrinsic::x86_avx_vtestc_ps:
10211 case Intrinsic::x86_avx_vtestnzc_ps:
10212 case Intrinsic::x86_avx_vtestz_pd:
10213 case Intrinsic::x86_avx_vtestc_pd:
10214 case Intrinsic::x86_avx_vtestnzc_pd:
10215 case Intrinsic::x86_avx_vtestz_ps_256:
10216 case Intrinsic::x86_avx_vtestc_ps_256:
10217 case Intrinsic::x86_avx_vtestnzc_ps_256:
10218 case Intrinsic::x86_avx_vtestz_pd_256:
10219 case Intrinsic::x86_avx_vtestc_pd_256:
10220 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10221 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010222 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010223 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010224 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010225 case Intrinsic::x86_avx_vtestz_ps:
10226 case Intrinsic::x86_avx_vtestz_pd:
10227 case Intrinsic::x86_avx_vtestz_ps_256:
10228 case Intrinsic::x86_avx_vtestz_pd_256:
10229 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010230 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010231 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010232 // ZF = 1
10233 X86CC = X86::COND_E;
10234 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010235 case Intrinsic::x86_avx_vtestc_ps:
10236 case Intrinsic::x86_avx_vtestc_pd:
10237 case Intrinsic::x86_avx_vtestc_ps_256:
10238 case Intrinsic::x86_avx_vtestc_pd_256:
10239 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010240 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010241 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010242 // CF = 1
10243 X86CC = X86::COND_B;
10244 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010245 case Intrinsic::x86_avx_vtestnzc_ps:
10246 case Intrinsic::x86_avx_vtestnzc_pd:
10247 case Intrinsic::x86_avx_vtestnzc_ps_256:
10248 case Intrinsic::x86_avx_vtestnzc_pd_256:
10249 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010250 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010251 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010252 // ZF and CF = 0
10253 X86CC = X86::COND_A;
10254 break;
10255 }
Eric Christopherfd179292009-08-27 18:07:15 +000010256
Eric Christopher71c67532009-07-29 00:28:05 +000010257 SDValue LHS = Op.getOperand(1);
10258 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010259 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10260 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010261 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10262 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10263 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010264 }
Evan Cheng5759f972008-05-04 09:15:50 +000010265
Craig Topper80e46362012-01-23 06:16:53 +000010266 // SSE/AVX shift intrinsics
10267 case Intrinsic::x86_sse2_psll_w:
10268 case Intrinsic::x86_sse2_psll_d:
10269 case Intrinsic::x86_sse2_psll_q:
10270 case Intrinsic::x86_avx2_psll_w:
10271 case Intrinsic::x86_avx2_psll_d:
10272 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010273 case Intrinsic::x86_sse2_psrl_w:
10274 case Intrinsic::x86_sse2_psrl_d:
10275 case Intrinsic::x86_sse2_psrl_q:
10276 case Intrinsic::x86_avx2_psrl_w:
10277 case Intrinsic::x86_avx2_psrl_d:
10278 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010279 case Intrinsic::x86_sse2_psra_w:
10280 case Intrinsic::x86_sse2_psra_d:
10281 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010282 case Intrinsic::x86_avx2_psra_d: {
10283 unsigned Opcode;
10284 switch (IntNo) {
10285 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10286 case Intrinsic::x86_sse2_psll_w:
10287 case Intrinsic::x86_sse2_psll_d:
10288 case Intrinsic::x86_sse2_psll_q:
10289 case Intrinsic::x86_avx2_psll_w:
10290 case Intrinsic::x86_avx2_psll_d:
10291 case Intrinsic::x86_avx2_psll_q:
10292 Opcode = X86ISD::VSHL;
10293 break;
10294 case Intrinsic::x86_sse2_psrl_w:
10295 case Intrinsic::x86_sse2_psrl_d:
10296 case Intrinsic::x86_sse2_psrl_q:
10297 case Intrinsic::x86_avx2_psrl_w:
10298 case Intrinsic::x86_avx2_psrl_d:
10299 case Intrinsic::x86_avx2_psrl_q:
10300 Opcode = X86ISD::VSRL;
10301 break;
10302 case Intrinsic::x86_sse2_psra_w:
10303 case Intrinsic::x86_sse2_psra_d:
10304 case Intrinsic::x86_avx2_psra_w:
10305 case Intrinsic::x86_avx2_psra_d:
10306 Opcode = X86ISD::VSRA;
10307 break;
10308 }
10309 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010310 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010311 }
10312
10313 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010314 case Intrinsic::x86_sse2_pslli_w:
10315 case Intrinsic::x86_sse2_pslli_d:
10316 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010317 case Intrinsic::x86_avx2_pslli_w:
10318 case Intrinsic::x86_avx2_pslli_d:
10319 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010320 case Intrinsic::x86_sse2_psrli_w:
10321 case Intrinsic::x86_sse2_psrli_d:
10322 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010323 case Intrinsic::x86_avx2_psrli_w:
10324 case Intrinsic::x86_avx2_psrli_d:
10325 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010326 case Intrinsic::x86_sse2_psrai_w:
10327 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010328 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010329 case Intrinsic::x86_avx2_psrai_d: {
10330 unsigned Opcode;
10331 switch (IntNo) {
10332 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10333 case Intrinsic::x86_sse2_pslli_w:
10334 case Intrinsic::x86_sse2_pslli_d:
10335 case Intrinsic::x86_sse2_pslli_q:
10336 case Intrinsic::x86_avx2_pslli_w:
10337 case Intrinsic::x86_avx2_pslli_d:
10338 case Intrinsic::x86_avx2_pslli_q:
10339 Opcode = X86ISD::VSHLI;
10340 break;
10341 case Intrinsic::x86_sse2_psrli_w:
10342 case Intrinsic::x86_sse2_psrli_d:
10343 case Intrinsic::x86_sse2_psrli_q:
10344 case Intrinsic::x86_avx2_psrli_w:
10345 case Intrinsic::x86_avx2_psrli_d:
10346 case Intrinsic::x86_avx2_psrli_q:
10347 Opcode = X86ISD::VSRLI;
10348 break;
10349 case Intrinsic::x86_sse2_psrai_w:
10350 case Intrinsic::x86_sse2_psrai_d:
10351 case Intrinsic::x86_avx2_psrai_w:
10352 case Intrinsic::x86_avx2_psrai_d:
10353 Opcode = X86ISD::VSRAI;
10354 break;
10355 }
10356 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010357 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010358 }
10359
Craig Topper4feb6472012-08-06 06:22:36 +000010360 case Intrinsic::x86_sse42_pcmpistria128:
10361 case Intrinsic::x86_sse42_pcmpestria128:
10362 case Intrinsic::x86_sse42_pcmpistric128:
10363 case Intrinsic::x86_sse42_pcmpestric128:
10364 case Intrinsic::x86_sse42_pcmpistrio128:
10365 case Intrinsic::x86_sse42_pcmpestrio128:
10366 case Intrinsic::x86_sse42_pcmpistris128:
10367 case Intrinsic::x86_sse42_pcmpestris128:
10368 case Intrinsic::x86_sse42_pcmpistriz128:
10369 case Intrinsic::x86_sse42_pcmpestriz128: {
10370 unsigned Opcode;
10371 unsigned X86CC;
10372 switch (IntNo) {
10373 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10374 case Intrinsic::x86_sse42_pcmpistria128:
10375 Opcode = X86ISD::PCMPISTRI;
10376 X86CC = X86::COND_A;
10377 break;
10378 case Intrinsic::x86_sse42_pcmpestria128:
10379 Opcode = X86ISD::PCMPESTRI;
10380 X86CC = X86::COND_A;
10381 break;
10382 case Intrinsic::x86_sse42_pcmpistric128:
10383 Opcode = X86ISD::PCMPISTRI;
10384 X86CC = X86::COND_B;
10385 break;
10386 case Intrinsic::x86_sse42_pcmpestric128:
10387 Opcode = X86ISD::PCMPESTRI;
10388 X86CC = X86::COND_B;
10389 break;
10390 case Intrinsic::x86_sse42_pcmpistrio128:
10391 Opcode = X86ISD::PCMPISTRI;
10392 X86CC = X86::COND_O;
10393 break;
10394 case Intrinsic::x86_sse42_pcmpestrio128:
10395 Opcode = X86ISD::PCMPESTRI;
10396 X86CC = X86::COND_O;
10397 break;
10398 case Intrinsic::x86_sse42_pcmpistris128:
10399 Opcode = X86ISD::PCMPISTRI;
10400 X86CC = X86::COND_S;
10401 break;
10402 case Intrinsic::x86_sse42_pcmpestris128:
10403 Opcode = X86ISD::PCMPESTRI;
10404 X86CC = X86::COND_S;
10405 break;
10406 case Intrinsic::x86_sse42_pcmpistriz128:
10407 Opcode = X86ISD::PCMPISTRI;
10408 X86CC = X86::COND_E;
10409 break;
10410 case Intrinsic::x86_sse42_pcmpestriz128:
10411 Opcode = X86ISD::PCMPESTRI;
10412 X86CC = X86::COND_E;
10413 break;
10414 }
10415 SmallVector<SDValue, 5> NewOps;
10416 NewOps.append(Op->op_begin()+1, Op->op_end());
10417 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10418 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10419 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10420 DAG.getConstant(X86CC, MVT::i8),
10421 SDValue(PCMP.getNode(), 1));
10422 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10423 }
Craig Topper6d688152012-08-14 07:43:25 +000010424
Craig Topper4feb6472012-08-06 06:22:36 +000010425 case Intrinsic::x86_sse42_pcmpistri128:
10426 case Intrinsic::x86_sse42_pcmpestri128: {
10427 unsigned Opcode;
10428 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10429 Opcode = X86ISD::PCMPISTRI;
10430 else
10431 Opcode = X86ISD::PCMPESTRI;
10432
10433 SmallVector<SDValue, 5> NewOps;
10434 NewOps.append(Op->op_begin()+1, Op->op_end());
10435 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10436 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10437 }
Craig Topper0e292372012-08-24 04:03:22 +000010438 case Intrinsic::x86_fma_vfmadd_ps:
10439 case Intrinsic::x86_fma_vfmadd_pd:
10440 case Intrinsic::x86_fma_vfmsub_ps:
10441 case Intrinsic::x86_fma_vfmsub_pd:
10442 case Intrinsic::x86_fma_vfnmadd_ps:
10443 case Intrinsic::x86_fma_vfnmadd_pd:
10444 case Intrinsic::x86_fma_vfnmsub_ps:
10445 case Intrinsic::x86_fma_vfnmsub_pd:
10446 case Intrinsic::x86_fma_vfmaddsub_ps:
10447 case Intrinsic::x86_fma_vfmaddsub_pd:
10448 case Intrinsic::x86_fma_vfmsubadd_ps:
10449 case Intrinsic::x86_fma_vfmsubadd_pd:
10450 case Intrinsic::x86_fma_vfmadd_ps_256:
10451 case Intrinsic::x86_fma_vfmadd_pd_256:
10452 case Intrinsic::x86_fma_vfmsub_ps_256:
10453 case Intrinsic::x86_fma_vfmsub_pd_256:
10454 case Intrinsic::x86_fma_vfnmadd_ps_256:
10455 case Intrinsic::x86_fma_vfnmadd_pd_256:
10456 case Intrinsic::x86_fma_vfnmsub_ps_256:
10457 case Intrinsic::x86_fma_vfnmsub_pd_256:
10458 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10459 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10460 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10461 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010462 unsigned Opc;
10463 switch (IntNo) {
10464 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10465 case Intrinsic::x86_fma_vfmadd_ps:
10466 case Intrinsic::x86_fma_vfmadd_pd:
10467 case Intrinsic::x86_fma_vfmadd_ps_256:
10468 case Intrinsic::x86_fma_vfmadd_pd_256:
10469 Opc = X86ISD::FMADD;
10470 break;
10471 case Intrinsic::x86_fma_vfmsub_ps:
10472 case Intrinsic::x86_fma_vfmsub_pd:
10473 case Intrinsic::x86_fma_vfmsub_ps_256:
10474 case Intrinsic::x86_fma_vfmsub_pd_256:
10475 Opc = X86ISD::FMSUB;
10476 break;
10477 case Intrinsic::x86_fma_vfnmadd_ps:
10478 case Intrinsic::x86_fma_vfnmadd_pd:
10479 case Intrinsic::x86_fma_vfnmadd_ps_256:
10480 case Intrinsic::x86_fma_vfnmadd_pd_256:
10481 Opc = X86ISD::FNMADD;
10482 break;
10483 case Intrinsic::x86_fma_vfnmsub_ps:
10484 case Intrinsic::x86_fma_vfnmsub_pd:
10485 case Intrinsic::x86_fma_vfnmsub_ps_256:
10486 case Intrinsic::x86_fma_vfnmsub_pd_256:
10487 Opc = X86ISD::FNMSUB;
10488 break;
10489 case Intrinsic::x86_fma_vfmaddsub_ps:
10490 case Intrinsic::x86_fma_vfmaddsub_pd:
10491 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10492 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10493 Opc = X86ISD::FMADDSUB;
10494 break;
10495 case Intrinsic::x86_fma_vfmsubadd_ps:
10496 case Intrinsic::x86_fma_vfmsubadd_pd:
10497 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10498 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10499 Opc = X86ISD::FMSUBADD;
10500 break;
10501 }
10502
10503 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10504 Op.getOperand(2), Op.getOperand(3));
10505 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010506 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010507}
Evan Cheng72261582005-12-20 06:22:03 +000010508
Craig Topper55b24052012-09-11 06:15:32 +000010509static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010510 DebugLoc dl = Op.getDebugLoc();
10511 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10512 switch (IntNo) {
10513 default: return SDValue(); // Don't custom lower most intrinsics.
10514
10515 // RDRAND intrinsics.
10516 case Intrinsic::x86_rdrand_16:
10517 case Intrinsic::x86_rdrand_32:
10518 case Intrinsic::x86_rdrand_64: {
10519 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010520 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10521 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010522
10523 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10524 // return the value from Rand, which is always 0, casted to i32.
10525 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10526 DAG.getConstant(1, Op->getValueType(1)),
10527 DAG.getConstant(X86::COND_B, MVT::i32),
10528 SDValue(Result.getNode(), 1) };
10529 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10530 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10531 Ops, 4);
10532
10533 // Return { result, isValid, chain }.
10534 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010535 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010536 }
10537 }
10538}
10539
Dan Gohmand858e902010-04-17 15:26:15 +000010540SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10541 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010542 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10543 MFI->setReturnAddressIsTaken(true);
10544
Bill Wendling64e87322009-01-16 19:25:27 +000010545 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010546 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010547 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010548
10549 if (Depth > 0) {
10550 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10551 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010552 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10553 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10554 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010555 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010556 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010557 }
10558
10559 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010560 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010561 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010562 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010563}
10564
Dan Gohmand858e902010-04-17 15:26:15 +000010565SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010566 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10567 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010568
Owen Andersone50ed302009-08-10 22:56:29 +000010569 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010570 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010571 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10572 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010573 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010574 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010575 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10576 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010577 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010578 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010579}
10580
Dan Gohman475871a2008-07-27 21:46:04 +000010581SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010582 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010583 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010584}
10585
Dan Gohmand858e902010-04-17 15:26:15 +000010586SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010587 SDValue Chain = Op.getOperand(0);
10588 SDValue Offset = Op.getOperand(1);
10589 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010590 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010591
Dan Gohmand8816272010-08-11 18:14:00 +000010592 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10593 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10594 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010595 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010596
Dan Gohmand8816272010-08-11 18:14:00 +000010597 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010598 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010599 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010600 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10601 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010602 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010603
Dale Johannesene4d209d2009-02-03 20:21:25 +000010604 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010605 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010606 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010607}
10608
Michael Liao6c0e04c2012-10-15 22:39:43 +000010609SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10610 SelectionDAG &DAG) const {
10611 DebugLoc DL = Op.getDebugLoc();
10612 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10613 DAG.getVTList(MVT::i32, MVT::Other),
10614 Op.getOperand(0), Op.getOperand(1));
10615}
10616
10617SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10618 SelectionDAG &DAG) const {
10619 DebugLoc DL = Op.getDebugLoc();
10620 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10621 Op.getOperand(0), Op.getOperand(1));
10622}
10623
Craig Topper55b24052012-09-11 06:15:32 +000010624static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010625 return Op.getOperand(0);
10626}
10627
10628SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10629 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010630 SDValue Root = Op.getOperand(0);
10631 SDValue Trmp = Op.getOperand(1); // trampoline
10632 SDValue FPtr = Op.getOperand(2); // nested function
10633 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010634 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010635
Dan Gohman69de1932008-02-06 22:27:42 +000010636 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010637 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010638
10639 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010640 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010641
10642 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010643 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10644 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010645
Michael Liao7abf67a2012-10-04 19:50:43 +000010646 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10647 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010648
10649 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10650
10651 // Load the pointer to the nested function into R11.
10652 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010653 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010654 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010655 Addr, MachinePointerInfo(TrmpAddr),
10656 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010657
Owen Anderson825b72b2009-08-11 20:47:22 +000010658 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10659 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010660 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10661 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010662 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010663
10664 // Load the 'nest' parameter value into R10.
10665 // R10 is specified in X86CallingConv.td
10666 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010667 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10668 DAG.getConstant(10, MVT::i64));
10669 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010670 Addr, MachinePointerInfo(TrmpAddr, 10),
10671 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010672
Owen Anderson825b72b2009-08-11 20:47:22 +000010673 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10674 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010675 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10676 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010677 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010678
10679 // Jump to the nested function.
10680 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010681 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10682 DAG.getConstant(20, MVT::i64));
10683 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010684 Addr, MachinePointerInfo(TrmpAddr, 20),
10685 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010686
10687 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010688 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10689 DAG.getConstant(22, MVT::i64));
10690 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010691 MachinePointerInfo(TrmpAddr, 22),
10692 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010693
Duncan Sands4a544a72011-09-06 13:37:06 +000010694 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010695 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010696 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010697 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010698 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010699 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010700
10701 switch (CC) {
10702 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010703 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010704 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010705 case CallingConv::X86_StdCall: {
10706 // Pass 'nest' parameter in ECX.
10707 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010708 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010709
10710 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010711 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010712 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010713
Chris Lattner58d74912008-03-12 17:45:29 +000010714 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010715 unsigned InRegCount = 0;
10716 unsigned Idx = 1;
10717
10718 for (FunctionType::param_iterator I = FTy->param_begin(),
10719 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010720 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010721 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010722 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010723
10724 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010725 report_fatal_error("Nest register in use - reduce number of inreg"
10726 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010727 }
10728 }
10729 break;
10730 }
10731 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010732 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010733 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010734 // Pass 'nest' parameter in EAX.
10735 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010736 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010737 break;
10738 }
10739
Dan Gohman475871a2008-07-27 21:46:04 +000010740 SDValue OutChains[4];
10741 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010742
Owen Anderson825b72b2009-08-11 20:47:22 +000010743 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10744 DAG.getConstant(10, MVT::i32));
10745 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010746
Chris Lattnera62fe662010-02-05 19:20:30 +000010747 // This is storing the opcode for MOV32ri.
10748 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010749 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010750 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010751 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010752 Trmp, MachinePointerInfo(TrmpAddr),
10753 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010754
Owen Anderson825b72b2009-08-11 20:47:22 +000010755 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10756 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010757 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10758 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010759 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010760
Chris Lattnera62fe662010-02-05 19:20:30 +000010761 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010762 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10763 DAG.getConstant(5, MVT::i32));
10764 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010765 MachinePointerInfo(TrmpAddr, 5),
10766 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010767
Owen Anderson825b72b2009-08-11 20:47:22 +000010768 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10769 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010770 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10771 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010772 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010773
Duncan Sands4a544a72011-09-06 13:37:06 +000010774 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010775 }
10776}
10777
Dan Gohmand858e902010-04-17 15:26:15 +000010778SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10779 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010780 /*
10781 The rounding mode is in bits 11:10 of FPSR, and has the following
10782 settings:
10783 00 Round to nearest
10784 01 Round to -inf
10785 10 Round to +inf
10786 11 Round to 0
10787
10788 FLT_ROUNDS, on the other hand, expects the following:
10789 -1 Undefined
10790 0 Round to 0
10791 1 Round to nearest
10792 2 Round to +inf
10793 3 Round to -inf
10794
10795 To perform the conversion, we do:
10796 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10797 */
10798
10799 MachineFunction &MF = DAG.getMachineFunction();
10800 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010801 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010802 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010803 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010804 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010805
10806 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010807 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010808 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010809
Michael J. Spencerec38de22010-10-10 22:04:20 +000010810
Chris Lattner2156b792010-09-22 01:11:26 +000010811 MachineMemOperand *MMO =
10812 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10813 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010814
Chris Lattner2156b792010-09-22 01:11:26 +000010815 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10816 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10817 DAG.getVTList(MVT::Other),
10818 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010819
10820 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010821 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010822 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010823
10824 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010825 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010826 DAG.getNode(ISD::SRL, DL, MVT::i16,
10827 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010828 CWD, DAG.getConstant(0x800, MVT::i16)),
10829 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010830 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010831 DAG.getNode(ISD::SRL, DL, MVT::i16,
10832 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010833 CWD, DAG.getConstant(0x400, MVT::i16)),
10834 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010835
Dan Gohman475871a2008-07-27 21:46:04 +000010836 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010837 DAG.getNode(ISD::AND, DL, MVT::i16,
10838 DAG.getNode(ISD::ADD, DL, MVT::i16,
10839 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010840 DAG.getConstant(1, MVT::i16)),
10841 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010842
10843
Duncan Sands83ec4b62008-06-06 12:08:01 +000010844 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010845 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010846}
10847
Craig Topper55b24052012-09-11 06:15:32 +000010848static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010849 EVT VT = Op.getValueType();
10850 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010851 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010852 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010853
10854 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010855 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010856 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010857 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010858 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010859 }
Evan Cheng18efe262007-12-14 02:13:44 +000010860
Evan Cheng152804e2007-12-14 08:30:15 +000010861 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010862 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010863 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010864
10865 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010866 SDValue Ops[] = {
10867 Op,
10868 DAG.getConstant(NumBits+NumBits-1, OpVT),
10869 DAG.getConstant(X86::COND_E, MVT::i8),
10870 Op.getValue(1)
10871 };
10872 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010873
10874 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010875 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010876
Owen Anderson825b72b2009-08-11 20:47:22 +000010877 if (VT == MVT::i8)
10878 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010879 return Op;
10880}
10881
Craig Topper55b24052012-09-11 06:15:32 +000010882static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010883 EVT VT = Op.getValueType();
10884 EVT OpVT = VT;
10885 unsigned NumBits = VT.getSizeInBits();
10886 DebugLoc dl = Op.getDebugLoc();
10887
10888 Op = Op.getOperand(0);
10889 if (VT == MVT::i8) {
10890 // Zero extend to i32 since there is not an i8 bsr.
10891 OpVT = MVT::i32;
10892 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10893 }
10894
10895 // Issue a bsr (scan bits in reverse).
10896 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10897 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10898
10899 // And xor with NumBits-1.
10900 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10901
10902 if (VT == MVT::i8)
10903 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10904 return Op;
10905}
10906
Craig Topper55b24052012-09-11 06:15:32 +000010907static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010908 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010909 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010910 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010911 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010912
10913 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010914 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010915 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010916
10917 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010918 SDValue Ops[] = {
10919 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010920 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010921 DAG.getConstant(X86::COND_E, MVT::i8),
10922 Op.getValue(1)
10923 };
Chandler Carruth77821022011-12-24 12:12:34 +000010924 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010925}
10926
Craig Topper13894fa2011-08-24 06:14:18 +000010927// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10928// ones, and then concatenate the result back.
10929static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010930 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010931
Craig Topper7a9a28b2012-08-12 02:23:29 +000010932 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010933 "Unsupported value type for operation");
10934
Craig Topper66ddd152012-04-27 22:54:43 +000010935 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010936 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010937
10938 // Extract the LHS vectors
10939 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010940 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10941 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010942
10943 // Extract the RHS vectors
10944 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010945 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10946 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010947
10948 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10949 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10950
10951 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10952 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10953 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10954}
10955
Craig Topper55b24052012-09-11 06:15:32 +000010956static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010957 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010958 Op.getValueType().isInteger() &&
10959 "Only handle AVX 256-bit vector integer operation");
10960 return Lower256IntArith(Op, DAG);
10961}
10962
Craig Topper55b24052012-09-11 06:15:32 +000010963static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010964 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010965 Op.getValueType().isInteger() &&
10966 "Only handle AVX 256-bit vector integer operation");
10967 return Lower256IntArith(Op, DAG);
10968}
10969
Craig Topper55b24052012-09-11 06:15:32 +000010970static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10971 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010972 EVT VT = Op.getValueType();
10973
10974 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010975 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010976 return Lower256IntArith(Op, DAG);
10977
Craig Topper5b209e82012-02-05 03:14:49 +000010978 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10979 "Only know how to lower V2I64/V4I64 multiply");
10980
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010981 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010982
Craig Topper5b209e82012-02-05 03:14:49 +000010983 // Ahi = psrlqi(a, 32);
10984 // Bhi = psrlqi(b, 32);
10985 //
10986 // AloBlo = pmuludq(a, b);
10987 // AloBhi = pmuludq(a, Bhi);
10988 // AhiBlo = pmuludq(Ahi, b);
10989
10990 // AloBhi = psllqi(AloBhi, 32);
10991 // AhiBlo = psllqi(AhiBlo, 32);
10992 // return AloBlo + AloBhi + AhiBlo;
10993
Craig Topperaaa643c2011-11-09 07:28:55 +000010994 SDValue A = Op.getOperand(0);
10995 SDValue B = Op.getOperand(1);
10996
Craig Topper5b209e82012-02-05 03:14:49 +000010997 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010998
Craig Topper5b209e82012-02-05 03:14:49 +000010999 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11000 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011001
Craig Topper5b209e82012-02-05 03:14:49 +000011002 // Bit cast to 32-bit vectors for MULUDQ
11003 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11004 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11005 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11006 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11007 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011008
Craig Topper5b209e82012-02-05 03:14:49 +000011009 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11010 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11011 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011012
Craig Topper5b209e82012-02-05 03:14:49 +000011013 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11014 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011015
Dale Johannesene4d209d2009-02-03 20:21:25 +000011016 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011017 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011018}
11019
Nadav Rotem43012222011-05-11 08:12:09 +000011020SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11021
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011022 EVT VT = Op.getValueType();
11023 DebugLoc dl = Op.getDebugLoc();
11024 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011025 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011026 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011027
Craig Topper1accb7e2012-01-10 06:54:16 +000011028 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011029 return SDValue();
11030
Nadav Rotem43012222011-05-11 08:12:09 +000011031 // Optimize shl/srl/sra with constant shift amount.
11032 if (isSplatVector(Amt.getNode())) {
11033 SDValue SclrAmt = Amt->getOperand(0);
11034 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11035 uint64_t ShiftAmt = C->getZExtValue();
11036
Craig Toppered2e13d2012-01-22 19:15:14 +000011037 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
11038 (Subtarget->hasAVX2() &&
11039 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11040 if (Op.getOpcode() == ISD::SHL)
11041 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11042 DAG.getConstant(ShiftAmt, MVT::i32));
11043 if (Op.getOpcode() == ISD::SRL)
11044 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11045 DAG.getConstant(ShiftAmt, MVT::i32));
11046 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11047 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11048 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011049 }
11050
Craig Toppered2e13d2012-01-22 19:15:14 +000011051 if (VT == MVT::v16i8) {
11052 if (Op.getOpcode() == ISD::SHL) {
11053 // Make a large shift.
11054 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11055 DAG.getConstant(ShiftAmt, MVT::i32));
11056 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11057 // Zero out the rightmost bits.
11058 SmallVector<SDValue, 16> V(16,
11059 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11060 MVT::i8));
11061 return DAG.getNode(ISD::AND, dl, VT, SHL,
11062 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011063 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011064 if (Op.getOpcode() == ISD::SRL) {
11065 // Make a large shift.
11066 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11067 DAG.getConstant(ShiftAmt, MVT::i32));
11068 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11069 // Zero out the leftmost bits.
11070 SmallVector<SDValue, 16> V(16,
11071 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11072 MVT::i8));
11073 return DAG.getNode(ISD::AND, dl, VT, SRL,
11074 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11075 }
11076 if (Op.getOpcode() == ISD::SRA) {
11077 if (ShiftAmt == 7) {
11078 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011079 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011080 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011081 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011082
Craig Toppered2e13d2012-01-22 19:15:14 +000011083 // R s>> a === ((R u>> a) ^ m) - m
11084 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11085 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11086 MVT::i8));
11087 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11088 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11089 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11090 return Res;
11091 }
Craig Topper731dfd02012-04-23 03:42:40 +000011092 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011093 }
Craig Topper46154eb2011-11-11 07:39:23 +000011094
Craig Topper0d86d462011-11-20 00:12:05 +000011095 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
11096 if (Op.getOpcode() == ISD::SHL) {
11097 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011098 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11099 DAG.getConstant(ShiftAmt, MVT::i32));
11100 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011101 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011102 SmallVector<SDValue, 32> V(32,
11103 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11104 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011105 return DAG.getNode(ISD::AND, dl, VT, SHL,
11106 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011107 }
Craig Topper0d86d462011-11-20 00:12:05 +000011108 if (Op.getOpcode() == ISD::SRL) {
11109 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011110 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11111 DAG.getConstant(ShiftAmt, MVT::i32));
11112 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011113 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011114 SmallVector<SDValue, 32> V(32,
11115 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11116 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011117 return DAG.getNode(ISD::AND, dl, VT, SRL,
11118 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11119 }
11120 if (Op.getOpcode() == ISD::SRA) {
11121 if (ShiftAmt == 7) {
11122 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011123 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011124 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011125 }
11126
11127 // R s>> a === ((R u>> a) ^ m) - m
11128 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11129 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11130 MVT::i8));
11131 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11132 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11133 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11134 return Res;
11135 }
Craig Topper731dfd02012-04-23 03:42:40 +000011136 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011137 }
Nadav Rotem43012222011-05-11 08:12:09 +000011138 }
11139 }
11140
11141 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011142 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011143 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11144 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011145
Chris Lattner7302d802012-02-06 21:56:39 +000011146 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11147 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011148 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11149 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011150 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011151 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011152
11153 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011154 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011155 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11156 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11157 }
Nadav Rotem43012222011-05-11 08:12:09 +000011158 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011159 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011160
Nate Begeman51409212010-07-28 00:21:48 +000011161 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011162 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11163 DAG.getConstant(5, MVT::i32));
11164 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011165
Lang Hames8b99c1e2011-12-17 01:08:46 +000011166 // Turn 'a' into a mask suitable for VSELECT
11167 SDValue VSelM = DAG.getConstant(0x80, VT);
11168 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011169 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011170
Lang Hames8b99c1e2011-12-17 01:08:46 +000011171 SDValue CM1 = DAG.getConstant(0x0f, VT);
11172 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011173
Lang Hames8b99c1e2011-12-17 01:08:46 +000011174 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11175 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011176 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11177 DAG.getConstant(4, MVT::i32), DAG);
11178 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011179 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11180
Nate Begeman51409212010-07-28 00:21:48 +000011181 // a += a
11182 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011183 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011184 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011185
Lang Hames8b99c1e2011-12-17 01:08:46 +000011186 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11187 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011188 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11189 DAG.getConstant(2, MVT::i32), DAG);
11190 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011191 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11192
Nate Begeman51409212010-07-28 00:21:48 +000011193 // a += a
11194 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011195 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011196 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011197
Lang Hames8b99c1e2011-12-17 01:08:46 +000011198 // return VSELECT(r, r+r, a);
11199 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011200 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011201 return R;
11202 }
Craig Topper46154eb2011-11-11 07:39:23 +000011203
11204 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011205 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011206 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011207 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11208 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11209
11210 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011211 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11212 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011213
11214 // Recreate the shift amount vectors
11215 SDValue Amt1, Amt2;
11216 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11217 // Constant shift amount
11218 SmallVector<SDValue, 4> Amt1Csts;
11219 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011220 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011221 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011222 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011223 Amt2Csts.push_back(Amt->getOperand(i));
11224
11225 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11226 &Amt1Csts[0], NumElems/2);
11227 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11228 &Amt2Csts[0], NumElems/2);
11229 } else {
11230 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011231 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11232 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011233 }
11234
11235 // Issue new vector shifts for the smaller types
11236 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11237 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11238
11239 // Concatenate the result back
11240 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11241 }
11242
Nate Begeman51409212010-07-28 00:21:48 +000011243 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011244}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011245
Craig Topper55b24052012-09-11 06:15:32 +000011246static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011247 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11248 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011249 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11250 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011251 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011252 SDValue LHS = N->getOperand(0);
11253 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011254 unsigned BaseOp = 0;
11255 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011256 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011257 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011258 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011259 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011260 // A subtract of one will be selected as a INC. Note that INC doesn't
11261 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011262 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11263 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011264 BaseOp = X86ISD::INC;
11265 Cond = X86::COND_O;
11266 break;
11267 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011268 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011269 Cond = X86::COND_O;
11270 break;
11271 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011272 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011273 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011274 break;
11275 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011276 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11277 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011278 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11279 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011280 BaseOp = X86ISD::DEC;
11281 Cond = X86::COND_O;
11282 break;
11283 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011284 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011285 Cond = X86::COND_O;
11286 break;
11287 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011288 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011289 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011290 break;
11291 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011292 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011293 Cond = X86::COND_O;
11294 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011295 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11296 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11297 MVT::i32);
11298 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011299
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011300 SDValue SetCC =
11301 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11302 DAG.getConstant(X86::COND_O, MVT::i32),
11303 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011304
Dan Gohman6e5fda22011-07-22 18:45:15 +000011305 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011306 }
Bill Wendling74c37652008-12-09 22:08:41 +000011307 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011308
Bill Wendling61edeb52008-12-02 01:06:39 +000011309 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011310 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011311 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011312
Bill Wendling61edeb52008-12-02 01:06:39 +000011313 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011314 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11315 DAG.getConstant(Cond, MVT::i32),
11316 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011317
Dan Gohman6e5fda22011-07-22 18:45:15 +000011318 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011319}
11320
Chad Rosier30450e82011-12-22 22:35:21 +000011321SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11322 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011323 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011324 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11325 EVT VT = Op.getValueType();
11326
Craig Toppered2e13d2012-01-22 19:15:14 +000011327 if (!Subtarget->hasSSE2() || !VT.isVector())
11328 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011329
Craig Toppered2e13d2012-01-22 19:15:14 +000011330 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11331 ExtraVT.getScalarType().getSizeInBits();
11332 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11333
11334 switch (VT.getSimpleVT().SimpleTy) {
11335 default: return SDValue();
11336 case MVT::v8i32:
11337 case MVT::v16i16:
11338 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011339 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011340 if (!Subtarget->hasAVX2()) {
11341 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011342 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011343
Craig Toppered2e13d2012-01-22 19:15:14 +000011344 // Extract the LHS vectors
11345 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011346 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11347 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011348
Craig Toppered2e13d2012-01-22 19:15:14 +000011349 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11350 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011351
Craig Toppered2e13d2012-01-22 19:15:14 +000011352 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011353 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011354 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11355 ExtraNumElems/2);
11356 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011357
Craig Toppered2e13d2012-01-22 19:15:14 +000011358 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11359 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011360
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011361 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011362 }
11363 // fall through
11364 case MVT::v4i32:
11365 case MVT::v8i16: {
11366 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11367 Op.getOperand(0), ShAmt, DAG);
11368 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011369 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011370 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011371}
11372
11373
Craig Topper55b24052012-09-11 06:15:32 +000011374static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11375 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011376 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011377
Eric Christopher77ed1352011-07-08 00:04:56 +000011378 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11379 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011380 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011381 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011382 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011383 SDValue Ops[] = {
11384 DAG.getRegister(X86::ESP, MVT::i32), // Base
11385 DAG.getTargetConstant(1, MVT::i8), // Scale
11386 DAG.getRegister(0, MVT::i32), // Index
11387 DAG.getTargetConstant(0, MVT::i32), // Disp
11388 DAG.getRegister(0, MVT::i32), // Segment.
11389 Zero,
11390 Chain
11391 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011392 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011393 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11394 array_lengthof(Ops));
11395 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011396 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011397
Eric Christopher9a9d2752010-07-22 02:48:34 +000011398 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011399 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011400 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011401
Chris Lattner132929a2010-08-14 17:26:09 +000011402 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11403 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11404 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11405 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011406
Chris Lattner132929a2010-08-14 17:26:09 +000011407 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11408 if (!Op1 && !Op2 && !Op3 && Op4)
11409 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011410
Chris Lattner132929a2010-08-14 17:26:09 +000011411 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11412 if (Op1 && !Op2 && !Op3 && !Op4)
11413 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011414
11415 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011416 // (MFENCE)>;
11417 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011418}
11419
Craig Topper55b24052012-09-11 06:15:32 +000011420static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11421 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011422 DebugLoc dl = Op.getDebugLoc();
11423 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11424 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11425 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11426 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11427
11428 // The only fence that needs an instruction is a sequentially-consistent
11429 // cross-thread fence.
11430 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11431 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11432 // no-sse2). There isn't any reason to disable it if the target processor
11433 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011434 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011435 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11436
11437 SDValue Chain = Op.getOperand(0);
11438 SDValue Zero = DAG.getConstant(0, MVT::i32);
11439 SDValue Ops[] = {
11440 DAG.getRegister(X86::ESP, MVT::i32), // Base
11441 DAG.getTargetConstant(1, MVT::i8), // Scale
11442 DAG.getRegister(0, MVT::i32), // Index
11443 DAG.getTargetConstant(0, MVT::i32), // Disp
11444 DAG.getRegister(0, MVT::i32), // Segment.
11445 Zero,
11446 Chain
11447 };
11448 SDNode *Res =
11449 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11450 array_lengthof(Ops));
11451 return SDValue(Res, 0);
11452 }
11453
11454 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11455 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11456}
11457
11458
Craig Topper55b24052012-09-11 06:15:32 +000011459static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11460 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011461 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011462 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011463 unsigned Reg = 0;
11464 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011465 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011466 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 case MVT::i8: Reg = X86::AL; size = 1; break;
11468 case MVT::i16: Reg = X86::AX; size = 2; break;
11469 case MVT::i32: Reg = X86::EAX; size = 4; break;
11470 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011471 assert(Subtarget->is64Bit() && "Node not type legal!");
11472 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011473 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011474 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011475 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011476 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011477 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011478 Op.getOperand(1),
11479 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011481 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011482 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011483 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11484 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11485 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011486 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011487 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011488 return cpOut;
11489}
11490
Craig Topper55b24052012-09-11 06:15:32 +000011491static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11492 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011493 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011494 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011495 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011496 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011497 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11499 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011500 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011501 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11502 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011503 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011504 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011505 rdx.getValue(1)
11506 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011507 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011508}
11509
Craig Topper55b24052012-09-11 06:15:32 +000011510SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011511 EVT SrcVT = Op.getOperand(0).getValueType();
11512 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011513 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011514 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011515 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011516 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011517 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011518 // i64 <=> MMX conversions are Legal.
11519 if (SrcVT==MVT::i64 && DstVT.isVector())
11520 return Op;
11521 if (DstVT==MVT::i64 && SrcVT.isVector())
11522 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011523 // MMX <=> MMX conversions are Legal.
11524 if (SrcVT.isVector() && DstVT.isVector())
11525 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011526 // All other conversions need to be expanded.
11527 return SDValue();
11528}
Chris Lattner5b856542010-12-20 00:59:46 +000011529
Craig Topper55b24052012-09-11 06:15:32 +000011530static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011531 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011532 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011533 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011534 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011535 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011536 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011537 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011538 Node->getOperand(0),
11539 Node->getOperand(1), negOp,
11540 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011541 cast<AtomicSDNode>(Node)->getAlignment(),
11542 cast<AtomicSDNode>(Node)->getOrdering(),
11543 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011544}
11545
Eli Friedman327236c2011-08-24 20:50:09 +000011546static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11547 SDNode *Node = Op.getNode();
11548 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011549 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011550
11551 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011552 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11553 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11554 // (The only way to get a 16-byte store is cmpxchg16b)
11555 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11556 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11557 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011558 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11559 cast<AtomicSDNode>(Node)->getMemoryVT(),
11560 Node->getOperand(0),
11561 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011562 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011563 cast<AtomicSDNode>(Node)->getOrdering(),
11564 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011565 return Swap.getValue(1);
11566 }
11567 // Other atomic stores have a simple pattern.
11568 return Op;
11569}
11570
Chris Lattner5b856542010-12-20 00:59:46 +000011571static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11572 EVT VT = Op.getNode()->getValueType(0);
11573
11574 // Let legalize expand this if it isn't a legal type yet.
11575 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11576 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011577
Chris Lattner5b856542010-12-20 00:59:46 +000011578 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011579
Chris Lattner5b856542010-12-20 00:59:46 +000011580 unsigned Opc;
11581 bool ExtraOp = false;
11582 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011583 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011584 case ISD::ADDC: Opc = X86ISD::ADD; break;
11585 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11586 case ISD::SUBC: Opc = X86ISD::SUB; break;
11587 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11588 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011589
Chris Lattner5b856542010-12-20 00:59:46 +000011590 if (!ExtraOp)
11591 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11592 Op.getOperand(1));
11593 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11594 Op.getOperand(1), Op.getOperand(2));
11595}
11596
Evan Cheng0db9fe62006-04-25 20:13:52 +000011597/// LowerOperation - Provide custom lowering hooks for some operations.
11598///
Dan Gohmand858e902010-04-17 15:26:15 +000011599SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011600 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011601 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011602 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011603 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11604 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11605 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011606 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011607 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011608 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011609 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011610 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11611 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11612 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011613 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11614 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011615 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11616 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11617 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011618 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011619 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011620 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011621 case ISD::SHL_PARTS:
11622 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011623 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011624 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011625 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011626 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Michael Liaoa7554632012-10-23 17:36:08 +000011627 case ISD::ZERO_EXTEND: return lowerZERO_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011628 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011629 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011630 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011631 case ISD::FABS: return LowerFABS(Op, DAG);
11632 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011633 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011634 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011635 case ISD::SETCC: return LowerSETCC(Op, DAG);
11636 case ISD::SELECT: return LowerSELECT(Op, DAG);
11637 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011638 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011639 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011640 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011641 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011642 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011643 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011644 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11645 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011646 case ISD::FRAME_TO_ARGS_OFFSET:
11647 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011648 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011649 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011650 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11651 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011652 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11653 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011654 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011655 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011656 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011657 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011658 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011659 case ISD::SRA:
11660 case ISD::SRL:
11661 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011662 case ISD::SADDO:
11663 case ISD::UADDO:
11664 case ISD::SSUBO:
11665 case ISD::USUBO:
11666 case ISD::SMULO:
11667 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011668 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011669 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011670 case ISD::ADDC:
11671 case ISD::ADDE:
11672 case ISD::SUBC:
11673 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011674 case ISD::ADD: return LowerADD(Op, DAG);
11675 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011676 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011677}
11678
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011679static void ReplaceATOMIC_LOAD(SDNode *Node,
11680 SmallVectorImpl<SDValue> &Results,
11681 SelectionDAG &DAG) {
11682 DebugLoc dl = Node->getDebugLoc();
11683 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11684
11685 // Convert wide load -> cmpxchg8b/cmpxchg16b
11686 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11687 // (The only way to get a 16-byte load is cmpxchg16b)
11688 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011689 SDValue Zero = DAG.getConstant(0, VT);
11690 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011691 Node->getOperand(0),
11692 Node->getOperand(1), Zero, Zero,
11693 cast<AtomicSDNode>(Node)->getMemOperand(),
11694 cast<AtomicSDNode>(Node)->getOrdering(),
11695 cast<AtomicSDNode>(Node)->getSynchScope());
11696 Results.push_back(Swap.getValue(0));
11697 Results.push_back(Swap.getValue(1));
11698}
11699
Craig Topperc0878702012-08-17 06:55:11 +000011700static void
Duncan Sands1607f052008-12-01 11:39:25 +000011701ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011702 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011703 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011704 assert (Node->getValueType(0) == MVT::i64 &&
11705 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011706
11707 SDValue Chain = Node->getOperand(0);
11708 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011709 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011710 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011711 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011712 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011713 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011714 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011715 SDValue Result =
11716 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11717 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011718 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011719 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011720 Results.push_back(Result.getValue(2));
11721}
11722
Duncan Sands126d9072008-07-04 11:47:58 +000011723/// ReplaceNodeResults - Replace a node with an illegal result type
11724/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011725void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11726 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011727 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011728 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011729 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011730 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011731 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011732 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011733 case ISD::ADDC:
11734 case ISD::ADDE:
11735 case ISD::SUBC:
11736 case ISD::SUBE:
11737 // We don't want to expand or promote these.
11738 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011739 case ISD::FP_TO_SINT:
11740 case ISD::FP_TO_UINT: {
11741 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11742
11743 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11744 return;
11745
Eli Friedman948e95a2009-05-23 09:59:16 +000011746 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011747 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011748 SDValue FIST = Vals.first, StackSlot = Vals.second;
11749 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011750 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011751 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011752 if (StackSlot.getNode() != 0)
11753 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11754 MachinePointerInfo(),
11755 false, false, false, 0));
11756 else
11757 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011758 }
11759 return;
11760 }
Michael Liao991b6a22012-10-24 04:09:32 +000011761 case ISD::UINT_TO_FP: {
11762 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
11763 N->getValueType(0) != MVT::v2f32)
11764 return;
11765 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
11766 N->getOperand(0));
11767 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
11768 MVT::f64);
11769 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
11770 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
11771 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
11772 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
11773 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
11774 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
11775 return;
11776 }
Michael Liao44c2d612012-10-10 16:53:28 +000011777 case ISD::FP_ROUND: {
11778 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11779 Results.push_back(V);
11780 return;
11781 }
Duncan Sands1607f052008-12-01 11:39:25 +000011782 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011783 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011784 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011785 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011786 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011787 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011788 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011789 eax.getValue(2));
11790 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11791 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011792 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011793 Results.push_back(edx.getValue(1));
11794 return;
11795 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011796 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011797 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011798 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011799 bool Regs64bit = T == MVT::i128;
11800 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011801 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011802 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11803 DAG.getConstant(0, HalfT));
11804 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11805 DAG.getConstant(1, HalfT));
11806 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11807 Regs64bit ? X86::RAX : X86::EAX,
11808 cpInL, SDValue());
11809 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11810 Regs64bit ? X86::RDX : X86::EDX,
11811 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011812 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011813 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11814 DAG.getConstant(0, HalfT));
11815 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11816 DAG.getConstant(1, HalfT));
11817 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11818 Regs64bit ? X86::RBX : X86::EBX,
11819 swapInL, cpInH.getValue(1));
11820 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011821 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011822 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011823 SDValue Ops[] = { swapInH.getValue(0),
11824 N->getOperand(1),
11825 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011826 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011827 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011828 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11829 X86ISD::LCMPXCHG8_DAG;
11830 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011831 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011832 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11833 Regs64bit ? X86::RAX : X86::EAX,
11834 HalfT, Result.getValue(1));
11835 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11836 Regs64bit ? X86::RDX : X86::EDX,
11837 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011838 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011839 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011840 Results.push_back(cpOutH.getValue(1));
11841 return;
11842 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011843 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011844 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011845 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011846 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011847 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011848 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011849 case ISD::ATOMIC_LOAD_MAX:
11850 case ISD::ATOMIC_LOAD_MIN:
11851 case ISD::ATOMIC_LOAD_UMAX:
11852 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011853 case ISD::ATOMIC_SWAP: {
11854 unsigned Opc;
11855 switch (N->getOpcode()) {
11856 default: llvm_unreachable("Unexpected opcode");
11857 case ISD::ATOMIC_LOAD_ADD:
11858 Opc = X86ISD::ATOMADD64_DAG;
11859 break;
11860 case ISD::ATOMIC_LOAD_AND:
11861 Opc = X86ISD::ATOMAND64_DAG;
11862 break;
11863 case ISD::ATOMIC_LOAD_NAND:
11864 Opc = X86ISD::ATOMNAND64_DAG;
11865 break;
11866 case ISD::ATOMIC_LOAD_OR:
11867 Opc = X86ISD::ATOMOR64_DAG;
11868 break;
11869 case ISD::ATOMIC_LOAD_SUB:
11870 Opc = X86ISD::ATOMSUB64_DAG;
11871 break;
11872 case ISD::ATOMIC_LOAD_XOR:
11873 Opc = X86ISD::ATOMXOR64_DAG;
11874 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011875 case ISD::ATOMIC_LOAD_MAX:
11876 Opc = X86ISD::ATOMMAX64_DAG;
11877 break;
11878 case ISD::ATOMIC_LOAD_MIN:
11879 Opc = X86ISD::ATOMMIN64_DAG;
11880 break;
11881 case ISD::ATOMIC_LOAD_UMAX:
11882 Opc = X86ISD::ATOMUMAX64_DAG;
11883 break;
11884 case ISD::ATOMIC_LOAD_UMIN:
11885 Opc = X86ISD::ATOMUMIN64_DAG;
11886 break;
Craig Topperc0878702012-08-17 06:55:11 +000011887 case ISD::ATOMIC_SWAP:
11888 Opc = X86ISD::ATOMSWAP64_DAG;
11889 break;
11890 }
11891 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011892 return;
Craig Topperc0878702012-08-17 06:55:11 +000011893 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011894 case ISD::ATOMIC_LOAD:
11895 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011896 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011897}
11898
Evan Cheng72261582005-12-20 06:22:03 +000011899const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11900 switch (Opcode) {
11901 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011902 case X86ISD::BSF: return "X86ISD::BSF";
11903 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011904 case X86ISD::SHLD: return "X86ISD::SHLD";
11905 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011906 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011907 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011908 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011909 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011910 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011911 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011912 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11913 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11914 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011915 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011916 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011917 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011918 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011919 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011920 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011921 case X86ISD::COMI: return "X86ISD::COMI";
11922 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011923 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011924 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011925 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11926 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011927 case X86ISD::CMOV: return "X86ISD::CMOV";
11928 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011929 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011930 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11931 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011932 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011933 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011934 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011935 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011936 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011937 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11938 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011939 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011940 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011941 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011942 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011943 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011944 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11945 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11946 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011947 case X86ISD::HADD: return "X86ISD::HADD";
11948 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011949 case X86ISD::FHADD: return "X86ISD::FHADD";
11950 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011951 case X86ISD::FMAX: return "X86ISD::FMAX";
11952 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011953 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11954 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011955 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11956 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011957 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011958 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011959 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000011960 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
11961 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011962 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011963 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011964 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011965 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011966 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11967 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011968 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11969 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11970 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11971 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11972 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11973 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011974 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011975 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011976 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000011977 case X86ISD::VZEXT: return "X86ISD::VZEXT";
11978 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000011979 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000011980 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000011981 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11982 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011983 case X86ISD::VSHL: return "X86ISD::VSHL";
11984 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011985 case X86ISD::VSRA: return "X86ISD::VSRA";
11986 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11987 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11988 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011989 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011990 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11991 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011992 case X86ISD::ADD: return "X86ISD::ADD";
11993 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011994 case X86ISD::ADC: return "X86ISD::ADC";
11995 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011996 case X86ISD::SMUL: return "X86ISD::SMUL";
11997 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011998 case X86ISD::INC: return "X86ISD::INC";
11999 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012000 case X86ISD::OR: return "X86ISD::OR";
12001 case X86ISD::XOR: return "X86ISD::XOR";
12002 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000012003 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000012004 case X86ISD::BLSI: return "X86ISD::BLSI";
12005 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12006 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012007 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012008 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012009 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012010 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12011 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12012 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012013 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012014 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012015 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012016 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012017 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012018 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12019 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012020 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12021 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12022 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012023 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12024 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012025 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12026 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012027 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012028 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012029 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012030 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12031 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012032 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012033 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012034 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012035 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012036 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012037 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012038 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012039 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012040 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012041 case X86ISD::FMADD: return "X86ISD::FMADD";
12042 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12043 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12044 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12045 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12046 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000012047 }
12048}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012049
Chris Lattnerc9addb72007-03-30 23:15:24 +000012050// isLegalAddressingMode - Return true if the addressing mode represented
12051// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012052bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012053 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012054 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012055 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012056 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012057
Chris Lattnerc9addb72007-03-30 23:15:24 +000012058 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012059 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012060 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012061
Chris Lattnerc9addb72007-03-30 23:15:24 +000012062 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012063 unsigned GVFlags =
12064 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012065
Chris Lattnerdfed4132009-07-10 07:38:24 +000012066 // If a reference to this global requires an extra load, we can't fold it.
12067 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012068 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012069
Chris Lattnerdfed4132009-07-10 07:38:24 +000012070 // If BaseGV requires a register for the PIC base, we cannot also have a
12071 // BaseReg specified.
12072 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012073 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012074
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012075 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012076 if ((M != CodeModel::Small || R != Reloc::Static) &&
12077 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012078 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012079 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012080
Chris Lattnerc9addb72007-03-30 23:15:24 +000012081 switch (AM.Scale) {
12082 case 0:
12083 case 1:
12084 case 2:
12085 case 4:
12086 case 8:
12087 // These scales always work.
12088 break;
12089 case 3:
12090 case 5:
12091 case 9:
12092 // These scales are formed with basereg+scalereg. Only accept if there is
12093 // no basereg yet.
12094 if (AM.HasBaseReg)
12095 return false;
12096 break;
12097 default: // Other stuff never works.
12098 return false;
12099 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012100
Chris Lattnerc9addb72007-03-30 23:15:24 +000012101 return true;
12102}
12103
12104
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012105bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012106 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012107 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012108 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12109 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012110 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000012111 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012112 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012113}
12114
Evan Cheng70e10d32012-07-17 06:53:39 +000012115bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12116 return Imm == (int32_t)Imm;
12117}
12118
12119bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012120 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000012121 return Imm == (int32_t)Imm;
12122}
12123
Owen Andersone50ed302009-08-10 22:56:29 +000012124bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012125 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012126 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012127 unsigned NumBits1 = VT1.getSizeInBits();
12128 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012129 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012130 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012131 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012132}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012133
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012134bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012135 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012136 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012137}
12138
Owen Andersone50ed302009-08-10 22:56:29 +000012139bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012140 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012141 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012142}
12143
Owen Andersone50ed302009-08-10 22:56:29 +000012144bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012145 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012146 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012147}
12148
Evan Cheng60c07e12006-07-05 22:17:51 +000012149/// isShuffleMaskLegal - Targets can use this to indicate that they only
12150/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12151/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12152/// are assumed to be legal.
12153bool
Eric Christopherfd179292009-08-27 18:07:15 +000012154X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012155 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012156 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012157 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012158 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012159
Nate Begemana09008b2009-10-19 02:17:23 +000012160 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012161 return (VT.getVectorNumElements() == 2 ||
12162 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12163 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012164 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012165 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000012166 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
12167 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012168 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000012169 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
12170 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000012171 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
12172 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012173}
12174
Dan Gohman7d8143f2008-04-09 20:09:42 +000012175bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012176X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012177 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012178 unsigned NumElts = VT.getVectorNumElements();
12179 // FIXME: This collection of masks seems suspect.
12180 if (NumElts == 2)
12181 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012182 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012183 return (isMOVLMask(Mask, VT) ||
12184 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012185 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
12186 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012187 }
12188 return false;
12189}
12190
12191//===----------------------------------------------------------------------===//
12192// X86 Scheduler Hooks
12193//===----------------------------------------------------------------------===//
12194
Mon P Wang63307c32008-05-05 19:05:59 +000012195// private utility function
Scott Michelfdc40a02009-02-17 22:15:04 +000012196
Michael Liaob118a072012-09-20 03:06:15 +000012197// Get CMPXCHG opcode for the specified data type.
12198static unsigned getCmpXChgOpcode(EVT VT) {
12199 switch (VT.getSimpleVT().SimpleTy) {
12200 case MVT::i8: return X86::LCMPXCHG8;
12201 case MVT::i16: return X86::LCMPXCHG16;
12202 case MVT::i32: return X86::LCMPXCHG32;
12203 case MVT::i64: return X86::LCMPXCHG64;
12204 default:
12205 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012206 }
Michael Liaob118a072012-09-20 03:06:15 +000012207 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012208}
12209
Michael Liaob118a072012-09-20 03:06:15 +000012210// Get LOAD opcode for the specified data type.
12211static unsigned getLoadOpcode(EVT VT) {
12212 switch (VT.getSimpleVT().SimpleTy) {
12213 case MVT::i8: return X86::MOV8rm;
12214 case MVT::i16: return X86::MOV16rm;
12215 case MVT::i32: return X86::MOV32rm;
12216 case MVT::i64: return X86::MOV64rm;
12217 default:
12218 break;
12219 }
12220 llvm_unreachable("Invalid operand size!");
12221}
12222
12223// Get opcode of the non-atomic one from the specified atomic instruction.
12224static unsigned getNonAtomicOpcode(unsigned Opc) {
12225 switch (Opc) {
12226 case X86::ATOMAND8: return X86::AND8rr;
12227 case X86::ATOMAND16: return X86::AND16rr;
12228 case X86::ATOMAND32: return X86::AND32rr;
12229 case X86::ATOMAND64: return X86::AND64rr;
12230 case X86::ATOMOR8: return X86::OR8rr;
12231 case X86::ATOMOR16: return X86::OR16rr;
12232 case X86::ATOMOR32: return X86::OR32rr;
12233 case X86::ATOMOR64: return X86::OR64rr;
12234 case X86::ATOMXOR8: return X86::XOR8rr;
12235 case X86::ATOMXOR16: return X86::XOR16rr;
12236 case X86::ATOMXOR32: return X86::XOR32rr;
12237 case X86::ATOMXOR64: return X86::XOR64rr;
12238 }
12239 llvm_unreachable("Unhandled atomic-load-op opcode!");
12240}
12241
12242// Get opcode of the non-atomic one from the specified atomic instruction with
12243// extra opcode.
12244static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12245 unsigned &ExtraOpc) {
12246 switch (Opc) {
12247 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12248 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12249 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12250 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012251 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012252 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12253 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12254 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012255 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012256 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12257 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12258 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012259 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012260 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12261 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12262 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012263 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012264 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12265 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12266 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12267 }
12268 llvm_unreachable("Unhandled atomic-load-op opcode!");
12269}
12270
12271// Get opcode of the non-atomic one from the specified atomic instruction for
12272// 64-bit data type on 32-bit target.
12273static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12274 switch (Opc) {
12275 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12276 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12277 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12278 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12279 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12280 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012281 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12282 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12283 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12284 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012285 }
12286 llvm_unreachable("Unhandled atomic-load-op opcode!");
12287}
12288
12289// Get opcode of the non-atomic one from the specified atomic instruction for
12290// 64-bit data type on 32-bit target with extra opcode.
12291static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12292 unsigned &HiOpc,
12293 unsigned &ExtraOpc) {
12294 switch (Opc) {
12295 case X86::ATOMNAND6432:
12296 ExtraOpc = X86::NOT32r;
12297 HiOpc = X86::AND32rr;
12298 return X86::AND32rr;
12299 }
12300 llvm_unreachable("Unhandled atomic-load-op opcode!");
12301}
12302
12303// Get pseudo CMOV opcode from the specified data type.
12304static unsigned getPseudoCMOVOpc(EVT VT) {
12305 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012306 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012307 case MVT::i16: return X86::CMOV_GR16;
12308 case MVT::i32: return X86::CMOV_GR32;
12309 default:
12310 break;
12311 }
12312 llvm_unreachable("Unknown CMOV opcode!");
12313}
12314
12315// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12316// They will be translated into a spin-loop or compare-exchange loop from
12317//
12318// ...
12319// dst = atomic-fetch-op MI.addr, MI.val
12320// ...
12321//
12322// to
12323//
12324// ...
12325// EAX = LOAD MI.addr
12326// loop:
12327// t1 = OP MI.val, EAX
12328// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12329// JNE loop
12330// sink:
12331// dst = EAX
12332// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012333MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012334X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12335 MachineBasicBlock *MBB) const {
12336 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12337 DebugLoc DL = MI->getDebugLoc();
12338
12339 MachineFunction *MF = MBB->getParent();
12340 MachineRegisterInfo &MRI = MF->getRegInfo();
12341
12342 const BasicBlock *BB = MBB->getBasicBlock();
12343 MachineFunction::iterator I = MBB;
12344 ++I;
12345
12346 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12347 "Unexpected number of operands");
12348
12349 assert(MI->hasOneMemOperand() &&
12350 "Expected atomic-load-op to have one memoperand");
12351
12352 // Memory Reference
12353 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12354 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12355
12356 unsigned DstReg, SrcReg;
12357 unsigned MemOpndSlot;
12358
12359 unsigned CurOp = 0;
12360
12361 DstReg = MI->getOperand(CurOp++).getReg();
12362 MemOpndSlot = CurOp;
12363 CurOp += X86::AddrNumOperands;
12364 SrcReg = MI->getOperand(CurOp++).getReg();
12365
12366 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012367 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012368 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12369
12370 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12371 unsigned LOADOpc = getLoadOpcode(VT);
12372
12373 // For the atomic load-arith operator, we generate
12374 //
12375 // thisMBB:
12376 // EAX = LOAD [MI.addr]
12377 // mainMBB:
12378 // t1 = OP MI.val, EAX
12379 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12380 // JNE mainMBB
12381 // sinkMBB:
12382
12383 MachineBasicBlock *thisMBB = MBB;
12384 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12385 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12386 MF->insert(I, mainMBB);
12387 MF->insert(I, sinkMBB);
12388
12389 MachineInstrBuilder MIB;
12390
12391 // Transfer the remainder of BB and its successor edges to sinkMBB.
12392 sinkMBB->splice(sinkMBB->begin(), MBB,
12393 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12394 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12395
12396 // thisMBB:
12397 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12398 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12399 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12400 MIB.setMemRefs(MMOBegin, MMOEnd);
12401
12402 thisMBB->addSuccessor(mainMBB);
12403
12404 // mainMBB:
12405 MachineBasicBlock *origMainMBB = mainMBB;
12406 mainMBB->addLiveIn(AccPhyReg);
12407
12408 // Copy AccPhyReg as it is used more than once.
12409 unsigned AccReg = MRI.createVirtualRegister(RC);
12410 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12411 .addReg(AccPhyReg);
12412
12413 unsigned t1 = MRI.createVirtualRegister(RC);
12414 unsigned Opc = MI->getOpcode();
12415 switch (Opc) {
12416 default:
12417 llvm_unreachable("Unhandled atomic-load-op opcode!");
12418 case X86::ATOMAND8:
12419 case X86::ATOMAND16:
12420 case X86::ATOMAND32:
12421 case X86::ATOMAND64:
12422 case X86::ATOMOR8:
12423 case X86::ATOMOR16:
12424 case X86::ATOMOR32:
12425 case X86::ATOMOR64:
12426 case X86::ATOMXOR8:
12427 case X86::ATOMXOR16:
12428 case X86::ATOMXOR32:
12429 case X86::ATOMXOR64: {
12430 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12431 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12432 .addReg(AccReg);
12433 break;
12434 }
12435 case X86::ATOMNAND8:
12436 case X86::ATOMNAND16:
12437 case X86::ATOMNAND32:
12438 case X86::ATOMNAND64: {
12439 unsigned t2 = MRI.createVirtualRegister(RC);
12440 unsigned NOTOpc;
12441 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12442 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12443 .addReg(AccReg);
12444 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12445 break;
12446 }
Michael Liao08382492012-09-21 03:00:17 +000012447 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012448 case X86::ATOMMAX16:
12449 case X86::ATOMMAX32:
12450 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012451 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012452 case X86::ATOMMIN16:
12453 case X86::ATOMMIN32:
12454 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012455 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012456 case X86::ATOMUMAX16:
12457 case X86::ATOMUMAX32:
12458 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012459 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012460 case X86::ATOMUMIN16:
12461 case X86::ATOMUMIN32:
12462 case X86::ATOMUMIN64: {
12463 unsigned CMPOpc;
12464 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12465
12466 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12467 .addReg(SrcReg)
12468 .addReg(AccReg);
12469
12470 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012471 if (VT != MVT::i8) {
12472 // Native support
12473 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12474 .addReg(SrcReg)
12475 .addReg(AccReg);
12476 } else {
12477 // Promote i8 to i32 to use CMOV32
12478 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12479 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12480 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12481 unsigned t2 = MRI.createVirtualRegister(RC32);
12482
12483 unsigned Undef = MRI.createVirtualRegister(RC32);
12484 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12485
12486 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12487 .addReg(Undef)
12488 .addReg(SrcReg)
12489 .addImm(X86::sub_8bit);
12490 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12491 .addReg(Undef)
12492 .addReg(AccReg)
12493 .addImm(X86::sub_8bit);
12494
12495 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12496 .addReg(SrcReg32)
12497 .addReg(AccReg32);
12498
12499 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12500 .addReg(t2, 0, X86::sub_8bit);
12501 }
Michael Liaob118a072012-09-20 03:06:15 +000012502 } else {
12503 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012504 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012505 "Invalid atomic-load-op transformation!");
12506 unsigned SelOpc = getPseudoCMOVOpc(VT);
12507 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12508 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12509 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12510 .addReg(SrcReg).addReg(AccReg)
12511 .addImm(CC);
12512 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12513 }
12514 break;
12515 }
12516 }
12517
12518 // Copy AccPhyReg back from virtual register.
12519 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12520 .addReg(AccReg);
12521
12522 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12523 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12524 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12525 MIB.addReg(t1);
12526 MIB.setMemRefs(MMOBegin, MMOEnd);
12527
12528 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12529
12530 mainMBB->addSuccessor(origMainMBB);
12531 mainMBB->addSuccessor(sinkMBB);
12532
12533 // sinkMBB:
12534 sinkMBB->addLiveIn(AccPhyReg);
12535
12536 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12537 TII->get(TargetOpcode::COPY), DstReg)
12538 .addReg(AccPhyReg);
12539
12540 MI->eraseFromParent();
12541 return sinkMBB;
12542}
12543
12544// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12545// instructions. They will be translated into a spin-loop or compare-exchange
12546// loop from
12547//
12548// ...
12549// dst = atomic-fetch-op MI.addr, MI.val
12550// ...
12551//
12552// to
12553//
12554// ...
12555// EAX = LOAD [MI.addr + 0]
12556// EDX = LOAD [MI.addr + 4]
12557// loop:
12558// EBX = OP MI.val.lo, EAX
12559// ECX = OP MI.val.hi, EDX
12560// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12561// JNE loop
12562// sink:
12563// dst = EDX:EAX
12564// ...
12565MachineBasicBlock *
12566X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12567 MachineBasicBlock *MBB) const {
12568 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12569 DebugLoc DL = MI->getDebugLoc();
12570
12571 MachineFunction *MF = MBB->getParent();
12572 MachineRegisterInfo &MRI = MF->getRegInfo();
12573
12574 const BasicBlock *BB = MBB->getBasicBlock();
12575 MachineFunction::iterator I = MBB;
12576 ++I;
12577
12578 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12579 "Unexpected number of operands");
12580
12581 assert(MI->hasOneMemOperand() &&
12582 "Expected atomic-load-op32 to have one memoperand");
12583
12584 // Memory Reference
12585 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12586 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12587
12588 unsigned DstLoReg, DstHiReg;
12589 unsigned SrcLoReg, SrcHiReg;
12590 unsigned MemOpndSlot;
12591
12592 unsigned CurOp = 0;
12593
12594 DstLoReg = MI->getOperand(CurOp++).getReg();
12595 DstHiReg = MI->getOperand(CurOp++).getReg();
12596 MemOpndSlot = CurOp;
12597 CurOp += X86::AddrNumOperands;
12598 SrcLoReg = MI->getOperand(CurOp++).getReg();
12599 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012600
Craig Topperc9099502012-04-20 06:31:50 +000012601 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012602 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012603
Michael Liaob118a072012-09-20 03:06:15 +000012604 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12605 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012606
Michael Liaob118a072012-09-20 03:06:15 +000012607 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012608 //
Michael Liaob118a072012-09-20 03:06:15 +000012609 // thisMBB:
12610 // EAX = LOAD [MI.addr + 0]
12611 // EDX = LOAD [MI.addr + 4]
12612 // mainMBB:
12613 // EBX = OP MI.vallo, EAX
12614 // ECX = OP MI.valhi, EDX
12615 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12616 // JNE mainMBB
12617 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012618
Mon P Wang63307c32008-05-05 19:05:59 +000012619 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012620 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12621 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12622 MF->insert(I, mainMBB);
12623 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012624
Michael Liaob118a072012-09-20 03:06:15 +000012625 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012626
Michael Liaob118a072012-09-20 03:06:15 +000012627 // Transfer the remainder of BB and its successor edges to sinkMBB.
12628 sinkMBB->splice(sinkMBB->begin(), MBB,
12629 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12630 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012631
Michael Liaob118a072012-09-20 03:06:15 +000012632 // thisMBB:
12633 // Lo
12634 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12635 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12636 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12637 MIB.setMemRefs(MMOBegin, MMOEnd);
12638 // Hi
12639 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12640 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012641 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012642 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012643 else
Michael Liaob118a072012-09-20 03:06:15 +000012644 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12645 }
12646 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012647
Michael Liaob118a072012-09-20 03:06:15 +000012648 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012649
Michael Liaob118a072012-09-20 03:06:15 +000012650 // mainMBB:
12651 MachineBasicBlock *origMainMBB = mainMBB;
12652 mainMBB->addLiveIn(X86::EAX);
12653 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012654
Michael Liaob118a072012-09-20 03:06:15 +000012655 // Copy EDX:EAX as they are used more than once.
12656 unsigned LoReg = MRI.createVirtualRegister(RC);
12657 unsigned HiReg = MRI.createVirtualRegister(RC);
12658 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12659 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012660
Michael Liaob118a072012-09-20 03:06:15 +000012661 unsigned t1L = MRI.createVirtualRegister(RC);
12662 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012663
Michael Liaob118a072012-09-20 03:06:15 +000012664 unsigned Opc = MI->getOpcode();
12665 switch (Opc) {
12666 default:
12667 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12668 case X86::ATOMAND6432:
12669 case X86::ATOMOR6432:
12670 case X86::ATOMXOR6432:
12671 case X86::ATOMADD6432:
12672 case X86::ATOMSUB6432: {
12673 unsigned HiOpc;
12674 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12675 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg).addReg(LoReg);
12676 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg).addReg(HiReg);
12677 break;
12678 }
12679 case X86::ATOMNAND6432: {
12680 unsigned HiOpc, NOTOpc;
12681 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12682 unsigned t2L = MRI.createVirtualRegister(RC);
12683 unsigned t2H = MRI.createVirtualRegister(RC);
12684 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12685 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12686 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12687 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12688 break;
12689 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012690 case X86::ATOMMAX6432:
12691 case X86::ATOMMIN6432:
12692 case X86::ATOMUMAX6432:
12693 case X86::ATOMUMIN6432: {
12694 unsigned HiOpc;
12695 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12696 unsigned cL = MRI.createVirtualRegister(RC8);
12697 unsigned cH = MRI.createVirtualRegister(RC8);
12698 unsigned cL32 = MRI.createVirtualRegister(RC);
12699 unsigned cH32 = MRI.createVirtualRegister(RC);
12700 unsigned cc = MRI.createVirtualRegister(RC);
12701 // cl := cmp src_lo, lo
12702 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12703 .addReg(SrcLoReg).addReg(LoReg);
12704 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12705 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12706 // ch := cmp src_hi, hi
12707 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12708 .addReg(SrcHiReg).addReg(HiReg);
12709 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12710 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12711 // cc := if (src_hi == hi) ? cl : ch;
12712 if (Subtarget->hasCMov()) {
12713 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12714 .addReg(cH32).addReg(cL32);
12715 } else {
12716 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12717 .addReg(cH32).addReg(cL32)
12718 .addImm(X86::COND_E);
12719 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12720 }
12721 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12722 if (Subtarget->hasCMov()) {
12723 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12724 .addReg(SrcLoReg).addReg(LoReg);
12725 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12726 .addReg(SrcHiReg).addReg(HiReg);
12727 } else {
12728 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12729 .addReg(SrcLoReg).addReg(LoReg)
12730 .addImm(X86::COND_NE);
12731 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12732 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12733 .addReg(SrcHiReg).addReg(HiReg)
12734 .addImm(X86::COND_NE);
12735 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12736 }
12737 break;
12738 }
Michael Liaob118a072012-09-20 03:06:15 +000012739 case X86::ATOMSWAP6432: {
12740 unsigned HiOpc;
12741 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12742 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12743 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12744 break;
12745 }
12746 }
Mon P Wang63307c32008-05-05 19:05:59 +000012747
Michael Liaob118a072012-09-20 03:06:15 +000012748 // Copy EDX:EAX back from HiReg:LoReg
12749 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12750 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12751 // Copy ECX:EBX from t1H:t1L
12752 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12753 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012754
Michael Liaob118a072012-09-20 03:06:15 +000012755 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12756 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12757 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12758 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012759
Michael Liaob118a072012-09-20 03:06:15 +000012760 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012761
Michael Liaob118a072012-09-20 03:06:15 +000012762 mainMBB->addSuccessor(origMainMBB);
12763 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012764
Michael Liaob118a072012-09-20 03:06:15 +000012765 // sinkMBB:
12766 sinkMBB->addLiveIn(X86::EAX);
12767 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012768
Michael Liaob118a072012-09-20 03:06:15 +000012769 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12770 TII->get(TargetOpcode::COPY), DstLoReg)
12771 .addReg(X86::EAX);
12772 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12773 TII->get(TargetOpcode::COPY), DstHiReg)
12774 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012775
Michael Liaob118a072012-09-20 03:06:15 +000012776 MI->eraseFromParent();
12777 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012778}
12779
Eric Christopherf83a5de2009-08-27 18:08:16 +000012780// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012781// or XMM0_V32I8 in AVX all of this code can be replaced with that
12782// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012783MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012784X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012785 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012786 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012787 "Target must have SSE4.2 or AVX features enabled");
12788
Eric Christopherb120ab42009-08-18 22:50:32 +000012789 DebugLoc dl = MI->getDebugLoc();
12790 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012791 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012792 if (!Subtarget->hasAVX()) {
12793 if (memArg)
12794 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12795 else
12796 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12797 } else {
12798 if (memArg)
12799 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12800 else
12801 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12802 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012803
Eric Christopher41c902f2010-11-30 08:20:21 +000012804 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012805 for (unsigned i = 0; i < numArgs; ++i) {
12806 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012807 if (!(Op.isReg() && Op.isImplicit()))
12808 MIB.addOperand(Op);
12809 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012810 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012811 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012812 .addReg(X86::XMM0);
12813
Dan Gohman14152b42010-07-06 20:24:04 +000012814 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012815 return BB;
12816}
12817
12818MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012819X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012820 DebugLoc dl = MI->getDebugLoc();
12821 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012822
Eric Christopher228232b2010-11-30 07:20:12 +000012823 // Address into RAX/EAX, other two args into ECX, EDX.
12824 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12825 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12826 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12827 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012828 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012829
Eric Christopher228232b2010-11-30 07:20:12 +000012830 unsigned ValOps = X86::AddrNumOperands;
12831 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12832 .addReg(MI->getOperand(ValOps).getReg());
12833 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12834 .addReg(MI->getOperand(ValOps+1).getReg());
12835
12836 // The instruction doesn't actually take any operands though.
12837 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012838
Eric Christopher228232b2010-11-30 07:20:12 +000012839 MI->eraseFromParent(); // The pseudo is gone now.
12840 return BB;
12841}
12842
12843MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012844X86TargetLowering::EmitVAARG64WithCustomInserter(
12845 MachineInstr *MI,
12846 MachineBasicBlock *MBB) const {
12847 // Emit va_arg instruction on X86-64.
12848
12849 // Operands to this pseudo-instruction:
12850 // 0 ) Output : destination address (reg)
12851 // 1-5) Input : va_list address (addr, i64mem)
12852 // 6 ) ArgSize : Size (in bytes) of vararg type
12853 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12854 // 8 ) Align : Alignment of type
12855 // 9 ) EFLAGS (implicit-def)
12856
12857 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12858 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12859
12860 unsigned DestReg = MI->getOperand(0).getReg();
12861 MachineOperand &Base = MI->getOperand(1);
12862 MachineOperand &Scale = MI->getOperand(2);
12863 MachineOperand &Index = MI->getOperand(3);
12864 MachineOperand &Disp = MI->getOperand(4);
12865 MachineOperand &Segment = MI->getOperand(5);
12866 unsigned ArgSize = MI->getOperand(6).getImm();
12867 unsigned ArgMode = MI->getOperand(7).getImm();
12868 unsigned Align = MI->getOperand(8).getImm();
12869
12870 // Memory Reference
12871 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12872 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12873 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12874
12875 // Machine Information
12876 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12877 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12878 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12879 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12880 DebugLoc DL = MI->getDebugLoc();
12881
12882 // struct va_list {
12883 // i32 gp_offset
12884 // i32 fp_offset
12885 // i64 overflow_area (address)
12886 // i64 reg_save_area (address)
12887 // }
12888 // sizeof(va_list) = 24
12889 // alignment(va_list) = 8
12890
12891 unsigned TotalNumIntRegs = 6;
12892 unsigned TotalNumXMMRegs = 8;
12893 bool UseGPOffset = (ArgMode == 1);
12894 bool UseFPOffset = (ArgMode == 2);
12895 unsigned MaxOffset = TotalNumIntRegs * 8 +
12896 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12897
12898 /* Align ArgSize to a multiple of 8 */
12899 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12900 bool NeedsAlign = (Align > 8);
12901
12902 MachineBasicBlock *thisMBB = MBB;
12903 MachineBasicBlock *overflowMBB;
12904 MachineBasicBlock *offsetMBB;
12905 MachineBasicBlock *endMBB;
12906
12907 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12908 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12909 unsigned OffsetReg = 0;
12910
12911 if (!UseGPOffset && !UseFPOffset) {
12912 // If we only pull from the overflow region, we don't create a branch.
12913 // We don't need to alter control flow.
12914 OffsetDestReg = 0; // unused
12915 OverflowDestReg = DestReg;
12916
12917 offsetMBB = NULL;
12918 overflowMBB = thisMBB;
12919 endMBB = thisMBB;
12920 } else {
12921 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12922 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12923 // If not, pull from overflow_area. (branch to overflowMBB)
12924 //
12925 // thisMBB
12926 // | .
12927 // | .
12928 // offsetMBB overflowMBB
12929 // | .
12930 // | .
12931 // endMBB
12932
12933 // Registers for the PHI in endMBB
12934 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12935 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12936
12937 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12938 MachineFunction *MF = MBB->getParent();
12939 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12940 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12941 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12942
12943 MachineFunction::iterator MBBIter = MBB;
12944 ++MBBIter;
12945
12946 // Insert the new basic blocks
12947 MF->insert(MBBIter, offsetMBB);
12948 MF->insert(MBBIter, overflowMBB);
12949 MF->insert(MBBIter, endMBB);
12950
12951 // Transfer the remainder of MBB and its successor edges to endMBB.
12952 endMBB->splice(endMBB->begin(), thisMBB,
12953 llvm::next(MachineBasicBlock::iterator(MI)),
12954 thisMBB->end());
12955 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12956
12957 // Make offsetMBB and overflowMBB successors of thisMBB
12958 thisMBB->addSuccessor(offsetMBB);
12959 thisMBB->addSuccessor(overflowMBB);
12960
12961 // endMBB is a successor of both offsetMBB and overflowMBB
12962 offsetMBB->addSuccessor(endMBB);
12963 overflowMBB->addSuccessor(endMBB);
12964
12965 // Load the offset value into a register
12966 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12967 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12968 .addOperand(Base)
12969 .addOperand(Scale)
12970 .addOperand(Index)
12971 .addDisp(Disp, UseFPOffset ? 4 : 0)
12972 .addOperand(Segment)
12973 .setMemRefs(MMOBegin, MMOEnd);
12974
12975 // Check if there is enough room left to pull this argument.
12976 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12977 .addReg(OffsetReg)
12978 .addImm(MaxOffset + 8 - ArgSizeA8);
12979
12980 // Branch to "overflowMBB" if offset >= max
12981 // Fall through to "offsetMBB" otherwise
12982 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12983 .addMBB(overflowMBB);
12984 }
12985
12986 // In offsetMBB, emit code to use the reg_save_area.
12987 if (offsetMBB) {
12988 assert(OffsetReg != 0);
12989
12990 // Read the reg_save_area address.
12991 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12992 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12993 .addOperand(Base)
12994 .addOperand(Scale)
12995 .addOperand(Index)
12996 .addDisp(Disp, 16)
12997 .addOperand(Segment)
12998 .setMemRefs(MMOBegin, MMOEnd);
12999
13000 // Zero-extend the offset
13001 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13002 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13003 .addImm(0)
13004 .addReg(OffsetReg)
13005 .addImm(X86::sub_32bit);
13006
13007 // Add the offset to the reg_save_area to get the final address.
13008 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13009 .addReg(OffsetReg64)
13010 .addReg(RegSaveReg);
13011
13012 // Compute the offset for the next argument
13013 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13014 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13015 .addReg(OffsetReg)
13016 .addImm(UseFPOffset ? 16 : 8);
13017
13018 // Store it back into the va_list.
13019 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13020 .addOperand(Base)
13021 .addOperand(Scale)
13022 .addOperand(Index)
13023 .addDisp(Disp, UseFPOffset ? 4 : 0)
13024 .addOperand(Segment)
13025 .addReg(NextOffsetReg)
13026 .setMemRefs(MMOBegin, MMOEnd);
13027
13028 // Jump to endMBB
13029 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13030 .addMBB(endMBB);
13031 }
13032
13033 //
13034 // Emit code to use overflow area
13035 //
13036
13037 // Load the overflow_area address into a register.
13038 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13039 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13040 .addOperand(Base)
13041 .addOperand(Scale)
13042 .addOperand(Index)
13043 .addDisp(Disp, 8)
13044 .addOperand(Segment)
13045 .setMemRefs(MMOBegin, MMOEnd);
13046
13047 // If we need to align it, do so. Otherwise, just copy the address
13048 // to OverflowDestReg.
13049 if (NeedsAlign) {
13050 // Align the overflow address
13051 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13052 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13053
13054 // aligned_addr = (addr + (align-1)) & ~(align-1)
13055 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13056 .addReg(OverflowAddrReg)
13057 .addImm(Align-1);
13058
13059 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13060 .addReg(TmpReg)
13061 .addImm(~(uint64_t)(Align-1));
13062 } else {
13063 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13064 .addReg(OverflowAddrReg);
13065 }
13066
13067 // Compute the next overflow address after this argument.
13068 // (the overflow address should be kept 8-byte aligned)
13069 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13070 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13071 .addReg(OverflowDestReg)
13072 .addImm(ArgSizeA8);
13073
13074 // Store the new overflow address.
13075 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13076 .addOperand(Base)
13077 .addOperand(Scale)
13078 .addOperand(Index)
13079 .addDisp(Disp, 8)
13080 .addOperand(Segment)
13081 .addReg(NextAddrReg)
13082 .setMemRefs(MMOBegin, MMOEnd);
13083
13084 // If we branched, emit the PHI to the front of endMBB.
13085 if (offsetMBB) {
13086 BuildMI(*endMBB, endMBB->begin(), DL,
13087 TII->get(X86::PHI), DestReg)
13088 .addReg(OffsetDestReg).addMBB(offsetMBB)
13089 .addReg(OverflowDestReg).addMBB(overflowMBB);
13090 }
13091
13092 // Erase the pseudo instruction
13093 MI->eraseFromParent();
13094
13095 return endMBB;
13096}
13097
13098MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013099X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13100 MachineInstr *MI,
13101 MachineBasicBlock *MBB) const {
13102 // Emit code to save XMM registers to the stack. The ABI says that the
13103 // number of registers to save is given in %al, so it's theoretically
13104 // possible to do an indirect jump trick to avoid saving all of them,
13105 // however this code takes a simpler approach and just executes all
13106 // of the stores if %al is non-zero. It's less code, and it's probably
13107 // easier on the hardware branch predictor, and stores aren't all that
13108 // expensive anyway.
13109
13110 // Create the new basic blocks. One block contains all the XMM stores,
13111 // and one block is the final destination regardless of whether any
13112 // stores were performed.
13113 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13114 MachineFunction *F = MBB->getParent();
13115 MachineFunction::iterator MBBIter = MBB;
13116 ++MBBIter;
13117 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13118 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13119 F->insert(MBBIter, XMMSaveMBB);
13120 F->insert(MBBIter, EndMBB);
13121
Dan Gohman14152b42010-07-06 20:24:04 +000013122 // Transfer the remainder of MBB and its successor edges to EndMBB.
13123 EndMBB->splice(EndMBB->begin(), MBB,
13124 llvm::next(MachineBasicBlock::iterator(MI)),
13125 MBB->end());
13126 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13127
Dan Gohmand6708ea2009-08-15 01:38:56 +000013128 // The original block will now fall through to the XMM save block.
13129 MBB->addSuccessor(XMMSaveMBB);
13130 // The XMMSaveMBB will fall through to the end block.
13131 XMMSaveMBB->addSuccessor(EndMBB);
13132
13133 // Now add the instructions.
13134 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13135 DebugLoc DL = MI->getDebugLoc();
13136
13137 unsigned CountReg = MI->getOperand(0).getReg();
13138 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13139 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13140
13141 if (!Subtarget->isTargetWin64()) {
13142 // If %al is 0, branch around the XMM save block.
13143 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013144 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013145 MBB->addSuccessor(EndMBB);
13146 }
13147
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013148 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013149 // In the XMM save block, save all the XMM argument registers.
13150 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13151 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013152 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013153 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013154 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013155 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013156 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013157 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013158 .addFrameIndex(RegSaveFrameIndex)
13159 .addImm(/*Scale=*/1)
13160 .addReg(/*IndexReg=*/0)
13161 .addImm(/*Disp=*/Offset)
13162 .addReg(/*Segment=*/0)
13163 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013164 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013165 }
13166
Dan Gohman14152b42010-07-06 20:24:04 +000013167 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013168
13169 return EndMBB;
13170}
Mon P Wang63307c32008-05-05 19:05:59 +000013171
Lang Hames6e3f7e42012-02-03 01:13:49 +000013172// The EFLAGS operand of SelectItr might be missing a kill marker
13173// because there were multiple uses of EFLAGS, and ISel didn't know
13174// which to mark. Figure out whether SelectItr should have had a
13175// kill marker, and set it if it should. Returns the correct kill
13176// marker value.
13177static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13178 MachineBasicBlock* BB,
13179 const TargetRegisterInfo* TRI) {
13180 // Scan forward through BB for a use/def of EFLAGS.
13181 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13182 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013183 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013184 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013185 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013186 if (mi.definesRegister(X86::EFLAGS))
13187 break; // Should have kill-flag - update below.
13188 }
13189
13190 // If we hit the end of the block, check whether EFLAGS is live into a
13191 // successor.
13192 if (miI == BB->end()) {
13193 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13194 sEnd = BB->succ_end();
13195 sItr != sEnd; ++sItr) {
13196 MachineBasicBlock* succ = *sItr;
13197 if (succ->isLiveIn(X86::EFLAGS))
13198 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013199 }
13200 }
13201
Lang Hames6e3f7e42012-02-03 01:13:49 +000013202 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13203 // out. SelectMI should have a kill flag on EFLAGS.
13204 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013205 return true;
13206}
13207
Evan Cheng60c07e12006-07-05 22:17:51 +000013208MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013209X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013210 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013211 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13212 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013213
Chris Lattner52600972009-09-02 05:57:00 +000013214 // To "insert" a SELECT_CC instruction, we actually have to insert the
13215 // diamond control-flow pattern. The incoming instruction knows the
13216 // destination vreg to set, the condition code register to branch on, the
13217 // true/false values to select between, and a branch opcode to use.
13218 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13219 MachineFunction::iterator It = BB;
13220 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013221
Chris Lattner52600972009-09-02 05:57:00 +000013222 // thisMBB:
13223 // ...
13224 // TrueVal = ...
13225 // cmpTY ccX, r1, r2
13226 // bCC copy1MBB
13227 // fallthrough --> copy0MBB
13228 MachineBasicBlock *thisMBB = BB;
13229 MachineFunction *F = BB->getParent();
13230 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13231 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013232 F->insert(It, copy0MBB);
13233 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013234
Bill Wendling730c07e2010-06-25 20:48:10 +000013235 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13236 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013237 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13238 if (!MI->killsRegister(X86::EFLAGS) &&
13239 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13240 copy0MBB->addLiveIn(X86::EFLAGS);
13241 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013242 }
13243
Dan Gohman14152b42010-07-06 20:24:04 +000013244 // Transfer the remainder of BB and its successor edges to sinkMBB.
13245 sinkMBB->splice(sinkMBB->begin(), BB,
13246 llvm::next(MachineBasicBlock::iterator(MI)),
13247 BB->end());
13248 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13249
13250 // Add the true and fallthrough blocks as its successors.
13251 BB->addSuccessor(copy0MBB);
13252 BB->addSuccessor(sinkMBB);
13253
13254 // Create the conditional branch instruction.
13255 unsigned Opc =
13256 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13257 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13258
Chris Lattner52600972009-09-02 05:57:00 +000013259 // copy0MBB:
13260 // %FalseValue = ...
13261 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013262 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013263
Chris Lattner52600972009-09-02 05:57:00 +000013264 // sinkMBB:
13265 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13266 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013267 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13268 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013269 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13270 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13271
Dan Gohman14152b42010-07-06 20:24:04 +000013272 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013273 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013274}
13275
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013276MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013277X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13278 bool Is64Bit) const {
13279 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13280 DebugLoc DL = MI->getDebugLoc();
13281 MachineFunction *MF = BB->getParent();
13282 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13283
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013284 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013285
13286 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13287 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13288
13289 // BB:
13290 // ... [Till the alloca]
13291 // If stacklet is not large enough, jump to mallocMBB
13292 //
13293 // bumpMBB:
13294 // Allocate by subtracting from RSP
13295 // Jump to continueMBB
13296 //
13297 // mallocMBB:
13298 // Allocate by call to runtime
13299 //
13300 // continueMBB:
13301 // ...
13302 // [rest of original BB]
13303 //
13304
13305 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13306 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13307 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13308
13309 MachineRegisterInfo &MRI = MF->getRegInfo();
13310 const TargetRegisterClass *AddrRegClass =
13311 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13312
13313 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13314 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13315 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013316 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013317 sizeVReg = MI->getOperand(1).getReg(),
13318 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13319
13320 MachineFunction::iterator MBBIter = BB;
13321 ++MBBIter;
13322
13323 MF->insert(MBBIter, bumpMBB);
13324 MF->insert(MBBIter, mallocMBB);
13325 MF->insert(MBBIter, continueMBB);
13326
13327 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13328 (MachineBasicBlock::iterator(MI)), BB->end());
13329 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13330
13331 // Add code to the main basic block to check if the stack limit has been hit,
13332 // and if so, jump to mallocMBB otherwise to bumpMBB.
13333 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013334 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013335 .addReg(tmpSPVReg).addReg(sizeVReg);
13336 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013337 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013338 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013339 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13340
13341 // bumpMBB simply decreases the stack pointer, since we know the current
13342 // stacklet has enough space.
13343 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013344 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013345 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013346 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013347 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13348
13349 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013350 const uint32_t *RegMask =
13351 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013352 if (Is64Bit) {
13353 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13354 .addReg(sizeVReg);
13355 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013356 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013357 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013358 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013359 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013360 } else {
13361 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13362 .addImm(12);
13363 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13364 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013365 .addExternalSymbol("__morestack_allocate_stack_space")
13366 .addRegMask(RegMask)
13367 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013368 }
13369
13370 if (!Is64Bit)
13371 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13372 .addImm(16);
13373
13374 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13375 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13376 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13377
13378 // Set up the CFG correctly.
13379 BB->addSuccessor(bumpMBB);
13380 BB->addSuccessor(mallocMBB);
13381 mallocMBB->addSuccessor(continueMBB);
13382 bumpMBB->addSuccessor(continueMBB);
13383
13384 // Take care of the PHI nodes.
13385 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13386 MI->getOperand(0).getReg())
13387 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13388 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13389
13390 // Delete the original pseudo instruction.
13391 MI->eraseFromParent();
13392
13393 // And we're done.
13394 return continueMBB;
13395}
13396
13397MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013398X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013399 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013400 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13401 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013402
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013403 assert(!Subtarget->isTargetEnvMacho());
13404
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013405 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13406 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013407
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013408 if (Subtarget->isTargetWin64()) {
13409 if (Subtarget->isTargetCygMing()) {
13410 // ___chkstk(Mingw64):
13411 // Clobbers R10, R11, RAX and EFLAGS.
13412 // Updates RSP.
13413 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13414 .addExternalSymbol("___chkstk")
13415 .addReg(X86::RAX, RegState::Implicit)
13416 .addReg(X86::RSP, RegState::Implicit)
13417 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13418 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13419 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13420 } else {
13421 // __chkstk(MSVCRT): does not update stack pointer.
13422 // Clobbers R10, R11 and EFLAGS.
13423 // FIXME: RAX(allocated size) might be reused and not killed.
13424 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13425 .addExternalSymbol("__chkstk")
13426 .addReg(X86::RAX, RegState::Implicit)
13427 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13428 // RAX has the offset to subtracted from RSP.
13429 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13430 .addReg(X86::RSP)
13431 .addReg(X86::RAX);
13432 }
13433 } else {
13434 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013435 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13436
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013437 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13438 .addExternalSymbol(StackProbeSymbol)
13439 .addReg(X86::EAX, RegState::Implicit)
13440 .addReg(X86::ESP, RegState::Implicit)
13441 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13442 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13443 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13444 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013445
Dan Gohman14152b42010-07-06 20:24:04 +000013446 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013447 return BB;
13448}
Chris Lattner52600972009-09-02 05:57:00 +000013449
13450MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013451X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13452 MachineBasicBlock *BB) const {
13453 // This is pretty easy. We're taking the value that we received from
13454 // our load from the relocation, sticking it in either RDI (x86-64)
13455 // or EAX and doing an indirect call. The return value will then
13456 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013457 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013458 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013459 DebugLoc DL = MI->getDebugLoc();
13460 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013461
13462 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013463 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013464
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013465 // Get a register mask for the lowered call.
13466 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13467 // proper register mask.
13468 const uint32_t *RegMask =
13469 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013470 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013471 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13472 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013473 .addReg(X86::RIP)
13474 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013475 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013476 MI->getOperand(3).getTargetFlags())
13477 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013478 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013479 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013480 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013481 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013482 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13483 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013484 .addReg(0)
13485 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013486 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013487 MI->getOperand(3).getTargetFlags())
13488 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013489 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013490 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013491 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013492 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013493 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13494 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013495 .addReg(TII->getGlobalBaseReg(F))
13496 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013497 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013498 MI->getOperand(3).getTargetFlags())
13499 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013500 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013501 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013502 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013503 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013504
Dan Gohman14152b42010-07-06 20:24:04 +000013505 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013506 return BB;
13507}
13508
13509MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013510X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13511 MachineBasicBlock *MBB) const {
13512 DebugLoc DL = MI->getDebugLoc();
13513 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13514
13515 MachineFunction *MF = MBB->getParent();
13516 MachineRegisterInfo &MRI = MF->getRegInfo();
13517
13518 const BasicBlock *BB = MBB->getBasicBlock();
13519 MachineFunction::iterator I = MBB;
13520 ++I;
13521
13522 // Memory Reference
13523 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13524 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13525
13526 unsigned DstReg;
13527 unsigned MemOpndSlot = 0;
13528
13529 unsigned CurOp = 0;
13530
13531 DstReg = MI->getOperand(CurOp++).getReg();
13532 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13533 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13534 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13535 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13536
13537 MemOpndSlot = CurOp;
13538
13539 MVT PVT = getPointerTy();
13540 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13541 "Invalid Pointer Size!");
13542
13543 // For v = setjmp(buf), we generate
13544 //
13545 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013546 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013547 // SjLjSetup restoreMBB
13548 //
13549 // mainMBB:
13550 // v_main = 0
13551 //
13552 // sinkMBB:
13553 // v = phi(main, restore)
13554 //
13555 // restoreMBB:
13556 // v_restore = 1
13557
13558 MachineBasicBlock *thisMBB = MBB;
13559 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13560 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13561 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13562 MF->insert(I, mainMBB);
13563 MF->insert(I, sinkMBB);
13564 MF->push_back(restoreMBB);
13565
13566 MachineInstrBuilder MIB;
13567
13568 // Transfer the remainder of BB and its successor edges to sinkMBB.
13569 sinkMBB->splice(sinkMBB->begin(), MBB,
13570 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13571 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13572
13573 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013574 unsigned PtrStoreOpc = 0;
13575 unsigned LabelReg = 0;
13576 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13577 Reloc::Model RM = getTargetMachine().getRelocationModel();
13578 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13579 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013580
Michael Liao281ae5a2012-10-17 02:22:27 +000013581 // Prepare IP either in reg or imm.
13582 if (!UseImmLabel) {
13583 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13584 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13585 LabelReg = MRI.createVirtualRegister(PtrRC);
13586 if (Subtarget->is64Bit()) {
13587 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13588 .addReg(X86::RIP)
13589 .addImm(0)
13590 .addReg(0)
13591 .addMBB(restoreMBB)
13592 .addReg(0);
13593 } else {
13594 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13595 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13596 .addReg(XII->getGlobalBaseReg(MF))
13597 .addImm(0)
13598 .addReg(0)
13599 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13600 .addReg(0);
13601 }
13602 } else
13603 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013604 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013605 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013606 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13607 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013608 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013609 else
13610 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13611 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013612 if (!UseImmLabel)
13613 MIB.addReg(LabelReg);
13614 else
13615 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013616 MIB.setMemRefs(MMOBegin, MMOEnd);
13617 // Setup
13618 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13619 .addMBB(restoreMBB);
13620 MIB.addRegMask(RegInfo->getNoPreservedMask());
13621 thisMBB->addSuccessor(mainMBB);
13622 thisMBB->addSuccessor(restoreMBB);
13623
13624 // mainMBB:
13625 // EAX = 0
13626 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13627 mainMBB->addSuccessor(sinkMBB);
13628
13629 // sinkMBB:
13630 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13631 TII->get(X86::PHI), DstReg)
13632 .addReg(mainDstReg).addMBB(mainMBB)
13633 .addReg(restoreDstReg).addMBB(restoreMBB);
13634
13635 // restoreMBB:
13636 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13637 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13638 restoreMBB->addSuccessor(sinkMBB);
13639
13640 MI->eraseFromParent();
13641 return sinkMBB;
13642}
13643
13644MachineBasicBlock *
13645X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13646 MachineBasicBlock *MBB) const {
13647 DebugLoc DL = MI->getDebugLoc();
13648 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13649
13650 MachineFunction *MF = MBB->getParent();
13651 MachineRegisterInfo &MRI = MF->getRegInfo();
13652
13653 // Memory Reference
13654 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13655 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13656
13657 MVT PVT = getPointerTy();
13658 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13659 "Invalid Pointer Size!");
13660
13661 const TargetRegisterClass *RC =
13662 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13663 unsigned Tmp = MRI.createVirtualRegister(RC);
13664 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13665 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13666 unsigned SP = RegInfo->getStackRegister();
13667
13668 MachineInstrBuilder MIB;
13669
Michael Liao281ae5a2012-10-17 02:22:27 +000013670 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13671 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013672
13673 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13674 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13675
13676 // Reload FP
13677 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13678 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13679 MIB.addOperand(MI->getOperand(i));
13680 MIB.setMemRefs(MMOBegin, MMOEnd);
13681 // Reload IP
13682 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13683 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13684 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013685 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013686 else
13687 MIB.addOperand(MI->getOperand(i));
13688 }
13689 MIB.setMemRefs(MMOBegin, MMOEnd);
13690 // Reload SP
13691 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13692 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13693 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013694 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013695 else
13696 MIB.addOperand(MI->getOperand(i));
13697 }
13698 MIB.setMemRefs(MMOBegin, MMOEnd);
13699 // Jump
13700 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13701
13702 MI->eraseFromParent();
13703 return MBB;
13704}
13705
13706MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013707X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013708 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013709 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013710 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013711 case X86::TAILJMPd64:
13712 case X86::TAILJMPr64:
13713 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013714 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013715 case X86::TCRETURNdi64:
13716 case X86::TCRETURNri64:
13717 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013718 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013719 case X86::WIN_ALLOCA:
13720 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013721 case X86::SEG_ALLOCA_32:
13722 return EmitLoweredSegAlloca(MI, BB, false);
13723 case X86::SEG_ALLOCA_64:
13724 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013725 case X86::TLSCall_32:
13726 case X86::TLSCall_64:
13727 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013728 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013729 case X86::CMOV_FR32:
13730 case X86::CMOV_FR64:
13731 case X86::CMOV_V4F32:
13732 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013733 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013734 case X86::CMOV_V8F32:
13735 case X86::CMOV_V4F64:
13736 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013737 case X86::CMOV_GR16:
13738 case X86::CMOV_GR32:
13739 case X86::CMOV_RFP32:
13740 case X86::CMOV_RFP64:
13741 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013742 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013743
Dale Johannesen849f2142007-07-03 00:53:03 +000013744 case X86::FP32_TO_INT16_IN_MEM:
13745 case X86::FP32_TO_INT32_IN_MEM:
13746 case X86::FP32_TO_INT64_IN_MEM:
13747 case X86::FP64_TO_INT16_IN_MEM:
13748 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013749 case X86::FP64_TO_INT64_IN_MEM:
13750 case X86::FP80_TO_INT16_IN_MEM:
13751 case X86::FP80_TO_INT32_IN_MEM:
13752 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013753 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13754 DebugLoc DL = MI->getDebugLoc();
13755
Evan Cheng60c07e12006-07-05 22:17:51 +000013756 // Change the floating point control register to use "round towards zero"
13757 // mode when truncating to an integer value.
13758 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013759 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013760 addFrameReference(BuildMI(*BB, MI, DL,
13761 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013762
13763 // Load the old value of the high byte of the control word...
13764 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013765 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013766 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013767 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013768
13769 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013770 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013771 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013772
13773 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013774 addFrameReference(BuildMI(*BB, MI, DL,
13775 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013776
13777 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013778 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013779 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013780
13781 // Get the X86 opcode to use.
13782 unsigned Opc;
13783 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013784 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013785 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13786 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13787 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13788 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13789 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13790 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013791 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13792 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13793 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013794 }
13795
13796 X86AddressMode AM;
13797 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013798 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013799 AM.BaseType = X86AddressMode::RegBase;
13800 AM.Base.Reg = Op.getReg();
13801 } else {
13802 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013803 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013804 }
13805 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013806 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013807 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013808 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013809 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013810 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013811 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013812 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013813 AM.GV = Op.getGlobal();
13814 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013815 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013816 }
Dan Gohman14152b42010-07-06 20:24:04 +000013817 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013818 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013819
13820 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013821 addFrameReference(BuildMI(*BB, MI, DL,
13822 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013823
Dan Gohman14152b42010-07-06 20:24:04 +000013824 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013825 return BB;
13826 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013827 // String/text processing lowering.
13828 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013829 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013830 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013831 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013832 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013833 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013834 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013835 case X86::VPCMPESTRM128MEM: {
13836 unsigned NumArgs;
13837 bool MemArg;
13838 switch (MI->getOpcode()) {
13839 default: llvm_unreachable("illegal opcode!");
13840 case X86::PCMPISTRM128REG:
13841 case X86::VPCMPISTRM128REG:
13842 NumArgs = 3; MemArg = false; break;
13843 case X86::PCMPISTRM128MEM:
13844 case X86::VPCMPISTRM128MEM:
13845 NumArgs = 3; MemArg = true; break;
13846 case X86::PCMPESTRM128REG:
13847 case X86::VPCMPESTRM128REG:
13848 NumArgs = 5; MemArg = false; break;
13849 case X86::PCMPESTRM128MEM:
13850 case X86::VPCMPESTRM128MEM:
13851 NumArgs = 5; MemArg = true; break;
13852 }
13853 return EmitPCMP(MI, BB, NumArgs, MemArg);
13854 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013855
Eric Christopher228232b2010-11-30 07:20:12 +000013856 // Thread synchronization.
13857 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013858 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013859
Eric Christopherb120ab42009-08-18 22:50:32 +000013860 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013861 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013862 case X86::ATOMAND16:
13863 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013864 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013865 // Fall through
13866 case X86::ATOMOR8:
13867 case X86::ATOMOR16:
13868 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013869 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013870 // Fall through
13871 case X86::ATOMXOR16:
13872 case X86::ATOMXOR8:
13873 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013874 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013875 // Fall through
13876 case X86::ATOMNAND8:
13877 case X86::ATOMNAND16:
13878 case X86::ATOMNAND32:
13879 case X86::ATOMNAND64:
13880 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013881 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013882 case X86::ATOMMAX16:
13883 case X86::ATOMMAX32:
13884 case X86::ATOMMAX64:
13885 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013886 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013887 case X86::ATOMMIN16:
13888 case X86::ATOMMIN32:
13889 case X86::ATOMMIN64:
13890 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013891 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013892 case X86::ATOMUMAX16:
13893 case X86::ATOMUMAX32:
13894 case X86::ATOMUMAX64:
13895 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013896 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013897 case X86::ATOMUMIN16:
13898 case X86::ATOMUMIN32:
13899 case X86::ATOMUMIN64:
13900 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013901
13902 // This group does 64-bit operations on a 32-bit host.
13903 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013904 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013905 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013906 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013907 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013908 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000013909 case X86::ATOMMAX6432:
13910 case X86::ATOMMIN6432:
13911 case X86::ATOMUMAX6432:
13912 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000013913 case X86::ATOMSWAP6432:
13914 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000013915
Dan Gohmand6708ea2009-08-15 01:38:56 +000013916 case X86::VASTART_SAVE_XMM_REGS:
13917 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013918
13919 case X86::VAARG_64:
13920 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013921
13922 case X86::EH_SjLj_SetJmp32:
13923 case X86::EH_SjLj_SetJmp64:
13924 return emitEHSjLjSetJmp(MI, BB);
13925
13926 case X86::EH_SjLj_LongJmp32:
13927 case X86::EH_SjLj_LongJmp64:
13928 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013929 }
13930}
13931
13932//===----------------------------------------------------------------------===//
13933// X86 Optimization Hooks
13934//===----------------------------------------------------------------------===//
13935
Dan Gohman475871a2008-07-27 21:46:04 +000013936void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013937 APInt &KnownZero,
13938 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013939 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013940 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013941 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013942 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013943 assert((Opc >= ISD::BUILTIN_OP_END ||
13944 Opc == ISD::INTRINSIC_WO_CHAIN ||
13945 Opc == ISD::INTRINSIC_W_CHAIN ||
13946 Opc == ISD::INTRINSIC_VOID) &&
13947 "Should use MaskedValueIsZero if you don't know whether Op"
13948 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013949
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013950 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013951 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013952 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013953 case X86ISD::ADD:
13954 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013955 case X86ISD::ADC:
13956 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013957 case X86ISD::SMUL:
13958 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013959 case X86ISD::INC:
13960 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013961 case X86ISD::OR:
13962 case X86ISD::XOR:
13963 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013964 // These nodes' second result is a boolean.
13965 if (Op.getResNo() == 0)
13966 break;
13967 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013968 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013969 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013970 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013971 case ISD::INTRINSIC_WO_CHAIN: {
13972 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13973 unsigned NumLoBits = 0;
13974 switch (IntId) {
13975 default: break;
13976 case Intrinsic::x86_sse_movmsk_ps:
13977 case Intrinsic::x86_avx_movmsk_ps_256:
13978 case Intrinsic::x86_sse2_movmsk_pd:
13979 case Intrinsic::x86_avx_movmsk_pd_256:
13980 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013981 case Intrinsic::x86_sse2_pmovmskb_128:
13982 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013983 // High bits of movmskp{s|d}, pmovmskb are known zero.
13984 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013985 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013986 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13987 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13988 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13989 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13990 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13991 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013992 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013993 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013994 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013995 break;
13996 }
13997 }
13998 break;
13999 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014000 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014001}
Chris Lattner259e97c2006-01-31 19:43:35 +000014002
Owen Andersonbc146b02010-09-21 20:42:50 +000014003unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14004 unsigned Depth) const {
14005 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14006 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14007 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014008
Owen Andersonbc146b02010-09-21 20:42:50 +000014009 // Fallback case.
14010 return 1;
14011}
14012
Evan Cheng206ee9d2006-07-07 08:33:52 +000014013/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014014/// node is a GlobalAddress + offset.
14015bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014016 const GlobalValue* &GA,
14017 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014018 if (N->getOpcode() == X86ISD::Wrapper) {
14019 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014020 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014021 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014022 return true;
14023 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014024 }
Evan Chengad4196b2008-05-12 19:56:52 +000014025 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014026}
14027
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014028/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14029/// same as extracting the high 128-bit part of 256-bit vector and then
14030/// inserting the result into the low part of a new 256-bit vector
14031static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14032 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014033 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014034
14035 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014036 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014037 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14038 SVOp->getMaskElt(j) >= 0)
14039 return false;
14040
14041 return true;
14042}
14043
14044/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14045/// same as extracting the low 128-bit part of 256-bit vector and then
14046/// inserting the result into the high part of a new 256-bit vector
14047static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14048 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014049 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014050
14051 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014052 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014053 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14054 SVOp->getMaskElt(j) >= 0)
14055 return false;
14056
14057 return true;
14058}
14059
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014060/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14061static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014062 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014063 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014064 DebugLoc dl = N->getDebugLoc();
14065 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14066 SDValue V1 = SVOp->getOperand(0);
14067 SDValue V2 = SVOp->getOperand(1);
14068 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014069 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014070
14071 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14072 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14073 //
14074 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014075 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014076 // V UNDEF BUILD_VECTOR UNDEF
14077 // \ / \ /
14078 // CONCAT_VECTOR CONCAT_VECTOR
14079 // \ /
14080 // \ /
14081 // RESULT: V + zero extended
14082 //
14083 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14084 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14085 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14086 return SDValue();
14087
14088 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14089 return SDValue();
14090
14091 // To match the shuffle mask, the first half of the mask should
14092 // be exactly the first vector, and all the rest a splat with the
14093 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014094 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014095 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14096 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14097 return SDValue();
14098
Chad Rosier3d1161e2012-01-03 21:05:52 +000014099 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14100 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014101 if (Ld->hasNUsesOfValue(1, 0)) {
14102 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14103 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14104 SDValue ResNode =
14105 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14106 Ld->getMemoryVT(),
14107 Ld->getPointerInfo(),
14108 Ld->getAlignment(),
14109 false/*isVolatile*/, true/*ReadMem*/,
14110 false/*WriteMem*/);
14111 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14112 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014113 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014114
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014115 // Emit a zeroed vector and insert the desired subvector on its
14116 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014117 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014118 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014119 return DCI.CombineTo(N, InsV);
14120 }
14121
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014122 //===--------------------------------------------------------------------===//
14123 // Combine some shuffles into subvector extracts and inserts:
14124 //
14125
14126 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14127 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014128 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14129 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014130 return DCI.CombineTo(N, InsV);
14131 }
14132
14133 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14134 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014135 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14136 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014137 return DCI.CombineTo(N, InsV);
14138 }
14139
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014140 return SDValue();
14141}
14142
14143/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014144static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014145 TargetLowering::DAGCombinerInfo &DCI,
14146 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014147 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014148 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014149
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014150 // Don't create instructions with illegal types after legalize types has run.
14151 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14152 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14153 return SDValue();
14154
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014155 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000014156 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014157 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014158 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014159
14160 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014161 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014162 return SDValue();
14163
14164 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14165 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14166 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014167 SmallVector<SDValue, 16> Elts;
14168 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014169 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014170
Nate Begemanfdea31a2010-03-24 20:49:50 +000014171 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014172}
Evan Chengd880b972008-05-09 21:53:03 +000014173
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014174
Craig Topper55b24052012-09-11 06:15:32 +000014175/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014176/// a sequence of vector shuffle operations.
14177/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014178static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14179 TargetLowering::DAGCombinerInfo &DCI,
14180 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014181 if (!DCI.isBeforeLegalizeOps())
14182 return SDValue();
14183
Craig Topper3ef43cf2012-04-24 06:36:35 +000014184 if (!Subtarget->hasAVX())
14185 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014186
14187 EVT VT = N->getValueType(0);
14188 SDValue Op = N->getOperand(0);
14189 EVT OpVT = Op.getValueType();
14190 DebugLoc dl = N->getDebugLoc();
14191
14192 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
14193
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014194 if (Subtarget->hasAVX2()) {
14195 // AVX2: v4i64 -> v4i32
14196
14197 // VPERMD
14198 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
14199
14200 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
14201 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
14202 ShufMask);
14203
Craig Topperd63fa652012-04-22 18:51:37 +000014204 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
14205 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014206 }
14207
14208 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014209 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014210 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014211
14212 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014213 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014214
14215 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14216 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14217
14218 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000014219 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014220
Craig Toppercacafd42012-08-14 08:18:43 +000014221 SDValue Undef = DAG.getUNDEF(VT);
14222 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
14223 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014224
14225 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014226 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014227
Elena Demikhovsky73252572012-02-01 10:33:05 +000014228 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014229 }
Craig Topperd63fa652012-04-22 18:51:37 +000014230
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014231 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14232
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014233 if (Subtarget->hasAVX2()) {
14234 // AVX2: v8i32 -> v8i16
14235
14236 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014237
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014238 // PSHUFB
14239 SmallVector<SDValue,32> pshufbMask;
14240 for (unsigned i = 0; i < 2; ++i) {
14241 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14242 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14243 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14244 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14245 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14246 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14247 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14248 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14249 for (unsigned j = 0; j < 8; ++j)
14250 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14251 }
Craig Topperd63fa652012-04-22 18:51:37 +000014252 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14253 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014254 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14255
14256 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14257
14258 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014259 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014260 &ShufMask[0]);
14261
Craig Topperd63fa652012-04-22 18:51:37 +000014262 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14263 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014264
14265 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14266 }
14267
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014268 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014269 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014270
14271 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014272 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014273
14274 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14275 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14276
14277 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014278 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14279 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014280
Craig Toppercacafd42012-08-14 08:18:43 +000014281 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14282 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14283 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014284
14285 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14286 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14287
14288 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014289 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014290
Elena Demikhovsky73252572012-02-01 10:33:05 +000014291 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014292 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014293 }
14294
14295 return SDValue();
14296}
14297
Craig Topper89f4e662012-03-20 07:17:59 +000014298/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14299/// specific shuffle of a load can be folded into a single element load.
14300/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14301/// shuffles have been customed lowered so we need to handle those here.
14302static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14303 TargetLowering::DAGCombinerInfo &DCI) {
14304 if (DCI.isBeforeLegalizeOps())
14305 return SDValue();
14306
14307 SDValue InVec = N->getOperand(0);
14308 SDValue EltNo = N->getOperand(1);
14309
14310 if (!isa<ConstantSDNode>(EltNo))
14311 return SDValue();
14312
14313 EVT VT = InVec.getValueType();
14314
14315 bool HasShuffleIntoBitcast = false;
14316 if (InVec.getOpcode() == ISD::BITCAST) {
14317 // Don't duplicate a load with other uses.
14318 if (!InVec.hasOneUse())
14319 return SDValue();
14320 EVT BCVT = InVec.getOperand(0).getValueType();
14321 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14322 return SDValue();
14323 InVec = InVec.getOperand(0);
14324 HasShuffleIntoBitcast = true;
14325 }
14326
14327 if (!isTargetShuffle(InVec.getOpcode()))
14328 return SDValue();
14329
14330 // Don't duplicate a load with other uses.
14331 if (!InVec.hasOneUse())
14332 return SDValue();
14333
14334 SmallVector<int, 16> ShuffleMask;
14335 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014336 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14337 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014338 return SDValue();
14339
14340 // Select the input vector, guarding against out of range extract vector.
14341 unsigned NumElems = VT.getVectorNumElements();
14342 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14343 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14344 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14345 : InVec.getOperand(1);
14346
14347 // If inputs to shuffle are the same for both ops, then allow 2 uses
14348 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14349
14350 if (LdNode.getOpcode() == ISD::BITCAST) {
14351 // Don't duplicate a load with other uses.
14352 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14353 return SDValue();
14354
14355 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14356 LdNode = LdNode.getOperand(0);
14357 }
14358
14359 if (!ISD::isNormalLoad(LdNode.getNode()))
14360 return SDValue();
14361
14362 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14363
14364 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14365 return SDValue();
14366
14367 if (HasShuffleIntoBitcast) {
14368 // If there's a bitcast before the shuffle, check if the load type and
14369 // alignment is valid.
14370 unsigned Align = LN0->getAlignment();
14371 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014372 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014373 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14374
14375 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14376 return SDValue();
14377 }
14378
14379 // All checks match so transform back to vector_shuffle so that DAG combiner
14380 // can finish the job
14381 DebugLoc dl = N->getDebugLoc();
14382
14383 // Create shuffle node taking into account the case that its a unary shuffle
14384 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14385 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14386 InVec.getOperand(0), Shuffle,
14387 &ShuffleMask[0]);
14388 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14389 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14390 EltNo);
14391}
14392
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014393/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14394/// generation and convert it from being a bunch of shuffles and extracts
14395/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014396static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014397 TargetLowering::DAGCombinerInfo &DCI) {
14398 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14399 if (NewOp.getNode())
14400 return NewOp;
14401
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014402 SDValue InputVector = N->getOperand(0);
14403
14404 // Only operate on vectors of 4 elements, where the alternative shuffling
14405 // gets to be more expensive.
14406 if (InputVector.getValueType() != MVT::v4i32)
14407 return SDValue();
14408
14409 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14410 // single use which is a sign-extend or zero-extend, and all elements are
14411 // used.
14412 SmallVector<SDNode *, 4> Uses;
14413 unsigned ExtractedElements = 0;
14414 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14415 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14416 if (UI.getUse().getResNo() != InputVector.getResNo())
14417 return SDValue();
14418
14419 SDNode *Extract = *UI;
14420 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14421 return SDValue();
14422
14423 if (Extract->getValueType(0) != MVT::i32)
14424 return SDValue();
14425 if (!Extract->hasOneUse())
14426 return SDValue();
14427 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14428 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14429 return SDValue();
14430 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14431 return SDValue();
14432
14433 // Record which element was extracted.
14434 ExtractedElements |=
14435 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14436
14437 Uses.push_back(Extract);
14438 }
14439
14440 // If not all the elements were used, this may not be worthwhile.
14441 if (ExtractedElements != 15)
14442 return SDValue();
14443
14444 // Ok, we've now decided to do the transformation.
14445 DebugLoc dl = InputVector.getDebugLoc();
14446
14447 // Store the value to a temporary stack slot.
14448 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014449 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14450 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014451
14452 // Replace each use (extract) with a load of the appropriate element.
14453 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14454 UE = Uses.end(); UI != UE; ++UI) {
14455 SDNode *Extract = *UI;
14456
Nadav Rotem86694292011-05-17 08:31:57 +000014457 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014458 SDValue Idx = Extract->getOperand(1);
14459 unsigned EltSize =
14460 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14461 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014462 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014463 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14464
Nadav Rotem86694292011-05-17 08:31:57 +000014465 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014466 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014467
14468 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014469 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014470 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014471 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014472
14473 // Replace the exact with the load.
14474 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14475 }
14476
14477 // The replacement was made in place; don't return anything.
14478 return SDValue();
14479}
14480
Duncan Sands6bcd2192011-09-17 16:49:39 +000014481/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14482/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014483static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014484 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014485 const X86Subtarget *Subtarget) {
14486 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014487 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014488 // Get the LHS/RHS of the select.
14489 SDValue LHS = N->getOperand(1);
14490 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014491 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014492
Dan Gohman670e5392009-09-21 18:03:22 +000014493 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014494 // instructions match the semantics of the common C idiom x<y?x:y but not
14495 // x<=y?x:y, because of how they handle negative zero (which can be
14496 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014497 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14498 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014499 (Subtarget->hasSSE2() ||
14500 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014501 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014502
Chris Lattner47b4ce82009-03-11 05:48:52 +000014503 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014504 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014505 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14506 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014507 switch (CC) {
14508 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014509 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014510 // Converting this to a min would handle NaNs incorrectly, and swapping
14511 // the operands would cause it to handle comparisons between positive
14512 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014513 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014514 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014515 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14516 break;
14517 std::swap(LHS, RHS);
14518 }
Dan Gohman670e5392009-09-21 18:03:22 +000014519 Opcode = X86ISD::FMIN;
14520 break;
14521 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014522 // Converting this to a min would handle comparisons between positive
14523 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014524 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014525 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14526 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014527 Opcode = X86ISD::FMIN;
14528 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014529 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014530 // Converting this to a min would handle both negative zeros and NaNs
14531 // incorrectly, but we can swap the operands to fix both.
14532 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014533 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014534 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014535 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014536 Opcode = X86ISD::FMIN;
14537 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014538
Dan Gohman670e5392009-09-21 18:03:22 +000014539 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014540 // Converting this to a max would handle comparisons between positive
14541 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014542 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014543 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014544 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014545 Opcode = X86ISD::FMAX;
14546 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014547 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014548 // Converting this to a max would handle NaNs incorrectly, and swapping
14549 // the operands would cause it to handle comparisons between positive
14550 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014551 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014552 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014553 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14554 break;
14555 std::swap(LHS, RHS);
14556 }
Dan Gohman670e5392009-09-21 18:03:22 +000014557 Opcode = X86ISD::FMAX;
14558 break;
14559 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014560 // Converting this to a max would handle both negative zeros and NaNs
14561 // incorrectly, but we can swap the operands to fix both.
14562 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014563 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014564 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014565 case ISD::SETGE:
14566 Opcode = X86ISD::FMAX;
14567 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014568 }
Dan Gohman670e5392009-09-21 18:03:22 +000014569 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014570 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14571 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014572 switch (CC) {
14573 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014574 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014575 // Converting this to a min would handle comparisons between positive
14576 // and negative zero incorrectly, and swapping the operands would
14577 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014578 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014579 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014580 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014581 break;
14582 std::swap(LHS, RHS);
14583 }
Dan Gohman670e5392009-09-21 18:03:22 +000014584 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014585 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014586 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014587 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014588 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014589 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14590 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014591 Opcode = X86ISD::FMIN;
14592 break;
14593 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014594 // Converting this to a min would handle both negative zeros and NaNs
14595 // incorrectly, but we can swap the operands to fix both.
14596 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014597 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014598 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014599 case ISD::SETGE:
14600 Opcode = X86ISD::FMIN;
14601 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014602
Dan Gohman670e5392009-09-21 18:03:22 +000014603 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014604 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014605 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014606 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014607 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014608 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014609 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014610 // Converting this to a max would handle comparisons between positive
14611 // and negative zero incorrectly, and swapping the operands would
14612 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014613 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014614 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014615 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014616 break;
14617 std::swap(LHS, RHS);
14618 }
Dan Gohman670e5392009-09-21 18:03:22 +000014619 Opcode = X86ISD::FMAX;
14620 break;
14621 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014622 // Converting this to a max would handle both negative zeros and NaNs
14623 // incorrectly, but we can swap the operands to fix both.
14624 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014625 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014626 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014627 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014628 Opcode = X86ISD::FMAX;
14629 break;
14630 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014631 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014632
Chris Lattner47b4ce82009-03-11 05:48:52 +000014633 if (Opcode)
14634 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014635 }
Eric Christopherfd179292009-08-27 18:07:15 +000014636
Chris Lattnerd1980a52009-03-12 06:52:53 +000014637 // If this is a select between two integer constants, try to do some
14638 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014639 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14640 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014641 // Don't do this for crazy integer types.
14642 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14643 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014644 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014645 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014646
Chris Lattnercee56e72009-03-13 05:53:31 +000014647 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014648 // Efficiently invertible.
14649 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14650 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14651 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14652 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014653 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014654 }
Eric Christopherfd179292009-08-27 18:07:15 +000014655
Chris Lattnerd1980a52009-03-12 06:52:53 +000014656 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014657 if (FalseC->getAPIntValue() == 0 &&
14658 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014659 if (NeedsCondInvert) // Invert the condition if needed.
14660 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14661 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014662
Chris Lattnerd1980a52009-03-12 06:52:53 +000014663 // Zero extend the condition if needed.
14664 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014665
Chris Lattnercee56e72009-03-13 05:53:31 +000014666 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014667 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014668 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014669 }
Eric Christopherfd179292009-08-27 18:07:15 +000014670
Chris Lattner97a29a52009-03-13 05:22:11 +000014671 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014672 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014673 if (NeedsCondInvert) // Invert the condition if needed.
14674 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14675 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014676
Chris Lattner97a29a52009-03-13 05:22:11 +000014677 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014678 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14679 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014680 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014681 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014682 }
Eric Christopherfd179292009-08-27 18:07:15 +000014683
Chris Lattnercee56e72009-03-13 05:53:31 +000014684 // Optimize cases that will turn into an LEA instruction. This requires
14685 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014686 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014687 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014688 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014689
Chris Lattnercee56e72009-03-13 05:53:31 +000014690 bool isFastMultiplier = false;
14691 if (Diff < 10) {
14692 switch ((unsigned char)Diff) {
14693 default: break;
14694 case 1: // result = add base, cond
14695 case 2: // result = lea base( , cond*2)
14696 case 3: // result = lea base(cond, cond*2)
14697 case 4: // result = lea base( , cond*4)
14698 case 5: // result = lea base(cond, cond*4)
14699 case 8: // result = lea base( , cond*8)
14700 case 9: // result = lea base(cond, cond*8)
14701 isFastMultiplier = true;
14702 break;
14703 }
14704 }
Eric Christopherfd179292009-08-27 18:07:15 +000014705
Chris Lattnercee56e72009-03-13 05:53:31 +000014706 if (isFastMultiplier) {
14707 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14708 if (NeedsCondInvert) // Invert the condition if needed.
14709 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14710 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014711
Chris Lattnercee56e72009-03-13 05:53:31 +000014712 // Zero extend the condition if needed.
14713 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14714 Cond);
14715 // Scale the condition by the difference.
14716 if (Diff != 1)
14717 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14718 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014719
Chris Lattnercee56e72009-03-13 05:53:31 +000014720 // Add the base if non-zero.
14721 if (FalseC->getAPIntValue() != 0)
14722 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14723 SDValue(FalseC, 0));
14724 return Cond;
14725 }
Eric Christopherfd179292009-08-27 18:07:15 +000014726 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014727 }
14728 }
Eric Christopherfd179292009-08-27 18:07:15 +000014729
Evan Cheng56f582d2012-01-04 01:41:39 +000014730 // Canonicalize max and min:
14731 // (x > y) ? x : y -> (x >= y) ? x : y
14732 // (x < y) ? x : y -> (x <= y) ? x : y
14733 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14734 // the need for an extra compare
14735 // against zero. e.g.
14736 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14737 // subl %esi, %edi
14738 // testl %edi, %edi
14739 // movl $0, %eax
14740 // cmovgl %edi, %eax
14741 // =>
14742 // xorl %eax, %eax
14743 // subl %esi, $edi
14744 // cmovsl %eax, %edi
14745 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14746 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14747 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14748 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14749 switch (CC) {
14750 default: break;
14751 case ISD::SETLT:
14752 case ISD::SETGT: {
14753 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14754 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14755 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14756 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14757 }
14758 }
14759 }
14760
Nadav Rotemcc616562012-01-15 19:27:55 +000014761 // If we know that this node is legal then we know that it is going to be
14762 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14763 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14764 // to simplify previous instructions.
14765 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14766 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014767 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014768 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014769
14770 // Don't optimize vector selects that map to mask-registers.
14771 if (BitWidth == 1)
14772 return SDValue();
14773
Nadav Rotemcc616562012-01-15 19:27:55 +000014774 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14775 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14776
14777 APInt KnownZero, KnownOne;
14778 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14779 DCI.isBeforeLegalizeOps());
14780 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14781 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14782 DCI.CommitTargetLoweringOpt(TLO);
14783 }
14784
Dan Gohman475871a2008-07-27 21:46:04 +000014785 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014786}
14787
Michael Liao2a33cec2012-08-10 19:58:13 +000014788// Check whether a boolean test is testing a boolean value generated by
14789// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14790// code.
14791//
14792// Simplify the following patterns:
14793// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14794// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14795// to (Op EFLAGS Cond)
14796//
14797// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14798// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14799// to (Op EFLAGS !Cond)
14800//
14801// where Op could be BRCOND or CMOV.
14802//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014803static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014804 // Quit if not CMP and SUB with its value result used.
14805 if (Cmp.getOpcode() != X86ISD::CMP &&
14806 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14807 return SDValue();
14808
14809 // Quit if not used as a boolean value.
14810 if (CC != X86::COND_E && CC != X86::COND_NE)
14811 return SDValue();
14812
14813 // Check CMP operands. One of them should be 0 or 1 and the other should be
14814 // an SetCC or extended from it.
14815 SDValue Op1 = Cmp.getOperand(0);
14816 SDValue Op2 = Cmp.getOperand(1);
14817
14818 SDValue SetCC;
14819 const ConstantSDNode* C = 0;
14820 bool needOppositeCond = (CC == X86::COND_E);
14821
14822 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14823 SetCC = Op2;
14824 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14825 SetCC = Op1;
14826 else // Quit if all operands are not constants.
14827 return SDValue();
14828
14829 if (C->getZExtValue() == 1)
14830 needOppositeCond = !needOppositeCond;
14831 else if (C->getZExtValue() != 0)
14832 // Quit if the constant is neither 0 or 1.
14833 return SDValue();
14834
14835 // Skip 'zext' node.
14836 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14837 SetCC = SetCC.getOperand(0);
14838
Michael Liao7fdc66b2012-09-10 16:36:16 +000014839 switch (SetCC.getOpcode()) {
14840 case X86ISD::SETCC:
14841 // Set the condition code or opposite one if necessary.
14842 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14843 if (needOppositeCond)
14844 CC = X86::GetOppositeBranchCondition(CC);
14845 return SetCC.getOperand(1);
14846 case X86ISD::CMOV: {
14847 // Check whether false/true value has canonical one, i.e. 0 or 1.
14848 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14849 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14850 // Quit if true value is not a constant.
14851 if (!TVal)
14852 return SDValue();
14853 // Quit if false value is not a constant.
14854 if (!FVal) {
14855 // A special case for rdrand, where 0 is set if false cond is found.
14856 SDValue Op = SetCC.getOperand(0);
14857 if (Op.getOpcode() != X86ISD::RDRAND)
14858 return SDValue();
14859 }
14860 // Quit if false value is not the constant 0 or 1.
14861 bool FValIsFalse = true;
14862 if (FVal && FVal->getZExtValue() != 0) {
14863 if (FVal->getZExtValue() != 1)
14864 return SDValue();
14865 // If FVal is 1, opposite cond is needed.
14866 needOppositeCond = !needOppositeCond;
14867 FValIsFalse = false;
14868 }
14869 // Quit if TVal is not the constant opposite of FVal.
14870 if (FValIsFalse && TVal->getZExtValue() != 1)
14871 return SDValue();
14872 if (!FValIsFalse && TVal->getZExtValue() != 0)
14873 return SDValue();
14874 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14875 if (needOppositeCond)
14876 CC = X86::GetOppositeBranchCondition(CC);
14877 return SetCC.getOperand(3);
14878 }
14879 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014880
Michael Liao7fdc66b2012-09-10 16:36:16 +000014881 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014882}
14883
Chris Lattnerd1980a52009-03-12 06:52:53 +000014884/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14885static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014886 TargetLowering::DAGCombinerInfo &DCI,
14887 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014888 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014889
Chris Lattnerd1980a52009-03-12 06:52:53 +000014890 // If the flag operand isn't dead, don't touch this CMOV.
14891 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14892 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014893
Evan Chengb5a55d92011-05-24 01:48:22 +000014894 SDValue FalseOp = N->getOperand(0);
14895 SDValue TrueOp = N->getOperand(1);
14896 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14897 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014898
Evan Chengb5a55d92011-05-24 01:48:22 +000014899 if (CC == X86::COND_E || CC == X86::COND_NE) {
14900 switch (Cond.getOpcode()) {
14901 default: break;
14902 case X86ISD::BSR:
14903 case X86ISD::BSF:
14904 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14905 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14906 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14907 }
14908 }
14909
Michael Liao2a33cec2012-08-10 19:58:13 +000014910 SDValue Flags;
14911
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014912 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014913 if (Flags.getNode() &&
14914 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014915 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014916 SDValue Ops[] = { FalseOp, TrueOp,
14917 DAG.getConstant(CC, MVT::i8), Flags };
14918 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14919 Ops, array_lengthof(Ops));
14920 }
14921
Chris Lattnerd1980a52009-03-12 06:52:53 +000014922 // If this is a select between two integer constants, try to do some
14923 // optimizations. Note that the operands are ordered the opposite of SELECT
14924 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014925 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14926 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014927 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14928 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014929 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14930 CC = X86::GetOppositeBranchCondition(CC);
14931 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000014932 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014933 }
Eric Christopherfd179292009-08-27 18:07:15 +000014934
Chris Lattnerd1980a52009-03-12 06:52:53 +000014935 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014936 // This is efficient for any integer data type (including i8/i16) and
14937 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014938 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014939 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14940 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014941
Chris Lattnerd1980a52009-03-12 06:52:53 +000014942 // Zero extend the condition if needed.
14943 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014944
Chris Lattnerd1980a52009-03-12 06:52:53 +000014945 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14946 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014947 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014948 if (N->getNumValues() == 2) // Dead flag value?
14949 return DCI.CombineTo(N, Cond, SDValue());
14950 return Cond;
14951 }
Eric Christopherfd179292009-08-27 18:07:15 +000014952
Chris Lattnercee56e72009-03-13 05:53:31 +000014953 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14954 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014955 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014956 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14957 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014958
Chris Lattner97a29a52009-03-13 05:22:11 +000014959 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014960 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14961 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014962 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14963 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014964
Chris Lattner97a29a52009-03-13 05:22:11 +000014965 if (N->getNumValues() == 2) // Dead flag value?
14966 return DCI.CombineTo(N, Cond, SDValue());
14967 return Cond;
14968 }
Eric Christopherfd179292009-08-27 18:07:15 +000014969
Chris Lattnercee56e72009-03-13 05:53:31 +000014970 // Optimize cases that will turn into an LEA instruction. This requires
14971 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014972 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014973 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014974 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014975
Chris Lattnercee56e72009-03-13 05:53:31 +000014976 bool isFastMultiplier = false;
14977 if (Diff < 10) {
14978 switch ((unsigned char)Diff) {
14979 default: break;
14980 case 1: // result = add base, cond
14981 case 2: // result = lea base( , cond*2)
14982 case 3: // result = lea base(cond, cond*2)
14983 case 4: // result = lea base( , cond*4)
14984 case 5: // result = lea base(cond, cond*4)
14985 case 8: // result = lea base( , cond*8)
14986 case 9: // result = lea base(cond, cond*8)
14987 isFastMultiplier = true;
14988 break;
14989 }
14990 }
Eric Christopherfd179292009-08-27 18:07:15 +000014991
Chris Lattnercee56e72009-03-13 05:53:31 +000014992 if (isFastMultiplier) {
14993 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014994 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14995 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014996 // Zero extend the condition if needed.
14997 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14998 Cond);
14999 // Scale the condition by the difference.
15000 if (Diff != 1)
15001 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15002 DAG.getConstant(Diff, Cond.getValueType()));
15003
15004 // Add the base if non-zero.
15005 if (FalseC->getAPIntValue() != 0)
15006 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15007 SDValue(FalseC, 0));
15008 if (N->getNumValues() == 2) // Dead flag value?
15009 return DCI.CombineTo(N, Cond, SDValue());
15010 return Cond;
15011 }
Eric Christopherfd179292009-08-27 18:07:15 +000015012 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015013 }
15014 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015015
15016 // Handle these cases:
15017 // (select (x != c), e, c) -> select (x != c), e, x),
15018 // (select (x == c), c, e) -> select (x == c), x, e)
15019 // where the c is an integer constant, and the "select" is the combination
15020 // of CMOV and CMP.
15021 //
15022 // The rationale for this change is that the conditional-move from a constant
15023 // needs two instructions, however, conditional-move from a register needs
15024 // only one instruction.
15025 //
15026 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15027 // some instruction-combining opportunities. This opt needs to be
15028 // postponed as late as possible.
15029 //
15030 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15031 // the DCI.xxxx conditions are provided to postpone the optimization as
15032 // late as possible.
15033
15034 ConstantSDNode *CmpAgainst = 0;
15035 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15036 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15037 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15038
15039 if (CC == X86::COND_NE &&
15040 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15041 CC = X86::GetOppositeBranchCondition(CC);
15042 std::swap(TrueOp, FalseOp);
15043 }
15044
15045 if (CC == X86::COND_E &&
15046 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15047 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15048 DAG.getConstant(CC, MVT::i8), Cond };
15049 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15050 array_lengthof(Ops));
15051 }
15052 }
15053 }
15054
Chris Lattnerd1980a52009-03-12 06:52:53 +000015055 return SDValue();
15056}
15057
15058
Evan Cheng0b0cd912009-03-28 05:57:29 +000015059/// PerformMulCombine - Optimize a single multiply with constant into two
15060/// in order to implement it with two cheaper instructions, e.g.
15061/// LEA + SHL, LEA + LEA.
15062static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15063 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015064 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15065 return SDValue();
15066
Owen Andersone50ed302009-08-10 22:56:29 +000015067 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015068 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015069 return SDValue();
15070
15071 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15072 if (!C)
15073 return SDValue();
15074 uint64_t MulAmt = C->getZExtValue();
15075 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15076 return SDValue();
15077
15078 uint64_t MulAmt1 = 0;
15079 uint64_t MulAmt2 = 0;
15080 if ((MulAmt % 9) == 0) {
15081 MulAmt1 = 9;
15082 MulAmt2 = MulAmt / 9;
15083 } else if ((MulAmt % 5) == 0) {
15084 MulAmt1 = 5;
15085 MulAmt2 = MulAmt / 5;
15086 } else if ((MulAmt % 3) == 0) {
15087 MulAmt1 = 3;
15088 MulAmt2 = MulAmt / 3;
15089 }
15090 if (MulAmt2 &&
15091 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15092 DebugLoc DL = N->getDebugLoc();
15093
15094 if (isPowerOf2_64(MulAmt2) &&
15095 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15096 // If second multiplifer is pow2, issue it first. We want the multiply by
15097 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15098 // is an add.
15099 std::swap(MulAmt1, MulAmt2);
15100
15101 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015102 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015103 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015104 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015105 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015106 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015107 DAG.getConstant(MulAmt1, VT));
15108
Eric Christopherfd179292009-08-27 18:07:15 +000015109 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015110 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015111 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015112 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015113 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015114 DAG.getConstant(MulAmt2, VT));
15115
15116 // Do not add new nodes to DAG combiner worklist.
15117 DCI.CombineTo(N, NewMul, false);
15118 }
15119 return SDValue();
15120}
15121
Evan Chengad9c0a32009-12-15 00:53:42 +000015122static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15123 SDValue N0 = N->getOperand(0);
15124 SDValue N1 = N->getOperand(1);
15125 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15126 EVT VT = N0.getValueType();
15127
15128 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15129 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015130 if (VT.isInteger() && !VT.isVector() &&
15131 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015132 N0.getOperand(1).getOpcode() == ISD::Constant) {
15133 SDValue N00 = N0.getOperand(0);
15134 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15135 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15136 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15137 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15138 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15139 APInt ShAmt = N1C->getAPIntValue();
15140 Mask = Mask.shl(ShAmt);
15141 if (Mask != 0)
15142 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15143 N00, DAG.getConstant(Mask, VT));
15144 }
15145 }
15146
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015147
15148 // Hardware support for vector shifts is sparse which makes us scalarize the
15149 // vector operations in many cases. Also, on sandybridge ADD is faster than
15150 // shl.
15151 // (shl V, 1) -> add V,V
15152 if (isSplatVector(N1.getNode())) {
15153 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15154 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15155 // We shift all of the values by one. In many cases we do not have
15156 // hardware support for this operation. This is better expressed as an ADD
15157 // of two values.
15158 if (N1C && (1 == N1C->getZExtValue())) {
15159 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15160 }
15161 }
15162
Evan Chengad9c0a32009-12-15 00:53:42 +000015163 return SDValue();
15164}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015165
Nate Begeman740ab032009-01-26 00:52:55 +000015166/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15167/// when possible.
15168static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015169 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015170 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015171 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015172 if (N->getOpcode() == ISD::SHL) {
15173 SDValue V = PerformSHLCombine(N, DAG);
15174 if (V.getNode()) return V;
15175 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015176
Nate Begeman740ab032009-01-26 00:52:55 +000015177 // On X86 with SSE2 support, we can transform this to a vector shift if
15178 // all elements are shifted by the same amount. We can't do this in legalize
15179 // because the a constant vector is typically transformed to a constant pool
15180 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015181 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015182 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015183
Craig Topper7be5dfd2011-11-12 09:58:49 +000015184 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
15185 (!Subtarget->hasAVX2() ||
15186 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015187 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015188
Mon P Wang3becd092009-01-28 08:12:05 +000015189 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015190 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015191 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015192 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015193 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15194 unsigned NumElts = VT.getVectorNumElements();
15195 unsigned i = 0;
15196 for (; i != NumElts; ++i) {
15197 SDValue Arg = ShAmtOp.getOperand(i);
15198 if (Arg.getOpcode() == ISD::UNDEF) continue;
15199 BaseShAmt = Arg;
15200 break;
15201 }
Craig Topper37c26772012-01-17 04:44:50 +000015202 // Handle the case where the build_vector is all undef
15203 // FIXME: Should DAG allow this?
15204 if (i == NumElts)
15205 return SDValue();
15206
Mon P Wang3becd092009-01-28 08:12:05 +000015207 for (; i != NumElts; ++i) {
15208 SDValue Arg = ShAmtOp.getOperand(i);
15209 if (Arg.getOpcode() == ISD::UNDEF) continue;
15210 if (Arg != BaseShAmt) {
15211 return SDValue();
15212 }
15213 }
15214 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015215 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015216 SDValue InVec = ShAmtOp.getOperand(0);
15217 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15218 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15219 unsigned i = 0;
15220 for (; i != NumElts; ++i) {
15221 SDValue Arg = InVec.getOperand(i);
15222 if (Arg.getOpcode() == ISD::UNDEF) continue;
15223 BaseShAmt = Arg;
15224 break;
15225 }
15226 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15227 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015228 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015229 if (C->getZExtValue() == SplatIdx)
15230 BaseShAmt = InVec.getOperand(1);
15231 }
15232 }
Mon P Wang845b1892012-02-01 22:15:20 +000015233 if (BaseShAmt.getNode() == 0) {
15234 // Don't create instructions with illegal types after legalize
15235 // types has run.
15236 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15237 !DCI.isBeforeLegalize())
15238 return SDValue();
15239
Mon P Wangefa42202009-09-03 19:56:25 +000015240 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15241 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015242 }
Mon P Wang3becd092009-01-28 08:12:05 +000015243 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015244 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015245
Mon P Wangefa42202009-09-03 19:56:25 +000015246 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015247 if (EltVT.bitsGT(MVT::i32))
15248 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15249 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015250 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015251
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015252 // The shift amount is identical so we can do a vector shift.
15253 SDValue ValOp = N->getOperand(0);
15254 switch (N->getOpcode()) {
15255 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015256 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015257 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015258 switch (VT.getSimpleVT().SimpleTy) {
15259 default: return SDValue();
15260 case MVT::v2i64:
15261 case MVT::v4i32:
15262 case MVT::v8i16:
15263 case MVT::v4i64:
15264 case MVT::v8i32:
15265 case MVT::v16i16:
15266 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15267 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015268 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015269 switch (VT.getSimpleVT().SimpleTy) {
15270 default: return SDValue();
15271 case MVT::v4i32:
15272 case MVT::v8i16:
15273 case MVT::v8i32:
15274 case MVT::v16i16:
15275 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15276 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015277 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015278 switch (VT.getSimpleVT().SimpleTy) {
15279 default: return SDValue();
15280 case MVT::v2i64:
15281 case MVT::v4i32:
15282 case MVT::v8i16:
15283 case MVT::v4i64:
15284 case MVT::v8i32:
15285 case MVT::v16i16:
15286 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15287 }
Nate Begeman740ab032009-01-26 00:52:55 +000015288 }
Nate Begeman740ab032009-01-26 00:52:55 +000015289}
15290
Nate Begemanb65c1752010-12-17 22:55:37 +000015291
Stuart Hastings865f0932011-06-03 23:53:54 +000015292// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15293// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15294// and friends. Likewise for OR -> CMPNEQSS.
15295static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15296 TargetLowering::DAGCombinerInfo &DCI,
15297 const X86Subtarget *Subtarget) {
15298 unsigned opcode;
15299
15300 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15301 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015302 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015303 SDValue N0 = N->getOperand(0);
15304 SDValue N1 = N->getOperand(1);
15305 SDValue CMP0 = N0->getOperand(1);
15306 SDValue CMP1 = N1->getOperand(1);
15307 DebugLoc DL = N->getDebugLoc();
15308
15309 // The SETCCs should both refer to the same CMP.
15310 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15311 return SDValue();
15312
15313 SDValue CMP00 = CMP0->getOperand(0);
15314 SDValue CMP01 = CMP0->getOperand(1);
15315 EVT VT = CMP00.getValueType();
15316
15317 if (VT == MVT::f32 || VT == MVT::f64) {
15318 bool ExpectingFlags = false;
15319 // Check for any users that want flags:
15320 for (SDNode::use_iterator UI = N->use_begin(),
15321 UE = N->use_end();
15322 !ExpectingFlags && UI != UE; ++UI)
15323 switch (UI->getOpcode()) {
15324 default:
15325 case ISD::BR_CC:
15326 case ISD::BRCOND:
15327 case ISD::SELECT:
15328 ExpectingFlags = true;
15329 break;
15330 case ISD::CopyToReg:
15331 case ISD::SIGN_EXTEND:
15332 case ISD::ZERO_EXTEND:
15333 case ISD::ANY_EXTEND:
15334 break;
15335 }
15336
15337 if (!ExpectingFlags) {
15338 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15339 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15340
15341 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15342 X86::CondCode tmp = cc0;
15343 cc0 = cc1;
15344 cc1 = tmp;
15345 }
15346
15347 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15348 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15349 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15350 X86ISD::NodeType NTOperator = is64BitFP ?
15351 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15352 // FIXME: need symbolic constants for these magic numbers.
15353 // See X86ATTInstPrinter.cpp:printSSECC().
15354 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15355 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15356 DAG.getConstant(x86cc, MVT::i8));
15357 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15358 OnesOrZeroesF);
15359 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15360 DAG.getConstant(1, MVT::i32));
15361 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15362 return OneBitOfTruth;
15363 }
15364 }
15365 }
15366 }
15367 return SDValue();
15368}
15369
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015370/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15371/// so it can be folded inside ANDNP.
15372static bool CanFoldXORWithAllOnes(const SDNode *N) {
15373 EVT VT = N->getValueType(0);
15374
15375 // Match direct AllOnes for 128 and 256-bit vectors
15376 if (ISD::isBuildVectorAllOnes(N))
15377 return true;
15378
15379 // Look through a bit convert.
15380 if (N->getOpcode() == ISD::BITCAST)
15381 N = N->getOperand(0).getNode();
15382
15383 // Sometimes the operand may come from a insert_subvector building a 256-bit
15384 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015385 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015386 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15387 SDValue V1 = N->getOperand(0);
15388 SDValue V2 = N->getOperand(1);
15389
15390 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15391 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15392 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15393 ISD::isBuildVectorAllOnes(V2.getNode()))
15394 return true;
15395 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015396
15397 return false;
15398}
15399
Nate Begemanb65c1752010-12-17 22:55:37 +000015400static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15401 TargetLowering::DAGCombinerInfo &DCI,
15402 const X86Subtarget *Subtarget) {
15403 if (DCI.isBeforeLegalizeOps())
15404 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015405
Stuart Hastings865f0932011-06-03 23:53:54 +000015406 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15407 if (R.getNode())
15408 return R;
15409
Craig Topper54a11172011-10-14 07:06:56 +000015410 EVT VT = N->getValueType(0);
15411
Craig Topperb4c94572011-10-21 06:55:01 +000015412 // Create ANDN, BLSI, and BLSR instructions
15413 // BLSI is X & (-X)
15414 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015415 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15416 SDValue N0 = N->getOperand(0);
15417 SDValue N1 = N->getOperand(1);
15418 DebugLoc DL = N->getDebugLoc();
15419
15420 // Check LHS for not
15421 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
15422 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
15423 // Check RHS for not
15424 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
15425 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
15426
Craig Topperb4c94572011-10-21 06:55:01 +000015427 // Check LHS for neg
15428 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15429 isZero(N0.getOperand(0)))
15430 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15431
15432 // Check RHS for neg
15433 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15434 isZero(N1.getOperand(0)))
15435 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15436
15437 // Check LHS for X-1
15438 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15439 isAllOnes(N0.getOperand(1)))
15440 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15441
15442 // Check RHS for X-1
15443 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15444 isAllOnes(N1.getOperand(1)))
15445 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15446
Craig Topper54a11172011-10-14 07:06:56 +000015447 return SDValue();
15448 }
15449
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015450 // Want to form ANDNP nodes:
15451 // 1) In the hopes of then easily combining them with OR and AND nodes
15452 // to form PBLEND/PSIGN.
15453 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015454 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015455 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015456
Nate Begemanb65c1752010-12-17 22:55:37 +000015457 SDValue N0 = N->getOperand(0);
15458 SDValue N1 = N->getOperand(1);
15459 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015460
Nate Begemanb65c1752010-12-17 22:55:37 +000015461 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015462 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015463 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15464 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015465 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015466
15467 // Check RHS for vnot
15468 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015469 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15470 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015471 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015472
Nate Begemanb65c1752010-12-17 22:55:37 +000015473 return SDValue();
15474}
15475
Evan Cheng760d1942010-01-04 21:22:48 +000015476static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015477 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015478 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015479 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015480 return SDValue();
15481
Stuart Hastings865f0932011-06-03 23:53:54 +000015482 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15483 if (R.getNode())
15484 return R;
15485
Evan Cheng760d1942010-01-04 21:22:48 +000015486 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015487
Evan Cheng760d1942010-01-04 21:22:48 +000015488 SDValue N0 = N->getOperand(0);
15489 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015490
Nate Begemanb65c1752010-12-17 22:55:37 +000015491 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015492 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015493 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000015494 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
15495 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015496
Craig Topper1666cb62011-11-19 07:07:26 +000015497 // Canonicalize pandn to RHS
15498 if (N0.getOpcode() == X86ISD::ANDNP)
15499 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015500 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015501 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15502 SDValue Mask = N1.getOperand(0);
15503 SDValue X = N1.getOperand(1);
15504 SDValue Y;
15505 if (N0.getOperand(0) == Mask)
15506 Y = N0.getOperand(1);
15507 if (N0.getOperand(1) == Mask)
15508 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015509
Craig Topper1666cb62011-11-19 07:07:26 +000015510 // Check to see if the mask appeared in both the AND and ANDNP and
15511 if (!Y.getNode())
15512 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015513
Craig Topper1666cb62011-11-19 07:07:26 +000015514 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015515 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015516 if (Mask.getOpcode() == ISD::BITCAST)
15517 Mask = Mask.getOperand(0);
15518 if (X.getOpcode() == ISD::BITCAST)
15519 X = X.getOperand(0);
15520 if (Y.getOpcode() == ISD::BITCAST)
15521 Y = Y.getOperand(0);
15522
Craig Topper1666cb62011-11-19 07:07:26 +000015523 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015524
Craig Toppered2e13d2012-01-22 19:15:14 +000015525 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015526 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15527 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015528 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015529 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015530
15531 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015532 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015533 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15534 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15535 if ((SraAmt + 1) != EltBits)
15536 return SDValue();
15537
15538 DebugLoc DL = N->getDebugLoc();
15539
15540 // Now we know we at least have a plendvb with the mask val. See if
15541 // we can form a psignb/w/d.
15542 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015543 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15544 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015545 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15546 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15547 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015548 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015549 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015550 }
15551 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015552 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015553 return SDValue();
15554
15555 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15556
15557 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15558 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15559 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015560 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015561 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015562 }
15563 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015564
Craig Topper1666cb62011-11-19 07:07:26 +000015565 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15566 return SDValue();
15567
Nate Begemanb65c1752010-12-17 22:55:37 +000015568 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015569 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15570 std::swap(N0, N1);
15571 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15572 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015573 if (!N0.hasOneUse() || !N1.hasOneUse())
15574 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015575
15576 SDValue ShAmt0 = N0.getOperand(1);
15577 if (ShAmt0.getValueType() != MVT::i8)
15578 return SDValue();
15579 SDValue ShAmt1 = N1.getOperand(1);
15580 if (ShAmt1.getValueType() != MVT::i8)
15581 return SDValue();
15582 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15583 ShAmt0 = ShAmt0.getOperand(0);
15584 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15585 ShAmt1 = ShAmt1.getOperand(0);
15586
15587 DebugLoc DL = N->getDebugLoc();
15588 unsigned Opc = X86ISD::SHLD;
15589 SDValue Op0 = N0.getOperand(0);
15590 SDValue Op1 = N1.getOperand(0);
15591 if (ShAmt0.getOpcode() == ISD::SUB) {
15592 Opc = X86ISD::SHRD;
15593 std::swap(Op0, Op1);
15594 std::swap(ShAmt0, ShAmt1);
15595 }
15596
Evan Cheng8b1190a2010-04-28 01:18:01 +000015597 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015598 if (ShAmt1.getOpcode() == ISD::SUB) {
15599 SDValue Sum = ShAmt1.getOperand(0);
15600 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015601 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15602 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15603 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15604 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015605 return DAG.getNode(Opc, DL, VT,
15606 Op0, Op1,
15607 DAG.getNode(ISD::TRUNCATE, DL,
15608 MVT::i8, ShAmt0));
15609 }
15610 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15611 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15612 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015613 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015614 return DAG.getNode(Opc, DL, VT,
15615 N0.getOperand(0), N1.getOperand(0),
15616 DAG.getNode(ISD::TRUNCATE, DL,
15617 MVT::i8, ShAmt0));
15618 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015619
Evan Cheng760d1942010-01-04 21:22:48 +000015620 return SDValue();
15621}
15622
Manman Ren92363622012-06-07 22:39:10 +000015623// Generate NEG and CMOV for integer abs.
15624static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15625 EVT VT = N->getValueType(0);
15626
15627 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15628 // 8-bit integer abs to NEG and CMOV.
15629 if (VT.isInteger() && VT.getSizeInBits() == 8)
15630 return SDValue();
15631
15632 SDValue N0 = N->getOperand(0);
15633 SDValue N1 = N->getOperand(1);
15634 DebugLoc DL = N->getDebugLoc();
15635
15636 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15637 // and change it to SUB and CMOV.
15638 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15639 N0.getOpcode() == ISD::ADD &&
15640 N0.getOperand(1) == N1 &&
15641 N1.getOpcode() == ISD::SRA &&
15642 N1.getOperand(0) == N0.getOperand(0))
15643 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15644 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15645 // Generate SUB & CMOV.
15646 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15647 DAG.getConstant(0, VT), N0.getOperand(0));
15648
15649 SDValue Ops[] = { N0.getOperand(0), Neg,
15650 DAG.getConstant(X86::COND_GE, MVT::i8),
15651 SDValue(Neg.getNode(), 1) };
15652 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15653 Ops, array_lengthof(Ops));
15654 }
15655 return SDValue();
15656}
15657
Craig Topper3738ccd2011-12-27 06:27:23 +000015658// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015659static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15660 TargetLowering::DAGCombinerInfo &DCI,
15661 const X86Subtarget *Subtarget) {
15662 if (DCI.isBeforeLegalizeOps())
15663 return SDValue();
15664
Manman Ren45d53b82012-06-08 18:58:26 +000015665 if (Subtarget->hasCMov()) {
15666 SDValue RV = performIntegerAbsCombine(N, DAG);
15667 if (RV.getNode())
15668 return RV;
15669 }
Manman Ren92363622012-06-07 22:39:10 +000015670
15671 // Try forming BMI if it is available.
15672 if (!Subtarget->hasBMI())
15673 return SDValue();
15674
Craig Topperb4c94572011-10-21 06:55:01 +000015675 EVT VT = N->getValueType(0);
15676
15677 if (VT != MVT::i32 && VT != MVT::i64)
15678 return SDValue();
15679
Craig Topper3738ccd2011-12-27 06:27:23 +000015680 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15681
Craig Topperb4c94572011-10-21 06:55:01 +000015682 // Create BLSMSK instructions by finding X ^ (X-1)
15683 SDValue N0 = N->getOperand(0);
15684 SDValue N1 = N->getOperand(1);
15685 DebugLoc DL = N->getDebugLoc();
15686
15687 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15688 isAllOnes(N0.getOperand(1)))
15689 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15690
15691 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15692 isAllOnes(N1.getOperand(1)))
15693 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15694
15695 return SDValue();
15696}
15697
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015698/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15699static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015700 TargetLowering::DAGCombinerInfo &DCI,
15701 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015702 LoadSDNode *Ld = cast<LoadSDNode>(N);
15703 EVT RegVT = Ld->getValueType(0);
15704 EVT MemVT = Ld->getMemoryVT();
15705 DebugLoc dl = Ld->getDebugLoc();
15706 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15707
15708 ISD::LoadExtType Ext = Ld->getExtensionType();
15709
Nadav Rotemca6f2962011-09-18 19:00:23 +000015710 // If this is a vector EXT Load then attempt to optimize it using a
Michael Liao35a56402012-10-17 03:59:18 +000015711 // shuffle. We need SSSE3 shuffles.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015712 // TODO: It is possible to support ZExt by zeroing the undef values
15713 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015714 if (RegVT.isVector() && RegVT.isInteger() &&
Michael Liao35a56402012-10-17 03:59:18 +000015715 Ext == ISD::EXTLOAD && Subtarget->hasSSSE3()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015716 assert(MemVT != RegVT && "Cannot extend to the same type");
15717 assert(MemVT.isVector() && "Must load a vector from memory");
15718
15719 unsigned NumElems = RegVT.getVectorNumElements();
15720 unsigned RegSz = RegVT.getSizeInBits();
15721 unsigned MemSz = MemVT.getSizeInBits();
15722 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015723
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015724 // All sizes must be a power of two.
15725 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15726 return SDValue();
15727
15728 // Attempt to load the original value using scalar loads.
15729 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015730 MVT SclrLoadTy = MVT::i8;
15731 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15732 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15733 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015734 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015735 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015736 }
15737 }
15738
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015739 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15740 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15741 (64 <= MemSz))
15742 SclrLoadTy = MVT::f64;
15743
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015744 // Calculate the number of scalar loads that we need to perform
15745 // in order to load our vector from memory.
15746 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015747
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015748 // Represent our vector as a sequence of elements which are the
15749 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015750 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15751 RegSz/SclrLoadTy.getSizeInBits());
15752
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015753 // Represent the data using the same element type that is stored in
15754 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015755 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15756 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015757
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015758 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15759 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015760
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015761 // We can't shuffle using an illegal type.
15762 if (!TLI.isTypeLegal(WideVecVT))
15763 return SDValue();
15764
15765 SmallVector<SDValue, 8> Chains;
15766 SDValue Ptr = Ld->getBasePtr();
15767 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15768 TLI.getPointerTy());
15769 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15770
15771 for (unsigned i = 0; i < NumLoads; ++i) {
15772 // Perform a single load.
15773 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15774 Ptr, Ld->getPointerInfo(),
15775 Ld->isVolatile(), Ld->isNonTemporal(),
15776 Ld->isInvariant(), Ld->getAlignment());
15777 Chains.push_back(ScalarLoad.getValue(1));
15778 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15779 // another round of DAGCombining.
15780 if (i == 0)
15781 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15782 else
15783 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15784 ScalarLoad, DAG.getIntPtrConstant(i));
15785
15786 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15787 }
15788
15789 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15790 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015791
15792 // Bitcast the loaded value to a vector of the original element type, in
15793 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015794 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015795 unsigned SizeRatio = RegSz/MemSz;
15796
15797 // Redistribute the loaded elements into the different locations.
15798 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015799 for (unsigned i = 0; i != NumElems; ++i)
15800 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015801
15802 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015803 DAG.getUNDEF(WideVecVT),
15804 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015805
15806 // Bitcast to the requested type.
15807 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15808 // Replace the original load with the new sequence
15809 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015810 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015811 }
15812
15813 return SDValue();
15814}
15815
Chris Lattner149a4e52008-02-22 02:09:43 +000015816/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015817static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015818 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015819 StoreSDNode *St = cast<StoreSDNode>(N);
15820 EVT VT = St->getValue().getValueType();
15821 EVT StVT = St->getMemoryVT();
15822 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015823 SDValue StoredVal = St->getOperand(1);
15824 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15825
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015826 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015827 // On Sandy Bridge, 256-bit memory operations are executed by two
15828 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15829 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015830 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015831 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15832 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015833 SDValue Value0 = StoredVal.getOperand(0);
15834 SDValue Value1 = StoredVal.getOperand(1);
15835
15836 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15837 SDValue Ptr0 = St->getBasePtr();
15838 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15839
15840 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15841 St->getPointerInfo(), St->isVolatile(),
15842 St->isNonTemporal(), St->getAlignment());
15843 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15844 St->getPointerInfo(), St->isVolatile(),
15845 St->isNonTemporal(), St->getAlignment());
15846 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15847 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015848
15849 // Optimize trunc store (of multiple scalars) to shuffle and store.
15850 // First, pack all of the elements in one place. Next, store to memory
15851 // in fewer chunks.
15852 if (St->isTruncatingStore() && VT.isVector()) {
15853 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15854 unsigned NumElems = VT.getVectorNumElements();
15855 assert(StVT != VT && "Cannot truncate to the same type");
15856 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15857 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15858
15859 // From, To sizes and ElemCount must be pow of two
15860 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015861 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015862 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015863 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015864
Nadav Rotem614061b2011-08-10 19:30:14 +000015865 unsigned SizeRatio = FromSz / ToSz;
15866
15867 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15868
15869 // Create a type on which we perform the shuffle
15870 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15871 StVT.getScalarType(), NumElems*SizeRatio);
15872
15873 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15874
15875 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15876 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015877 for (unsigned i = 0; i != NumElems; ++i)
15878 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015879
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015880 // Can't shuffle using an illegal type.
15881 if (!TLI.isTypeLegal(WideVecVT))
15882 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015883
15884 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015885 DAG.getUNDEF(WideVecVT),
15886 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015887 // At this point all of the data is stored at the bottom of the
15888 // register. We now need to save it to mem.
15889
15890 // Find the largest store unit
15891 MVT StoreType = MVT::i8;
15892 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15893 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15894 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015895 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015896 StoreType = Tp;
15897 }
15898
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015899 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15900 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15901 (64 <= NumElems * ToSz))
15902 StoreType = MVT::f64;
15903
Nadav Rotem614061b2011-08-10 19:30:14 +000015904 // Bitcast the original vector into a vector of store-size units
15905 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015906 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015907 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15908 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15909 SmallVector<SDValue, 8> Chains;
15910 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15911 TLI.getPointerTy());
15912 SDValue Ptr = St->getBasePtr();
15913
15914 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015915 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015916 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15917 StoreType, ShuffWide,
15918 DAG.getIntPtrConstant(i));
15919 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15920 St->getPointerInfo(), St->isVolatile(),
15921 St->isNonTemporal(), St->getAlignment());
15922 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15923 Chains.push_back(Ch);
15924 }
15925
15926 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15927 Chains.size());
15928 }
15929
15930
Chris Lattner149a4e52008-02-22 02:09:43 +000015931 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15932 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015933 // A preferable solution to the general problem is to figure out the right
15934 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015935
15936 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015937 if (VT.getSizeInBits() != 64)
15938 return SDValue();
15939
Devang Patel578efa92009-06-05 21:57:13 +000015940 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000015941 bool NoImplicitFloatOps = F->getFnAttributes().
15942 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015943 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015944 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015945 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015946 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015947 isa<LoadSDNode>(St->getValue()) &&
15948 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15949 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015950 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015951 LoadSDNode *Ld = 0;
15952 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015953 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015954 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015955 // Must be a store of a load. We currently handle two cases: the load
15956 // is a direct child, and it's under an intervening TokenFactor. It is
15957 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015958 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015959 Ld = cast<LoadSDNode>(St->getChain());
15960 else if (St->getValue().hasOneUse() &&
15961 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015962 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015963 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015964 TokenFactorIndex = i;
15965 Ld = cast<LoadSDNode>(St->getValue());
15966 } else
15967 Ops.push_back(ChainVal->getOperand(i));
15968 }
15969 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015970
Evan Cheng536e6672009-03-12 05:59:15 +000015971 if (!Ld || !ISD::isNormalLoad(Ld))
15972 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015973
Evan Cheng536e6672009-03-12 05:59:15 +000015974 // If this is not the MMX case, i.e. we are just turning i64 load/store
15975 // into f64 load/store, avoid the transformation if there are multiple
15976 // uses of the loaded value.
15977 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15978 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015979
Evan Cheng536e6672009-03-12 05:59:15 +000015980 DebugLoc LdDL = Ld->getDebugLoc();
15981 DebugLoc StDL = N->getDebugLoc();
15982 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15983 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15984 // pair instead.
15985 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015986 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015987 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15988 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015989 Ld->isNonTemporal(), Ld->isInvariant(),
15990 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015991 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015992 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015993 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015994 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015995 Ops.size());
15996 }
Evan Cheng536e6672009-03-12 05:59:15 +000015997 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015998 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015999 St->isVolatile(), St->isNonTemporal(),
16000 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016001 }
Evan Cheng536e6672009-03-12 05:59:15 +000016002
16003 // Otherwise, lower to two pairs of 32-bit loads / stores.
16004 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016005 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16006 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016007
Owen Anderson825b72b2009-08-11 20:47:22 +000016008 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016009 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016010 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016011 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016012 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016013 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016014 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016015 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016016 MinAlign(Ld->getAlignment(), 4));
16017
16018 SDValue NewChain = LoLd.getValue(1);
16019 if (TokenFactorIndex != -1) {
16020 Ops.push_back(LoLd);
16021 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016022 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016023 Ops.size());
16024 }
16025
16026 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016027 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16028 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016029
16030 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016031 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016032 St->isVolatile(), St->isNonTemporal(),
16033 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016034 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016035 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016036 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016037 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016038 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016039 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016040 }
Dan Gohman475871a2008-07-27 21:46:04 +000016041 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016042}
16043
Duncan Sands17470be2011-09-22 20:15:48 +000016044/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16045/// and return the operands for the horizontal operation in LHS and RHS. A
16046/// horizontal operation performs the binary operation on successive elements
16047/// of its first operand, then on successive elements of its second operand,
16048/// returning the resulting values in a vector. For example, if
16049/// A = < float a0, float a1, float a2, float a3 >
16050/// and
16051/// B = < float b0, float b1, float b2, float b3 >
16052/// then the result of doing a horizontal operation on A and B is
16053/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16054/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16055/// A horizontal-op B, for some already available A and B, and if so then LHS is
16056/// set to A, RHS to B, and the routine returns 'true'.
16057/// Note that the binary operation should have the property that if one of the
16058/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016059static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016060 // Look for the following pattern: if
16061 // A = < float a0, float a1, float a2, float a3 >
16062 // B = < float b0, float b1, float b2, float b3 >
16063 // and
16064 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16065 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16066 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16067 // which is A horizontal-op B.
16068
16069 // At least one of the operands should be a vector shuffle.
16070 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16071 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16072 return false;
16073
16074 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016075
16076 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16077 "Unsupported vector type for horizontal add/sub");
16078
16079 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16080 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016081 unsigned NumElts = VT.getVectorNumElements();
16082 unsigned NumLanes = VT.getSizeInBits()/128;
16083 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016084 assert((NumLaneElts % 2 == 0) &&
16085 "Vector type should have an even number of elements in each lane");
16086 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016087
16088 // View LHS in the form
16089 // LHS = VECTOR_SHUFFLE A, B, LMask
16090 // If LHS is not a shuffle then pretend it is the shuffle
16091 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16092 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16093 // type VT.
16094 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016095 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016096 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16097 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16098 A = LHS.getOperand(0);
16099 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16100 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016101 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16102 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016103 } else {
16104 if (LHS.getOpcode() != ISD::UNDEF)
16105 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016106 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016107 LMask[i] = i;
16108 }
16109
16110 // Likewise, view RHS in the form
16111 // RHS = VECTOR_SHUFFLE C, D, RMask
16112 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016113 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016114 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16115 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16116 C = RHS.getOperand(0);
16117 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16118 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016119 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16120 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016121 } else {
16122 if (RHS.getOpcode() != ISD::UNDEF)
16123 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016124 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016125 RMask[i] = i;
16126 }
16127
16128 // Check that the shuffles are both shuffling the same vectors.
16129 if (!(A == C && B == D) && !(A == D && B == C))
16130 return false;
16131
16132 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16133 if (!A.getNode() && !B.getNode())
16134 return false;
16135
16136 // If A and B occur in reverse order in RHS, then "swap" them (which means
16137 // rewriting the mask).
16138 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016139 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016140
16141 // At this point LHS and RHS are equivalent to
16142 // LHS = VECTOR_SHUFFLE A, B, LMask
16143 // RHS = VECTOR_SHUFFLE A, B, RMask
16144 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016145 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016146 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016147
Craig Topperf8363302011-12-02 08:18:41 +000016148 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016149 if (LIdx < 0 || RIdx < 0 ||
16150 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16151 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016152 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016153
Craig Topperf8363302011-12-02 08:18:41 +000016154 // Check that successive elements are being operated on. If not, this is
16155 // not a horizontal operation.
16156 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16157 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016158 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016159 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016160 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016161 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016162 }
16163
16164 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16165 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16166 return true;
16167}
16168
16169/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16170static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16171 const X86Subtarget *Subtarget) {
16172 EVT VT = N->getValueType(0);
16173 SDValue LHS = N->getOperand(0);
16174 SDValue RHS = N->getOperand(1);
16175
16176 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016177 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016178 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016179 isHorizontalBinOp(LHS, RHS, true))
16180 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16181 return SDValue();
16182}
16183
16184/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16185static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16186 const X86Subtarget *Subtarget) {
16187 EVT VT = N->getValueType(0);
16188 SDValue LHS = N->getOperand(0);
16189 SDValue RHS = N->getOperand(1);
16190
16191 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016192 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016193 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016194 isHorizontalBinOp(LHS, RHS, false))
16195 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16196 return SDValue();
16197}
16198
Chris Lattner6cf73262008-01-25 06:14:17 +000016199/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16200/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016201static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016202 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16203 // F[X]OR(0.0, x) -> x
16204 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016205 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16206 if (C->getValueAPF().isPosZero())
16207 return N->getOperand(1);
16208 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16209 if (C->getValueAPF().isPosZero())
16210 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016211 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016212}
16213
Nadav Rotemd60cb112012-08-19 13:06:16 +000016214/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16215/// X86ISD::FMAX nodes.
16216static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16217 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16218
16219 // Only perform optimizations if UnsafeMath is used.
16220 if (!DAG.getTarget().Options.UnsafeFPMath)
16221 return SDValue();
16222
16223 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016224 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016225 unsigned NewOp = 0;
16226 switch (N->getOpcode()) {
16227 default: llvm_unreachable("unknown opcode");
16228 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16229 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16230 }
16231
16232 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16233 N->getOperand(0), N->getOperand(1));
16234}
16235
16236
Chris Lattneraf723b92008-01-25 05:46:26 +000016237/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016238static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016239 // FAND(0.0, x) -> 0.0
16240 // FAND(x, 0.0) -> 0.0
16241 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16242 if (C->getValueAPF().isPosZero())
16243 return N->getOperand(0);
16244 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16245 if (C->getValueAPF().isPosZero())
16246 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016247 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016248}
16249
Dan Gohmane5af2d32009-01-29 01:59:02 +000016250static SDValue PerformBTCombine(SDNode *N,
16251 SelectionDAG &DAG,
16252 TargetLowering::DAGCombinerInfo &DCI) {
16253 // BT ignores high bits in the bit index operand.
16254 SDValue Op1 = N->getOperand(1);
16255 if (Op1.hasOneUse()) {
16256 unsigned BitWidth = Op1.getValueSizeInBits();
16257 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16258 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016259 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16260 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016261 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016262 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16263 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16264 DCI.CommitTargetLoweringOpt(TLO);
16265 }
16266 return SDValue();
16267}
Chris Lattner83e6c992006-10-04 06:57:07 +000016268
Eli Friedman7a5e5552009-06-07 06:52:44 +000016269static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16270 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016271 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016272 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016273 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016274 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016275 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016276 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016277 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016278 }
16279 return SDValue();
16280}
16281
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016282static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16283 TargetLowering::DAGCombinerInfo &DCI,
16284 const X86Subtarget *Subtarget) {
16285 if (!DCI.isBeforeLegalizeOps())
16286 return SDValue();
16287
Craig Topper3ef43cf2012-04-24 06:36:35 +000016288 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016289 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016290
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016291 EVT VT = N->getValueType(0);
16292 SDValue Op = N->getOperand(0);
16293 EVT OpVT = Op.getValueType();
16294 DebugLoc dl = N->getDebugLoc();
16295
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016296 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16297 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016298
Craig Topper3ef43cf2012-04-24 06:36:35 +000016299 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016300 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016301
16302 // Optimize vectors in AVX mode
16303 // Sign extend v8i16 to v8i32 and
16304 // v4i32 to v4i64
16305 //
16306 // Divide input vector into two parts
16307 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16308 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16309 // concat the vectors to original VT
16310
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016311 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016312 SDValue Undef = DAG.getUNDEF(OpVT);
16313
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016314 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016315 for (unsigned i = 0; i != NumElems/2; ++i)
16316 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016317
Craig Toppercacafd42012-08-14 08:18:43 +000016318 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016319
16320 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016321 for (unsigned i = 0; i != NumElems/2; ++i)
16322 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016323
Craig Toppercacafd42012-08-14 08:18:43 +000016324 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016325
Craig Topper3ef43cf2012-04-24 06:36:35 +000016326 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016327 VT.getVectorNumElements()/2);
16328
Craig Topper3ef43cf2012-04-24 06:36:35 +000016329 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016330 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16331
16332 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16333 }
16334 return SDValue();
16335}
16336
Michael Liaof6c24ee2012-08-10 14:39:24 +000016337static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016338 const X86Subtarget* Subtarget) {
16339 DebugLoc dl = N->getDebugLoc();
16340 EVT VT = N->getValueType(0);
16341
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016342 // Let legalize expand this if it isn't a legal type yet.
16343 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16344 return SDValue();
16345
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016346 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016347 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16348 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016349 return SDValue();
16350
16351 SDValue A = N->getOperand(0);
16352 SDValue B = N->getOperand(1);
16353 SDValue C = N->getOperand(2);
16354
16355 bool NegA = (A.getOpcode() == ISD::FNEG);
16356 bool NegB = (B.getOpcode() == ISD::FNEG);
16357 bool NegC = (C.getOpcode() == ISD::FNEG);
16358
Michael Liaof6c24ee2012-08-10 14:39:24 +000016359 // Negative multiplication when NegA xor NegB
16360 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016361 if (NegA)
16362 A = A.getOperand(0);
16363 if (NegB)
16364 B = B.getOperand(0);
16365 if (NegC)
16366 C = C.getOperand(0);
16367
16368 unsigned Opcode;
16369 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016370 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016371 else
Craig Topperbf404372012-08-31 15:40:30 +000016372 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16373
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016374 return DAG.getNode(Opcode, dl, VT, A, B, C);
16375}
16376
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016377static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016378 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016379 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016380 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16381 // (and (i32 x86isd::setcc_carry), 1)
16382 // This eliminates the zext. This transformation is necessary because
16383 // ISD::SETCC is always legalized to i8.
16384 DebugLoc dl = N->getDebugLoc();
16385 SDValue N0 = N->getOperand(0);
16386 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016387 EVT OpVT = N0.getValueType();
16388
Evan Cheng2e489c42009-12-16 00:53:11 +000016389 if (N0.getOpcode() == ISD::AND &&
16390 N0.hasOneUse() &&
16391 N0.getOperand(0).hasOneUse()) {
16392 SDValue N00 = N0.getOperand(0);
16393 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16394 return SDValue();
16395 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16396 if (!C || C->getZExtValue() != 1)
16397 return SDValue();
16398 return DAG.getNode(ISD::AND, dl, VT,
16399 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16400 N00.getOperand(0), N00.getOperand(1)),
16401 DAG.getConstant(1, VT));
16402 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016403
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016404 // Optimize vectors in AVX mode:
16405 //
16406 // v8i16 -> v8i32
16407 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16408 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16409 // Concat upper and lower parts.
16410 //
16411 // v4i32 -> v4i64
16412 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16413 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16414 // Concat upper and lower parts.
16415 //
Craig Topperc16f8512012-04-25 06:39:39 +000016416 if (!DCI.isBeforeLegalizeOps())
16417 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016418
Craig Topperc16f8512012-04-25 06:39:39 +000016419 if (!Subtarget->hasAVX())
16420 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016421
Craig Topperc16f8512012-04-25 06:39:39 +000016422 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16423 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016424
Craig Topperc16f8512012-04-25 06:39:39 +000016425 if (Subtarget->hasAVX2())
16426 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016427
Craig Topperc16f8512012-04-25 06:39:39 +000016428 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16429 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16430 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016431
Craig Topperc16f8512012-04-25 06:39:39 +000016432 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16433 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016434
Craig Topperc16f8512012-04-25 06:39:39 +000016435 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16436 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16437
16438 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016439 }
16440
Evan Cheng2e489c42009-12-16 00:53:11 +000016441 return SDValue();
16442}
16443
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016444// Optimize x == -y --> x+y == 0
16445// x != -y --> x+y != 0
16446static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16447 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16448 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016449 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016450
16451 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16452 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16453 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16454 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16455 LHS.getValueType(), RHS, LHS.getOperand(1));
16456 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16457 addV, DAG.getConstant(0, addV.getValueType()), CC);
16458 }
16459 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16461 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16462 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16463 RHS.getValueType(), LHS, RHS.getOperand(1));
16464 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16465 addV, DAG.getConstant(0, addV.getValueType()), CC);
16466 }
16467 return SDValue();
16468}
16469
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016470// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016471static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16472 TargetLowering::DAGCombinerInfo &DCI,
16473 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016474 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016475 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16476 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016477
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016478 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16479 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16480 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016481 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016482 return DAG.getNode(ISD::AND, DL, MVT::i8,
16483 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000016484 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016485 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016486
Michael Liao2a33cec2012-08-10 19:58:13 +000016487 SDValue Flags;
16488
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016489 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16490 if (Flags.getNode()) {
16491 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16492 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16493 }
16494
Michael Liao2a33cec2012-08-10 19:58:13 +000016495 return SDValue();
16496}
16497
16498// Optimize branch condition evaluation.
16499//
16500static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16501 TargetLowering::DAGCombinerInfo &DCI,
16502 const X86Subtarget *Subtarget) {
16503 DebugLoc DL = N->getDebugLoc();
16504 SDValue Chain = N->getOperand(0);
16505 SDValue Dest = N->getOperand(1);
16506 SDValue EFLAGS = N->getOperand(3);
16507 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16508
16509 SDValue Flags;
16510
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016511 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16512 if (Flags.getNode()) {
16513 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16514 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16515 Flags);
16516 }
16517
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016518 return SDValue();
16519}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016520
Benjamin Kramer1396c402011-06-18 11:09:41 +000016521static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16522 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016523 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016524 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016525
16526 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016527 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016528 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016529 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016530 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16531 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16532 }
16533
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016534 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16535 // a 32-bit target where SSE doesn't support i64->FP operations.
16536 if (Op0.getOpcode() == ISD::LOAD) {
16537 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16538 EVT VT = Ld->getValueType(0);
16539 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16540 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16541 !XTLI->getSubtarget()->is64Bit() &&
16542 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016543 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16544 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016545 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16546 return FILDChain;
16547 }
16548 }
16549 return SDValue();
16550}
16551
Chris Lattner23a01992010-12-20 01:37:09 +000016552// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16553static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16554 X86TargetLowering::DAGCombinerInfo &DCI) {
16555 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16556 // the result is either zero or one (depending on the input carry bit).
16557 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16558 if (X86::isZeroNode(N->getOperand(0)) &&
16559 X86::isZeroNode(N->getOperand(1)) &&
16560 // We don't have a good way to replace an EFLAGS use, so only do this when
16561 // dead right now.
16562 SDValue(N, 1).use_empty()) {
16563 DebugLoc DL = N->getDebugLoc();
16564 EVT VT = N->getValueType(0);
16565 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16566 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16567 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16568 DAG.getConstant(X86::COND_B,MVT::i8),
16569 N->getOperand(2)),
16570 DAG.getConstant(1, VT));
16571 return DCI.CombineTo(N, Res1, CarryOut);
16572 }
16573
16574 return SDValue();
16575}
16576
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016577// fold (add Y, (sete X, 0)) -> adc 0, Y
16578// (add Y, (setne X, 0)) -> sbb -1, Y
16579// (sub (sete X, 0), Y) -> sbb 0, Y
16580// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016581static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016582 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016583
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016584 // Look through ZExts.
16585 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16586 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16587 return SDValue();
16588
16589 SDValue SetCC = Ext.getOperand(0);
16590 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16591 return SDValue();
16592
16593 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16594 if (CC != X86::COND_E && CC != X86::COND_NE)
16595 return SDValue();
16596
16597 SDValue Cmp = SetCC.getOperand(1);
16598 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016599 !X86::isZeroNode(Cmp.getOperand(1)) ||
16600 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016601 return SDValue();
16602
16603 SDValue CmpOp0 = Cmp.getOperand(0);
16604 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16605 DAG.getConstant(1, CmpOp0.getValueType()));
16606
16607 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16608 if (CC == X86::COND_NE)
16609 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16610 DL, OtherVal.getValueType(), OtherVal,
16611 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16612 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16613 DL, OtherVal.getValueType(), OtherVal,
16614 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16615}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016616
Craig Topper54f952a2011-11-19 09:02:40 +000016617/// PerformADDCombine - Do target-specific dag combines on integer adds.
16618static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16619 const X86Subtarget *Subtarget) {
16620 EVT VT = N->getValueType(0);
16621 SDValue Op0 = N->getOperand(0);
16622 SDValue Op1 = N->getOperand(1);
16623
16624 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016625 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016626 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016627 isHorizontalBinOp(Op0, Op1, true))
16628 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16629
16630 return OptimizeConditionalInDecrement(N, DAG);
16631}
16632
16633static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16634 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016635 SDValue Op0 = N->getOperand(0);
16636 SDValue Op1 = N->getOperand(1);
16637
16638 // X86 can't encode an immediate LHS of a sub. See if we can push the
16639 // negation into a preceding instruction.
16640 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016641 // If the RHS of the sub is a XOR with one use and a constant, invert the
16642 // immediate. Then add one to the LHS of the sub so we can turn
16643 // X-Y -> X+~Y+1, saving one register.
16644 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16645 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016646 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016647 EVT VT = Op0.getValueType();
16648 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16649 Op1.getOperand(0),
16650 DAG.getConstant(~XorC, VT));
16651 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016652 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016653 }
16654 }
16655
Craig Topper54f952a2011-11-19 09:02:40 +000016656 // Try to synthesize horizontal adds from adds of shuffles.
16657 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016658 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016659 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16660 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016661 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16662
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016663 return OptimizeConditionalInDecrement(N, DAG);
16664}
16665
Michael Liaod9d09602012-10-23 17:34:00 +000016666/// performVZEXTCombine - Performs build vector combines
16667static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
16668 TargetLowering::DAGCombinerInfo &DCI,
16669 const X86Subtarget *Subtarget) {
16670 // (vzext (bitcast (vzext (x)) -> (vzext x)
16671 SDValue In = N->getOperand(0);
16672 while (In.getOpcode() == ISD::BITCAST)
16673 In = In.getOperand(0);
16674
16675 if (In.getOpcode() != X86ISD::VZEXT)
16676 return SDValue();
16677
16678 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
16679}
16680
Dan Gohman475871a2008-07-27 21:46:04 +000016681SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016682 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016683 SelectionDAG &DAG = DCI.DAG;
16684 switch (N->getOpcode()) {
16685 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016686 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016687 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016688 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016689 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016690 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016691 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16692 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016693 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016694 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016695 case ISD::SHL:
16696 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016697 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016698 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016699 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016700 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016701 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016702 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016703 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000016704 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16705 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016706 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016707 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016708 case X86ISD::FMIN:
16709 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016710 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016711 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016712 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016713 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016714 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016715 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016716 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016717 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016718 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016719 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000016720 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016721 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016722 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016723 case X86ISD::UNPCKH:
16724 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016725 case X86ISD::MOVHLPS:
16726 case X86ISD::MOVLHPS:
16727 case X86ISD::PSHUFD:
16728 case X86ISD::PSHUFHW:
16729 case X86ISD::PSHUFLW:
16730 case X86ISD::MOVSS:
16731 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016732 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016733 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016734 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016735 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016736 }
16737
Dan Gohman475871a2008-07-27 21:46:04 +000016738 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016739}
16740
Evan Chenge5b51ac2010-04-17 06:13:15 +000016741/// isTypeDesirableForOp - Return true if the target has native support for
16742/// the specified value type and it is 'desirable' to use the type for the
16743/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16744/// instruction encodings are longer and some i16 instructions are slow.
16745bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16746 if (!isTypeLegal(VT))
16747 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016748 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016749 return true;
16750
16751 switch (Opc) {
16752 default:
16753 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016754 case ISD::LOAD:
16755 case ISD::SIGN_EXTEND:
16756 case ISD::ZERO_EXTEND:
16757 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016758 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016759 case ISD::SRL:
16760 case ISD::SUB:
16761 case ISD::ADD:
16762 case ISD::MUL:
16763 case ISD::AND:
16764 case ISD::OR:
16765 case ISD::XOR:
16766 return false;
16767 }
16768}
16769
16770/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016771/// beneficial for dag combiner to promote the specified node. If true, it
16772/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016773bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016774 EVT VT = Op.getValueType();
16775 if (VT != MVT::i16)
16776 return false;
16777
Evan Cheng4c26e932010-04-19 19:29:22 +000016778 bool Promote = false;
16779 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016780 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016781 default: break;
16782 case ISD::LOAD: {
16783 LoadSDNode *LD = cast<LoadSDNode>(Op);
16784 // If the non-extending load has a single use and it's not live out, then it
16785 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016786 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16787 Op.hasOneUse()*/) {
16788 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16789 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16790 // The only case where we'd want to promote LOAD (rather then it being
16791 // promoted as an operand is when it's only use is liveout.
16792 if (UI->getOpcode() != ISD::CopyToReg)
16793 return false;
16794 }
16795 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016796 Promote = true;
16797 break;
16798 }
16799 case ISD::SIGN_EXTEND:
16800 case ISD::ZERO_EXTEND:
16801 case ISD::ANY_EXTEND:
16802 Promote = true;
16803 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016804 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016805 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016806 SDValue N0 = Op.getOperand(0);
16807 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016808 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016809 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016810 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016811 break;
16812 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016813 case ISD::ADD:
16814 case ISD::MUL:
16815 case ISD::AND:
16816 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016817 case ISD::XOR:
16818 Commute = true;
16819 // fallthrough
16820 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016821 SDValue N0 = Op.getOperand(0);
16822 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016823 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016824 return false;
16825 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016826 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016827 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016828 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016829 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016830 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016831 }
16832 }
16833
16834 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016835 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016836}
16837
Evan Cheng60c07e12006-07-05 22:17:51 +000016838//===----------------------------------------------------------------------===//
16839// X86 Inline Assembly Support
16840//===----------------------------------------------------------------------===//
16841
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016842namespace {
16843 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016844 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016845 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016846
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016847 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016848 StringRef piece(*args[i]);
16849 if (!s.startswith(piece)) // Check if the piece matches.
16850 return false;
16851
16852 s = s.substr(piece.size());
16853 StringRef::size_type pos = s.find_first_not_of(" \t");
16854 if (pos == 0) // We matched a prefix.
16855 return false;
16856
16857 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016858 }
16859
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016860 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016861 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016862 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016863}
16864
Chris Lattnerb8105652009-07-20 17:51:36 +000016865bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16866 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016867
16868 std::string AsmStr = IA->getAsmString();
16869
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016870 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16871 if (!Ty || Ty->getBitWidth() % 16 != 0)
16872 return false;
16873
Chris Lattnerb8105652009-07-20 17:51:36 +000016874 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016875 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016876 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016877
16878 switch (AsmPieces.size()) {
16879 default: return false;
16880 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016881 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016882 // we will turn this bswap into something that will be lowered to logical
16883 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16884 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016885 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016886 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16887 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16888 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16889 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16890 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16891 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016892 // No need to check constraints, nothing other than the equivalent of
16893 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016894 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016895 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016896
Chris Lattnerb8105652009-07-20 17:51:36 +000016897 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016898 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016899 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016900 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16901 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016902 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016903 const std::string &ConstraintsStr = IA->getConstraintString();
16904 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016905 std::sort(AsmPieces.begin(), AsmPieces.end());
16906 if (AsmPieces.size() == 4 &&
16907 AsmPieces[0] == "~{cc}" &&
16908 AsmPieces[1] == "~{dirflag}" &&
16909 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016910 AsmPieces[3] == "~{fpsr}")
16911 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016912 }
16913 break;
16914 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016915 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016916 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016917 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16918 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16919 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016920 AsmPieces.clear();
16921 const std::string &ConstraintsStr = IA->getConstraintString();
16922 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16923 std::sort(AsmPieces.begin(), AsmPieces.end());
16924 if (AsmPieces.size() == 4 &&
16925 AsmPieces[0] == "~{cc}" &&
16926 AsmPieces[1] == "~{dirflag}" &&
16927 AsmPieces[2] == "~{flags}" &&
16928 AsmPieces[3] == "~{fpsr}")
16929 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016930 }
Evan Cheng55d42002011-01-08 01:24:27 +000016931
16932 if (CI->getType()->isIntegerTy(64)) {
16933 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16934 if (Constraints.size() >= 2 &&
16935 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16936 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16937 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016938 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16939 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16940 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016941 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016942 }
16943 }
16944 break;
16945 }
16946 return false;
16947}
16948
16949
16950
Chris Lattnerf4dff842006-07-11 02:54:03 +000016951/// getConstraintType - Given a constraint letter, return the type of
16952/// constraint it is for this target.
16953X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016954X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16955 if (Constraint.size() == 1) {
16956 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016957 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016958 case 'q':
16959 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016960 case 'f':
16961 case 't':
16962 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016963 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016964 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016965 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016966 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016967 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016968 case 'a':
16969 case 'b':
16970 case 'c':
16971 case 'd':
16972 case 'S':
16973 case 'D':
16974 case 'A':
16975 return C_Register;
16976 case 'I':
16977 case 'J':
16978 case 'K':
16979 case 'L':
16980 case 'M':
16981 case 'N':
16982 case 'G':
16983 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016984 case 'e':
16985 case 'Z':
16986 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016987 default:
16988 break;
16989 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016990 }
Chris Lattner4234f572007-03-25 02:14:49 +000016991 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016992}
16993
John Thompson44ab89e2010-10-29 17:29:13 +000016994/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016995/// This object must already have been set up with the operand type
16996/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016997TargetLowering::ConstraintWeight
16998 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016999 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017000 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017001 Value *CallOperandVal = info.CallOperandVal;
17002 // If we don't have a value, we can't do a match,
17003 // but allow it at the lowest weight.
17004 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017005 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017006 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017007 // Look at the constraint type.
17008 switch (*constraint) {
17009 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017010 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17011 case 'R':
17012 case 'q':
17013 case 'Q':
17014 case 'a':
17015 case 'b':
17016 case 'c':
17017 case 'd':
17018 case 'S':
17019 case 'D':
17020 case 'A':
17021 if (CallOperandVal->getType()->isIntegerTy())
17022 weight = CW_SpecificReg;
17023 break;
17024 case 'f':
17025 case 't':
17026 case 'u':
17027 if (type->isFloatingPointTy())
17028 weight = CW_SpecificReg;
17029 break;
17030 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000017031 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000017032 weight = CW_SpecificReg;
17033 break;
17034 case 'x':
17035 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017036 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000017037 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000017038 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017039 break;
17040 case 'I':
17041 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17042 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017043 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017044 }
17045 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017046 case 'J':
17047 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17048 if (C->getZExtValue() <= 63)
17049 weight = CW_Constant;
17050 }
17051 break;
17052 case 'K':
17053 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17054 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17055 weight = CW_Constant;
17056 }
17057 break;
17058 case 'L':
17059 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17060 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17061 weight = CW_Constant;
17062 }
17063 break;
17064 case 'M':
17065 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17066 if (C->getZExtValue() <= 3)
17067 weight = CW_Constant;
17068 }
17069 break;
17070 case 'N':
17071 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17072 if (C->getZExtValue() <= 0xff)
17073 weight = CW_Constant;
17074 }
17075 break;
17076 case 'G':
17077 case 'C':
17078 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17079 weight = CW_Constant;
17080 }
17081 break;
17082 case 'e':
17083 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17084 if ((C->getSExtValue() >= -0x80000000LL) &&
17085 (C->getSExtValue() <= 0x7fffffffLL))
17086 weight = CW_Constant;
17087 }
17088 break;
17089 case 'Z':
17090 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17091 if (C->getZExtValue() <= 0xffffffff)
17092 weight = CW_Constant;
17093 }
17094 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017095 }
17096 return weight;
17097}
17098
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017099/// LowerXConstraint - try to replace an X constraint, which matches anything,
17100/// with another that has more specific requirements based on the type of the
17101/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017102const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017103LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017104 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17105 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017106 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017107 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017108 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017109 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017110 return "x";
17111 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017112
Chris Lattner5e764232008-04-26 23:02:14 +000017113 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017114}
17115
Chris Lattner48884cd2007-08-25 00:47:38 +000017116/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17117/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017118void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017119 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017120 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017121 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017122 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017123
Eric Christopher100c8332011-06-02 23:16:42 +000017124 // Only support length 1 constraints for now.
17125 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017126
Eric Christopher100c8332011-06-02 23:16:42 +000017127 char ConstraintLetter = Constraint[0];
17128 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017129 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017130 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017131 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017132 if (C->getZExtValue() <= 31) {
17133 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017134 break;
17135 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017136 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017137 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017138 case 'J':
17139 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017140 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017141 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17142 break;
17143 }
17144 }
17145 return;
17146 case 'K':
17147 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017148 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000017149 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17150 break;
17151 }
17152 }
17153 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017154 case 'N':
17155 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017156 if (C->getZExtValue() <= 255) {
17157 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017158 break;
17159 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017160 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017161 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017162 case 'e': {
17163 // 32-bit signed value
17164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017165 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17166 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017167 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017168 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017169 break;
17170 }
17171 // FIXME gcc accepts some relocatable values here too, but only in certain
17172 // memory models; it's complicated.
17173 }
17174 return;
17175 }
17176 case 'Z': {
17177 // 32-bit unsigned value
17178 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017179 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17180 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017181 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17182 break;
17183 }
17184 }
17185 // FIXME gcc accepts some relocatable values here too, but only in certain
17186 // memory models; it's complicated.
17187 return;
17188 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017189 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017190 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017191 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017192 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017193 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017194 break;
17195 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017196
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017197 // In any sort of PIC mode addresses need to be computed at runtime by
17198 // adding in a register or some sort of table lookup. These can't
17199 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017200 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017201 return;
17202
Chris Lattnerdc43a882007-05-03 16:52:29 +000017203 // If we are in non-pic codegen mode, we allow the address of a global (with
17204 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017205 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017206 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017207
Chris Lattner49921962009-05-08 18:23:14 +000017208 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17209 while (1) {
17210 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17211 Offset += GA->getOffset();
17212 break;
17213 } else if (Op.getOpcode() == ISD::ADD) {
17214 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17215 Offset += C->getZExtValue();
17216 Op = Op.getOperand(0);
17217 continue;
17218 }
17219 } else if (Op.getOpcode() == ISD::SUB) {
17220 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17221 Offset += -C->getZExtValue();
17222 Op = Op.getOperand(0);
17223 continue;
17224 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017225 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017226
Chris Lattner49921962009-05-08 18:23:14 +000017227 // Otherwise, this isn't something we can handle, reject it.
17228 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017229 }
Eric Christopherfd179292009-08-27 18:07:15 +000017230
Dan Gohman46510a72010-04-15 01:51:59 +000017231 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017232 // If we require an extra load to get this address, as in PIC mode, we
17233 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017234 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17235 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017236 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017237
Devang Patel0d881da2010-07-06 22:08:15 +000017238 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17239 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017240 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017241 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017242 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017243
Gabor Greifba36cb52008-08-28 21:40:38 +000017244 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017245 Ops.push_back(Result);
17246 return;
17247 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017248 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017249}
17250
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017251std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017252X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017253 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017254 // First, see if this is a constraint that directly corresponds to an LLVM
17255 // register class.
17256 if (Constraint.size() == 1) {
17257 // GCC Constraint Letters
17258 switch (Constraint[0]) {
17259 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017260 // TODO: Slight differences here in allocation order and leaving
17261 // RIP in the class. Do they matter any more here than they do
17262 // in the normal allocation?
17263 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17264 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017265 if (VT == MVT::i32 || VT == MVT::f32)
17266 return std::make_pair(0U, &X86::GR32RegClass);
17267 if (VT == MVT::i16)
17268 return std::make_pair(0U, &X86::GR16RegClass);
17269 if (VT == MVT::i8 || VT == MVT::i1)
17270 return std::make_pair(0U, &X86::GR8RegClass);
17271 if (VT == MVT::i64 || VT == MVT::f64)
17272 return std::make_pair(0U, &X86::GR64RegClass);
17273 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017274 }
17275 // 32-bit fallthrough
17276 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017277 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017278 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17279 if (VT == MVT::i16)
17280 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17281 if (VT == MVT::i8 || VT == MVT::i1)
17282 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17283 if (VT == MVT::i64)
17284 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017285 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017286 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017287 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017288 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017289 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017290 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017291 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017292 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017293 return std::make_pair(0U, &X86::GR32RegClass);
17294 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017295 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017296 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017297 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017298 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017299 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017300 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017301 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17302 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017303 case 'f': // FP Stack registers.
17304 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17305 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017306 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017307 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017308 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017309 return std::make_pair(0U, &X86::RFP64RegClass);
17310 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017311 case 'y': // MMX_REGS if MMX allowed.
17312 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017313 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017314 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017315 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017316 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017317 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017318 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017319
Owen Anderson825b72b2009-08-11 20:47:22 +000017320 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017321 default: break;
17322 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017323 case MVT::f32:
17324 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017325 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017326 case MVT::f64:
17327 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017328 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017329 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017330 case MVT::v16i8:
17331 case MVT::v8i16:
17332 case MVT::v4i32:
17333 case MVT::v2i64:
17334 case MVT::v4f32:
17335 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017336 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017337 // AVX types.
17338 case MVT::v32i8:
17339 case MVT::v16i16:
17340 case MVT::v8i32:
17341 case MVT::v4i64:
17342 case MVT::v8f32:
17343 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017344 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017345 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017346 break;
17347 }
17348 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017349
Chris Lattnerf76d1802006-07-31 23:26:50 +000017350 // Use the default implementation in TargetLowering to convert the register
17351 // constraint into a member of a register class.
17352 std::pair<unsigned, const TargetRegisterClass*> Res;
17353 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017354
17355 // Not found as a standard register?
17356 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017357 // Map st(0) -> st(7) -> ST0
17358 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17359 tolower(Constraint[1]) == 's' &&
17360 tolower(Constraint[2]) == 't' &&
17361 Constraint[3] == '(' &&
17362 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17363 Constraint[5] == ')' &&
17364 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017365
Chris Lattner56d77c72009-09-13 22:41:48 +000017366 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017367 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017368 return Res;
17369 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017370
Chris Lattner56d77c72009-09-13 22:41:48 +000017371 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017372 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017373 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017374 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017375 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017376 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017377
17378 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017379 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017380 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017381 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017382 return Res;
17383 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017384
Dale Johannesen330169f2008-11-13 21:52:36 +000017385 // 'A' means EAX + EDX.
17386 if (Constraint == "A") {
17387 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017388 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017389 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017390 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017391 return Res;
17392 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017393
Chris Lattnerf76d1802006-07-31 23:26:50 +000017394 // Otherwise, check to see if this is a register class of the wrong value
17395 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17396 // turn into {ax},{dx}.
17397 if (Res.second->hasType(VT))
17398 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017399
Chris Lattnerf76d1802006-07-31 23:26:50 +000017400 // All of the single-register GCC register classes map their values onto
17401 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17402 // really want an 8-bit or 32-bit register, map to the appropriate register
17403 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017404 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017405 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017406 unsigned DestReg = 0;
17407 switch (Res.first) {
17408 default: break;
17409 case X86::AX: DestReg = X86::AL; break;
17410 case X86::DX: DestReg = X86::DL; break;
17411 case X86::CX: DestReg = X86::CL; break;
17412 case X86::BX: DestReg = X86::BL; break;
17413 }
17414 if (DestReg) {
17415 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017416 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017417 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017418 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017419 unsigned DestReg = 0;
17420 switch (Res.first) {
17421 default: break;
17422 case X86::AX: DestReg = X86::EAX; break;
17423 case X86::DX: DestReg = X86::EDX; break;
17424 case X86::CX: DestReg = X86::ECX; break;
17425 case X86::BX: DestReg = X86::EBX; break;
17426 case X86::SI: DestReg = X86::ESI; break;
17427 case X86::DI: DestReg = X86::EDI; break;
17428 case X86::BP: DestReg = X86::EBP; break;
17429 case X86::SP: DestReg = X86::ESP; break;
17430 }
17431 if (DestReg) {
17432 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017433 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017434 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017435 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017436 unsigned DestReg = 0;
17437 switch (Res.first) {
17438 default: break;
17439 case X86::AX: DestReg = X86::RAX; break;
17440 case X86::DX: DestReg = X86::RDX; break;
17441 case X86::CX: DestReg = X86::RCX; break;
17442 case X86::BX: DestReg = X86::RBX; break;
17443 case X86::SI: DestReg = X86::RSI; break;
17444 case X86::DI: DestReg = X86::RDI; break;
17445 case X86::BP: DestReg = X86::RBP; break;
17446 case X86::SP: DestReg = X86::RSP; break;
17447 }
17448 if (DestReg) {
17449 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017450 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017451 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017452 }
Craig Topperc9099502012-04-20 06:31:50 +000017453 } else if (Res.second == &X86::FR32RegClass ||
17454 Res.second == &X86::FR64RegClass ||
17455 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017456 // Handle references to XMM physical registers that got mapped into the
17457 // wrong class. This can happen with constraints like {xmm0} where the
17458 // target independent register mapper will just pick the first match it can
17459 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017460
17461 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017462 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017463 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017464 Res.second = &X86::FR64RegClass;
17465 else if (X86::VR128RegClass.hasType(VT))
17466 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017467 else if (X86::VR256RegClass.hasType(VT))
17468 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017469 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017470
Chris Lattnerf76d1802006-07-31 23:26:50 +000017471 return Res;
17472}