blob: 863379ec5af42b84c664b2092a67b4cca6d553f6 [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();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000161
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000162 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000163 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000164
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000166 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
168 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000169 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000170 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
171 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000172
Eric Christopherde5e1012011-03-11 01:05:58 +0000173 // For 64-bit since we have so many registers use the ILP scheduler, for
174 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000175 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000176 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000177 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000178 else if (Subtarget->is64Bit())
179 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000180 else
181 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000182 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000183
Preston Gurd2e2efd92012-09-04 18:22:17 +0000184 // Bypass i32 with i8 on Atom when compiling with O2
185 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
Preston Gurd8d662b52012-10-04 21:33:40 +0000186 addBypassSlowDiv(32, 8);
Preston Gurd2e2efd92012-09-04 18:22:17 +0000187
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000188 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000189 // Setup Windows compiler runtime calls.
190 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000191 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000192 setLibcallName(RTLIB::SREM_I64, "_allrem");
193 setLibcallName(RTLIB::UREM_I64, "_aullrem");
194 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000196 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000197 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
198 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000200
201 // The _ftol2 runtime function has an unusual calling conv, which
202 // is modeled by a special pseudo-instruction.
203 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
204 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
206 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000207 }
208
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000209 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000210 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 setUseUnderscoreSetJmp(false);
212 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000213 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 // MS runtime is weird: it exports _setjmp, but longjmp!
215 setUseUnderscoreSetJmp(true);
216 setUseUnderscoreLongJmp(false);
217 } else {
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(true);
220 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000223 addRegisterClass(MVT::i8, &X86::GR8RegClass);
224 addRegisterClass(MVT::i16, &X86::GR16RegClass);
225 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000227 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000230
Scott Michelfdc40a02009-02-17 22:15:04 +0000231 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000233 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000235 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
237 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000238
239 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
243 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000246
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
248 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
250 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000252
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000256 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000257 // We have an algorithm for SSE2->double, and we turn this into a
258 // 64-bit FILD followed by conditional FADD for other targets.
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 // We have an algorithm for SSE2, and we turn this into a 64-bit
261 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000262 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
265 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
266 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
268 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000269
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000270 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000271 // SSE has no i16 to fp conversion, only i32
272 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000283 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284
Dale Johannesen73328d12007-09-19 23:55:34 +0000285 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
286 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
288 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000289
Evan Cheng02568ff2006-01-30 22:13:22 +0000290 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
291 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
293 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000294
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000295 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000297 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000299 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000302 }
303
304 // Handle FP_TO_UINT by promoting the destination to a larger signed
305 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
307 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000309
Evan Cheng25ab6902006-09-08 06:48:29 +0000310 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000313 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000314 // Since AVX is a superset of SSE3, only check for SSE here.
315 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 // Expand FP_TO_UINT into a select.
317 // FIXME: We would like to use a Custom expander here eventually to do
318 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000321 // With SSE3 we can use fisttpll to convert to a signed i64; without
322 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000325
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000326 if (isTargetFTOL()) {
327 // Use the _ftol2 runtime function, which has a pseudo-instruction
328 // to handle its weird calling convention.
329 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
330 }
331
Chris Lattner399610a2006-12-05 18:22:22 +0000332 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000333 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
335 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000336 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000338 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000339 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000340 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000341 }
Chris Lattner21f66852005-12-23 05:15:23 +0000342
Dan Gohmanb00ee212008-02-18 19:34:53 +0000343 // Scalar integer divide and remainder are lowered to use operations that
344 // produce two results, to match the available instructions. This exposes
345 // the two-result form to trivial CSE, which is able to combine x/y and x%y
346 // into a single instruction.
347 //
348 // Scalar integer multiply-high is also lowered to use two-result
349 // operations, to match the available instructions. However, plain multiply
350 // (low) operations are left as Legal, as there are single-result
351 // instructions for this in x86. Using the two-result multiply instructions
352 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000353 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000354 MVT VT = IntVTs[i];
355 setOperationAction(ISD::MULHS, VT, Expand);
356 setOperationAction(ISD::MULHU, VT, Expand);
357 setOperationAction(ISD::SDIV, VT, Expand);
358 setOperationAction(ISD::UDIV, VT, Expand);
359 setOperationAction(ISD::SREM, VT, Expand);
360 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000361
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000362 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000363 setOperationAction(ISD::ADDC, VT, Custom);
364 setOperationAction(ISD::ADDE, VT, Custom);
365 setOperationAction(ISD::SUBC, VT, Custom);
366 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000368
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
370 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
371 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
372 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
378 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
379 setOperationAction(ISD::FREM , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f64 , Expand);
381 setOperationAction(ISD::FREM , MVT::f80 , Expand);
382 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000383
Chandler Carruth77821022011-12-24 12:12:34 +0000384 // Promote the i8 variants and force them on up to i32 which has a shorter
385 // encoding.
386 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
387 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
388 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
389 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000390 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000391 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
393 if (Subtarget->is64Bit())
394 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000395 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000396 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
400 }
Craig Topper37f21672011-10-11 06:44:02 +0000401
402 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000403 // When promoting the i8 variants, force them to i32 for a shorter
404 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000405 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000406 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
408 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
411 if (Subtarget->is64Bit())
412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000413 } else {
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000417 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
420 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000422 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
423 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
425
Benjamin Kramer1292c222010-12-04 20:32:23 +0000426 if (Subtarget->hasPOPCNT()) {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
428 } else {
429 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
432 if (Subtarget->is64Bit())
433 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
434 }
435
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
437 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000438
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000439 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000441 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000442 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000443 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
448 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000449 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000454 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000456 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000457 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000459 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
460 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000461 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000462 // other SjLj exception interfaces are implemented and please don't build
463 // your own exception handling based on them.
464 // LLVM/Clang supports zero-cost DWARF exception handling.
465 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
466 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000467
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000468 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
472 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000473 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
475 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000476 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000477 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
479 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
480 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
481 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000482 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000483 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000484 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
486 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
487 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000488 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
490 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
491 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000492 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493
Craig Topper1accb7e2012-01-10 06:54:16 +0000494 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000496
Eric Christopher9a9d2752010-07-22 02:48:34 +0000497 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000498 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000499
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000500 // On X86 and X86-64, atomic operations are lowered to locked instructions.
501 // Locked instructions, in turn, have implicit fence semantics (all memory
502 // operations are flushed before issuing the locked instruction, and they
503 // are not buffered), so we can fold away the common pattern of
504 // fence-atomic-fence.
505 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000506
Mon P Wang63307c32008-05-05 19:05:59 +0000507 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000508 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000509 MVT VT = IntVTs[i];
510 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000512 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000513 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000514
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000515 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000516 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000524 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
527 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000528 }
529
Eli Friedman43f51ae2011-08-26 21:21:21 +0000530 if (Subtarget->hasCmpxchg16b()) {
531 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
532 }
533
Evan Cheng3c992d22006-03-07 02:02:57 +0000534 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000535 if (!Subtarget->isTargetDarwin() &&
536 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000537 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000539 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
542 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
543 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
544 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000545 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000546 setExceptionPointerRegister(X86::RAX);
547 setExceptionSelectorRegister(X86::RDX);
548 } else {
549 setExceptionPointerRegister(X86::EAX);
550 setExceptionSelectorRegister(X86::EDX);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
553 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000554
Duncan Sands4a544a72011-09-06 13:37:06 +0000555 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
556 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000557
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000559 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000560
Nate Begemanacc398c2006-01-25 18:21:52 +0000561 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::VASTART , MVT::Other, Custom);
563 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000564 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VAARG , MVT::Other, Custom);
566 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000567 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VAARG , MVT::Other, Expand);
569 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000570 }
Evan Chengae642192007-03-02 23:16:35 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
573 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000574
575 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
576 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
577 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000578 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000579 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
580 MVT::i64 : MVT::i32, Custom);
581 else
582 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
583 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000584
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000585 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000587 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000588 addRegisterClass(MVT::f32, &X86::FR32RegClass);
589 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590
Evan Cheng223547a2006-01-31 22:28:30 +0000591 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FABS , MVT::f64, Custom);
593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f64, Custom);
597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000598
Evan Cheng68c47cb2007-01-05 07:55:56 +0000599 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
601 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000602
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000603 // Lower this to FGETSIGNx86 plus an AND.
604 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
605 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
606
Evan Chengd25e9e82006-02-02 00:28:23 +0000607 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612
Chris Lattnera54aa942006-01-29 06:26:08 +0000613 // Expand FP immediates into loads from the stack, except for the special
614 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 addLegalFPImmediate(APFloat(+0.0)); // xorpd
616 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000617 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 // Use SSE for f32, x87 for f64.
619 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000620 addRegisterClass(MVT::f32, &X86::FR32RegClass);
621 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625
626 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
631 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634
635 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f32, Expand);
637 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
Nate Begemane1795842008-02-14 08:57:00 +0000639 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000640 addLegalFPImmediate(APFloat(+0.0f)); // xorps
641 addLegalFPImmediate(APFloat(+0.0)); // FLD0
642 addLegalFPImmediate(APFloat(+1.0)); // FLD1
643 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
644 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
645
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000646 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
648 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000650 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000653 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
654 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
657 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000662 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000664 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000666 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000667 addLegalFPImmediate(APFloat(+0.0)); // FLD0
668 addLegalFPImmediate(APFloat(+1.0)); // FLD1
669 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
670 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000671 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
672 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
673 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
674 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Cameron Zwarich33390842011-07-08 21:39:21 +0000677 // We don't support FMA.
678 setOperationAction(ISD::FMA, MVT::f64, Expand);
679 setOperationAction(ISD::FMA, MVT::f32, Expand);
680
Dale Johannesen59a58732007-08-05 18:49:15 +0000681 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000682 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000683 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
685 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000687 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000688 addLegalFPImmediate(TmpFlt); // FLD0
689 TmpFlt.changeSign();
690 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000691
692 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000693 APFloat TmpFlt2(+1.0);
694 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
695 &ignored);
696 addLegalFPImmediate(TmpFlt2); // FLD1
697 TmpFlt2.changeSign();
698 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
699 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000700
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000701 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
703 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000704 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000705
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000706 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
707 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
708 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
709 setOperationAction(ISD::FRINT, MVT::f80, Expand);
710 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000711 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000712 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000713
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000714 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
716 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000718
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FLOG, MVT::f80, Expand);
720 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
722 setOperationAction(ISD::FEXP, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000724
Mon P Wangf007a8b2008-11-06 05:31:54 +0000725 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000726 // (for widening) or expand (for scalarization). Then we will selectively
727 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000728 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
729 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
730 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
731 setOperationAction(ISD::ADD , VT, Expand);
732 setOperationAction(ISD::SUB , VT, Expand);
733 setOperationAction(ISD::FADD, VT, Expand);
734 setOperationAction(ISD::FNEG, VT, Expand);
735 setOperationAction(ISD::FSUB, VT, Expand);
736 setOperationAction(ISD::MUL , VT, Expand);
737 setOperationAction(ISD::FMUL, VT, Expand);
738 setOperationAction(ISD::SDIV, VT, Expand);
739 setOperationAction(ISD::UDIV, VT, Expand);
740 setOperationAction(ISD::FDIV, VT, Expand);
741 setOperationAction(ISD::SREM, VT, Expand);
742 setOperationAction(ISD::UREM, VT, Expand);
743 setOperationAction(ISD::LOAD, VT, Expand);
744 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
746 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
747 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
748 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
749 setOperationAction(ISD::FABS, VT, Expand);
750 setOperationAction(ISD::FSIN, VT, Expand);
751 setOperationAction(ISD::FCOS, VT, Expand);
752 setOperationAction(ISD::FREM, VT, Expand);
753 setOperationAction(ISD::FMA, VT, Expand);
754 setOperationAction(ISD::FPOWI, VT, Expand);
755 setOperationAction(ISD::FSQRT, VT, Expand);
756 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
757 setOperationAction(ISD::FFLOOR, VT, Expand);
758 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
759 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
760 setOperationAction(ISD::SDIVREM, VT, Expand);
761 setOperationAction(ISD::UDIVREM, VT, Expand);
762 setOperationAction(ISD::FPOW, VT, Expand);
763 setOperationAction(ISD::CTPOP, VT, Expand);
764 setOperationAction(ISD::CTTZ, VT, Expand);
765 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
766 setOperationAction(ISD::CTLZ, VT, Expand);
767 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
768 setOperationAction(ISD::SHL, VT, Expand);
769 setOperationAction(ISD::SRA, VT, Expand);
770 setOperationAction(ISD::SRL, VT, Expand);
771 setOperationAction(ISD::ROTL, VT, Expand);
772 setOperationAction(ISD::ROTR, VT, Expand);
773 setOperationAction(ISD::BSWAP, VT, Expand);
774 setOperationAction(ISD::SETCC, VT, Expand);
775 setOperationAction(ISD::FLOG, VT, Expand);
776 setOperationAction(ISD::FLOG2, VT, Expand);
777 setOperationAction(ISD::FLOG10, VT, Expand);
778 setOperationAction(ISD::FEXP, VT, Expand);
779 setOperationAction(ISD::FEXP2, VT, Expand);
780 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
781 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
782 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
783 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
784 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
785 setOperationAction(ISD::TRUNCATE, VT, Expand);
786 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
787 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
788 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
789 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000790 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
791 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000792 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000793 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000794 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
795 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
796 setLoadExtAction(ISD::EXTLOAD, 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)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002354 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2355 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002356 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2357 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002358 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002359 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002360
Evan Cheng32fe1032006-05-25 00:59:30 +00002361 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002362 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002363 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002364
Chris Lattner88e1fd52009-07-09 04:24:46 +00002365 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002366 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2367 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002369 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2370 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002371 } else {
2372 // If we are tail calling and generating PIC/GOT style code load the
2373 // address of the callee into ECX. The value in ecx is used as target of
2374 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2375 // for tail calls on PIC/GOT architectures. Normally we would just put the
2376 // address of GOT into ebx and then call target@PLT. But for tail calls
2377 // ebx would be restored (since ebx is callee saved) before jumping to the
2378 // target@PLT.
2379
2380 // Note: The actual moving to ECX is done further down.
2381 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2382 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2383 !G->getGlobal()->hasProtectedVisibility())
2384 Callee = LowerGlobalAddress(Callee, DAG);
2385 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002386 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002387 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002388 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002389
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002390 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002391 // From AMD64 ABI document:
2392 // For calls that may call functions that use varargs or stdargs
2393 // (prototype-less calls or calls to functions containing ellipsis (...) in
2394 // the declaration) %al is used as hidden argument to specify the number
2395 // of SSE registers used. The contents of %al do not need to match exactly
2396 // the number of registers, but must be an ubound on the number of SSE
2397 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002398
Gordon Henriksen86737662008-01-05 16:56:59 +00002399 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002400 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002401 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2402 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2403 };
2404 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002405 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002406 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002407
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002408 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2409 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002410 }
2411
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002412 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002413 if (isTailCall) {
2414 // Force all the incoming stack arguments to be loaded from the stack
2415 // before any new outgoing arguments are stored to the stack, because the
2416 // outgoing stack slots may alias the incoming argument stack slots, and
2417 // the alias isn't otherwise explicit. This is slightly more conservative
2418 // than necessary, because it means that each store effectively depends
2419 // on every argument instead of just those arguments it would clobber.
2420 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2421
Dan Gohman475871a2008-07-27 21:46:04 +00002422 SmallVector<SDValue, 8> MemOpChains2;
2423 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002424 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002425 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002426 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2427 CCValAssign &VA = ArgLocs[i];
2428 if (VA.isRegLoc())
2429 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002430 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002431 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002432 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002433 // Create frame index.
2434 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002435 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002436 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002437 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002438
Duncan Sands276dcbd2008-03-21 09:14:45 +00002439 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002440 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002441 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002442 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002443 StackPtr = DAG.getCopyFromReg(Chain, dl,
2444 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002445 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002446 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002447
Dan Gohman98ca4f22009-08-05 01:29:28 +00002448 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2449 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002450 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002451 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002452 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002453 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002454 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002455 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002456 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002457 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002458 }
2459 }
2460
2461 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002462 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002463 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002464
2465 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002466 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2467 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002468 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002469 }
2470
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002471 // Build a sequence of copy-to-reg nodes chained together with token chain
2472 // and flag operands which copy the outgoing args into registers.
2473 SDValue InFlag;
2474 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2475 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2476 RegsToPass[i].second, InFlag);
2477 InFlag = Chain.getValue(1);
2478 }
2479
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002480 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2481 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2482 // In the 64-bit large code model, we have to make all calls
2483 // through a register, since the call instruction's 32-bit
2484 // pc-relative offset may not be large enough to hold the whole
2485 // address.
2486 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002487 // If the callee is a GlobalAddress node (quite common, every direct call
2488 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2489 // it.
2490
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002491 // We should use extra load for direct calls to dllimported functions in
2492 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002493 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002494 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002495 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002496 bool ExtraLoad = false;
2497 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002498
Chris Lattner48a7d022009-07-09 05:02:21 +00002499 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2500 // external symbols most go through the PLT in PIC mode. If the symbol
2501 // has hidden or protected visibility, or if it is static or local, then
2502 // we don't need to use the PLT - we can directly call it.
2503 if (Subtarget->isTargetELF() &&
2504 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002505 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002506 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002507 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002508 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002509 (!Subtarget->getTargetTriple().isMacOSX() ||
2510 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002511 // PC-relative references to external symbols should go through $stub,
2512 // unless we're building with the leopard linker or later, which
2513 // automatically synthesizes these stubs.
2514 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002515 } else if (Subtarget->isPICStyleRIPRel() &&
2516 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002517 cast<Function>(GV)->getFnAttributes().
2518 hasAttribute(Attributes::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002519 // If the function is marked as non-lazy, generate an indirect call
2520 // which loads from the GOT directly. This avoids runtime overhead
2521 // at the cost of eager binding (and one extra byte of encoding).
2522 OpFlags = X86II::MO_GOTPCREL;
2523 WrapperKind = X86ISD::WrapperRIP;
2524 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002525 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002526
Devang Patel0d881da2010-07-06 22:08:15 +00002527 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002528 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002529
2530 // Add a wrapper if needed.
2531 if (WrapperKind != ISD::DELETED_NODE)
2532 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2533 // Add extra indirection if needed.
2534 if (ExtraLoad)
2535 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2536 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002537 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002538 }
Bill Wendling056292f2008-09-16 21:48:12 +00002539 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002540 unsigned char OpFlags = 0;
2541
Evan Cheng1bf891a2010-12-01 22:59:46 +00002542 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2543 // external symbols should go through the PLT.
2544 if (Subtarget->isTargetELF() &&
2545 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2546 OpFlags = X86II::MO_PLT;
2547 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002548 (!Subtarget->getTargetTriple().isMacOSX() ||
2549 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002550 // PC-relative references to external symbols should go through $stub,
2551 // unless we're building with the leopard linker or later, which
2552 // automatically synthesizes these stubs.
2553 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002554 }
Eric Christopherfd179292009-08-27 18:07:15 +00002555
Chris Lattner48a7d022009-07-09 05:02:21 +00002556 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2557 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002558 }
2559
Chris Lattnerd96d0722007-02-25 06:40:16 +00002560 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002561 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002562 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002563
Evan Chengf22f9b32010-02-06 03:28:46 +00002564 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002565 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2566 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002567 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002568 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002569
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002570 Ops.push_back(Chain);
2571 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002572
Dan Gohman98ca4f22009-08-05 01:29:28 +00002573 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002574 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002575
Gordon Henriksen86737662008-01-05 16:56:59 +00002576 // Add argument registers to the end of the list so that they are known live
2577 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002578 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2579 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2580 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002581
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002582 // Add a register mask operand representing the call-preserved registers.
2583 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2584 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2585 assert(Mask && "Missing call preserved mask for calling convention");
2586 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002587
Gabor Greifba36cb52008-08-28 21:40:38 +00002588 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002589 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002590
Dan Gohman98ca4f22009-08-05 01:29:28 +00002591 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002592 // We used to do:
2593 //// If this is the first return lowered for this function, add the regs
2594 //// to the liveout set for the function.
2595 // This isn't right, although it's probably harmless on x86; liveouts
2596 // should be computed from returns not tail calls. Consider a void
2597 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002598 return DAG.getNode(X86ISD::TC_RETURN, dl,
2599 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002600 }
2601
Dale Johannesenace16102009-02-03 19:33:06 +00002602 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002603 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002604
Chris Lattner2d297092006-05-23 18:50:38 +00002605 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002606 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002607 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2608 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002609 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002610 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002611 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002612 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002613 // pops the hidden struct pointer, so we have to push it back.
2614 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002615 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002616 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002617 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002618 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002619
Gordon Henriksenae636f82008-01-03 16:47:34 +00002620 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002621 if (!IsSibcall) {
2622 Chain = DAG.getCALLSEQ_END(Chain,
2623 DAG.getIntPtrConstant(NumBytes, true),
2624 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2625 true),
2626 InFlag);
2627 InFlag = Chain.getValue(1);
2628 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002629
Chris Lattner3085e152007-02-25 08:59:22 +00002630 // Handle result values, copying them out of physregs into vregs that we
2631 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002632 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2633 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002634}
2635
Evan Cheng25ab6902006-09-08 06:48:29 +00002636
2637//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002638// Fast Calling Convention (tail call) implementation
2639//===----------------------------------------------------------------------===//
2640
2641// Like std call, callee cleans arguments, convention except that ECX is
2642// reserved for storing the tail called function address. Only 2 registers are
2643// free for argument passing (inreg). Tail call optimization is performed
2644// provided:
2645// * tailcallopt is enabled
2646// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002647// On X86_64 architecture with GOT-style position independent code only local
2648// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002649// To keep the stack aligned according to platform abi the function
2650// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2651// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002652// If a tail called function callee has more arguments than the caller the
2653// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002654// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002655// original REtADDR, but before the saved framepointer or the spilled registers
2656// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2657// stack layout:
2658// arg1
2659// arg2
2660// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002661// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002662// move area ]
2663// (possible EBP)
2664// ESI
2665// EDI
2666// local1 ..
2667
2668/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2669/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002670unsigned
2671X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2672 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002673 MachineFunction &MF = DAG.getMachineFunction();
2674 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002675 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002676 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002677 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002678 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002679 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002680 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2681 // Number smaller than 12 so just add the difference.
2682 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2683 } else {
2684 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002685 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002686 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002687 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002688 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002689}
2690
Evan Cheng5f941932010-02-05 02:21:12 +00002691/// MatchingStackOffset - Return true if the given stack call argument is
2692/// already available in the same position (relatively) of the caller's
2693/// incoming argument stack.
2694static
2695bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2696 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2697 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002698 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2699 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002700 if (Arg.getOpcode() == ISD::CopyFromReg) {
2701 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002702 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002703 return false;
2704 MachineInstr *Def = MRI->getVRegDef(VR);
2705 if (!Def)
2706 return false;
2707 if (!Flags.isByVal()) {
2708 if (!TII->isLoadFromStackSlot(Def, FI))
2709 return false;
2710 } else {
2711 unsigned Opcode = Def->getOpcode();
2712 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2713 Def->getOperand(1).isFI()) {
2714 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002715 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002716 } else
2717 return false;
2718 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002719 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2720 if (Flags.isByVal())
2721 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002722 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002723 // define @foo(%struct.X* %A) {
2724 // tail call @bar(%struct.X* byval %A)
2725 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002726 return false;
2727 SDValue Ptr = Ld->getBasePtr();
2728 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2729 if (!FINode)
2730 return false;
2731 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002732 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002733 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002734 FI = FINode->getIndex();
2735 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002736 } else
2737 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002738
Evan Cheng4cae1332010-03-05 08:38:04 +00002739 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002740 if (!MFI->isFixedObjectIndex(FI))
2741 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002742 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002743}
2744
Dan Gohman98ca4f22009-08-05 01:29:28 +00002745/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2746/// for tail call optimization. Targets which want to do tail call
2747/// optimization should implement this function.
2748bool
2749X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002750 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002751 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002752 bool isCalleeStructRet,
2753 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002754 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002755 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002756 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002757 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002758 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002759 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002760 CalleeCC != CallingConv::C)
2761 return false;
2762
Evan Cheng7096ae42010-01-29 06:45:59 +00002763 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002764 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002765 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002766
2767 // If the function return type is x86_fp80 and the callee return type is not,
2768 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2769 // perform a tailcall optimization here.
2770 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2771 return false;
2772
Evan Cheng13617962010-04-30 01:12:32 +00002773 CallingConv::ID CallerCC = CallerF->getCallingConv();
2774 bool CCMatch = CallerCC == CalleeCC;
2775
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002776 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002777 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002778 return true;
2779 return false;
2780 }
2781
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002782 // Look for obvious safe cases to perform tail call optimization that do not
2783 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002784
Evan Cheng2c12cb42010-03-26 16:26:03 +00002785 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2786 // emit a special epilogue.
2787 if (RegInfo->needsStackRealignment(MF))
2788 return false;
2789
Evan Chenga375d472010-03-15 18:54:48 +00002790 // Also avoid sibcall optimization if either caller or callee uses struct
2791 // return semantics.
2792 if (isCalleeStructRet || isCallerStructRet)
2793 return false;
2794
Chad Rosier2416da32011-06-24 21:15:36 +00002795 // An stdcall caller is expected to clean up its arguments; the callee
2796 // isn't going to do that.
2797 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2798 return false;
2799
Chad Rosier871f6642011-05-18 19:59:50 +00002800 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002801 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002802 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002803
2804 // Optimizing for varargs on Win64 is unlikely to be safe without
2805 // additional testing.
2806 if (Subtarget->isTargetWin64())
2807 return false;
2808
Chad Rosier871f6642011-05-18 19:59:50 +00002809 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002810 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002811 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002812
Chad Rosier871f6642011-05-18 19:59:50 +00002813 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2814 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2815 if (!ArgLocs[i].isRegLoc())
2816 return false;
2817 }
2818
Chad Rosier30450e82011-12-22 22:35:21 +00002819 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2820 // stack. Therefore, if it's not used by the call it is not safe to optimize
2821 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002822 bool Unused = false;
2823 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2824 if (!Ins[i].Used) {
2825 Unused = true;
2826 break;
2827 }
2828 }
2829 if (Unused) {
2830 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002831 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002832 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002833 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002834 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002835 CCValAssign &VA = RVLocs[i];
2836 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2837 return false;
2838 }
2839 }
2840
Evan Cheng13617962010-04-30 01:12:32 +00002841 // If the calling conventions do not match, then we'd better make sure the
2842 // results are returned in the same way as what the caller expects.
2843 if (!CCMatch) {
2844 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002845 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002846 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002847 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2848
2849 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002850 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002851 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002852 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2853
2854 if (RVLocs1.size() != RVLocs2.size())
2855 return false;
2856 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2857 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2858 return false;
2859 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2860 return false;
2861 if (RVLocs1[i].isRegLoc()) {
2862 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2863 return false;
2864 } else {
2865 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2866 return false;
2867 }
2868 }
2869 }
2870
Evan Chenga6bff982010-01-30 01:22:00 +00002871 // If the callee takes no arguments then go on to check the results of the
2872 // call.
2873 if (!Outs.empty()) {
2874 // Check if stack adjustment is needed. For now, do not do this if any
2875 // argument is passed on the stack.
2876 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002877 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002878 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002879
2880 // Allocate shadow area for Win64
2881 if (Subtarget->isTargetWin64()) {
2882 CCInfo.AllocateStack(32, 8);
2883 }
2884
Duncan Sands45907662010-10-31 13:21:44 +00002885 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002886 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002887 MachineFunction &MF = DAG.getMachineFunction();
2888 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2889 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002890
2891 // Check if the arguments are already laid out in the right way as
2892 // the caller's fixed stack objects.
2893 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002894 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2895 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002896 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002897 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2898 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002899 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002900 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002901 if (VA.getLocInfo() == CCValAssign::Indirect)
2902 return false;
2903 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002904 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2905 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002906 return false;
2907 }
2908 }
2909 }
Evan Cheng9c044672010-05-29 01:35:22 +00002910
2911 // If the tailcall address may be in a register, then make sure it's
2912 // possible to register allocate for it. In 32-bit, the call address can
2913 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002914 // callee-saved registers are restored. These happen to be the same
2915 // registers used to pass 'inreg' arguments so watch out for those.
2916 if (!Subtarget->is64Bit() &&
2917 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002918 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002919 unsigned NumInRegs = 0;
2920 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2921 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002922 if (!VA.isRegLoc())
2923 continue;
2924 unsigned Reg = VA.getLocReg();
2925 switch (Reg) {
2926 default: break;
2927 case X86::EAX: case X86::EDX: case X86::ECX:
2928 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002929 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002930 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002931 }
2932 }
2933 }
Evan Chenga6bff982010-01-30 01:22:00 +00002934 }
Evan Chengb1712452010-01-27 06:25:16 +00002935
Evan Cheng86809cc2010-02-03 03:28:02 +00002936 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002937}
2938
Dan Gohman3df24e62008-09-03 23:12:08 +00002939FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002940X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2941 const TargetLibraryInfo *libInfo) const {
2942 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002943}
2944
2945
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002946//===----------------------------------------------------------------------===//
2947// Other Lowering Hooks
2948//===----------------------------------------------------------------------===//
2949
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002950static bool MayFoldLoad(SDValue Op) {
2951 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2952}
2953
2954static bool MayFoldIntoStore(SDValue Op) {
2955 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2956}
2957
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002958static bool isTargetShuffle(unsigned Opcode) {
2959 switch(Opcode) {
2960 default: return false;
2961 case X86ISD::PSHUFD:
2962 case X86ISD::PSHUFHW:
2963 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002964 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002965 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002966 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002967 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002968 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002969 case X86ISD::MOVLPS:
2970 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002971 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002972 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002973 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002974 case X86ISD::MOVSS:
2975 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002976 case X86ISD::UNPCKL:
2977 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002978 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002979 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002980 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002981 return true;
2982 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002983}
2984
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002985static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002986 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002987 switch(Opc) {
2988 default: llvm_unreachable("Unknown x86 shuffle node");
2989 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002990 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002991 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002992 return DAG.getNode(Opc, dl, VT, V1);
2993 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002994}
2995
2996static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002997 SDValue V1, unsigned TargetMask,
2998 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002999 switch(Opc) {
3000 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003001 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003002 case X86ISD::PSHUFHW:
3003 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003004 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003005 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003006 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3007 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003008}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003009
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003010static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003011 SDValue V1, SDValue V2, unsigned TargetMask,
3012 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003013 switch(Opc) {
3014 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003015 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003016 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003017 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003018 return DAG.getNode(Opc, dl, VT, V1, V2,
3019 DAG.getConstant(TargetMask, MVT::i8));
3020 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003021}
3022
3023static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3024 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3025 switch(Opc) {
3026 default: llvm_unreachable("Unknown x86 shuffle node");
3027 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003028 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003029 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003030 case X86ISD::MOVLPS:
3031 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003032 case X86ISD::MOVSS:
3033 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003034 case X86ISD::UNPCKL:
3035 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003036 return DAG.getNode(Opc, dl, VT, V1, V2);
3037 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003038}
3039
Dan Gohmand858e902010-04-17 15:26:15 +00003040SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003041 MachineFunction &MF = DAG.getMachineFunction();
3042 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3043 int ReturnAddrIndex = FuncInfo->getRAIndex();
3044
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003045 if (ReturnAddrIndex == 0) {
3046 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003047 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003048 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003049 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003050 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003051 }
3052
Evan Cheng25ab6902006-09-08 06:48:29 +00003053 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003054}
3055
3056
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003057bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3058 bool hasSymbolicDisplacement) {
3059 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003060 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003061 return false;
3062
3063 // If we don't have a symbolic displacement - we don't have any extra
3064 // restrictions.
3065 if (!hasSymbolicDisplacement)
3066 return true;
3067
3068 // FIXME: Some tweaks might be needed for medium code model.
3069 if (M != CodeModel::Small && M != CodeModel::Kernel)
3070 return false;
3071
3072 // For small code model we assume that latest object is 16MB before end of 31
3073 // bits boundary. We may also accept pretty large negative constants knowing
3074 // that all objects are in the positive half of address space.
3075 if (M == CodeModel::Small && Offset < 16*1024*1024)
3076 return true;
3077
3078 // For kernel code model we know that all object resist in the negative half
3079 // of 32bits address space. We may not accept negative offsets, since they may
3080 // be just off and we may accept pretty large positive ones.
3081 if (M == CodeModel::Kernel && Offset > 0)
3082 return true;
3083
3084 return false;
3085}
3086
Evan Chengef41ff62011-06-23 17:54:54 +00003087/// isCalleePop - Determines whether the callee is required to pop its
3088/// own arguments. Callee pop is necessary to support tail calls.
3089bool X86::isCalleePop(CallingConv::ID CallingConv,
3090 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3091 if (IsVarArg)
3092 return false;
3093
3094 switch (CallingConv) {
3095 default:
3096 return false;
3097 case CallingConv::X86_StdCall:
3098 return !is64Bit;
3099 case CallingConv::X86_FastCall:
3100 return !is64Bit;
3101 case CallingConv::X86_ThisCall:
3102 return !is64Bit;
3103 case CallingConv::Fast:
3104 return TailCallOpt;
3105 case CallingConv::GHC:
3106 return TailCallOpt;
3107 }
3108}
3109
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003110/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3111/// specific condition code, returning the condition code and the LHS/RHS of the
3112/// comparison to make.
3113static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3114 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003115 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003116 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3117 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3118 // X > -1 -> X == 0, jump !sign.
3119 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003120 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003121 }
3122 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003123 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003124 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003125 }
3126 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003127 // X < 1 -> X <= 0
3128 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003129 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003130 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003131 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003132
Evan Chengd9558e02006-01-06 00:43:03 +00003133 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003134 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003135 case ISD::SETEQ: return X86::COND_E;
3136 case ISD::SETGT: return X86::COND_G;
3137 case ISD::SETGE: return X86::COND_GE;
3138 case ISD::SETLT: return X86::COND_L;
3139 case ISD::SETLE: return X86::COND_LE;
3140 case ISD::SETNE: return X86::COND_NE;
3141 case ISD::SETULT: return X86::COND_B;
3142 case ISD::SETUGT: return X86::COND_A;
3143 case ISD::SETULE: return X86::COND_BE;
3144 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003145 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003146 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003147
Chris Lattner4c78e022008-12-23 23:42:27 +00003148 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003149
Chris Lattner4c78e022008-12-23 23:42:27 +00003150 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003151 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3152 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003153 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3154 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003155 }
3156
Chris Lattner4c78e022008-12-23 23:42:27 +00003157 switch (SetCCOpcode) {
3158 default: break;
3159 case ISD::SETOLT:
3160 case ISD::SETOLE:
3161 case ISD::SETUGT:
3162 case ISD::SETUGE:
3163 std::swap(LHS, RHS);
3164 break;
3165 }
3166
3167 // On a floating point condition, the flags are set as follows:
3168 // ZF PF CF op
3169 // 0 | 0 | 0 | X > Y
3170 // 0 | 0 | 1 | X < Y
3171 // 1 | 0 | 0 | X == Y
3172 // 1 | 1 | 1 | unordered
3173 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003174 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003175 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003176 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003177 case ISD::SETOLT: // flipped
3178 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003179 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003180 case ISD::SETOLE: // flipped
3181 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003182 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003183 case ISD::SETUGT: // flipped
3184 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003185 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003186 case ISD::SETUGE: // flipped
3187 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003188 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003189 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003190 case ISD::SETNE: return X86::COND_NE;
3191 case ISD::SETUO: return X86::COND_P;
3192 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003193 case ISD::SETOEQ:
3194 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003195 }
Evan Chengd9558e02006-01-06 00:43:03 +00003196}
3197
Evan Cheng4a460802006-01-11 00:33:36 +00003198/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3199/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003200/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003201static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003202 switch (X86CC) {
3203 default:
3204 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003205 case X86::COND_B:
3206 case X86::COND_BE:
3207 case X86::COND_E:
3208 case X86::COND_P:
3209 case X86::COND_A:
3210 case X86::COND_AE:
3211 case X86::COND_NE:
3212 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003213 return true;
3214 }
3215}
3216
Evan Chengeb2f9692009-10-27 19:56:55 +00003217/// isFPImmLegal - Returns true if the target can instruction select the
3218/// specified FP immediate natively. If false, the legalizer will
3219/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003220bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003221 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3222 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3223 return true;
3224 }
3225 return false;
3226}
3227
Nate Begeman9008ca62009-04-27 18:41:29 +00003228/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3229/// the specified range (L, H].
3230static bool isUndefOrInRange(int Val, int Low, int Hi) {
3231 return (Val < 0) || (Val >= Low && Val < Hi);
3232}
3233
3234/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3235/// specified value.
3236static bool isUndefOrEqual(int Val, int CmpVal) {
3237 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003238 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003239 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003240}
3241
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003242/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003243/// from position Pos and ending in Pos+Size, falls within the specified
3244/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003245static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003246 unsigned Pos, unsigned Size, int Low) {
3247 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003248 if (!isUndefOrEqual(Mask[i], Low))
3249 return false;
3250 return true;
3251}
3252
Nate Begeman9008ca62009-04-27 18:41:29 +00003253/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3254/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3255/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003256static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003257 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 return (Mask[0] < 2 && Mask[1] < 2);
3261 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003262}
3263
Nate Begeman9008ca62009-04-27 18:41:29 +00003264/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3265/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003266static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3267 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003268 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003269
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003271 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3272 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003273
Evan Cheng506d3df2006-03-29 23:07:14 +00003274 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003275 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003276 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003277 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003278
Craig Toppera9a568a2012-05-02 08:03:44 +00003279 if (VT == MVT::v16i16) {
3280 // Lower quadword copied in order or undef.
3281 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3282 return false;
3283
3284 // Upper quadword shuffled.
3285 for (unsigned i = 12; i != 16; ++i)
3286 if (!isUndefOrInRange(Mask[i], 12, 16))
3287 return false;
3288 }
3289
Evan Cheng506d3df2006-03-29 23:07:14 +00003290 return true;
3291}
3292
Nate Begeman9008ca62009-04-27 18:41:29 +00003293/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3294/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003295static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3296 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003297 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003298
Rafael Espindola15684b22009-04-24 12:40:33 +00003299 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003300 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3301 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003302
Rafael Espindola15684b22009-04-24 12:40:33 +00003303 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003304 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003305 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003306 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003307
Craig Toppera9a568a2012-05-02 08:03:44 +00003308 if (VT == MVT::v16i16) {
3309 // Upper quadword copied in order.
3310 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3311 return false;
3312
3313 // Lower quadword shuffled.
3314 for (unsigned i = 8; i != 12; ++i)
3315 if (!isUndefOrInRange(Mask[i], 8, 12))
3316 return false;
3317 }
3318
Rafael Espindola15684b22009-04-24 12:40:33 +00003319 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003320}
3321
Nate Begemana09008b2009-10-19 02:17:23 +00003322/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3323/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003324static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3325 const X86Subtarget *Subtarget) {
3326 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3327 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003328 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003329
Craig Topper0e2037b2012-01-20 05:53:00 +00003330 unsigned NumElts = VT.getVectorNumElements();
3331 unsigned NumLanes = VT.getSizeInBits()/128;
3332 unsigned NumLaneElts = NumElts/NumLanes;
3333
3334 // Do not handle 64-bit element shuffles with palignr.
3335 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003336 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003337
Craig Topper0e2037b2012-01-20 05:53:00 +00003338 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3339 unsigned i;
3340 for (i = 0; i != NumLaneElts; ++i) {
3341 if (Mask[i+l] >= 0)
3342 break;
3343 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003344
Craig Topper0e2037b2012-01-20 05:53:00 +00003345 // Lane is all undef, go to next lane
3346 if (i == NumLaneElts)
3347 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003348
Craig Topper0e2037b2012-01-20 05:53:00 +00003349 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003350
Craig Topper0e2037b2012-01-20 05:53:00 +00003351 // Make sure its in this lane in one of the sources
3352 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3353 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003354 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003355
3356 // If not lane 0, then we must match lane 0
3357 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3358 return false;
3359
3360 // Correct second source to be contiguous with first source
3361 if (Start >= (int)NumElts)
3362 Start -= NumElts - NumLaneElts;
3363
3364 // Make sure we're shifting in the right direction.
3365 if (Start <= (int)(i+l))
3366 return false;
3367
3368 Start -= i;
3369
3370 // Check the rest of the elements to see if they are consecutive.
3371 for (++i; i != NumLaneElts; ++i) {
3372 int Idx = Mask[i+l];
3373
3374 // Make sure its in this lane
3375 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3376 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3377 return false;
3378
3379 // If not lane 0, then we must match lane 0
3380 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3381 return false;
3382
3383 if (Idx >= (int)NumElts)
3384 Idx -= NumElts - NumLaneElts;
3385
3386 if (!isUndefOrEqual(Idx, Start+i))
3387 return false;
3388
3389 }
Nate Begemana09008b2009-10-19 02:17:23 +00003390 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003391
Nate Begemana09008b2009-10-19 02:17:23 +00003392 return true;
3393}
3394
Craig Topper1a7700a2012-01-19 08:19:12 +00003395/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3396/// the two vector operands have swapped position.
3397static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3398 unsigned NumElems) {
3399 for (unsigned i = 0; i != NumElems; ++i) {
3400 int idx = Mask[i];
3401 if (idx < 0)
3402 continue;
3403 else if (idx < (int)NumElems)
3404 Mask[i] = idx + NumElems;
3405 else
3406 Mask[i] = idx - NumElems;
3407 }
3408}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003409
Craig Topper1a7700a2012-01-19 08:19:12 +00003410/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3411/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3412/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3413/// reverse of what x86 shuffles want.
3414static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3415 bool Commuted = false) {
3416 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003417 return false;
3418
Craig Topper1a7700a2012-01-19 08:19:12 +00003419 unsigned NumElems = VT.getVectorNumElements();
3420 unsigned NumLanes = VT.getSizeInBits()/128;
3421 unsigned NumLaneElems = NumElems/NumLanes;
3422
3423 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003424 return false;
3425
3426 // VSHUFPSY divides the resulting vector into 4 chunks.
3427 // The sources are also splitted into 4 chunks, and each destination
3428 // chunk must come from a different source chunk.
3429 //
3430 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3431 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3432 //
3433 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3434 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3435 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003436 // VSHUFPDY divides the resulting vector into 4 chunks.
3437 // The sources are also splitted into 4 chunks, and each destination
3438 // chunk must come from a different source chunk.
3439 //
3440 // SRC1 => X3 X2 X1 X0
3441 // SRC2 => Y3 Y2 Y1 Y0
3442 //
3443 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3444 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003445 unsigned HalfLaneElems = NumLaneElems/2;
3446 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3447 for (unsigned i = 0; i != NumLaneElems; ++i) {
3448 int Idx = Mask[i+l];
3449 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3450 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3451 return false;
3452 // For VSHUFPSY, the mask of the second half must be the same as the
3453 // first but with the appropriate offsets. This works in the same way as
3454 // VPERMILPS works with masks.
3455 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3456 continue;
3457 if (!isUndefOrEqual(Idx, Mask[i]+l))
3458 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003459 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003460 }
3461
3462 return true;
3463}
3464
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003465/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3466/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003467static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003468 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003469 return false;
3470
Craig Topper7a9a28b2012-08-12 02:23:29 +00003471 unsigned NumElems = VT.getVectorNumElements();
3472
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003473 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003474 return false;
3475
Evan Cheng2064a2b2006-03-28 06:50:32 +00003476 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003477 return isUndefOrEqual(Mask[0], 6) &&
3478 isUndefOrEqual(Mask[1], 7) &&
3479 isUndefOrEqual(Mask[2], 2) &&
3480 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003481}
3482
Nate Begeman0b10b912009-11-07 23:17:15 +00003483/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3484/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3485/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003486static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003487 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003488 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003489
Craig Topper7a9a28b2012-08-12 02:23:29 +00003490 unsigned NumElems = VT.getVectorNumElements();
3491
Nate Begeman0b10b912009-11-07 23:17:15 +00003492 if (NumElems != 4)
3493 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003494
Craig Topperdd637ae2012-02-19 05:41:45 +00003495 return isUndefOrEqual(Mask[0], 2) &&
3496 isUndefOrEqual(Mask[1], 3) &&
3497 isUndefOrEqual(Mask[2], 2) &&
3498 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003499}
3500
Evan Cheng5ced1d82006-04-06 23:23:56 +00003501/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3502/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003503static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003504 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003505 return false;
3506
Craig Topperdd637ae2012-02-19 05:41:45 +00003507 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003508
Evan Cheng5ced1d82006-04-06 23:23:56 +00003509 if (NumElems != 2 && NumElems != 4)
3510 return false;
3511
Chad Rosier238ae312012-04-30 17:47:15 +00003512 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003513 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003514 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003515
Chad Rosier238ae312012-04-30 17:47:15 +00003516 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003517 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003518 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003519
3520 return true;
3521}
3522
Nate Begeman0b10b912009-11-07 23:17:15 +00003523/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3524/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003525static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003526 if (!VT.is128BitVector())
3527 return false;
3528
Craig Topperdd637ae2012-02-19 05:41:45 +00003529 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003530
Craig Topper7a9a28b2012-08-12 02:23:29 +00003531 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003532 return false;
3533
Chad Rosier238ae312012-04-30 17:47:15 +00003534 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003535 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003536 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003537
Chad Rosier238ae312012-04-30 17:47:15 +00003538 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3539 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003540 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003541
3542 return true;
3543}
3544
Elena Demikhovsky15963732012-06-26 08:04:10 +00003545//
3546// Some special combinations that can be optimized.
3547//
3548static
3549SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3550 SelectionDAG &DAG) {
3551 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003552 DebugLoc dl = SVOp->getDebugLoc();
3553
3554 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3555 return SDValue();
3556
3557 ArrayRef<int> Mask = SVOp->getMask();
3558
3559 // These are the special masks that may be optimized.
3560 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3561 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3562 bool MatchEvenMask = true;
3563 bool MatchOddMask = true;
3564 for (int i=0; i<8; ++i) {
3565 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3566 MatchEvenMask = false;
3567 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3568 MatchOddMask = false;
3569 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003570
Elena Demikhovsky32510202012-09-04 12:49:02 +00003571 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003572 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003573
Elena Demikhovsky15963732012-06-26 08:04:10 +00003574 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3575
Elena Demikhovsky32510202012-09-04 12:49:02 +00003576 SDValue Op0 = SVOp->getOperand(0);
3577 SDValue Op1 = SVOp->getOperand(1);
3578
3579 if (MatchEvenMask) {
3580 // Shift the second operand right to 32 bits.
3581 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3582 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3583 } else {
3584 // Shift the first operand left to 32 bits.
3585 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3586 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3587 }
3588 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3589 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003590}
3591
Evan Cheng0038e592006-03-28 00:39:58 +00003592/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3593/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003594static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003595 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003596 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003597
3598 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3599 "Unsupported vector type for unpckh");
3600
Craig Topper6347e862011-11-21 06:57:39 +00003601 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003602 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003603 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003604
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003605 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3606 // independently on 128-bit lanes.
3607 unsigned NumLanes = VT.getSizeInBits()/128;
3608 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003609
Craig Topper94438ba2011-12-16 08:06:31 +00003610 for (unsigned l = 0; l != NumLanes; ++l) {
3611 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3612 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003613 i += 2, ++j) {
3614 int BitI = Mask[i];
3615 int BitI1 = Mask[i+1];
3616 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003617 return false;
David Greenea20244d2011-03-02 17:23:43 +00003618 if (V2IsSplat) {
3619 if (!isUndefOrEqual(BitI1, NumElts))
3620 return false;
3621 } else {
3622 if (!isUndefOrEqual(BitI1, j + NumElts))
3623 return false;
3624 }
Evan Cheng39623da2006-04-20 08:58:49 +00003625 }
Evan Cheng0038e592006-03-28 00:39:58 +00003626 }
David Greenea20244d2011-03-02 17:23:43 +00003627
Evan Cheng0038e592006-03-28 00:39:58 +00003628 return true;
3629}
3630
Evan Cheng4fcb9222006-03-28 02:43:26 +00003631/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3632/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003633static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003634 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003635 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003636
3637 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3638 "Unsupported vector type for unpckh");
3639
Craig Topper6347e862011-11-21 06:57:39 +00003640 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003641 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003642 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003643
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003644 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3645 // independently on 128-bit lanes.
3646 unsigned NumLanes = VT.getSizeInBits()/128;
3647 unsigned NumLaneElts = NumElts/NumLanes;
3648
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003649 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003650 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3651 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003652 int BitI = Mask[i];
3653 int BitI1 = Mask[i+1];
3654 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003655 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003656 if (V2IsSplat) {
3657 if (isUndefOrEqual(BitI1, NumElts))
3658 return false;
3659 } else {
3660 if (!isUndefOrEqual(BitI1, j+NumElts))
3661 return false;
3662 }
Evan Cheng39623da2006-04-20 08:58:49 +00003663 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003664 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003665 return true;
3666}
3667
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003668/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3669/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3670/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003671static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003672 bool HasAVX2) {
3673 unsigned NumElts = VT.getVectorNumElements();
3674
3675 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3676 "Unsupported vector type for unpckh");
3677
3678 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3679 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003680 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003681
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003682 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3683 // FIXME: Need a better way to get rid of this, there's no latency difference
3684 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3685 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003686 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003687 return false;
3688
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003689 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3690 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003691 unsigned NumLanes = VT.getSizeInBits()/128;
3692 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003693
Craig Topper94438ba2011-12-16 08:06:31 +00003694 for (unsigned l = 0; l != NumLanes; ++l) {
3695 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3696 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003697 i += 2, ++j) {
3698 int BitI = Mask[i];
3699 int BitI1 = Mask[i+1];
3700
3701 if (!isUndefOrEqual(BitI, j))
3702 return false;
3703 if (!isUndefOrEqual(BitI1, j))
3704 return false;
3705 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003706 }
David Greenea20244d2011-03-02 17:23:43 +00003707
Rafael Espindola15684b22009-04-24 12:40:33 +00003708 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003709}
3710
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003711/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3712/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3713/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003714static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003715 unsigned NumElts = VT.getVectorNumElements();
3716
3717 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3718 "Unsupported vector type for unpckh");
3719
3720 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3721 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003722 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003723
Craig Topper94438ba2011-12-16 08:06:31 +00003724 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3725 // independently on 128-bit lanes.
3726 unsigned NumLanes = VT.getSizeInBits()/128;
3727 unsigned NumLaneElts = NumElts/NumLanes;
3728
3729 for (unsigned l = 0; l != NumLanes; ++l) {
3730 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3731 i != (l+1)*NumLaneElts; i += 2, ++j) {
3732 int BitI = Mask[i];
3733 int BitI1 = Mask[i+1];
3734 if (!isUndefOrEqual(BitI, j))
3735 return false;
3736 if (!isUndefOrEqual(BitI1, j))
3737 return false;
3738 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003739 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003740 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003741}
3742
Evan Cheng017dcc62006-04-21 01:05:10 +00003743/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3744/// specifies a shuffle of elements that is suitable for input to MOVSS,
3745/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003746static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003747 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003748 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003749 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003750 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003751
Craig Topperc612d792012-01-02 09:17:37 +00003752 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003753
Nate Begeman9008ca62009-04-27 18:41:29 +00003754 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003755 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003756
Craig Topperc612d792012-01-02 09:17:37 +00003757 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003758 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003759 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003760
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003761 return true;
3762}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003763
Craig Topper70b883b2011-11-28 10:14:51 +00003764/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003765/// as permutations between 128-bit chunks or halves. As an example: this
3766/// shuffle bellow:
3767/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3768/// The first half comes from the second half of V1 and the second half from the
3769/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003770static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003771 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003772 return false;
3773
3774 // The shuffle result is divided into half A and half B. In total the two
3775 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3776 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003777 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003778 bool MatchA = false, MatchB = false;
3779
3780 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003781 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003782 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3783 MatchA = true;
3784 break;
3785 }
3786 }
3787
3788 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003789 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003790 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3791 MatchB = true;
3792 break;
3793 }
3794 }
3795
3796 return MatchA && MatchB;
3797}
3798
Craig Topper70b883b2011-11-28 10:14:51 +00003799/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3800/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003801static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003802 EVT VT = SVOp->getValueType(0);
3803
Craig Topperc612d792012-01-02 09:17:37 +00003804 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003805
Craig Topperc612d792012-01-02 09:17:37 +00003806 unsigned FstHalf = 0, SndHalf = 0;
3807 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003808 if (SVOp->getMaskElt(i) > 0) {
3809 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3810 break;
3811 }
3812 }
Craig Topperc612d792012-01-02 09:17:37 +00003813 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003814 if (SVOp->getMaskElt(i) > 0) {
3815 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3816 break;
3817 }
3818 }
3819
3820 return (FstHalf | (SndHalf << 4));
3821}
3822
Craig Topper70b883b2011-11-28 10:14:51 +00003823/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003824/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3825/// Note that VPERMIL mask matching is different depending whether theunderlying
3826/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3827/// to the same elements of the low, but to the higher half of the source.
3828/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003829/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003830static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003831 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003832 return false;
3833
Craig Topperc612d792012-01-02 09:17:37 +00003834 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003835 // Only match 256-bit with 32/64-bit types
3836 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003837 return false;
3838
Craig Topperc612d792012-01-02 09:17:37 +00003839 unsigned NumLanes = VT.getSizeInBits()/128;
3840 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003841 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003842 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003843 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003844 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003845 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003846 continue;
3847 // VPERMILPS handling
3848 if (Mask[i] < 0)
3849 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003850 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003851 return false;
3852 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003853 }
3854
3855 return true;
3856}
3857
Craig Topper5aaffa82012-02-19 02:53:47 +00003858/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003859/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003860/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003861static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003863 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003864 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003865
3866 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003867 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003868 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003869
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003871 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003872
Craig Topperc612d792012-01-02 09:17:37 +00003873 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3875 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3876 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003877 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003878
Evan Cheng39623da2006-04-20 08:58:49 +00003879 return true;
3880}
3881
Evan Chengd9539472006-04-14 21:59:03 +00003882/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3883/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003884/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003885static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003886 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003887 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003888 return false;
3889
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003890 unsigned NumElems = VT.getVectorNumElements();
3891
3892 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3893 (VT.getSizeInBits() == 256 && NumElems != 8))
3894 return false;
3895
3896 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003897 for (unsigned i = 0; i != NumElems; i += 2)
3898 if (!isUndefOrEqual(Mask[i], i+1) ||
3899 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003901
3902 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003903}
3904
3905/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3906/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003907/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003908static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003909 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003910 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003911 return false;
3912
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003913 unsigned NumElems = VT.getVectorNumElements();
3914
3915 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3916 (VT.getSizeInBits() == 256 && NumElems != 8))
3917 return false;
3918
3919 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003920 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003921 if (!isUndefOrEqual(Mask[i], i) ||
3922 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003923 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003924
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003926}
3927
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003928/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3929/// specifies a shuffle of elements that is suitable for input to 256-bit
3930/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003931static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003932 if (!HasAVX || !VT.is256BitVector())
3933 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003934
Craig Topper7a9a28b2012-08-12 02:23:29 +00003935 unsigned NumElts = VT.getVectorNumElements();
3936 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003937 return false;
3938
Craig Topperc612d792012-01-02 09:17:37 +00003939 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003940 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003941 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003942 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003943 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003944 return false;
3945 return true;
3946}
3947
Evan Cheng0b457f02008-09-25 20:50:48 +00003948/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003949/// specifies a shuffle of elements that is suitable for input to 128-bit
3950/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003951static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003952 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003953 return false;
3954
Craig Topperc612d792012-01-02 09:17:37 +00003955 unsigned e = VT.getVectorNumElements() / 2;
3956 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003957 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003958 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003959 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003960 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003961 return false;
3962 return true;
3963}
3964
David Greenec38a03e2011-02-03 15:50:00 +00003965/// isVEXTRACTF128Index - Return true if the specified
3966/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3967/// suitable for input to VEXTRACTF128.
3968bool X86::isVEXTRACTF128Index(SDNode *N) {
3969 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3970 return false;
3971
3972 // The index should be aligned on a 128-bit boundary.
3973 uint64_t Index =
3974 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3975
3976 unsigned VL = N->getValueType(0).getVectorNumElements();
3977 unsigned VBits = N->getValueType(0).getSizeInBits();
3978 unsigned ElSize = VBits / VL;
3979 bool Result = (Index * ElSize) % 128 == 0;
3980
3981 return Result;
3982}
3983
David Greeneccacdc12011-02-04 16:08:29 +00003984/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3985/// operand specifies a subvector insert that is suitable for input to
3986/// VINSERTF128.
3987bool X86::isVINSERTF128Index(SDNode *N) {
3988 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3989 return false;
3990
3991 // The index should be aligned on a 128-bit boundary.
3992 uint64_t Index =
3993 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3994
3995 unsigned VL = N->getValueType(0).getVectorNumElements();
3996 unsigned VBits = N->getValueType(0).getSizeInBits();
3997 unsigned ElSize = VBits / VL;
3998 bool Result = (Index * ElSize) % 128 == 0;
3999
4000 return Result;
4001}
4002
Evan Cheng63d33002006-03-22 08:01:21 +00004003/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004004/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004005/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004006static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004007 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004008
Craig Topper1a7700a2012-01-19 08:19:12 +00004009 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4010 "Unsupported vector type for PSHUF/SHUFP");
4011
4012 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4013 // independently on 128-bit lanes.
4014 unsigned NumElts = VT.getVectorNumElements();
4015 unsigned NumLanes = VT.getSizeInBits()/128;
4016 unsigned NumLaneElts = NumElts/NumLanes;
4017
4018 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4019 "Only supports 2 or 4 elements per lane");
4020
4021 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004022 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004023 for (unsigned i = 0; i != NumElts; ++i) {
4024 int Elt = N->getMaskElt(i);
4025 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004026 Elt &= NumLaneElts - 1;
4027 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004028 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004029 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004030
Evan Cheng63d33002006-03-22 08:01:21 +00004031 return Mask;
4032}
4033
Evan Cheng506d3df2006-03-29 23:07:14 +00004034/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004035/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004036static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004037 EVT VT = N->getValueType(0);
4038
4039 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4040 "Unsupported vector type for PSHUFHW");
4041
4042 unsigned NumElts = VT.getVectorNumElements();
4043
Evan Cheng506d3df2006-03-29 23:07:14 +00004044 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004045 for (unsigned l = 0; l != NumElts; l += 8) {
4046 // 8 nodes per lane, but we only care about the last 4.
4047 for (unsigned i = 0; i < 4; ++i) {
4048 int Elt = N->getMaskElt(l+i+4);
4049 if (Elt < 0) continue;
4050 Elt &= 0x3; // only 2-bits.
4051 Mask |= Elt << (i * 2);
4052 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004053 }
Craig Topper6b28d352012-05-03 07:12:59 +00004054
Evan Cheng506d3df2006-03-29 23:07:14 +00004055 return Mask;
4056}
4057
4058/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004059/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004060static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004061 EVT VT = N->getValueType(0);
4062
4063 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4064 "Unsupported vector type for PSHUFHW");
4065
4066 unsigned NumElts = VT.getVectorNumElements();
4067
Evan Cheng506d3df2006-03-29 23:07:14 +00004068 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004069 for (unsigned l = 0; l != NumElts; l += 8) {
4070 // 8 nodes per lane, but we only care about the first 4.
4071 for (unsigned i = 0; i < 4; ++i) {
4072 int Elt = N->getMaskElt(l+i);
4073 if (Elt < 0) continue;
4074 Elt &= 0x3; // only 2-bits
4075 Mask |= Elt << (i * 2);
4076 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004077 }
Craig Topper6b28d352012-05-03 07:12:59 +00004078
Evan Cheng506d3df2006-03-29 23:07:14 +00004079 return Mask;
4080}
4081
Nate Begemana09008b2009-10-19 02:17:23 +00004082/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4083/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004084static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4085 EVT VT = SVOp->getValueType(0);
4086 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004087
Craig Topper0e2037b2012-01-20 05:53:00 +00004088 unsigned NumElts = VT.getVectorNumElements();
4089 unsigned NumLanes = VT.getSizeInBits()/128;
4090 unsigned NumLaneElts = NumElts/NumLanes;
4091
4092 int Val = 0;
4093 unsigned i;
4094 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004095 Val = SVOp->getMaskElt(i);
4096 if (Val >= 0)
4097 break;
4098 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004099 if (Val >= (int)NumElts)
4100 Val -= NumElts - NumLaneElts;
4101
Eli Friedman63f8dde2011-07-25 21:36:45 +00004102 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004103 return (Val - i) * EltSize;
4104}
4105
David Greenec38a03e2011-02-03 15:50:00 +00004106/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4107/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4108/// instructions.
4109unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4110 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4111 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4112
4113 uint64_t Index =
4114 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4115
4116 EVT VecVT = N->getOperand(0).getValueType();
4117 EVT ElVT = VecVT.getVectorElementType();
4118
4119 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004120 return Index / NumElemsPerChunk;
4121}
4122
David Greeneccacdc12011-02-04 16:08:29 +00004123/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4124/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4125/// instructions.
4126unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4127 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4128 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4129
4130 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004131 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004132
4133 EVT VecVT = N->getValueType(0);
4134 EVT ElVT = VecVT.getVectorElementType();
4135
4136 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004137 return Index / NumElemsPerChunk;
4138}
4139
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004140/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4141/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4142/// Handles 256-bit.
4143static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4144 EVT VT = N->getValueType(0);
4145
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004146 unsigned NumElts = VT.getVectorNumElements();
4147
Craig Topper095c5282012-04-15 23:48:57 +00004148 assert((VT.is256BitVector() && NumElts == 4) &&
4149 "Unsupported vector type for VPERMQ/VPERMPD");
4150
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004151 unsigned Mask = 0;
4152 for (unsigned i = 0; i != NumElts; ++i) {
4153 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004154 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004155 continue;
4156 Mask |= Elt << (i*2);
4157 }
4158
4159 return Mask;
4160}
Evan Cheng37b73872009-07-30 08:33:02 +00004161/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4162/// constant +0.0.
4163bool X86::isZeroNode(SDValue Elt) {
4164 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004165 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004166 (isa<ConstantFPSDNode>(Elt) &&
4167 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4168}
4169
Nate Begeman9008ca62009-04-27 18:41:29 +00004170/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4171/// their permute mask.
4172static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4173 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004174 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004175 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004176 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004177
Nate Begeman5a5ca152009-04-29 05:20:52 +00004178 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004179 int Idx = SVOp->getMaskElt(i);
4180 if (Idx >= 0) {
4181 if (Idx < (int)NumElems)
4182 Idx += NumElems;
4183 else
4184 Idx -= NumElems;
4185 }
4186 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004187 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004188 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4189 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004190}
4191
Evan Cheng533a0aa2006-04-19 20:35:22 +00004192/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4193/// match movhlps. The lower half elements should come from upper half of
4194/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004195/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004196static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004197 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004198 return false;
4199 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004200 return false;
4201 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004202 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004203 return false;
4204 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004205 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004206 return false;
4207 return true;
4208}
4209
Evan Cheng5ced1d82006-04-06 23:23:56 +00004210/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004211/// is promoted to a vector. It also returns the LoadSDNode by reference if
4212/// required.
4213static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004214 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4215 return false;
4216 N = N->getOperand(0).getNode();
4217 if (!ISD::isNON_EXTLoad(N))
4218 return false;
4219 if (LD)
4220 *LD = cast<LoadSDNode>(N);
4221 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004222}
4223
Dan Gohman65fd6562011-11-03 21:49:52 +00004224// Test whether the given value is a vector value which will be legalized
4225// into a load.
4226static bool WillBeConstantPoolLoad(SDNode *N) {
4227 if (N->getOpcode() != ISD::BUILD_VECTOR)
4228 return false;
4229
4230 // Check for any non-constant elements.
4231 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4232 switch (N->getOperand(i).getNode()->getOpcode()) {
4233 case ISD::UNDEF:
4234 case ISD::ConstantFP:
4235 case ISD::Constant:
4236 break;
4237 default:
4238 return false;
4239 }
4240
4241 // Vectors of all-zeros and all-ones are materialized with special
4242 // instructions rather than being loaded.
4243 return !ISD::isBuildVectorAllZeros(N) &&
4244 !ISD::isBuildVectorAllOnes(N);
4245}
4246
Evan Cheng533a0aa2006-04-19 20:35:22 +00004247/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4248/// match movlp{s|d}. The lower half elements should come from lower half of
4249/// V1 (and in order), and the upper half elements should come from the upper
4250/// half of V2 (and in order). And since V1 will become the source of the
4251/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004252static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004253 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004254 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004255 return false;
4256
Evan Cheng466685d2006-10-09 20:57:25 +00004257 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004258 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004259 // Is V2 is a vector load, don't do this transformation. We will try to use
4260 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004261 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004262 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004263
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004264 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004265
Evan Cheng533a0aa2006-04-19 20:35:22 +00004266 if (NumElems != 2 && NumElems != 4)
4267 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004268 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004269 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004270 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004271 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004272 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004273 return false;
4274 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004275}
4276
Evan Cheng39623da2006-04-20 08:58:49 +00004277/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4278/// all the same.
4279static bool isSplatVector(SDNode *N) {
4280 if (N->getOpcode() != ISD::BUILD_VECTOR)
4281 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004282
Dan Gohman475871a2008-07-27 21:46:04 +00004283 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004284 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4285 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004286 return false;
4287 return true;
4288}
4289
Evan Cheng213d2cf2007-05-17 18:45:50 +00004290/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004291/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004292/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004293static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004294 SDValue V1 = N->getOperand(0);
4295 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004296 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4297 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004298 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004299 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004300 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004301 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4302 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004303 if (Opc != ISD::BUILD_VECTOR ||
4304 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 return false;
4306 } else if (Idx >= 0) {
4307 unsigned Opc = V1.getOpcode();
4308 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4309 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004310 if (Opc != ISD::BUILD_VECTOR ||
4311 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004312 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004313 }
4314 }
4315 return true;
4316}
4317
4318/// getZeroVector - Returns a vector of specified type with all zero elements.
4319///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004320static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004321 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004322 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004323 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004324
Dale Johannesen0488fb62010-09-30 23:57:10 +00004325 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004326 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004327 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004328 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004329 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004330 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4331 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4332 } else { // SSE1
4333 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4334 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4335 }
Craig Topper9d352402012-04-23 07:24:41 +00004336 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004337 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004338 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4339 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4340 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4341 } else {
4342 // 256-bit logic and arithmetic instructions in AVX are all
4343 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4344 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4345 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4346 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4347 }
Craig Topper9d352402012-04-23 07:24:41 +00004348 } else
4349 llvm_unreachable("Unexpected vector type");
4350
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004351 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004352}
4353
Chris Lattner8a594482007-11-25 00:24:49 +00004354/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004355/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4356/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4357/// Then bitcast to their original type, ensuring they get CSE'd.
4358static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4359 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004360 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004361 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004362
Owen Anderson825b72b2009-08-11 20:47:22 +00004363 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004364 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004365 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004366 if (HasAVX2) { // AVX2
4367 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4368 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4369 } else { // AVX
4370 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004371 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004372 }
Craig Topper9d352402012-04-23 07:24:41 +00004373 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004374 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004375 } else
4376 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004377
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004378 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004379}
4380
Evan Cheng39623da2006-04-20 08:58:49 +00004381/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4382/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004383static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004384 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004385 if (Mask[i] > (int)NumElems) {
4386 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004387 }
Evan Cheng39623da2006-04-20 08:58:49 +00004388 }
Evan Cheng39623da2006-04-20 08:58:49 +00004389}
4390
Evan Cheng017dcc62006-04-21 01:05:10 +00004391/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4392/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004393static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 SDValue V2) {
4395 unsigned NumElems = VT.getVectorNumElements();
4396 SmallVector<int, 8> Mask;
4397 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004398 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 Mask.push_back(i);
4400 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004401}
4402
Nate Begeman9008ca62009-04-27 18:41:29 +00004403/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004404static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004405 SDValue V2) {
4406 unsigned NumElems = VT.getVectorNumElements();
4407 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004408 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 Mask.push_back(i);
4410 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004411 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004413}
4414
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004415/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004416static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 SDValue V2) {
4418 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004420 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004421 Mask.push_back(i + Half);
4422 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004423 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004425}
4426
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004427// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004428// a generic shuffle instruction because the target has no such instructions.
4429// Generate shuffles which repeat i16 and i8 several times until they can be
4430// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004431static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004432 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004433 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004434 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004435
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 while (NumElems > 4) {
4437 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004438 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004440 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 EltNo -= NumElems/2;
4442 }
4443 NumElems >>= 1;
4444 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004445 return V;
4446}
Eric Christopherfd179292009-08-27 18:07:15 +00004447
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004448/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4449static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4450 EVT VT = V.getValueType();
4451 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004452 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004453
Craig Topper9d352402012-04-23 07:24:41 +00004454 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004455 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004456 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004457 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4458 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004459 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004460 // To use VPERMILPS to splat scalars, the second half of indicies must
4461 // refer to the higher part, which is a duplication of the lower one,
4462 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004463 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4464 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004465
4466 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4467 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4468 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004469 } else
4470 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004471
4472 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4473}
4474
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004475/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004476static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4477 EVT SrcVT = SV->getValueType(0);
4478 SDValue V1 = SV->getOperand(0);
4479 DebugLoc dl = SV->getDebugLoc();
4480
4481 int EltNo = SV->getSplatIndex();
4482 int NumElems = SrcVT.getVectorNumElements();
4483 unsigned Size = SrcVT.getSizeInBits();
4484
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004485 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4486 "Unknown how to promote splat for type");
4487
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004488 // Extract the 128-bit part containing the splat element and update
4489 // the splat element index when it refers to the higher register.
4490 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004491 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4492 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004493 EltNo -= NumElems/2;
4494 }
4495
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004496 // All i16 and i8 vector types can't be used directly by a generic shuffle
4497 // instruction because the target has no such instruction. Generate shuffles
4498 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004499 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004500 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004501 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004502 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503
4504 // Recreate the 256-bit vector and place the same 128-bit vector
4505 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004506 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004508 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004509 }
4510
4511 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004512}
4513
Evan Chengba05f722006-04-21 23:03:30 +00004514/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004515/// vector of zero or undef vector. This produces a shuffle where the low
4516/// element of V2 is swizzled into the zero/undef vector, landing at element
4517/// 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 +00004518static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004519 bool IsZero,
4520 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004521 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004522 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004523 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004524 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004525 unsigned NumElems = VT.getVectorNumElements();
4526 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004527 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004528 // If this is the insertion idx, put the low elt of V2 here.
4529 MaskVec.push_back(i == Idx ? NumElems : i);
4530 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004531}
4532
Craig Toppera1ffc682012-03-20 06:42:26 +00004533/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4534/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004535/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004536static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004537 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004538 unsigned NumElems = VT.getVectorNumElements();
4539 SDValue ImmN;
4540
Craig Topper89f4e662012-03-20 07:17:59 +00004541 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004542 switch(N->getOpcode()) {
4543 case X86ISD::SHUFP:
4544 ImmN = N->getOperand(N->getNumOperands()-1);
4545 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4546 break;
4547 case X86ISD::UNPCKH:
4548 DecodeUNPCKHMask(VT, Mask);
4549 break;
4550 case X86ISD::UNPCKL:
4551 DecodeUNPCKLMask(VT, Mask);
4552 break;
4553 case X86ISD::MOVHLPS:
4554 DecodeMOVHLPSMask(NumElems, Mask);
4555 break;
4556 case X86ISD::MOVLHPS:
4557 DecodeMOVLHPSMask(NumElems, Mask);
4558 break;
4559 case X86ISD::PSHUFD:
4560 case X86ISD::VPERMILP:
4561 ImmN = N->getOperand(N->getNumOperands()-1);
4562 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004563 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004564 break;
4565 case X86ISD::PSHUFHW:
4566 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004567 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004568 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004569 break;
4570 case X86ISD::PSHUFLW:
4571 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004572 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004573 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004574 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004575 case X86ISD::VPERMI:
4576 ImmN = N->getOperand(N->getNumOperands()-1);
4577 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4578 IsUnary = true;
4579 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004580 case X86ISD::MOVSS:
4581 case X86ISD::MOVSD: {
4582 // The index 0 always comes from the first element of the second source,
4583 // this is why MOVSS and MOVSD are used in the first place. The other
4584 // elements come from the other positions of the first source vector
4585 Mask.push_back(NumElems);
4586 for (unsigned i = 1; i != NumElems; ++i) {
4587 Mask.push_back(i);
4588 }
4589 break;
4590 }
4591 case X86ISD::VPERM2X128:
4592 ImmN = N->getOperand(N->getNumOperands()-1);
4593 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004594 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004595 break;
4596 case X86ISD::MOVDDUP:
4597 case X86ISD::MOVLHPD:
4598 case X86ISD::MOVLPD:
4599 case X86ISD::MOVLPS:
4600 case X86ISD::MOVSHDUP:
4601 case X86ISD::MOVSLDUP:
4602 case X86ISD::PALIGN:
4603 // Not yet implemented
4604 return false;
4605 default: llvm_unreachable("unknown target shuffle node");
4606 }
4607
4608 return true;
4609}
4610
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004611/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4612/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004613static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004614 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004615 if (Depth == 6)
4616 return SDValue(); // Limit search depth.
4617
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004618 SDValue V = SDValue(N, 0);
4619 EVT VT = V.getValueType();
4620 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004621
4622 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4623 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004624 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004625
Craig Topper3d092db2012-03-21 02:14:01 +00004626 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627 return DAG.getUNDEF(VT.getVectorElementType());
4628
Craig Topperd156dc12012-02-06 07:17:51 +00004629 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004630 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4631 : SV->getOperand(1);
4632 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004633 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634
4635 // Recurse into target specific vector shuffles to find scalars.
4636 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004637 MVT ShufVT = V.getValueType().getSimpleVT();
4638 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004639 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004640 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004641
Craig Topperd978c542012-05-06 19:46:21 +00004642 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004643 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004644
Craig Topper3d092db2012-03-21 02:14:01 +00004645 int Elt = ShuffleMask[Index];
4646 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004647 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004648
Craig Topper3d092db2012-03-21 02:14:01 +00004649 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004650 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004651 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004652 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004653 }
4654
4655 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004656 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657 V = V.getOperand(0);
4658 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004659 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004661 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004662 return SDValue();
4663 }
4664
4665 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4666 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004667 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004668
4669 if (V.getOpcode() == ISD::BUILD_VECTOR)
4670 return V.getOperand(Index);
4671
4672 return SDValue();
4673}
4674
4675/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4676/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004677/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678static
Craig Topper3d092db2012-03-21 02:14:01 +00004679unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004680 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004681 unsigned i;
4682 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004683 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004684 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004685 if (!(Elt.getNode() &&
4686 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4687 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004688 }
4689
4690 return i;
4691}
4692
Craig Topper3d092db2012-03-21 02:14:01 +00004693/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4694/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004695/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4696static
Craig Topper3d092db2012-03-21 02:14:01 +00004697bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4698 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4699 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004700 bool SeenV1 = false;
4701 bool SeenV2 = false;
4702
Craig Topper3d092db2012-03-21 02:14:01 +00004703 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004704 int Idx = SVOp->getMaskElt(i);
4705 // Ignore undef indicies
4706 if (Idx < 0)
4707 continue;
4708
Craig Topper3d092db2012-03-21 02:14:01 +00004709 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004710 SeenV1 = true;
4711 else
4712 SeenV2 = true;
4713
4714 // Only accept consecutive elements from the same vector
4715 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4716 return false;
4717 }
4718
4719 OpNum = SeenV1 ? 0 : 1;
4720 return true;
4721}
4722
4723/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4724/// logical left shift of a vector.
4725static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4726 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4727 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4728 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4729 false /* check zeros from right */, DAG);
4730 unsigned OpSrc;
4731
4732 if (!NumZeros)
4733 return false;
4734
4735 // Considering the elements in the mask that are not consecutive zeros,
4736 // check if they consecutively come from only one of the source vectors.
4737 //
4738 // V1 = {X, A, B, C} 0
4739 // \ \ \ /
4740 // vector_shuffle V1, V2 <1, 2, 3, X>
4741 //
4742 if (!isShuffleMaskConsecutive(SVOp,
4743 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004744 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004745 NumZeros, // Where to start looking in the src vector
4746 NumElems, // Number of elements in vector
4747 OpSrc)) // Which source operand ?
4748 return false;
4749
4750 isLeft = false;
4751 ShAmt = NumZeros;
4752 ShVal = SVOp->getOperand(OpSrc);
4753 return true;
4754}
4755
4756/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4757/// logical left shift of a vector.
4758static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4759 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4760 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4761 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4762 true /* check zeros from left */, DAG);
4763 unsigned OpSrc;
4764
4765 if (!NumZeros)
4766 return false;
4767
4768 // Considering the elements in the mask that are not consecutive zeros,
4769 // check if they consecutively come from only one of the source vectors.
4770 //
4771 // 0 { A, B, X, X } = V2
4772 // / \ / /
4773 // vector_shuffle V1, V2 <X, X, 4, 5>
4774 //
4775 if (!isShuffleMaskConsecutive(SVOp,
4776 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004777 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004778 0, // Where to start looking in the src vector
4779 NumElems, // Number of elements in vector
4780 OpSrc)) // Which source operand ?
4781 return false;
4782
4783 isLeft = true;
4784 ShAmt = NumZeros;
4785 ShVal = SVOp->getOperand(OpSrc);
4786 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004787}
4788
4789/// isVectorShift - Returns true if the shuffle can be implemented as a
4790/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004791static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004792 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004793 // Although the logic below support any bitwidth size, there are no
4794 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004795 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004796 return false;
4797
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004798 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4799 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4800 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004801
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004802 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004803}
4804
Evan Chengc78d3b42006-04-24 18:01:45 +00004805/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4806///
Dan Gohman475871a2008-07-27 21:46:04 +00004807static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004808 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004809 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004810 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004811 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004812 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004813 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004814
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004815 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004816 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004817 bool First = true;
4818 for (unsigned i = 0; i < 16; ++i) {
4819 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4820 if (ThisIsNonZero && First) {
4821 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004822 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004824 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004825 First = false;
4826 }
4827
4828 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004829 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004830 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4831 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004832 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004833 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004834 }
4835 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004836 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4837 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4838 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004840 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004841 } else
4842 ThisElt = LastElt;
4843
Gabor Greifba36cb52008-08-28 21:40:38 +00004844 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004846 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004847 }
4848 }
4849
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004850 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004851}
4852
Bill Wendlinga348c562007-03-22 18:42:45 +00004853/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004854///
Dan Gohman475871a2008-07-27 21:46:04 +00004855static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004856 unsigned NumNonZero, unsigned NumZero,
4857 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004858 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004859 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004860 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004861 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004862
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004863 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004864 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004865 bool First = true;
4866 for (unsigned i = 0; i < 8; ++i) {
4867 bool isNonZero = (NonZeros & (1 << i)) != 0;
4868 if (isNonZero) {
4869 if (First) {
4870 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004871 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004872 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004874 First = false;
4875 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004876 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004878 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004879 }
4880 }
4881
4882 return V;
4883}
4884
Evan Chengf26ffe92008-05-29 08:22:04 +00004885/// getVShift - Return a vector logical shift node.
4886///
Owen Andersone50ed302009-08-10 22:56:29 +00004887static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004888 unsigned NumBits, SelectionDAG &DAG,
4889 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004890 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004891 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004892 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004893 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4894 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004895 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004896 DAG.getConstant(NumBits,
4897 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004898}
4899
Dan Gohman475871a2008-07-27 21:46:04 +00004900SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004901X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004902 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004903
Evan Chengc3630942009-12-09 21:00:30 +00004904 // Check if the scalar load can be widened into a vector load. And if
4905 // the address is "base + cst" see if the cst can be "absorbed" into
4906 // the shuffle mask.
4907 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4908 SDValue Ptr = LD->getBasePtr();
4909 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4910 return SDValue();
4911 EVT PVT = LD->getValueType(0);
4912 if (PVT != MVT::i32 && PVT != MVT::f32)
4913 return SDValue();
4914
4915 int FI = -1;
4916 int64_t Offset = 0;
4917 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4918 FI = FINode->getIndex();
4919 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004920 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004921 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4922 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4923 Offset = Ptr.getConstantOperandVal(1);
4924 Ptr = Ptr.getOperand(0);
4925 } else {
4926 return SDValue();
4927 }
4928
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004929 // FIXME: 256-bit vector instructions don't require a strict alignment,
4930 // improve this code to support it better.
4931 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004932 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004933 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004934 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004935 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004936 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004937 // Can't change the alignment. FIXME: It's possible to compute
4938 // the exact stack offset and reference FI + adjust offset instead.
4939 // If someone *really* cares about this. That's the way to implement it.
4940 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004941 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004942 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004943 }
4944 }
4945
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004946 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004947 // Ptr + (Offset & ~15).
4948 if (Offset < 0)
4949 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004950 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004951 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004952 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004953 if (StartOffset)
4954 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4955 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4956
4957 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004958 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004959
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004960 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4961 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004962 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004963 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004964
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004965 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004966 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004967 Mask.push_back(EltNo);
4968
Craig Toppercc3000632012-01-30 07:50:31 +00004969 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004970 }
4971
4972 return SDValue();
4973}
4974
Michael J. Spencerec38de22010-10-10 22:04:20 +00004975/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4976/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004977/// load which has the same value as a build_vector whose operands are 'elts'.
4978///
4979/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004980///
Nate Begeman1449f292010-03-24 22:19:06 +00004981/// FIXME: we'd also like to handle the case where the last elements are zero
4982/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4983/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004984static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004985 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004986 EVT EltVT = VT.getVectorElementType();
4987 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004988
Nate Begemanfdea31a2010-03-24 20:49:50 +00004989 LoadSDNode *LDBase = NULL;
4990 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004991
Nate Begeman1449f292010-03-24 22:19:06 +00004992 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004993 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004994 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004995 for (unsigned i = 0; i < NumElems; ++i) {
4996 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004997
Nate Begemanfdea31a2010-03-24 20:49:50 +00004998 if (!Elt.getNode() ||
4999 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5000 return SDValue();
5001 if (!LDBase) {
5002 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5003 return SDValue();
5004 LDBase = cast<LoadSDNode>(Elt.getNode());
5005 LastLoadedElt = i;
5006 continue;
5007 }
5008 if (Elt.getOpcode() == ISD::UNDEF)
5009 continue;
5010
5011 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5012 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5013 return SDValue();
5014 LastLoadedElt = i;
5015 }
Nate Begeman1449f292010-03-24 22:19:06 +00005016
5017 // If we have found an entire vector of loads and undefs, then return a large
5018 // load of the entire vector width starting at the base pointer. If we found
5019 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005020 if (LastLoadedElt == NumElems - 1) {
5021 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005022 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005023 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005024 LDBase->isVolatile(), LDBase->isNonTemporal(),
5025 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005026 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005027 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005028 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005029 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005030 }
5031 if (NumElems == 4 && LastLoadedElt == 1 &&
5032 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005033 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5034 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005035 SDValue ResNode =
5036 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5037 LDBase->getPointerInfo(),
5038 LDBase->getAlignment(),
5039 false/*isVolatile*/, true/*ReadMem*/,
5040 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005041
5042 // Make sure the newly-created LOAD is in the same position as LDBase in
5043 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5044 // update uses of LDBase's output chain to use the TokenFactor.
5045 if (LDBase->hasAnyUseOfValue(1)) {
5046 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5047 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5048 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5049 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5050 SDValue(ResNode.getNode(), 1));
5051 }
5052
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005053 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005054 }
5055 return SDValue();
5056}
5057
Nadav Rotem9d68b062012-04-08 12:54:54 +00005058/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5059/// to generate a splat value for the following cases:
5060/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005061/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005062/// a scalar load, or a constant.
5063/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005064/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005065SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005066X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005067 if (!Subtarget->hasAVX())
5068 return SDValue();
5069
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005070 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005071 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005072
Craig Topper5da8a802012-05-04 05:49:51 +00005073 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5074 "Unsupported vector type for broadcast.");
5075
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005076 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005077 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005078
Nadav Rotem9d68b062012-04-08 12:54:54 +00005079 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005080 default:
5081 // Unknown pattern found.
5082 return SDValue();
5083
5084 case ISD::BUILD_VECTOR: {
5085 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005086 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005087 return SDValue();
5088
Nadav Rotem9d68b062012-04-08 12:54:54 +00005089 Ld = Op.getOperand(0);
5090 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5091 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005092
5093 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005094 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005095 // Constants may have multiple users.
5096 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005097 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005098 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005099 }
5100
5101 case ISD::VECTOR_SHUFFLE: {
5102 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5103
5104 // Shuffles must have a splat mask where the first element is
5105 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005106 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005107 return SDValue();
5108
5109 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005110 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005111 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5112
5113 if (!Subtarget->hasAVX2())
5114 return SDValue();
5115
5116 // Use the register form of the broadcast instruction available on AVX2.
5117 if (VT.is256BitVector())
5118 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5119 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5120 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005121
5122 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005123 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005124 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005125
5126 // The scalar_to_vector node and the suspected
5127 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005128 // Constants may have multiple users.
5129 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005130 return SDValue();
5131 break;
5132 }
5133 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005134
Craig Topper7a9a28b2012-08-12 02:23:29 +00005135 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005136
5137 // Handle the broadcasting a single constant scalar from the constant pool
5138 // into a vector. On Sandybridge it is still better to load a constant vector
5139 // from the constant pool and not to broadcast it from a scalar.
5140 if (ConstSplatVal && Subtarget->hasAVX2()) {
5141 EVT CVT = Ld.getValueType();
5142 assert(!CVT.isVector() && "Must not broadcast a vector type");
5143 unsigned ScalarSize = CVT.getSizeInBits();
5144
Craig Topper5da8a802012-05-04 05:49:51 +00005145 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005146 const Constant *C = 0;
5147 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5148 C = CI->getConstantIntValue();
5149 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5150 C = CF->getConstantFPValue();
5151
5152 assert(C && "Invalid constant type");
5153
Nadav Rotem154819d2012-04-09 07:45:58 +00005154 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005155 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005156 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005157 MachinePointerInfo::getConstantPool(),
5158 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005159
Nadav Rotem9d68b062012-04-08 12:54:54 +00005160 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5161 }
5162 }
5163
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005164 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005165 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5166
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005167 // Handle AVX2 in-register broadcasts.
5168 if (!IsLoad && Subtarget->hasAVX2() &&
5169 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5170 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5171
5172 // The scalar source must be a normal load.
5173 if (!IsLoad)
5174 return SDValue();
5175
Craig Topper5da8a802012-05-04 05:49:51 +00005176 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005177 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005178
Craig Toppera9376332012-01-10 08:23:59 +00005179 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005180 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005181 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005182 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005183 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005184 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005185
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005186 // Unsupported broadcast.
5187 return SDValue();
5188}
5189
Evan Chengc3630942009-12-09 21:00:30 +00005190SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005191X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5192 EVT VT = Op.getValueType();
5193
5194 // Skip if insert_vec_elt is not supported.
5195 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5196 return SDValue();
5197
5198 DebugLoc DL = Op.getDebugLoc();
5199 unsigned NumElems = Op.getNumOperands();
5200
5201 SDValue VecIn1;
5202 SDValue VecIn2;
5203 SmallVector<unsigned, 4> InsertIndices;
5204 SmallVector<int, 8> Mask(NumElems, -1);
5205
5206 for (unsigned i = 0; i != NumElems; ++i) {
5207 unsigned Opc = Op.getOperand(i).getOpcode();
5208
5209 if (Opc == ISD::UNDEF)
5210 continue;
5211
5212 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5213 // Quit if more than 1 elements need inserting.
5214 if (InsertIndices.size() > 1)
5215 return SDValue();
5216
5217 InsertIndices.push_back(i);
5218 continue;
5219 }
5220
5221 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5222 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5223
5224 // Quit if extracted from vector of different type.
5225 if (ExtractedFromVec.getValueType() != VT)
5226 return SDValue();
5227
5228 // Quit if non-constant index.
5229 if (!isa<ConstantSDNode>(ExtIdx))
5230 return SDValue();
5231
5232 if (VecIn1.getNode() == 0)
5233 VecIn1 = ExtractedFromVec;
5234 else if (VecIn1 != ExtractedFromVec) {
5235 if (VecIn2.getNode() == 0)
5236 VecIn2 = ExtractedFromVec;
5237 else if (VecIn2 != ExtractedFromVec)
5238 // Quit if more than 2 vectors to shuffle
5239 return SDValue();
5240 }
5241
5242 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5243
5244 if (ExtractedFromVec == VecIn1)
5245 Mask[i] = Idx;
5246 else if (ExtractedFromVec == VecIn2)
5247 Mask[i] = Idx + NumElems;
5248 }
5249
5250 if (VecIn1.getNode() == 0)
5251 return SDValue();
5252
5253 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5254 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5255 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5256 unsigned Idx = InsertIndices[i];
5257 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5258 DAG.getIntPtrConstant(Idx));
5259 }
5260
5261 return NV;
5262}
5263
5264SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005265X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005266 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005267
David Greenef125a292011-02-08 19:04:41 +00005268 EVT VT = Op.getValueType();
5269 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005270 unsigned NumElems = Op.getNumOperands();
5271
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005272 // Vectors containing all zeros can be matched by pxor and xorps later
5273 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5274 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5275 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005276 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005277 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005278
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005279 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005280 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005282 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005283 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5284 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005285 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005286 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005287 return Op;
5288
Craig Topper07a27622012-01-22 03:07:48 +00005289 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005290 }
5291
Nadav Rotem154819d2012-04-09 07:45:58 +00005292 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005293 if (Broadcast.getNode())
5294 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005295
Owen Andersone50ed302009-08-10 22:56:29 +00005296 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005297
Evan Cheng0db9fe62006-04-25 20:13:52 +00005298 unsigned NumZero = 0;
5299 unsigned NumNonZero = 0;
5300 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005301 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005302 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005303 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005304 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005305 if (Elt.getOpcode() == ISD::UNDEF)
5306 continue;
5307 Values.insert(Elt);
5308 if (Elt.getOpcode() != ISD::Constant &&
5309 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005310 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005311 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005312 NumZero++;
5313 else {
5314 NonZeros |= (1 << i);
5315 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005316 }
5317 }
5318
Chris Lattner97a2a562010-08-26 05:24:29 +00005319 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5320 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005321 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005322
Chris Lattner67f453a2008-03-09 05:42:06 +00005323 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005324 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005325 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005326 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005327
Chris Lattner62098042008-03-09 01:05:04 +00005328 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5329 // the value are obviously zero, truncate the value to i32 and do the
5330 // insertion that way. Only do this if the value is non-constant or if the
5331 // value is a constant being inserted into element 0. It is cheaper to do
5332 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005333 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005334 (!IsAllConstants || Idx == 0)) {
5335 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005336 // Handle SSE only.
5337 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5338 EVT VecVT = MVT::v4i32;
5339 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005340
Chris Lattner62098042008-03-09 01:05:04 +00005341 // Truncate the value (which may itself be a constant) to i32, and
5342 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005343 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005344 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005345 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005346
Chris Lattner62098042008-03-09 01:05:04 +00005347 // Now we have our 32-bit value zero extended in the low element of
5348 // a vector. If Idx != 0, swizzle it into place.
5349 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005350 SmallVector<int, 4> Mask;
5351 Mask.push_back(Idx);
5352 for (unsigned i = 1; i != VecElts; ++i)
5353 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005354 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005355 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005356 }
Craig Topper07a27622012-01-22 03:07:48 +00005357 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005358 }
5359 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005360
Chris Lattner19f79692008-03-08 22:59:52 +00005361 // If we have a constant or non-constant insertion into the low element of
5362 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5363 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005364 // depending on what the source datatype is.
5365 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005366 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005367 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005368
5369 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005370 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005371 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005372 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005373 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5374 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005375 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005376 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005377 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5378 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005379 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005380 }
5381
5382 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005383 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005384 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005385 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005386 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005387 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005388 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005389 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005390 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005391 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005392 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005393 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005394 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005395
5396 // Is it a vector logical left shift?
5397 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005398 X86::isZeroNode(Op.getOperand(0)) &&
5399 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005400 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005401 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005402 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005403 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005404 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005406
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005407 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005408 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005409
Chris Lattner19f79692008-03-08 22:59:52 +00005410 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5411 // is a non-constant being inserted into an element other than the low one,
5412 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5413 // movd/movss) to move this into the low element, then shuffle it into
5414 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005415 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005416 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005417
Evan Cheng0db9fe62006-04-25 20:13:52 +00005418 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005419 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005421 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005422 MaskVec.push_back(i == Idx ? 0 : 1);
5423 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005424 }
5425 }
5426
Chris Lattner67f453a2008-03-09 05:42:06 +00005427 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005428 if (Values.size() == 1) {
5429 if (EVTBits == 32) {
5430 // Instead of a shuffle like this:
5431 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5432 // Check if it's possible to issue this instead.
5433 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5434 unsigned Idx = CountTrailingZeros_32(NonZeros);
5435 SDValue Item = Op.getOperand(Idx);
5436 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5437 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5438 }
Dan Gohman475871a2008-07-27 21:46:04 +00005439 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005440 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005441
Dan Gohmana3941172007-07-24 22:55:08 +00005442 // A vector full of immediates; various special cases are already
5443 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005444 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005445 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005446
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005447 // For AVX-length vectors, build the individual 128-bit pieces and use
5448 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005449 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005450 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005451 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005452 V.push_back(Op.getOperand(i));
5453
5454 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5455
5456 // Build both the lower and upper subvector.
5457 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5458 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5459 NumElems/2);
5460
5461 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005462 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005463 }
5464
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005465 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005466 if (EVTBits == 64) {
5467 if (NumNonZero == 1) {
5468 // One half is zero or undef.
5469 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005470 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005471 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005472 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005473 }
Dan Gohman475871a2008-07-27 21:46:04 +00005474 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005475 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005476
5477 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005478 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005479 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005480 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005481 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005482 }
5483
Bill Wendling826f36f2007-03-28 00:57:11 +00005484 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005485 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005486 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005487 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005488 }
5489
5490 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005491 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492 if (NumElems == 4 && NumZero > 0) {
5493 for (unsigned i = 0; i < 4; ++i) {
5494 bool isZero = !(NonZeros & (1 << i));
5495 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005496 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005497 else
Dale Johannesenace16102009-02-03 19:33:06 +00005498 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499 }
5500
5501 for (unsigned i = 0; i < 2; ++i) {
5502 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5503 default: break;
5504 case 0:
5505 V[i] = V[i*2]; // Must be a zero vector.
5506 break;
5507 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005508 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005509 break;
5510 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005511 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005512 break;
5513 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005514 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005515 break;
5516 }
5517 }
5518
Benjamin Kramer9c683542012-01-30 15:16:21 +00005519 bool Reverse1 = (NonZeros & 0x3) == 2;
5520 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5521 int MaskVec[] = {
5522 Reverse1 ? 1 : 0,
5523 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005524 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5525 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005526 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005527 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005528 }
5529
Craig Topper7a9a28b2012-08-12 02:23:29 +00005530 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005531 // Check for a build vector of consecutive loads.
5532 for (unsigned i = 0; i < NumElems; ++i)
5533 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005534
Nate Begemanfdea31a2010-03-24 20:49:50 +00005535 // Check for elements which are consecutive loads.
5536 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5537 if (LD.getNode())
5538 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005539
Michael Liaofacace82012-10-19 17:15:18 +00005540 // Check for a build vector from mostly shuffle plus few inserting.
5541 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5542 if (Sh.getNode())
5543 return Sh;
5544
Michael J. Spencerec38de22010-10-10 22:04:20 +00005545 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005546 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005547 SDValue Result;
5548 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5549 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5550 else
5551 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005552
Chris Lattner24faf612010-08-28 17:59:08 +00005553 for (unsigned i = 1; i < NumElems; ++i) {
5554 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5555 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005556 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005557 }
5558 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005559 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005560
Chris Lattner6e80e442010-08-28 17:15:43 +00005561 // Otherwise, expand into a number of unpckl*, start by extending each of
5562 // our (non-undef) elements to the full vector width with the element in the
5563 // bottom slot of the vector (which generates no code for SSE).
5564 for (unsigned i = 0; i < NumElems; ++i) {
5565 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5566 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5567 else
5568 V[i] = DAG.getUNDEF(VT);
5569 }
5570
5571 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005572 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5573 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5574 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005575 unsigned EltStride = NumElems >> 1;
5576 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005577 for (unsigned i = 0; i < EltStride; ++i) {
5578 // If V[i+EltStride] is undef and this is the first round of mixing,
5579 // then it is safe to just drop this shuffle: V[i] is already in the
5580 // right place, the one element (since it's the first round) being
5581 // inserted as undef can be dropped. This isn't safe for successive
5582 // rounds because they will permute elements within both vectors.
5583 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5584 EltStride == NumElems/2)
5585 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005586
Chris Lattner6e80e442010-08-28 17:15:43 +00005587 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005588 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005589 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005590 }
5591 return V[0];
5592 }
Dan Gohman475871a2008-07-27 21:46:04 +00005593 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005594}
5595
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005596// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5597// to create 256-bit vectors from two other 128-bit ones.
5598static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5599 DebugLoc dl = Op.getDebugLoc();
5600 EVT ResVT = Op.getValueType();
5601
Craig Topper7a9a28b2012-08-12 02:23:29 +00005602 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005603
5604 SDValue V1 = Op.getOperand(0);
5605 SDValue V2 = Op.getOperand(1);
5606 unsigned NumElems = ResVT.getVectorNumElements();
5607
Craig Topper4c7972d2012-04-22 18:15:59 +00005608 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005609}
5610
Craig Topper55b24052012-09-11 06:15:32 +00005611static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005612 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005613
5614 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5615 // from two other 128-bit ones.
5616 return LowerAVXCONCAT_VECTORS(Op, DAG);
5617}
5618
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005619// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005620static SDValue
5621LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5622 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005623 SDValue V1 = SVOp->getOperand(0);
5624 SDValue V2 = SVOp->getOperand(1);
5625 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005626 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005627 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005628
Nadav Roteme6113782012-04-11 06:40:27 +00005629 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005630 return SDValue();
5631
Craig Topper1842ba02012-04-23 06:38:28 +00005632 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005633 MVT OpTy;
5634
Craig Topper708e44f2012-04-23 07:36:33 +00005635 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005636 default: return SDValue();
5637 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005638 ISDNo = X86ISD::BLENDPW;
5639 OpTy = MVT::v8i16;
5640 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005641 case MVT::v4i32:
5642 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005643 ISDNo = X86ISD::BLENDPS;
5644 OpTy = MVT::v4f32;
5645 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005646 case MVT::v2i64:
5647 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005648 ISDNo = X86ISD::BLENDPD;
5649 OpTy = MVT::v2f64;
5650 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005651 case MVT::v8i32:
5652 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005653 if (!Subtarget->hasAVX())
5654 return SDValue();
5655 ISDNo = X86ISD::BLENDPS;
5656 OpTy = MVT::v8f32;
5657 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005658 case MVT::v4i64:
5659 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005660 if (!Subtarget->hasAVX())
5661 return SDValue();
5662 ISDNo = X86ISD::BLENDPD;
5663 OpTy = MVT::v4f64;
5664 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005665 }
5666 assert(ISDNo && "Invalid Op Number");
5667
5668 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005669
Craig Topper1842ba02012-04-23 06:38:28 +00005670 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005671 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005672 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005673 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005674 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005675 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005676 else
5677 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005678 }
5679
Nadav Roteme6113782012-04-11 06:40:27 +00005680 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5681 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5682 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5683 DAG.getConstant(MaskVals, MVT::i32));
5684 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005685}
5686
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687// v8i16 shuffles - Prefer shuffles in the following order:
5688// 1. [all] pshuflw, pshufhw, optional move
5689// 2. [ssse3] 1 x pshufb
5690// 3. [ssse3] 2 x pshufb + 1 x por
5691// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005692static SDValue
5693LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5694 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005695 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005696 SDValue V1 = SVOp->getOperand(0);
5697 SDValue V2 = SVOp->getOperand(1);
5698 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005700
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 // Determine if more than 1 of the words in each of the low and high quadwords
5702 // of the result come from the same quadword of one of the two inputs. Undef
5703 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005704 unsigned LoQuad[] = { 0, 0, 0, 0 };
5705 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005706 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005708 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005709 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005710 MaskVals.push_back(EltIdx);
5711 if (EltIdx < 0) {
5712 ++Quad[0];
5713 ++Quad[1];
5714 ++Quad[2];
5715 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005716 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 }
5718 ++Quad[EltIdx / 4];
5719 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005720 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005723 unsigned MaxQuad = 1;
5724 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 if (LoQuad[i] > MaxQuad) {
5726 BestLoQuad = i;
5727 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005728 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005729 }
5730
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005732 MaxQuad = 1;
5733 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 if (HiQuad[i] > MaxQuad) {
5735 BestHiQuad = i;
5736 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005737 }
5738 }
5739
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005741 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 // single pshufb instruction is necessary. If There are more than 2 input
5743 // quads, disable the next transformation since it does not help SSSE3.
5744 bool V1Used = InputQuads[0] || InputQuads[1];
5745 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005746 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005748 BestLoQuad = InputQuads[0] ? 0 : 1;
5749 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 }
5751 if (InputQuads.count() > 2) {
5752 BestLoQuad = -1;
5753 BestHiQuad = -1;
5754 }
5755 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005756
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5758 // the shuffle mask. If a quad is scored as -1, that means that it contains
5759 // words from all 4 input quadwords.
5760 SDValue NewV;
5761 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005762 int MaskV[] = {
5763 BestLoQuad < 0 ? 0 : BestLoQuad,
5764 BestHiQuad < 0 ? 1 : BestHiQuad
5765 };
Eric Christopherfd179292009-08-27 18:07:15 +00005766 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005767 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5768 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5769 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005770
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5772 // source words for the shuffle, to aid later transformations.
5773 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005774 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005775 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005777 if (idx != (int)i)
5778 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005780 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 AllWordsInNewV = false;
5782 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005783 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005784
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5786 if (AllWordsInNewV) {
5787 for (int i = 0; i != 8; ++i) {
5788 int idx = MaskVals[i];
5789 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005790 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005791 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 if ((idx != i) && idx < 4)
5793 pshufhw = false;
5794 if ((idx != i) && idx > 3)
5795 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005796 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 V1 = NewV;
5798 V2Used = false;
5799 BestLoQuad = 0;
5800 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005801 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005802
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5804 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005805 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005806 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5807 unsigned TargetMask = 0;
5808 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005810 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5811 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5812 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005813 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005814 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005815 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005816 }
Eric Christopherfd179292009-08-27 18:07:15 +00005817
Nate Begemanb9a47b82009-02-23 08:49:38 +00005818 // If we have SSSE3, and all words of the result are from 1 input vector,
5819 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5820 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005821 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005823
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005825 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 // mask, and elements that come from V1 in the V2 mask, so that the two
5827 // results can be OR'd together.
5828 bool TwoInputs = V1Used && V2Used;
5829 for (unsigned i = 0; i != 8; ++i) {
5830 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005831 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5832 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5833 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5834 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005836 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005837 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005838 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005839 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005841 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005842
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 // Calculate the shuffle mask for the second input, shuffle it, and
5844 // OR it with the first shuffled input.
5845 pshufbMask.clear();
5846 for (unsigned i = 0; i != 8; ++i) {
5847 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005848 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5849 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5850 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5851 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005852 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005853 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005854 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005855 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005856 MVT::v16i8, &pshufbMask[0], 16));
5857 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005858 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 }
5860
5861 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5862 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005863 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005864 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005865 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005866 for (int i = 0; i != 4; ++i) {
5867 int idx = MaskVals[i];
5868 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005869 InOrder.set(i);
5870 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005871 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 }
5874 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005876 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005877
Craig Topperdd637ae2012-02-19 05:41:45 +00005878 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5879 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005880 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005881 NewV.getOperand(0),
5882 getShufflePSHUFLWImmediate(SVOp), DAG);
5883 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 }
Eric Christopherfd179292009-08-27 18:07:15 +00005885
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5887 // and update MaskVals with the new element order.
5888 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005889 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005890 for (unsigned i = 4; i != 8; ++i) {
5891 int idx = MaskVals[i];
5892 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 InOrder.set(i);
5894 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005895 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 }
5898 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005900 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005901
Craig Topperdd637ae2012-02-19 05:41:45 +00005902 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5903 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005904 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005905 NewV.getOperand(0),
5906 getShufflePSHUFHWImmediate(SVOp), DAG);
5907 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 }
Eric Christopherfd179292009-08-27 18:07:15 +00005909
Nate Begemanb9a47b82009-02-23 08:49:38 +00005910 // In case BestHi & BestLo were both -1, which means each quadword has a word
5911 // from each of the four input quadwords, calculate the InOrder bitvector now
5912 // before falling through to the insert/extract cleanup.
5913 if (BestLoQuad == -1 && BestHiQuad == -1) {
5914 NewV = V1;
5915 for (int i = 0; i != 8; ++i)
5916 if (MaskVals[i] < 0 || MaskVals[i] == i)
5917 InOrder.set(i);
5918 }
Eric Christopherfd179292009-08-27 18:07:15 +00005919
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 // The other elements are put in the right place using pextrw and pinsrw.
5921 for (unsigned i = 0; i != 8; ++i) {
5922 if (InOrder[i])
5923 continue;
5924 int EltIdx = MaskVals[i];
5925 if (EltIdx < 0)
5926 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005927 SDValue ExtOp = (EltIdx < 8) ?
5928 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5929 DAG.getIntPtrConstant(EltIdx)) :
5930 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005931 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005933 DAG.getIntPtrConstant(i));
5934 }
5935 return NewV;
5936}
5937
5938// v16i8 shuffles - Prefer shuffles in the following order:
5939// 1. [ssse3] 1 x pshufb
5940// 2. [ssse3] 2 x pshufb + 1 x por
5941// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5942static
Nate Begeman9008ca62009-04-27 18:41:29 +00005943SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005944 SelectionDAG &DAG,
5945 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005946 SDValue V1 = SVOp->getOperand(0);
5947 SDValue V2 = SVOp->getOperand(1);
5948 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005949 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005950
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005952 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005954
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005956 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005958
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005960 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005961 //
5962 // Otherwise, we have elements from both input vectors, and must zero out
5963 // elements that come from V2 in the first mask, and V1 in the second mask
5964 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005965 for (unsigned i = 0; i != 16; ++i) {
5966 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005967 if (EltIdx < 0 || EltIdx >= 16)
5968 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005969 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005971 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005972 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005973 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005974
5975 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5976 // the 2nd operand if it's undefined or zero.
5977 if (V2.getOpcode() == ISD::UNDEF ||
5978 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005980
Nate Begemanb9a47b82009-02-23 08:49:38 +00005981 // Calculate the shuffle mask for the second input, shuffle it, and
5982 // OR it with the first shuffled input.
5983 pshufbMask.clear();
5984 for (unsigned i = 0; i != 16; ++i) {
5985 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005986 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005987 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005988 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005990 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005991 MVT::v16i8, &pshufbMask[0], 16));
5992 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 }
Eric Christopherfd179292009-08-27 18:07:15 +00005994
Nate Begemanb9a47b82009-02-23 08:49:38 +00005995 // No SSSE3 - Calculate in place words and then fix all out of place words
5996 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5997 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005998 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5999 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006000 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006001 for (int i = 0; i != 8; ++i) {
6002 int Elt0 = MaskVals[i*2];
6003 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006004
Nate Begemanb9a47b82009-02-23 08:49:38 +00006005 // This word of the result is all undef, skip it.
6006 if (Elt0 < 0 && Elt1 < 0)
6007 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006008
Nate Begemanb9a47b82009-02-23 08:49:38 +00006009 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006010 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006011 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006012
Nate Begemanb9a47b82009-02-23 08:49:38 +00006013 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6014 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6015 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006016
6017 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6018 // using a single extract together, load it and store it.
6019 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006020 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006021 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006023 DAG.getIntPtrConstant(i));
6024 continue;
6025 }
6026
Nate Begemanb9a47b82009-02-23 08:49:38 +00006027 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006028 // source byte is not also odd, shift the extracted word left 8 bits
6029 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006030 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006031 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006032 DAG.getIntPtrConstant(Elt1 / 2));
6033 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006035 DAG.getConstant(8,
6036 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006037 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006038 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6039 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006040 }
6041 // If Elt0 is defined, extract it from the appropriate source. If the
6042 // source byte is not also even, shift the extracted word right 8 bits. If
6043 // Elt1 was also defined, OR the extracted values together before
6044 // inserting them in the result.
6045 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006046 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006047 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6048 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006049 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006050 DAG.getConstant(8,
6051 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006052 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006053 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6054 DAG.getConstant(0x00FF, MVT::i16));
6055 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006056 : InsElt0;
6057 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006058 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006059 DAG.getIntPtrConstant(i));
6060 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006061 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006062}
6063
Elena Demikhovsky41789462012-09-06 12:42:01 +00006064// v32i8 shuffles - Translate to VPSHUFB if possible.
6065static
6066SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006067 const X86Subtarget *Subtarget,
6068 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006069 EVT VT = SVOp->getValueType(0);
6070 SDValue V1 = SVOp->getOperand(0);
6071 SDValue V2 = SVOp->getOperand(1);
6072 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006073 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006074
6075 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006076 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6077 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006078
Michael Liao471b9172012-10-03 23:43:52 +00006079 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006080 // (1) one of input vector is undefined or zeroinitializer.
6081 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6082 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006083 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006084 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006085 return SDValue();
6086
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006087 if (V1IsAllZero && !V2IsAllZero) {
6088 CommuteVectorShuffleMask(MaskVals, 32);
6089 V1 = V2;
6090 }
6091 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006092 for (unsigned i = 0; i != 32; i++) {
6093 int EltIdx = MaskVals[i];
6094 if (EltIdx < 0 || EltIdx >= 32)
6095 EltIdx = 0x80;
6096 else {
6097 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6098 // Cross lane is not allowed.
6099 return SDValue();
6100 EltIdx &= 0xf;
6101 }
6102 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6103 }
6104 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6105 DAG.getNode(ISD::BUILD_VECTOR, dl,
6106 MVT::v32i8, &pshufbMask[0], 32));
6107}
6108
Evan Cheng7a831ce2007-12-15 03:00:47 +00006109/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006110/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006111/// done when every pair / quad of shuffle mask elements point to elements in
6112/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006113/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006114static
Nate Begeman9008ca62009-04-27 18:41:29 +00006115SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006116 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006117 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006119 MVT NewVT;
6120 unsigned Scale;
6121 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006122 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006123 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6124 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6125 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6126 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6127 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6128 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006129 }
6130
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006132 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006133 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006134 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006135 int EltIdx = SVOp->getMaskElt(i+j);
6136 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006137 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006138 if (StartIdx < 0)
6139 StartIdx = (EltIdx / Scale);
6140 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006141 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006142 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006143 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006144 }
6145
Craig Topper11ac1f82012-05-04 04:08:44 +00006146 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6147 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006148 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006149}
6150
Evan Chengd880b972008-05-09 21:53:03 +00006151/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006152///
Owen Andersone50ed302009-08-10 22:56:29 +00006153static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006154 SDValue SrcOp, SelectionDAG &DAG,
6155 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006156 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006157 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006158 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006159 LD = dyn_cast<LoadSDNode>(SrcOp);
6160 if (!LD) {
6161 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6162 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006163 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006164 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006165 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006166 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006167 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006168 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006169 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006170 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006171 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6172 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6173 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006174 SrcOp.getOperand(0)
6175 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006176 }
6177 }
6178 }
6179
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006180 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006181 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006182 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006183 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006184}
6185
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006186/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6187/// which could not be matched by any known target speficic shuffle
6188static SDValue
6189LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006190
6191 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6192 if (NewOp.getNode())
6193 return NewOp;
6194
Craig Topper8f35c132012-01-20 09:29:03 +00006195 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006196
Craig Topper8f35c132012-01-20 09:29:03 +00006197 unsigned NumElems = VT.getVectorNumElements();
6198 unsigned NumLaneElems = NumElems / 2;
6199
Craig Topper8f35c132012-01-20 09:29:03 +00006200 DebugLoc dl = SVOp->getDebugLoc();
6201 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006202 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006203 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006204
Craig Topper9a2b6e12012-04-06 07:45:23 +00006205 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006206 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006207 // Build a shuffle mask for the output, discovering on the fly which
6208 // input vectors to use as shuffle operands (recorded in InputUsed).
6209 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006210 // out with UseBuildVector set.
6211 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006212 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006213 unsigned LaneStart = l * NumLaneElems;
6214 for (unsigned i = 0; i != NumLaneElems; ++i) {
6215 // The mask element. This indexes into the input.
6216 int Idx = SVOp->getMaskElt(i+LaneStart);
6217 if (Idx < 0) {
6218 // the mask element does not index into any input vector.
6219 Mask.push_back(-1);
6220 continue;
6221 }
Craig Topper8f35c132012-01-20 09:29:03 +00006222
Craig Topper9a2b6e12012-04-06 07:45:23 +00006223 // The input vector this mask element indexes into.
6224 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006225
Craig Topper9a2b6e12012-04-06 07:45:23 +00006226 // Turn the index into an offset from the start of the input vector.
6227 Idx -= Input * NumLaneElems;
6228
6229 // Find or create a shuffle vector operand to hold this input.
6230 unsigned OpNo;
6231 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6232 if (InputUsed[OpNo] == Input)
6233 // This input vector is already an operand.
6234 break;
6235 if (InputUsed[OpNo] < 0) {
6236 // Create a new operand for this input vector.
6237 InputUsed[OpNo] = Input;
6238 break;
6239 }
6240 }
6241
6242 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006243 // More than two input vectors used! Give up on trying to create a
6244 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6245 UseBuildVector = true;
6246 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006247 }
6248
6249 // Add the mask index for the new shuffle vector.
6250 Mask.push_back(Idx + OpNo * NumLaneElems);
6251 }
6252
Craig Topper8ae97ba2012-05-21 06:40:16 +00006253 if (UseBuildVector) {
6254 SmallVector<SDValue, 16> SVOps;
6255 for (unsigned i = 0; i != NumLaneElems; ++i) {
6256 // The mask element. This indexes into the input.
6257 int Idx = SVOp->getMaskElt(i+LaneStart);
6258 if (Idx < 0) {
6259 SVOps.push_back(DAG.getUNDEF(EltVT));
6260 continue;
6261 }
6262
6263 // The input vector this mask element indexes into.
6264 int Input = Idx / NumElems;
6265
6266 // Turn the index into an offset from the start of the input vector.
6267 Idx -= Input * NumElems;
6268
6269 // Extract the vector element by hand.
6270 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6271 SVOp->getOperand(Input),
6272 DAG.getIntPtrConstant(Idx)));
6273 }
6274
6275 // Construct the output using a BUILD_VECTOR.
6276 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6277 SVOps.size());
6278 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006279 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006280 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006281 } else {
6282 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006283 (InputUsed[0] % 2) * NumLaneElems,
6284 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006285 // If only one input was used, use an undefined vector for the other.
6286 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6287 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006288 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006289 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006290 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006291 }
6292
6293 Mask.clear();
6294 }
Craig Topper8f35c132012-01-20 09:29:03 +00006295
6296 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006297 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006298}
6299
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006300/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6301/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006302static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006303LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006304 SDValue V1 = SVOp->getOperand(0);
6305 SDValue V2 = SVOp->getOperand(1);
6306 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006307 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006308
Craig Topper7a9a28b2012-08-12 02:23:29 +00006309 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006310
Benjamin Kramer9c683542012-01-30 15:16:21 +00006311 std::pair<int, int> Locs[4];
6312 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006313 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006314
Evan Chengace3c172008-07-22 21:13:36 +00006315 unsigned NumHi = 0;
6316 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006317 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006318 int Idx = PermMask[i];
6319 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006320 Locs[i] = std::make_pair(-1, -1);
6321 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006322 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6323 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006324 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006325 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006326 NumLo++;
6327 } else {
6328 Locs[i] = std::make_pair(1, NumHi);
6329 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006330 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006331 NumHi++;
6332 }
6333 }
6334 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006335
Evan Chengace3c172008-07-22 21:13:36 +00006336 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006337 // If no more than two elements come from either vector. This can be
6338 // implemented with two shuffles. First shuffle gather the elements.
6339 // The second shuffle, which takes the first shuffle as both of its
6340 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006341 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006342
Benjamin Kramer9c683542012-01-30 15:16:21 +00006343 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006344
Benjamin Kramer9c683542012-01-30 15:16:21 +00006345 for (unsigned i = 0; i != 4; ++i)
6346 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006347 unsigned Idx = (i < 2) ? 0 : 4;
6348 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006349 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006350 }
Evan Chengace3c172008-07-22 21:13:36 +00006351
Nate Begeman9008ca62009-04-27 18:41:29 +00006352 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006353 }
6354
6355 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006356 // Otherwise, we must have three elements from one vector, call it X, and
6357 // one element from the other, call it Y. First, use a shufps to build an
6358 // intermediate vector with the one element from Y and the element from X
6359 // that will be in the same half in the final destination (the indexes don't
6360 // matter). Then, use a shufps to build the final vector, taking the half
6361 // containing the element from Y from the intermediate, and the other half
6362 // from X.
6363 if (NumHi == 3) {
6364 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006365 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006366 std::swap(V1, V2);
6367 }
6368
6369 // Find the element from V2.
6370 unsigned HiIndex;
6371 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006372 int Val = PermMask[HiIndex];
6373 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006374 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006375 if (Val >= 4)
6376 break;
6377 }
6378
Nate Begeman9008ca62009-04-27 18:41:29 +00006379 Mask1[0] = PermMask[HiIndex];
6380 Mask1[1] = -1;
6381 Mask1[2] = PermMask[HiIndex^1];
6382 Mask1[3] = -1;
6383 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006384
6385 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006386 Mask1[0] = PermMask[0];
6387 Mask1[1] = PermMask[1];
6388 Mask1[2] = HiIndex & 1 ? 6 : 4;
6389 Mask1[3] = HiIndex & 1 ? 4 : 6;
6390 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006391 }
Craig Topper69947b92012-04-23 06:57:04 +00006392
6393 Mask1[0] = HiIndex & 1 ? 2 : 0;
6394 Mask1[1] = HiIndex & 1 ? 0 : 2;
6395 Mask1[2] = PermMask[2];
6396 Mask1[3] = PermMask[3];
6397 if (Mask1[2] >= 0)
6398 Mask1[2] += 4;
6399 if (Mask1[3] >= 0)
6400 Mask1[3] += 4;
6401 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006402 }
6403
6404 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006405 int LoMask[] = { -1, -1, -1, -1 };
6406 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006407
Benjamin Kramer9c683542012-01-30 15:16:21 +00006408 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006409 unsigned MaskIdx = 0;
6410 unsigned LoIdx = 0;
6411 unsigned HiIdx = 2;
6412 for (unsigned i = 0; i != 4; ++i) {
6413 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006414 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006415 MaskIdx = 1;
6416 LoIdx = 0;
6417 HiIdx = 2;
6418 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006419 int Idx = PermMask[i];
6420 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006421 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006422 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006423 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006424 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006425 LoIdx++;
6426 } else {
6427 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006428 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006429 HiIdx++;
6430 }
6431 }
6432
Nate Begeman9008ca62009-04-27 18:41:29 +00006433 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6434 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006435 int MaskOps[] = { -1, -1, -1, -1 };
6436 for (unsigned i = 0; i != 4; ++i)
6437 if (Locs[i].first != -1)
6438 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006439 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006440}
6441
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006442static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006443 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006444 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006445
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006446 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6447 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006448 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6449 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6450 // BUILD_VECTOR (load), undef
6451 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006452
6453 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006454}
6455
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006456// FIXME: the version above should always be used. Since there's
6457// a bug where several vector shuffles can't be folded because the
6458// DAG is not updated during lowering and a node claims to have two
6459// uses while it only has one, use this version, and let isel match
6460// another instruction if the load really happens to have more than
6461// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006462// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006463static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006464 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006465 V = V.getOperand(0);
6466 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6467 V = V.getOperand(0);
6468 if (ISD::isNormalLoad(V.getNode()))
6469 return true;
6470 return false;
6471}
6472
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006473static
Evan Cheng835580f2010-10-07 20:50:20 +00006474SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6475 EVT VT = Op.getValueType();
6476
6477 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006478 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6479 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006480 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6481 V1, DAG));
6482}
6483
6484static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006485SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006486 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006487 SDValue V1 = Op.getOperand(0);
6488 SDValue V2 = Op.getOperand(1);
6489 EVT VT = Op.getValueType();
6490
6491 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6492
Craig Topper1accb7e2012-01-10 06:54:16 +00006493 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006494 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6495
Evan Cheng0899f5c2011-08-31 02:05:24 +00006496 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6497 return DAG.getNode(ISD::BITCAST, dl, VT,
6498 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6499 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6500 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006501}
6502
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006503static
6504SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6505 SDValue V1 = Op.getOperand(0);
6506 SDValue V2 = Op.getOperand(1);
6507 EVT VT = Op.getValueType();
6508
6509 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6510 "unsupported shuffle type");
6511
6512 if (V2.getOpcode() == ISD::UNDEF)
6513 V2 = V1;
6514
6515 // v4i32 or v4f32
6516 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6517}
6518
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006519static
Craig Topper1accb7e2012-01-10 06:54:16 +00006520SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006521 SDValue V1 = Op.getOperand(0);
6522 SDValue V2 = Op.getOperand(1);
6523 EVT VT = Op.getValueType();
6524 unsigned NumElems = VT.getVectorNumElements();
6525
6526 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6527 // operand of these instructions is only memory, so check if there's a
6528 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6529 // same masks.
6530 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006531
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006532 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006533 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006534 CanFoldLoad = true;
6535
6536 // When V1 is a load, it can be folded later into a store in isel, example:
6537 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6538 // turns into:
6539 // (MOVLPSmr addr:$src1, VR128:$src2)
6540 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006541 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006542 CanFoldLoad = true;
6543
Dan Gohman65fd6562011-11-03 21:49:52 +00006544 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006545 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006546 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006547 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6548
6549 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006550 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006551 if (SVOp->getMaskElt(1) != -1)
6552 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006553 }
6554
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006555 // movl and movlp will both match v2i64, but v2i64 is never matched by
6556 // movl earlier because we make it strict to avoid messing with the movlp load
6557 // folding logic (see the code above getMOVLP call). Match it here then,
6558 // this is horrible, but will stay like this until we move all shuffle
6559 // matching to x86 specific nodes. Note that for the 1st condition all
6560 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006561 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006562 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6563 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006564 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006565 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006566 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006567 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006568
6569 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6570
6571 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006572 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006573 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006574}
6575
Michael Liaod9d09602012-10-23 17:34:00 +00006576// Reduce a vector shuffle to zext.
6577SDValue
6578X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6579 // PMOVZX is only available from SSE41.
6580 if (!Subtarget->hasSSE41())
6581 return SDValue();
6582
6583 EVT VT = Op.getValueType();
6584
6585 // Only AVX2 support 256-bit vector integer extending.
6586 if (!Subtarget->hasAVX2() && VT.is256BitVector())
6587 return SDValue();
6588
6589 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6590 DebugLoc DL = Op.getDebugLoc();
6591 SDValue V1 = Op.getOperand(0);
6592 SDValue V2 = Op.getOperand(1);
6593 unsigned NumElems = VT.getVectorNumElements();
6594
6595 // Extending is an unary operation and the element type of the source vector
6596 // won't be equal to or larger than i64.
6597 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6598 VT.getVectorElementType() == MVT::i64)
6599 return SDValue();
6600
6601 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6602 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006603 while ((1U << Shift) < NumElems) {
6604 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006605 break;
6606 Shift += 1;
6607 // The maximal ratio is 8, i.e. from i8 to i64.
6608 if (Shift > 3)
6609 return SDValue();
6610 }
6611
6612 // Check the shuffle mask.
6613 unsigned Mask = (1U << Shift) - 1;
6614 for (unsigned i = 0; i != NumElems; ++i) {
6615 int EltIdx = SVOp->getMaskElt(i);
6616 if ((i & Mask) != 0 && EltIdx != -1)
6617 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006618 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006619 return SDValue();
6620 }
6621
6622 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6623 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6624 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6625
6626 if (!isTypeLegal(NVT))
6627 return SDValue();
6628
6629 // Simplify the operand as it's prepared to be fed into shuffle.
6630 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6631 if (V1.getOpcode() == ISD::BITCAST &&
6632 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6633 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6634 V1.getOperand(0)
6635 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6636 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6637 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006638 ConstantSDNode *CIdx =
6639 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006640 // If it's foldable, i.e. normal load with single use, we will let code
6641 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006642 if (CIdx && CIdx->getZExtValue() == 0 &&
6643 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006644 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6645 }
6646
6647 return DAG.getNode(ISD::BITCAST, DL, VT,
6648 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6649}
6650
Nadav Rotem154819d2012-04-09 07:45:58 +00006651SDValue
6652X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006653 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6654 EVT VT = Op.getValueType();
6655 DebugLoc dl = Op.getDebugLoc();
6656 SDValue V1 = Op.getOperand(0);
6657 SDValue V2 = Op.getOperand(1);
6658
6659 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006660 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006661
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006662 // Handle splat operations
6663 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006664 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006665 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006666
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006667 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006668 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006669 if (Broadcast.getNode())
6670 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006671
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006672 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006673 if ((Size == 128 && NumElem <= 4) ||
6674 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006675 return SDValue();
6676
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006677 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006678 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006679 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006680
Michael Liaod9d09602012-10-23 17:34:00 +00006681 // Check integer expanding shuffles.
6682 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6683 if (NewOp.getNode())
6684 return NewOp;
6685
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006686 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6687 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006688 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6689 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006690 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6691 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006692 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006693 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006694 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006695 // FIXME: Figure out a cleaner way to do this.
6696 // Try to make use of movq to zero out the top part.
6697 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6698 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6699 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006700 EVT NewVT = NewOp.getValueType();
6701 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6702 NewVT, true, false))
6703 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006704 DAG, Subtarget, dl);
6705 }
6706 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6707 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006708 if (NewOp.getNode()) {
6709 EVT NewVT = NewOp.getValueType();
6710 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6711 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6712 DAG, Subtarget, dl);
6713 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006714 }
6715 }
6716 return SDValue();
6717}
6718
Dan Gohman475871a2008-07-27 21:46:04 +00006719SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006720X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006721 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006722 SDValue V1 = Op.getOperand(0);
6723 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006724 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006725 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006726 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006727 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006728 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006729 bool V1IsSplat = false;
6730 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006731 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006732 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006733 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006734 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006735 bool OptForSize = MF.getFunction()->getFnAttributes().
6736 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006737
Craig Topper3426a3e2011-11-14 06:46:21 +00006738 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006739
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006740 if (V1IsUndef && V2IsUndef)
6741 return DAG.getUNDEF(VT);
6742
6743 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006744
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006745 // Vector shuffle lowering takes 3 steps:
6746 //
6747 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6748 // narrowing and commutation of operands should be handled.
6749 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6750 // shuffle nodes.
6751 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6752 // so the shuffle can be broken into other shuffles and the legalizer can
6753 // try the lowering again.
6754 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006755 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006756 // be matched during isel, all of them must be converted to a target specific
6757 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006758
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006759 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6760 // narrowing and commutation of operands should be handled. The actual code
6761 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006762 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006763 if (NewOp.getNode())
6764 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006765
Craig Topper5aaffa82012-02-19 02:53:47 +00006766 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6767
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006768 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6769 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006770 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006771 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006772 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006773 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006774
Craig Topperdd637ae2012-02-19 05:41:45 +00006775 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006776 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006777 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006778
Craig Topperdd637ae2012-02-19 05:41:45 +00006779 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006780 return getMOVHighToLow(Op, dl, DAG);
6781
6782 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006783 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006784 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006785 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006786
Craig Topper5aaffa82012-02-19 02:53:47 +00006787 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006788 // The actual implementation will match the mask in the if above and then
6789 // during isel it can match several different instructions, not only pshufd
6790 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006791 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6792 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006793
Craig Topper5aaffa82012-02-19 02:53:47 +00006794 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006795
Craig Topperdbd98a42012-02-07 06:28:42 +00006796 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6797 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6798
Craig Topper1accb7e2012-01-10 06:54:16 +00006799 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006800 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6801
Craig Topperb3982da2011-12-31 23:50:21 +00006802 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006803 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006804 }
Eric Christopherfd179292009-08-27 18:07:15 +00006805
Evan Chengf26ffe92008-05-29 08:22:04 +00006806 // Check if this can be converted into a logical shift.
6807 bool isLeft = false;
6808 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006809 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006810 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006811 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006812 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006813 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006814 EVT EltVT = VT.getVectorElementType();
6815 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006816 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006817 }
Eric Christopherfd179292009-08-27 18:07:15 +00006818
Craig Topper5aaffa82012-02-19 02:53:47 +00006819 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006820 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006821 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006822 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006823 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006824 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6825
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006826 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006827 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6828 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006829 }
Eric Christopherfd179292009-08-27 18:07:15 +00006830
Nate Begeman9008ca62009-04-27 18:41:29 +00006831 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006832 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006833 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006834
Craig Topperdd637ae2012-02-19 05:41:45 +00006835 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006836 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006837
Craig Topperdd637ae2012-02-19 05:41:45 +00006838 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006839 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006840
Craig Topperdd637ae2012-02-19 05:41:45 +00006841 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006842 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006843
Craig Topperdd637ae2012-02-19 05:41:45 +00006844 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006845 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846
Craig Topperdd637ae2012-02-19 05:41:45 +00006847 if (ShouldXformToMOVHLPS(M, VT) ||
6848 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006849 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006850
Evan Chengf26ffe92008-05-29 08:22:04 +00006851 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006852 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006853 EVT EltVT = VT.getVectorElementType();
6854 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006855 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006856 }
Eric Christopherfd179292009-08-27 18:07:15 +00006857
Evan Cheng9eca5e82006-10-25 21:49:50 +00006858 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006859 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6860 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006861 V1IsSplat = isSplatVector(V1.getNode());
6862 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006863
Chris Lattner8a594482007-11-25 00:24:49 +00006864 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006865 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6866 CommuteVectorShuffleMask(M, NumElems);
6867 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006868 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006869 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006870 }
6871
Craig Topperbeabc6c2011-12-05 06:56:46 +00006872 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006873 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006874 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006875 return V1;
6876 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6877 // the instruction selector will not match, so get a canonical MOVL with
6878 // swapped operands to undo the commute.
6879 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006880 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006881
Craig Topperbeabc6c2011-12-05 06:56:46 +00006882 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006883 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006884
Craig Topperbeabc6c2011-12-05 06:56:46 +00006885 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006886 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006887
Evan Cheng9bbbb982006-10-25 20:48:19 +00006888 if (V2IsSplat) {
6889 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006890 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006891 // new vector_shuffle with the corrected mask.p
6892 SmallVector<int, 8> NewMask(M.begin(), M.end());
6893 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006894 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006895 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006896 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006897 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006898 }
6899
Evan Cheng9eca5e82006-10-25 21:49:50 +00006900 if (Commuted) {
6901 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006902 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006903 CommuteVectorShuffleMask(M, NumElems);
6904 std::swap(V1, V2);
6905 std::swap(V1IsSplat, V2IsSplat);
6906 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006907
Craig Topper39a9e482012-02-11 06:24:48 +00006908 if (isUNPCKLMask(M, VT, HasAVX2))
6909 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006910
Craig Topper39a9e482012-02-11 06:24:48 +00006911 if (isUNPCKHMask(M, VT, HasAVX2))
6912 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006913 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006914
Nate Begeman9008ca62009-04-27 18:41:29 +00006915 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006916 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006917 return CommuteVectorShuffle(SVOp, DAG);
6918
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006919 // The checks below are all present in isShuffleMaskLegal, but they are
6920 // inlined here right now to enable us to directly emit target specific
6921 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006922
Craig Topper0e2037b2012-01-20 05:53:00 +00006923 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006924 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006925 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006926 DAG);
6927
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006928 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6929 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006930 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006931 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006932 }
6933
Craig Toppera9a568a2012-05-02 08:03:44 +00006934 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006935 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006936 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006937 DAG);
6938
Craig Toppera9a568a2012-05-02 08:03:44 +00006939 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006940 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006941 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006942 DAG);
6943
Craig Topper1a7700a2012-01-19 08:19:12 +00006944 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006945 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006946 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006947
Craig Topper94438ba2011-12-16 08:06:31 +00006948 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006949 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006950 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006951 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006952
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006953 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006954 // Generate target specific nodes for 128 or 256-bit shuffles only
6955 // supported in the AVX instruction set.
6956 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006957
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006958 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006959 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006960 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6961
Craig Topper70b883b2011-11-28 10:14:51 +00006962 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006963 if (isVPERMILPMask(M, VT, HasAVX)) {
6964 if (HasAVX2 && VT == MVT::v8i32)
6965 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006966 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006967 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006968 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006969 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006970
Craig Topper70b883b2011-11-28 10:14:51 +00006971 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006972 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006973 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006974 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006975
Craig Topper1842ba02012-04-23 06:38:28 +00006976 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006977 if (BlendOp.getNode())
6978 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006979
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006980 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006981 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006982 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006983 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006984 }
Craig Topper92040742012-04-16 06:43:40 +00006985 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6986 &permclMask[0], 8);
6987 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006988 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006989 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006990 }
Craig Topper095c5282012-04-15 23:48:57 +00006991
Craig Topper8325c112012-04-16 00:41:45 +00006992 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6993 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006994 getShuffleCLImmediate(SVOp), DAG);
6995
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006996
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006997 //===--------------------------------------------------------------------===//
6998 // Since no target specific shuffle was selected for this generic one,
6999 // lower it into other known shuffles. FIXME: this isn't true yet, but
7000 // this is the plan.
7001 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007002
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007003 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7004 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007005 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007006 if (NewOp.getNode())
7007 return NewOp;
7008 }
7009
7010 if (VT == MVT::v16i8) {
7011 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7012 if (NewOp.getNode())
7013 return NewOp;
7014 }
7015
Elena Demikhovsky41789462012-09-06 12:42:01 +00007016 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007017 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007018 if (NewOp.getNode())
7019 return NewOp;
7020 }
7021
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007022 // Handle all 128-bit wide vectors with 4 elements, and match them with
7023 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007024 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007025 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7026
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007027 // Handle general 256-bit shuffles
7028 if (VT.is256BitVector())
7029 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7030
Dan Gohman475871a2008-07-27 21:46:04 +00007031 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007032}
7033
Dan Gohman475871a2008-07-27 21:46:04 +00007034SDValue
7035X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007036 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007037 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007038 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007039
Craig Topper7a9a28b2012-08-12 02:23:29 +00007040 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007041 return SDValue();
7042
Duncan Sands83ec4b62008-06-06 12:08:01 +00007043 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007044 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007045 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007047 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007048 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007049 }
7050
7051 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007052 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7053 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7054 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007055 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7056 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007057 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007058 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007059 Op.getOperand(0)),
7060 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007062 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007064 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007065 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007066 }
7067
7068 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007069 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7070 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007071 // result has a single use which is a store or a bitcast to i32. And in
7072 // the case of a store, it's not worth it if the index is a constant 0,
7073 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007074 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007075 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007076 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007077 if ((User->getOpcode() != ISD::STORE ||
7078 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7079 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007080 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007081 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007082 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007083 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007084 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007085 Op.getOperand(0)),
7086 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007087 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007088 }
7089
7090 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007091 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007092 if (isa<ConstantSDNode>(Op.getOperand(1)))
7093 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007094 }
Dan Gohman475871a2008-07-27 21:46:04 +00007095 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007096}
7097
7098
Dan Gohman475871a2008-07-27 21:46:04 +00007099SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007100X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7101 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007102 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007103 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007104
David Greene74a579d2011-02-10 16:57:36 +00007105 SDValue Vec = Op.getOperand(0);
7106 EVT VecVT = Vec.getValueType();
7107
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007108 // If this is a 256-bit vector result, first extract the 128-bit vector and
7109 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007110 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007111 DebugLoc dl = Op.getNode()->getDebugLoc();
7112 unsigned NumElems = VecVT.getVectorNumElements();
7113 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007114 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7115
7116 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007117 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007118
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007119 if (IdxVal >= NumElems/2)
7120 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007121 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007122 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007123 }
7124
Craig Topper7a9a28b2012-08-12 02:23:29 +00007125 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007126
Craig Topperd0a31172012-01-10 06:37:29 +00007127 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007128 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007129 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007130 return Res;
7131 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007132
Owen Andersone50ed302009-08-10 22:56:29 +00007133 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007134 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007135 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007136 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007137 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007138 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007139 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7141 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007142 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007144 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007145 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007146 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007147 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007148 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007149 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007150 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007151 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007152 }
7153
7154 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007155 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007156 if (Idx == 0)
7157 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007158
Evan Cheng0db9fe62006-04-25 20:13:52 +00007159 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007160 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007161 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007162 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007163 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007164 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007165 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007166 }
7167
7168 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007169 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7170 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7171 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007172 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007173 if (Idx == 0)
7174 return Op;
7175
7176 // UNPCKHPD the element to the lowest double word, then movsd.
7177 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7178 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007179 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007180 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007181 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007182 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007183 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007184 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007185 }
7186
Dan Gohman475871a2008-07-27 21:46:04 +00007187 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007188}
7189
Dan Gohman475871a2008-07-27 21:46:04 +00007190SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007191X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7192 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007193 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007194 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007195 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007196
Dan Gohman475871a2008-07-27 21:46:04 +00007197 SDValue N0 = Op.getOperand(0);
7198 SDValue N1 = Op.getOperand(1);
7199 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007200
Craig Topper7a9a28b2012-08-12 02:23:29 +00007201 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007202 return SDValue();
7203
Dan Gohman8a55ce42009-09-23 21:02:20 +00007204 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007205 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007206 unsigned Opc;
7207 if (VT == MVT::v8i16)
7208 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007209 else if (VT == MVT::v16i8)
7210 Opc = X86ISD::PINSRB;
7211 else
7212 Opc = X86ISD::PINSRB;
7213
Nate Begeman14d12ca2008-02-11 04:19:36 +00007214 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7215 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007216 if (N1.getValueType() != MVT::i32)
7217 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7218 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007219 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007220 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007221 }
7222
7223 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007224 // Bits [7:6] of the constant are the source select. This will always be
7225 // zero here. The DAG Combiner may combine an extract_elt index into these
7226 // bits. For example (insert (extract, 3), 2) could be matched by putting
7227 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007228 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007229 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007230 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007231 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007232 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007233 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007234 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007235 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007236 }
7237
7238 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007239 // PINSR* works with constant index.
7240 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007241 }
Dan Gohman475871a2008-07-27 21:46:04 +00007242 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007243}
7244
Dan Gohman475871a2008-07-27 21:46:04 +00007245SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007246X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007247 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007248 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007249
David Greene6b381262011-02-09 15:32:06 +00007250 DebugLoc dl = Op.getDebugLoc();
7251 SDValue N0 = Op.getOperand(0);
7252 SDValue N1 = Op.getOperand(1);
7253 SDValue N2 = Op.getOperand(2);
7254
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007255 // If this is a 256-bit vector result, first extract the 128-bit vector,
7256 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007257 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007258 if (!isa<ConstantSDNode>(N2))
7259 return SDValue();
7260
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007261 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007262 unsigned NumElems = VT.getVectorNumElements();
7263 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007264 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007265
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007266 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007267 bool Upper = IdxVal >= NumElems/2;
7268 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7269 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007270
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007271 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007272 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007273 }
7274
Craig Topperd0a31172012-01-10 06:37:29 +00007275 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007276 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7277
Dan Gohman8a55ce42009-09-23 21:02:20 +00007278 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007279 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007280
Dan Gohman8a55ce42009-09-23 21:02:20 +00007281 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007282 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7283 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007284 if (N1.getValueType() != MVT::i32)
7285 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7286 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007287 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007288 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007289 }
Dan Gohman475871a2008-07-27 21:46:04 +00007290 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007291}
7292
Craig Topper55b24052012-09-11 06:15:32 +00007293static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007294 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007295 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007296 EVT OpVT = Op.getValueType();
7297
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007298 // If this is a 256-bit vector result, first insert into a 128-bit
7299 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007300 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007301 // Insert into a 128-bit vector.
7302 EVT VT128 = EVT::getVectorVT(*Context,
7303 OpVT.getVectorElementType(),
7304 OpVT.getVectorNumElements() / 2);
7305
7306 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7307
7308 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007309 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007310 }
7311
Craig Topperd77d2fe2012-04-29 20:22:05 +00007312 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007313 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007315
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007317 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007318 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007319 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007320}
7321
David Greene91585092011-01-26 15:38:49 +00007322// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7323// a simple subregister reference or explicit instructions to grab
7324// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007325static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7326 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007327 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007328 DebugLoc dl = Op.getNode()->getDebugLoc();
7329 SDValue Vec = Op.getNode()->getOperand(0);
7330 SDValue Idx = Op.getNode()->getOperand(1);
7331
Craig Topper7a9a28b2012-08-12 02:23:29 +00007332 if (Op.getNode()->getValueType(0).is128BitVector() &&
7333 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007334 isa<ConstantSDNode>(Idx)) {
7335 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7336 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007337 }
David Greene91585092011-01-26 15:38:49 +00007338 }
7339 return SDValue();
7340}
7341
David Greenecfe33c42011-01-26 19:13:22 +00007342// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7343// simple superregister reference or explicit instructions to insert
7344// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007345static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7346 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007347 if (Subtarget->hasAVX()) {
7348 DebugLoc dl = Op.getNode()->getDebugLoc();
7349 SDValue Vec = Op.getNode()->getOperand(0);
7350 SDValue SubVec = Op.getNode()->getOperand(1);
7351 SDValue Idx = Op.getNode()->getOperand(2);
7352
Craig Topper7a9a28b2012-08-12 02:23:29 +00007353 if (Op.getNode()->getValueType(0).is256BitVector() &&
7354 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007355 isa<ConstantSDNode>(Idx)) {
7356 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7357 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007358 }
7359 }
7360 return SDValue();
7361}
7362
Bill Wendling056292f2008-09-16 21:48:12 +00007363// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7364// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7365// one of the above mentioned nodes. It has to be wrapped because otherwise
7366// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7367// be used to form addressing mode. These wrapped nodes will be selected
7368// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007369SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007370X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007371 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007372
Chris Lattner41621a22009-06-26 19:22:52 +00007373 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7374 // global base reg.
7375 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007376 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007377 CodeModel::Model M = getTargetMachine().getCodeModel();
7378
Chris Lattner4f066492009-07-11 20:29:19 +00007379 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007380 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007381 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007382 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007383 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007384 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007385 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007386
Evan Cheng1606e8e2009-03-13 07:51:59 +00007387 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007388 CP->getAlignment(),
7389 CP->getOffset(), OpFlag);
7390 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007391 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007392 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007393 if (OpFlag) {
7394 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007395 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007396 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007397 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007398 }
7399
7400 return Result;
7401}
7402
Dan Gohmand858e902010-04-17 15:26:15 +00007403SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007404 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007405
Chris Lattner18c59872009-06-27 04:16:01 +00007406 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7407 // global base reg.
7408 unsigned char OpFlag = 0;
7409 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007410 CodeModel::Model M = getTargetMachine().getCodeModel();
7411
Chris Lattner4f066492009-07-11 20:29:19 +00007412 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007413 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007414 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007415 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007416 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007417 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007418 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007419
Chris Lattner18c59872009-06-27 04:16:01 +00007420 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7421 OpFlag);
7422 DebugLoc DL = JT->getDebugLoc();
7423 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007424
Chris Lattner18c59872009-06-27 04:16:01 +00007425 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007426 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007427 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7428 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007429 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007430 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007431
Chris Lattner18c59872009-06-27 04:16:01 +00007432 return Result;
7433}
7434
7435SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007436X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007437 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007438
Chris Lattner18c59872009-06-27 04:16:01 +00007439 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7440 // global base reg.
7441 unsigned char OpFlag = 0;
7442 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007443 CodeModel::Model M = getTargetMachine().getCodeModel();
7444
Chris Lattner4f066492009-07-11 20:29:19 +00007445 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007446 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7447 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7448 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007449 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007450 } else if (Subtarget->isPICStyleGOT()) {
7451 OpFlag = X86II::MO_GOT;
7452 } else if (Subtarget->isPICStyleStubPIC()) {
7453 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7454 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7455 OpFlag = X86II::MO_DARWIN_NONLAZY;
7456 }
Eric Christopherfd179292009-08-27 18:07:15 +00007457
Chris Lattner18c59872009-06-27 04:16:01 +00007458 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007459
Chris Lattner18c59872009-06-27 04:16:01 +00007460 DebugLoc DL = Op.getDebugLoc();
7461 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007462
7463
Chris Lattner18c59872009-06-27 04:16:01 +00007464 // With PIC, the address is actually $g + Offset.
7465 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007466 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007467 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7468 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007469 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007470 Result);
7471 }
Eric Christopherfd179292009-08-27 18:07:15 +00007472
Eli Friedman586272d2011-08-11 01:48:05 +00007473 // For symbols that require a load from a stub to get the address, emit the
7474 // load.
7475 if (isGlobalStubReference(OpFlag))
7476 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007477 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007478
Chris Lattner18c59872009-06-27 04:16:01 +00007479 return Result;
7480}
7481
Dan Gohman475871a2008-07-27 21:46:04 +00007482SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007483X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007484 // Create the TargetBlockAddressAddress node.
7485 unsigned char OpFlags =
7486 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007487 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007488 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007489 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007490 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007491 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7492 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007493
Dan Gohmanf705adb2009-10-30 01:28:02 +00007494 if (Subtarget->isPICStyleRIPRel() &&
7495 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007496 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7497 else
7498 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007499
Dan Gohman29cbade2009-11-20 23:18:13 +00007500 // With PIC, the address is actually $g + Offset.
7501 if (isGlobalRelativeToPICBase(OpFlags)) {
7502 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7503 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7504 Result);
7505 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007506
7507 return Result;
7508}
7509
7510SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007511X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007512 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007513 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007514 // Create the TargetGlobalAddress node, folding in the constant
7515 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007516 unsigned char OpFlags =
7517 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007518 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007519 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007520 if (OpFlags == X86II::MO_NO_FLAG &&
7521 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007522 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007523 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007524 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007525 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007526 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007527 }
Eric Christopherfd179292009-08-27 18:07:15 +00007528
Chris Lattner4f066492009-07-11 20:29:19 +00007529 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007530 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007531 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7532 else
7533 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007534
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007535 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007536 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007537 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7538 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007539 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007540 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007541
Chris Lattner36c25012009-07-10 07:34:39 +00007542 // For globals that require a load from a stub to get the address, emit the
7543 // load.
7544 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007545 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007546 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007547
Dan Gohman6520e202008-10-18 02:06:02 +00007548 // If there was a non-zero offset that we didn't fold, create an explicit
7549 // addition for it.
7550 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007551 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007552 DAG.getConstant(Offset, getPointerTy()));
7553
Evan Cheng0db9fe62006-04-25 20:13:52 +00007554 return Result;
7555}
7556
Evan Chengda43bcf2008-09-24 00:05:32 +00007557SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007558X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007559 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007560 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007561 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007562}
7563
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007564static SDValue
7565GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007566 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007567 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007568 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007569 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007570 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007571 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007572 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007573 GA->getOffset(),
7574 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007575
7576 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7577 : X86ISD::TLSADDR;
7578
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007579 if (InFlag) {
7580 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007581 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007582 } else {
7583 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007584 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007585 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007586
7587 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007588 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007589
Rafael Espindola15f1b662009-04-24 12:59:40 +00007590 SDValue Flag = Chain.getValue(1);
7591 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007592}
7593
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007594// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007595static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007596LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007597 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007598 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007599 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7600 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007601 DAG.getNode(X86ISD::GlobalBaseReg,
7602 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007603 InFlag = Chain.getValue(1);
7604
Chris Lattnerb903bed2009-06-26 21:20:29 +00007605 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007606}
7607
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007608// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007609static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007610LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007611 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007612 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7613 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007614}
7615
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007616static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7617 SelectionDAG &DAG,
7618 const EVT PtrVT,
7619 bool is64Bit) {
7620 DebugLoc dl = GA->getDebugLoc();
7621
7622 // Get the start address of the TLS block for this module.
7623 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7624 .getInfo<X86MachineFunctionInfo>();
7625 MFI->incNumLocalDynamicTLSAccesses();
7626
7627 SDValue Base;
7628 if (is64Bit) {
7629 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7630 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7631 } else {
7632 SDValue InFlag;
7633 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7634 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7635 InFlag = Chain.getValue(1);
7636 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7637 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7638 }
7639
7640 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7641 // of Base.
7642
7643 // Build x@dtpoff.
7644 unsigned char OperandFlags = X86II::MO_DTPOFF;
7645 unsigned WrapperKind = X86ISD::Wrapper;
7646 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7647 GA->getValueType(0),
7648 GA->getOffset(), OperandFlags);
7649 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7650
7651 // Add x@dtpoff with the base.
7652 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7653}
7654
Hans Wennborg228756c2012-05-11 10:11:01 +00007655// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007656static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007657 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007658 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007659 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007660
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007661 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7662 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7663 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007664
Michael J. Spencerec38de22010-10-10 22:04:20 +00007665 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007666 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007667 MachinePointerInfo(Ptr),
7668 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007669
Chris Lattnerb903bed2009-06-26 21:20:29 +00007670 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007671 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7672 // initialexec.
7673 unsigned WrapperKind = X86ISD::Wrapper;
7674 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007675 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007676 } else if (model == TLSModel::InitialExec) {
7677 if (is64Bit) {
7678 OperandFlags = X86II::MO_GOTTPOFF;
7679 WrapperKind = X86ISD::WrapperRIP;
7680 } else {
7681 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7682 }
Chris Lattner18c59872009-06-27 04:16:01 +00007683 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007684 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007685 }
Eric Christopherfd179292009-08-27 18:07:15 +00007686
Hans Wennborg228756c2012-05-11 10:11:01 +00007687 // emit "addl x@ntpoff,%eax" (local exec)
7688 // or "addl x@indntpoff,%eax" (initial exec)
7689 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007690 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007691 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007692 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007693 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007694
Hans Wennborg228756c2012-05-11 10:11:01 +00007695 if (model == TLSModel::InitialExec) {
7696 if (isPIC && !is64Bit) {
7697 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7698 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7699 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007700 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007701
7702 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7703 MachinePointerInfo::getGOT(), false, false, false,
7704 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007705 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007706
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007707 // The address of the thread local variable is the add of the thread
7708 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007709 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007710}
7711
Dan Gohman475871a2008-07-27 21:46:04 +00007712SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007713X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007714
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007715 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007716 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007717
Eric Christopher30ef0e52010-06-03 04:07:48 +00007718 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007719 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007720
Eric Christopher30ef0e52010-06-03 04:07:48 +00007721 switch (model) {
7722 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007723 if (Subtarget->is64Bit())
7724 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7725 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007726 case TLSModel::LocalDynamic:
7727 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7728 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007729 case TLSModel::InitialExec:
7730 case TLSModel::LocalExec:
7731 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007732 Subtarget->is64Bit(),
7733 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007734 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007735 llvm_unreachable("Unknown TLS model.");
7736 }
7737
7738 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007739 // Darwin only has one model of TLS. Lower to that.
7740 unsigned char OpFlag = 0;
7741 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7742 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007743
Eric Christopher30ef0e52010-06-03 04:07:48 +00007744 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7745 // global base reg.
7746 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7747 !Subtarget->is64Bit();
7748 if (PIC32)
7749 OpFlag = X86II::MO_TLVP_PIC_BASE;
7750 else
7751 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007752 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007753 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007754 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007755 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007756 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007757
Eric Christopher30ef0e52010-06-03 04:07:48 +00007758 // With PIC32, the address is actually $g + Offset.
7759 if (PIC32)
7760 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7761 DAG.getNode(X86ISD::GlobalBaseReg,
7762 DebugLoc(), getPointerTy()),
7763 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007764
Eric Christopher30ef0e52010-06-03 04:07:48 +00007765 // Lowering the machine isd will make sure everything is in the right
7766 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007767 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007768 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007769 SDValue Args[] = { Chain, Offset };
7770 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007771
Eric Christopher30ef0e52010-06-03 04:07:48 +00007772 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7773 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7774 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007775
Eric Christopher30ef0e52010-06-03 04:07:48 +00007776 // And our return value (tls address) is in the standard call return value
7777 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007778 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007779 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7780 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007781 }
7782
7783 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007784 // Just use the implicit TLS architecture
7785 // Need to generate someting similar to:
7786 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7787 // ; from TEB
7788 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7789 // mov rcx, qword [rdx+rcx*8]
7790 // mov eax, .tls$:tlsvar
7791 // [rax+rcx] contains the address
7792 // Windows 64bit: gs:0x58
7793 // Windows 32bit: fs:__tls_array
7794
7795 // If GV is an alias then use the aliasee for determining
7796 // thread-localness.
7797 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7798 GV = GA->resolveAliasedGlobal(false);
7799 DebugLoc dl = GA->getDebugLoc();
7800 SDValue Chain = DAG.getEntryNode();
7801
7802 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7803 // %gs:0x58 (64-bit).
7804 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7805 ? Type::getInt8PtrTy(*DAG.getContext(),
7806 256)
7807 : Type::getInt32PtrTy(*DAG.getContext(),
7808 257));
7809
7810 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7811 Subtarget->is64Bit()
7812 ? DAG.getIntPtrConstant(0x58)
7813 : DAG.getExternalSymbol("_tls_array",
7814 getPointerTy()),
7815 MachinePointerInfo(Ptr),
7816 false, false, false, 0);
7817
7818 // Load the _tls_index variable
7819 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7820 if (Subtarget->is64Bit())
7821 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7822 IDX, MachinePointerInfo(), MVT::i32,
7823 false, false, 0);
7824 else
7825 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7826 false, false, false, 0);
7827
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007828 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007829 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007830 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7831
7832 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7833 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7834 false, false, false, 0);
7835
7836 // Get the offset of start of .tls section
7837 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7838 GA->getValueType(0),
7839 GA->getOffset(), X86II::MO_SECREL);
7840 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7841
7842 // The address of the thread local variable is the add of the thread
7843 // pointer with the offset of the variable.
7844 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007845 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007846
David Blaikie4d6ccb52012-01-20 21:51:11 +00007847 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007848}
7849
Evan Cheng0db9fe62006-04-25 20:13:52 +00007850
Chad Rosierb90d2a92012-01-03 23:19:12 +00007851/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7852/// and take a 2 x i32 value to shift plus a shift amount.
7853SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007854 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007855 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007856 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007857 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007858 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007859 SDValue ShOpLo = Op.getOperand(0);
7860 SDValue ShOpHi = Op.getOperand(1);
7861 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007862 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007863 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007864 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007865
Dan Gohman475871a2008-07-27 21:46:04 +00007866 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007867 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007868 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7869 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007870 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007871 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7872 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007873 }
Evan Chenge3413162006-01-09 18:33:28 +00007874
Owen Anderson825b72b2009-08-11 20:47:22 +00007875 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7876 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007877 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007878 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007879
Dan Gohman475871a2008-07-27 21:46:04 +00007880 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007881 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007882 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7883 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007884
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007885 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007886 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7887 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007888 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007889 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7890 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007891 }
7892
Dan Gohman475871a2008-07-27 21:46:04 +00007893 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007894 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007895}
Evan Chenga3195e82006-01-12 22:54:21 +00007896
Dan Gohmand858e902010-04-17 15:26:15 +00007897SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7898 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007899 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007900
Dale Johannesen0488fb62010-09-30 23:57:10 +00007901 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007902 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007903
Owen Anderson825b72b2009-08-11 20:47:22 +00007904 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007905 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007906
Eli Friedman36df4992009-05-27 00:47:34 +00007907 // These are really Legal; return the operand so the caller accepts it as
7908 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007909 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007910 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007911 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007912 Subtarget->is64Bit()) {
7913 return Op;
7914 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007915
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007916 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007917 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007918 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007919 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007920 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007921 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007922 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007923 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007924 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007925 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7926}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007927
Owen Andersone50ed302009-08-10 22:56:29 +00007928SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007929 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007930 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007931 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007932 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007933 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007934 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007935 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007936 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007937 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007938 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007939
Chris Lattner492a43e2010-09-22 01:28:21 +00007940 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007941
Stuart Hastings84be9582011-06-02 15:57:11 +00007942 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7943 MachineMemOperand *MMO;
7944 if (FI) {
7945 int SSFI = FI->getIndex();
7946 MMO =
7947 DAG.getMachineFunction()
7948 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7949 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7950 } else {
7951 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7952 StackSlot = StackSlot.getOperand(1);
7953 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007954 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007955 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7956 X86ISD::FILD, DL,
7957 Tys, Ops, array_lengthof(Ops),
7958 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007959
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007960 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007961 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007962 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007963
7964 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7965 // shouldn't be necessary except that RFP cannot be live across
7966 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007967 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007968 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7969 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007970 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007971 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007972 SDValue Ops[] = {
7973 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7974 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007975 MachineMemOperand *MMO =
7976 DAG.getMachineFunction()
7977 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007978 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007979
Chris Lattner492a43e2010-09-22 01:28:21 +00007980 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7981 Ops, array_lengthof(Ops),
7982 Op.getValueType(), MMO);
7983 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007984 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007985 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007986 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007987
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988 return Result;
7989}
7990
Bill Wendling8b8a6362009-01-17 03:56:04 +00007991// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007992SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7993 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007994 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007995 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007996 movq %rax, %xmm0
7997 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7998 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7999 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008000 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008001 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008002 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008003 addpd %xmm1, %xmm0
8004 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008005 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008006
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008007 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008008 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008009
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008010 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008011 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8012 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008013 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008014
Chris Lattner97484792012-01-25 09:56:22 +00008015 SmallVector<Constant*,2> CV1;
8016 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008017 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008018 CV1.push_back(
8019 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8020 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008021 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008022
Bill Wendling397ae212012-01-05 02:13:20 +00008023 // Load the 64-bit value into an XMM register.
8024 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8025 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008026 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008027 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008028 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008029 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8030 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8031 CLod0);
8032
Owen Anderson825b72b2009-08-11 20:47:22 +00008033 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008034 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008035 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008036 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008037 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008038 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008039
Craig Topperd0a31172012-01-10 06:37:29 +00008040 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008041 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8042 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8043 } else {
8044 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8045 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8046 S2F, 0x4E, DAG);
8047 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8048 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8049 Sub);
8050 }
8051
8052 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008053 DAG.getIntPtrConstant(0));
8054}
8055
Bill Wendling8b8a6362009-01-17 03:56:04 +00008056// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008057SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8058 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008059 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008060 // FP constant to bias correct the final result.
8061 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008063
8064 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008065 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008066 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008067
Eli Friedmanf3704762011-08-29 21:15:46 +00008068 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008069 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008070
Owen Anderson825b72b2009-08-11 20:47:22 +00008071 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008072 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008073 DAG.getIntPtrConstant(0));
8074
8075 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008076 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008077 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008078 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008079 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008080 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008081 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008082 MVT::v2f64, Bias)));
8083 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008084 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008085 DAG.getIntPtrConstant(0));
8086
8087 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008089
8090 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008091 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008092
Craig Topper69947b92012-04-23 06:57:04 +00008093 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008094 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008095 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008096 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008097 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008098
8099 // Handle final rounding.
8100 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008101}
8102
Michael Liaoa7554632012-10-23 17:36:08 +00008103SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8104 SelectionDAG &DAG) const {
8105 SDValue N0 = Op.getOperand(0);
8106 EVT SVT = N0.getValueType();
8107 DebugLoc dl = Op.getDebugLoc();
8108
8109 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8110 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8111 "Custom UINT_TO_FP is not supported!");
8112
8113 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8114 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8115 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8116}
8117
Dan Gohmand858e902010-04-17 15:26:15 +00008118SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8119 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008120 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008121 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008122
Michael Liaoa7554632012-10-23 17:36:08 +00008123 if (Op.getValueType().isVector())
8124 return lowerUINT_TO_FP_vec(Op, DAG);
8125
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008126 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008127 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8128 // the optimization here.
8129 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008130 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008131
Owen Andersone50ed302009-08-10 22:56:29 +00008132 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008133 EVT DstVT = Op.getValueType();
8134 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008135 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008136 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008137 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008138 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008139 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008140
8141 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008142 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008143 if (SrcVT == MVT::i32) {
8144 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8145 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8146 getPointerTy(), StackSlot, WordOff);
8147 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008148 StackSlot, MachinePointerInfo(),
8149 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008150 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008151 OffsetSlot, MachinePointerInfo(),
8152 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008153 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8154 return Fild;
8155 }
8156
8157 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8158 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008159 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008160 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008161 // For i64 source, we need to add the appropriate power of 2 if the input
8162 // was negative. This is the same as the optimization in
8163 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8164 // we must be careful to do the computation in x87 extended precision, not
8165 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008166 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8167 MachineMemOperand *MMO =
8168 DAG.getMachineFunction()
8169 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8170 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008171
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008172 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8173 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008174 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8175 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008176
8177 APInt FF(32, 0x5F800000ULL);
8178
8179 // Check whether the sign bit is set.
8180 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8181 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8182 ISD::SETLT);
8183
8184 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8185 SDValue FudgePtr = DAG.getConstantPool(
8186 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8187 getPointerTy());
8188
8189 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8190 SDValue Zero = DAG.getIntPtrConstant(0);
8191 SDValue Four = DAG.getIntPtrConstant(4);
8192 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8193 Zero, Four);
8194 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8195
8196 // Load the value out, extending it from f32 to f80.
8197 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008198 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008199 FudgePtr, MachinePointerInfo::getConstantPool(),
8200 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008201 // Extend everything to 80 bits to force it to be done on x87.
8202 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8203 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008204}
8205
Dan Gohman475871a2008-07-27 21:46:04 +00008206std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008207FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008208 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008209
Owen Andersone50ed302009-08-10 22:56:29 +00008210 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008211
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008212 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008213 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8214 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008215 }
8216
Owen Anderson825b72b2009-08-11 20:47:22 +00008217 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8218 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008219 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008220
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008221 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008222 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008223 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008224 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008225 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008226 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008227 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008228 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008229
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008230 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8231 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008232 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008233 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008234 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008235 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008236
Evan Cheng0db9fe62006-04-25 20:13:52 +00008237 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008238 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8239 Opc = X86ISD::WIN_FTOL;
8240 else
8241 switch (DstTy.getSimpleVT().SimpleTy) {
8242 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8243 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8244 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8245 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8246 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008247
Dan Gohman475871a2008-07-27 21:46:04 +00008248 SDValue Chain = DAG.getEntryNode();
8249 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008250 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008251 // FIXME This causes a redundant load/store if the SSE-class value is already
8252 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008253 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008254 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008255 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008256 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008257 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008258 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008259 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008260 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008261 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008262
Chris Lattner492a43e2010-09-22 01:28:21 +00008263 MachineMemOperand *MMO =
8264 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8265 MachineMemOperand::MOLoad, MemSize, MemSize);
8266 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8267 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008268 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008269 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008270 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8271 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008272
Chris Lattner07290932010-09-22 01:05:16 +00008273 MachineMemOperand *MMO =
8274 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8275 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008276
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008277 if (Opc != X86ISD::WIN_FTOL) {
8278 // Build the FP_TO_INT*_IN_MEM
8279 SDValue Ops[] = { Chain, Value, StackSlot };
8280 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8281 Ops, 3, DstTy, MMO);
8282 return std::make_pair(FIST, StackSlot);
8283 } else {
8284 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8285 DAG.getVTList(MVT::Other, MVT::Glue),
8286 Chain, Value);
8287 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8288 MVT::i32, ftol.getValue(1));
8289 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8290 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008291 SDValue Ops[] = { eax, edx };
8292 SDValue pair = IsReplace
8293 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8294 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008295 return std::make_pair(pair, SDValue());
8296 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008297}
8298
Michael Liaoa7554632012-10-23 17:36:08 +00008299SDValue X86TargetLowering::lowerZERO_EXTEND(SDValue Op, SelectionDAG &DAG) const {
8300 DebugLoc DL = Op.getDebugLoc();
8301 EVT VT = Op.getValueType();
8302 SDValue In = Op.getOperand(0);
8303 EVT SVT = In.getValueType();
8304
8305 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8306 VT.getVectorNumElements() != SVT.getVectorNumElements())
8307 return SDValue();
8308
8309 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8310
8311 // AVX2 has better support of integer extending.
8312 if (Subtarget->hasAVX2())
8313 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8314
8315 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8316 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8317 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
8318 DAG.getVectorShuffle(MVT::v8i16, DL, In, DAG.getUNDEF(MVT::v8i16), &Mask[0]));
8319
8320 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8321}
8322
Michael Liaobedcbd42012-10-16 18:14:11 +00008323SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8324 DebugLoc DL = Op.getDebugLoc();
8325 EVT VT = Op.getValueType();
8326 EVT SVT = Op.getOperand(0).getValueType();
8327
8328 if (!VT.is128BitVector() || !SVT.is256BitVector() ||
8329 VT.getVectorNumElements() != SVT.getVectorNumElements())
8330 return SDValue();
8331
8332 assert(Subtarget->hasAVX() && "256-bit vector is observed without AVX!");
8333
8334 unsigned NumElems = VT.getVectorNumElements();
8335 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8336 NumElems * 2);
8337
8338 SDValue In = Op.getOperand(0);
8339 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8340 // Prepare truncation shuffle mask
8341 for (unsigned i = 0; i != NumElems; ++i)
8342 MaskVec[i] = i * 2;
8343 SDValue V = DAG.getVectorShuffle(NVT, DL,
8344 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8345 DAG.getUNDEF(NVT), &MaskVec[0]);
8346 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8347 DAG.getIntPtrConstant(0));
8348}
8349
Dan Gohmand858e902010-04-17 15:26:15 +00008350SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8351 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008352 if (Op.getValueType().isVector()) {
8353 if (Op.getValueType() == MVT::v8i16)
8354 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8355 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8356 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008357 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008358 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008359
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008360 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8361 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008362 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008363 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8364 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008365
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008366 if (StackSlot.getNode())
8367 // Load the result.
8368 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8369 FIST, StackSlot, MachinePointerInfo(),
8370 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008371
8372 // The node is the result.
8373 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008374}
8375
Dan Gohmand858e902010-04-17 15:26:15 +00008376SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8377 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008378 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8379 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008380 SDValue FIST = Vals.first, StackSlot = Vals.second;
8381 assert(FIST.getNode() && "Unexpected failure");
8382
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008383 if (StackSlot.getNode())
8384 // Load the result.
8385 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8386 FIST, StackSlot, MachinePointerInfo(),
8387 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008388
8389 // The node is the result.
8390 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008391}
8392
Michael Liao9d796db2012-10-10 16:32:15 +00008393SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8394 SelectionDAG &DAG) const {
8395 DebugLoc DL = Op.getDebugLoc();
8396 EVT VT = Op.getValueType();
8397 SDValue In = Op.getOperand(0);
8398 EVT SVT = In.getValueType();
8399
8400 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8401
8402 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8403 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8404 In, DAG.getUNDEF(SVT)));
8405}
8406
Craig Topper43620672012-09-08 07:31:51 +00008407SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008408 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008409 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008410 EVT VT = Op.getValueType();
8411 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008412 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8413 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008414 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008415 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008416 }
Craig Topper43620672012-09-08 07:31:51 +00008417 Constant *C;
8418 if (EltVT == MVT::f64)
8419 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8420 else
8421 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8422 C = ConstantVector::getSplat(NumElts, C);
8423 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8424 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008425 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008426 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008427 false, false, false, Alignment);
8428 if (VT.isVector()) {
8429 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8430 return DAG.getNode(ISD::BITCAST, dl, VT,
8431 DAG.getNode(ISD::AND, dl, ANDVT,
8432 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8433 Op.getOperand(0)),
8434 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8435 }
Dale Johannesenace16102009-02-03 19:33:06 +00008436 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008437}
8438
Dan Gohmand858e902010-04-17 15:26:15 +00008439SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008440 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008441 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008442 EVT VT = Op.getValueType();
8443 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008444 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8445 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008446 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008447 NumElts = VT.getVectorNumElements();
8448 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008449 Constant *C;
8450 if (EltVT == MVT::f64)
8451 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8452 else
8453 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8454 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008455 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8456 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008457 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008458 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008459 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008460 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008461 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008462 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008463 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008464 DAG.getNode(ISD::BITCAST, dl, XORVT,
8465 Op.getOperand(0)),
8466 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008467 }
Craig Topper69947b92012-04-23 06:57:04 +00008468
8469 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008470}
8471
Dan Gohmand858e902010-04-17 15:26:15 +00008472SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008473 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008474 SDValue Op0 = Op.getOperand(0);
8475 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008476 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008477 EVT VT = Op.getValueType();
8478 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008479
8480 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008481 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008482 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008483 SrcVT = VT;
8484 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008485 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008486 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008487 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008488 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008489 }
8490
8491 // At this point the operands and the result should have the same
8492 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008493
Evan Cheng68c47cb2007-01-05 07:55:56 +00008494 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008495 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008496 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008497 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8498 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008499 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008500 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8501 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8502 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8503 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008504 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008505 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008506 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008507 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008508 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008509 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008510 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008511
8512 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008513 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008514 // Op0 is MVT::f32, Op1 is MVT::f64.
8515 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8516 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8517 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008518 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008519 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008520 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008521 }
8522
Evan Cheng73d6cf12007-01-05 21:37:56 +00008523 // Clear first operand sign bit.
8524 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008525 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008526 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8527 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008528 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008529 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8530 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8531 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8532 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008533 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008534 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008535 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008536 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008537 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008538 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008539 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008540
8541 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008542 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008543}
8544
Craig Topper55b24052012-09-11 06:15:32 +00008545static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008546 SDValue N0 = Op.getOperand(0);
8547 DebugLoc dl = Op.getDebugLoc();
8548 EVT VT = Op.getValueType();
8549
8550 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8551 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8552 DAG.getConstant(1, VT));
8553 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8554}
8555
Michael Liaof966e4e2012-09-13 20:24:54 +00008556// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8557//
8558SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8559 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8560
8561 if (!Subtarget->hasSSE41())
8562 return SDValue();
8563
8564 if (!Op->hasOneUse())
8565 return SDValue();
8566
8567 SDNode *N = Op.getNode();
8568 DebugLoc DL = N->getDebugLoc();
8569
8570 SmallVector<SDValue, 8> Opnds;
8571 DenseMap<SDValue, unsigned> VecInMap;
8572 EVT VT = MVT::Other;
8573
8574 // Recognize a special case where a vector is casted into wide integer to
8575 // test all 0s.
8576 Opnds.push_back(N->getOperand(0));
8577 Opnds.push_back(N->getOperand(1));
8578
8579 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8580 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8581 // BFS traverse all OR'd operands.
8582 if (I->getOpcode() == ISD::OR) {
8583 Opnds.push_back(I->getOperand(0));
8584 Opnds.push_back(I->getOperand(1));
8585 // Re-evaluate the number of nodes to be traversed.
8586 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8587 continue;
8588 }
8589
8590 // Quit if a non-EXTRACT_VECTOR_ELT
8591 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8592 return SDValue();
8593
8594 // Quit if without a constant index.
8595 SDValue Idx = I->getOperand(1);
8596 if (!isa<ConstantSDNode>(Idx))
8597 return SDValue();
8598
8599 SDValue ExtractedFromVec = I->getOperand(0);
8600 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8601 if (M == VecInMap.end()) {
8602 VT = ExtractedFromVec.getValueType();
8603 // Quit if not 128/256-bit vector.
8604 if (!VT.is128BitVector() && !VT.is256BitVector())
8605 return SDValue();
8606 // Quit if not the same type.
8607 if (VecInMap.begin() != VecInMap.end() &&
8608 VT != VecInMap.begin()->first.getValueType())
8609 return SDValue();
8610 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8611 }
8612 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8613 }
8614
8615 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008616 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008617
8618 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8619 SmallVector<SDValue, 8> VecIns;
8620
8621 for (DenseMap<SDValue, unsigned>::const_iterator
8622 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8623 // Quit if not all elements are used.
8624 if (I->second != FullMask)
8625 return SDValue();
8626 VecIns.push_back(I->first);
8627 }
8628
8629 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8630
8631 // Cast all vectors into TestVT for PTEST.
8632 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8633 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8634
8635 // If more than one full vectors are evaluated, OR them first before PTEST.
8636 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8637 // Each iteration will OR 2 nodes and append the result until there is only
8638 // 1 node left, i.e. the final OR'd value of all vectors.
8639 SDValue LHS = VecIns[Slot];
8640 SDValue RHS = VecIns[Slot + 1];
8641 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8642 }
8643
8644 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8645 VecIns.back(), VecIns.back());
8646}
8647
Dan Gohman076aee32009-03-04 19:44:21 +00008648/// Emit nodes that will be selected as "test Op0,Op0", or something
8649/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008650SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008651 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008652 DebugLoc dl = Op.getDebugLoc();
8653
Dan Gohman31125812009-03-07 01:58:32 +00008654 // CF and OF aren't always set the way we want. Determine which
8655 // of these we need.
8656 bool NeedCF = false;
8657 bool NeedOF = false;
8658 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008659 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008660 case X86::COND_A: case X86::COND_AE:
8661 case X86::COND_B: case X86::COND_BE:
8662 NeedCF = true;
8663 break;
8664 case X86::COND_G: case X86::COND_GE:
8665 case X86::COND_L: case X86::COND_LE:
8666 case X86::COND_O: case X86::COND_NO:
8667 NeedOF = true;
8668 break;
Dan Gohman31125812009-03-07 01:58:32 +00008669 }
8670
Dan Gohman076aee32009-03-04 19:44:21 +00008671 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008672 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8673 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008674 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8675 // Emit a CMP with 0, which is the TEST pattern.
8676 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8677 DAG.getConstant(0, Op.getValueType()));
8678
8679 unsigned Opcode = 0;
8680 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008681
8682 // Truncate operations may prevent the merge of the SETCC instruction
8683 // and the arithmetic intruction before it. Attempt to truncate the operands
8684 // of the arithmetic instruction and use a reduced bit-width instruction.
8685 bool NeedTruncation = false;
8686 SDValue ArithOp = Op;
8687 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8688 SDValue Arith = Op->getOperand(0);
8689 // Both the trunc and the arithmetic op need to have one user each.
8690 if (Arith->hasOneUse())
8691 switch (Arith.getOpcode()) {
8692 default: break;
8693 case ISD::ADD:
8694 case ISD::SUB:
8695 case ISD::AND:
8696 case ISD::OR:
8697 case ISD::XOR: {
8698 NeedTruncation = true;
8699 ArithOp = Arith;
8700 }
8701 }
8702 }
8703
8704 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8705 // which may be the result of a CAST. We use the variable 'Op', which is the
8706 // non-casted variable when we check for possible users.
8707 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008708 case ISD::ADD:
8709 // Due to an isel shortcoming, be conservative if this add is likely to be
8710 // selected as part of a load-modify-store instruction. When the root node
8711 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8712 // uses of other nodes in the match, such as the ADD in this case. This
8713 // leads to the ADD being left around and reselected, with the result being
8714 // two adds in the output. Alas, even if none our users are stores, that
8715 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8716 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8717 // climbing the DAG back to the root, and it doesn't seem to be worth the
8718 // effort.
8719 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008720 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8721 if (UI->getOpcode() != ISD::CopyToReg &&
8722 UI->getOpcode() != ISD::SETCC &&
8723 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008724 goto default_case;
8725
8726 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008727 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008728 // An add of one will be selected as an INC.
8729 if (C->getAPIntValue() == 1) {
8730 Opcode = X86ISD::INC;
8731 NumOperands = 1;
8732 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008733 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008734
8735 // An add of negative one (subtract of one) will be selected as a DEC.
8736 if (C->getAPIntValue().isAllOnesValue()) {
8737 Opcode = X86ISD::DEC;
8738 NumOperands = 1;
8739 break;
8740 }
Dan Gohman076aee32009-03-04 19:44:21 +00008741 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008742
8743 // Otherwise use a regular EFLAGS-setting add.
8744 Opcode = X86ISD::ADD;
8745 NumOperands = 2;
8746 break;
8747 case ISD::AND: {
8748 // If the primary and result isn't used, don't bother using X86ISD::AND,
8749 // because a TEST instruction will be better.
8750 bool NonFlagUse = false;
8751 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8752 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8753 SDNode *User = *UI;
8754 unsigned UOpNo = UI.getOperandNo();
8755 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8756 // Look pass truncate.
8757 UOpNo = User->use_begin().getOperandNo();
8758 User = *User->use_begin();
8759 }
8760
8761 if (User->getOpcode() != ISD::BRCOND &&
8762 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008763 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008764 NonFlagUse = true;
8765 break;
8766 }
Dan Gohman076aee32009-03-04 19:44:21 +00008767 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008768
8769 if (!NonFlagUse)
8770 break;
8771 }
8772 // FALL THROUGH
8773 case ISD::SUB:
8774 case ISD::OR:
8775 case ISD::XOR:
8776 // Due to the ISEL shortcoming noted above, be conservative if this op is
8777 // likely to be selected as part of a load-modify-store instruction.
8778 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8779 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8780 if (UI->getOpcode() == ISD::STORE)
8781 goto default_case;
8782
8783 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008784 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008785 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008786 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008787 case ISD::XOR: Opcode = X86ISD::XOR; break;
8788 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008789 case ISD::OR: {
8790 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8791 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8792 if (EFLAGS.getNode())
8793 return EFLAGS;
8794 }
8795 Opcode = X86ISD::OR;
8796 break;
8797 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008798 }
8799
8800 NumOperands = 2;
8801 break;
8802 case X86ISD::ADD:
8803 case X86ISD::SUB:
8804 case X86ISD::INC:
8805 case X86ISD::DEC:
8806 case X86ISD::OR:
8807 case X86ISD::XOR:
8808 case X86ISD::AND:
8809 return SDValue(Op.getNode(), 1);
8810 default:
8811 default_case:
8812 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008813 }
8814
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008815 // If we found that truncation is beneficial, perform the truncation and
8816 // update 'Op'.
8817 if (NeedTruncation) {
8818 EVT VT = Op.getValueType();
8819 SDValue WideVal = Op->getOperand(0);
8820 EVT WideVT = WideVal.getValueType();
8821 unsigned ConvertedOp = 0;
8822 // Use a target machine opcode to prevent further DAGCombine
8823 // optimizations that may separate the arithmetic operations
8824 // from the setcc node.
8825 switch (WideVal.getOpcode()) {
8826 default: break;
8827 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8828 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8829 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8830 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8831 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8832 }
8833
8834 if (ConvertedOp) {
8835 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8836 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8837 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8838 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8839 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8840 }
8841 }
8842 }
8843
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008844 if (Opcode == 0)
8845 // Emit a CMP with 0, which is the TEST pattern.
8846 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8847 DAG.getConstant(0, Op.getValueType()));
8848
8849 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8850 SmallVector<SDValue, 4> Ops;
8851 for (unsigned i = 0; i != NumOperands; ++i)
8852 Ops.push_back(Op.getOperand(i));
8853
8854 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8855 DAG.ReplaceAllUsesWith(Op, New);
8856 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008857}
8858
8859/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8860/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008861SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008862 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008863 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8864 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008865 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008866
8867 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008868 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8869 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8870 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8871 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8872 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8873 Op0, Op1);
8874 return SDValue(Sub.getNode(), 1);
8875 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008876 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008877}
8878
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008879/// Convert a comparison if required by the subtarget.
8880SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8881 SelectionDAG &DAG) const {
8882 // If the subtarget does not support the FUCOMI instruction, floating-point
8883 // comparisons have to be converted.
8884 if (Subtarget->hasCMov() ||
8885 Cmp.getOpcode() != X86ISD::CMP ||
8886 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8887 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8888 return Cmp;
8889
8890 // The instruction selector will select an FUCOM instruction instead of
8891 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8892 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8893 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8894 DebugLoc dl = Cmp.getDebugLoc();
8895 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8896 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8897 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8898 DAG.getConstant(8, MVT::i8));
8899 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8900 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8901}
8902
Evan Chengd40d03e2010-01-06 19:38:29 +00008903/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8904/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008905SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8906 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008907 SDValue Op0 = And.getOperand(0);
8908 SDValue Op1 = And.getOperand(1);
8909 if (Op0.getOpcode() == ISD::TRUNCATE)
8910 Op0 = Op0.getOperand(0);
8911 if (Op1.getOpcode() == ISD::TRUNCATE)
8912 Op1 = Op1.getOperand(0);
8913
Evan Chengd40d03e2010-01-06 19:38:29 +00008914 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008915 if (Op1.getOpcode() == ISD::SHL)
8916 std::swap(Op0, Op1);
8917 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008918 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8919 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008920 // If we looked past a truncate, check that it's only truncating away
8921 // known zeros.
8922 unsigned BitWidth = Op0.getValueSizeInBits();
8923 unsigned AndBitWidth = And.getValueSizeInBits();
8924 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008925 APInt Zeros, Ones;
8926 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008927 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8928 return SDValue();
8929 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008930 LHS = Op1;
8931 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008932 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008933 } else if (Op1.getOpcode() == ISD::Constant) {
8934 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008935 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008936 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008937
8938 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008939 LHS = AndLHS.getOperand(0);
8940 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008941 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008942
8943 // Use BT if the immediate can't be encoded in a TEST instruction.
8944 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8945 LHS = AndLHS;
8946 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8947 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008948 }
Evan Cheng0488db92007-09-25 01:57:46 +00008949
Evan Chengd40d03e2010-01-06 19:38:29 +00008950 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008951 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008952 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008953 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008954 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008955 // Also promote i16 to i32 for performance / code size reason.
8956 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008957 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008958 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008959
Evan Chengd40d03e2010-01-06 19:38:29 +00008960 // If the operand types disagree, extend the shift amount to match. Since
8961 // BT ignores high bits (like shifts) we can use anyextend.
8962 if (LHS.getValueType() != RHS.getValueType())
8963 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008964
Evan Chengd40d03e2010-01-06 19:38:29 +00008965 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8966 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8967 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8968 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008969 }
8970
Evan Cheng54de3ea2010-01-05 06:52:31 +00008971 return SDValue();
8972}
8973
Dan Gohmand858e902010-04-17 15:26:15 +00008974SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008975
8976 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8977
Evan Cheng54de3ea2010-01-05 06:52:31 +00008978 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8979 SDValue Op0 = Op.getOperand(0);
8980 SDValue Op1 = Op.getOperand(1);
8981 DebugLoc dl = Op.getDebugLoc();
8982 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8983
8984 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008985 // Lower (X & (1 << N)) == 0 to BT(X, N).
8986 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8987 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008988 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008989 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008990 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008991 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8992 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8993 if (NewSetCC.getNode())
8994 return NewSetCC;
8995 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008996
Chris Lattner481eebc2010-12-19 21:23:48 +00008997 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8998 // these.
8999 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009000 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009001 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9002 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009003
Chris Lattner481eebc2010-12-19 21:23:48 +00009004 // If the input is a setcc, then reuse the input setcc or use a new one with
9005 // the inverted condition.
9006 if (Op0.getOpcode() == X86ISD::SETCC) {
9007 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9008 bool Invert = (CC == ISD::SETNE) ^
9009 cast<ConstantSDNode>(Op1)->isNullValue();
9010 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009011
Evan Cheng2c755ba2010-02-27 07:36:59 +00009012 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009013 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9014 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9015 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009016 }
9017
Evan Chenge5b51ac2010-04-17 06:13:15 +00009018 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009019 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009020 if (X86CC == X86::COND_INVALID)
9021 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009022
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009023 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009024 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009025 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009026 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009027}
9028
Craig Topper89af15e2011-09-18 08:03:58 +00009029// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009030// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009031static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009032 EVT VT = Op.getValueType();
9033
Craig Topper7a9a28b2012-08-12 02:23:29 +00009034 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009035 "Unsupported value type for operation");
9036
Craig Topper66ddd152012-04-27 22:54:43 +00009037 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009038 DebugLoc dl = Op.getDebugLoc();
9039 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009040
9041 // Extract the LHS vectors
9042 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009043 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9044 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009045
9046 // Extract the RHS vectors
9047 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009048 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9049 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009050
9051 // Issue the operation on the smaller types and concatenate the result back
9052 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9053 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9054 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9055 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9056 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9057}
9058
9059
Dan Gohmand858e902010-04-17 15:26:15 +00009060SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009061 SDValue Cond;
9062 SDValue Op0 = Op.getOperand(0);
9063 SDValue Op1 = Op.getOperand(1);
9064 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009065 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009066 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9067 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009068 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009069
9070 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009071#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009072 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009073 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9074#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009075
Craig Topper523908d2012-08-13 02:34:03 +00009076 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009077 bool Swap = false;
9078
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009079 // SSE Condition code mapping:
9080 // 0 - EQ
9081 // 1 - LT
9082 // 2 - LE
9083 // 3 - UNORD
9084 // 4 - NEQ
9085 // 5 - NLT
9086 // 6 - NLE
9087 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009088 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009089 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009090 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009091 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009092 case ISD::SETOGT:
9093 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009094 case ISD::SETLT:
9095 case ISD::SETOLT: SSECC = 1; break;
9096 case ISD::SETOGE:
9097 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009098 case ISD::SETLE:
9099 case ISD::SETOLE: SSECC = 2; break;
9100 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009101 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009102 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009103 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009104 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009105 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009106 case ISD::SETUGT: SSECC = 6; break;
9107 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009108 case ISD::SETUEQ:
9109 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009110 }
9111 if (Swap)
9112 std::swap(Op0, Op1);
9113
Nate Begemanfb8ead02008-07-25 19:05:58 +00009114 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009115 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009116 unsigned CC0, CC1;
9117 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009118 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009119 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9120 } else {
9121 assert(SetCCOpcode == ISD::SETONE);
9122 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009123 }
Craig Topper523908d2012-08-13 02:34:03 +00009124
9125 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9126 DAG.getConstant(CC0, MVT::i8));
9127 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9128 DAG.getConstant(CC1, MVT::i8));
9129 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009130 }
9131 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009132 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9133 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009135
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009136 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00009137 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00009138 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009139
Nate Begeman30a0de92008-07-17 16:51:19 +00009140 // We are handling one of the integer comparisons here. Since SSE only has
9141 // GT and EQ comparisons for integer, swapping operands and multiple
9142 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009143 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009144 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009145
Nate Begeman30a0de92008-07-17 16:51:19 +00009146 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009147 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009148 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009149 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009150 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009151 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009152 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009153 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009154 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009155 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009156 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009157 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009158 }
9159 if (Swap)
9160 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009161
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009162 // Check that the operation in question is available (most are plain SSE2,
9163 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009164 if (VT == MVT::v2i64) {
9165 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9166 return SDValue();
9167 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
9168 return SDValue();
9169 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009170
Nate Begeman30a0de92008-07-17 16:51:19 +00009171 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9172 // bits of the inputs before performing those operations.
9173 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009174 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009175 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9176 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009177 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009178 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9179 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009180 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9181 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009182 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009183
Dale Johannesenace16102009-02-03 19:33:06 +00009184 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009185
9186 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009187 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009188 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009189
Nate Begeman30a0de92008-07-17 16:51:19 +00009190 return Result;
9191}
Evan Cheng0488db92007-09-25 01:57:46 +00009192
Evan Cheng370e5342008-12-03 08:38:43 +00009193// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009194static bool isX86LogicalCmp(SDValue Op) {
9195 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009196 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9197 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009198 return true;
9199 if (Op.getResNo() == 1 &&
9200 (Opc == X86ISD::ADD ||
9201 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009202 Opc == X86ISD::ADC ||
9203 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009204 Opc == X86ISD::SMUL ||
9205 Opc == X86ISD::UMUL ||
9206 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009207 Opc == X86ISD::DEC ||
9208 Opc == X86ISD::OR ||
9209 Opc == X86ISD::XOR ||
9210 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009211 return true;
9212
Chris Lattner9637d5b2010-12-05 07:49:54 +00009213 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9214 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009215
Dan Gohman076aee32009-03-04 19:44:21 +00009216 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009217}
9218
Chris Lattnera2b56002010-12-05 01:23:24 +00009219static bool isZero(SDValue V) {
9220 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9221 return C && C->isNullValue();
9222}
9223
Chris Lattner96908b12010-12-05 02:00:51 +00009224static bool isAllOnes(SDValue V) {
9225 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9226 return C && C->isAllOnesValue();
9227}
9228
Evan Chengb64dd5f2012-08-07 22:21:00 +00009229static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9230 if (V.getOpcode() != ISD::TRUNCATE)
9231 return false;
9232
9233 SDValue VOp0 = V.getOperand(0);
9234 unsigned InBits = VOp0.getValueSizeInBits();
9235 unsigned Bits = V.getValueSizeInBits();
9236 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9237}
9238
Dan Gohmand858e902010-04-17 15:26:15 +00009239SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009240 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009241 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009242 SDValue Op1 = Op.getOperand(1);
9243 SDValue Op2 = Op.getOperand(2);
9244 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009245 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009246
Dan Gohman1a492952009-10-20 16:22:37 +00009247 if (Cond.getOpcode() == ISD::SETCC) {
9248 SDValue NewCond = LowerSETCC(Cond, DAG);
9249 if (NewCond.getNode())
9250 Cond = NewCond;
9251 }
Evan Cheng734503b2006-09-11 02:19:56 +00009252
Chris Lattnera2b56002010-12-05 01:23:24 +00009253 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009254 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009255 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009256 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009257 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009258 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9259 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009260 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009261
Chris Lattnera2b56002010-12-05 01:23:24 +00009262 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009263
9264 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009265 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9266 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009267
9268 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009269 // Apply further optimizations for special cases
9270 // (select (x != 0), -1, 0) -> neg & sbb
9271 // (select (x == 0), 0, -1) -> neg & sbb
9272 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009273 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009274 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9275 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009276 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9277 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009278 CmpOp0);
9279 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9280 DAG.getConstant(X86::COND_B, MVT::i8),
9281 SDValue(Neg.getNode(), 1));
9282 return Res;
9283 }
9284
Chris Lattnera2b56002010-12-05 01:23:24 +00009285 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9286 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009287 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009288
Chris Lattner96908b12010-12-05 02:00:51 +00009289 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009290 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9291 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009292
Chris Lattner96908b12010-12-05 02:00:51 +00009293 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9294 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009295
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009296 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009297 if (N2C == 0 || !N2C->isNullValue())
9298 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9299 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009300 }
9301 }
9302
Chris Lattnera2b56002010-12-05 01:23:24 +00009303 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009304 if (Cond.getOpcode() == ISD::AND &&
9305 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9306 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009307 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009308 Cond = Cond.getOperand(0);
9309 }
9310
Evan Cheng3f41d662007-10-08 22:16:29 +00009311 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9312 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009313 unsigned CondOpcode = Cond.getOpcode();
9314 if (CondOpcode == X86ISD::SETCC ||
9315 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009316 CC = Cond.getOperand(0);
9317
Dan Gohman475871a2008-07-27 21:46:04 +00009318 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009319 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009320 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009321
Evan Cheng3f41d662007-10-08 22:16:29 +00009322 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009323 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009324 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009325 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009326
Chris Lattnerd1980a52009-03-12 06:52:53 +00009327 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9328 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009329 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009330 addTest = false;
9331 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009332 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9333 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9334 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9335 Cond.getOperand(0).getValueType() != MVT::i8)) {
9336 SDValue LHS = Cond.getOperand(0);
9337 SDValue RHS = Cond.getOperand(1);
9338 unsigned X86Opcode;
9339 unsigned X86Cond;
9340 SDVTList VTs;
9341 switch (CondOpcode) {
9342 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9343 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9344 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9345 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9346 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9347 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9348 default: llvm_unreachable("unexpected overflowing operator");
9349 }
9350 if (CondOpcode == ISD::UMULO)
9351 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9352 MVT::i32);
9353 else
9354 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9355
9356 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9357
9358 if (CondOpcode == ISD::UMULO)
9359 Cond = X86Op.getValue(2);
9360 else
9361 Cond = X86Op.getValue(1);
9362
9363 CC = DAG.getConstant(X86Cond, MVT::i8);
9364 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009365 }
9366
9367 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009368 // Look pass the truncate if the high bits are known zero.
9369 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9370 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009371
9372 // We know the result of AND is compared against zero. Try to match
9373 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009374 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009375 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009376 if (NewSetCC.getNode()) {
9377 CC = NewSetCC.getOperand(0);
9378 Cond = NewSetCC.getOperand(1);
9379 addTest = false;
9380 }
9381 }
9382 }
9383
9384 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009385 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009386 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009387 }
9388
Benjamin Kramere915ff32010-12-22 23:09:28 +00009389 // a < b ? -1 : 0 -> RES = ~setcc_carry
9390 // a < b ? 0 : -1 -> RES = setcc_carry
9391 // a >= b ? -1 : 0 -> RES = setcc_carry
9392 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009393 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009394 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009395 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9396
9397 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9398 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9399 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9400 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9401 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9402 return DAG.getNOT(DL, Res, Res.getValueType());
9403 return Res;
9404 }
9405 }
9406
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009407 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9408 // widen the cmov and push the truncate through. This avoids introducing a new
9409 // branch during isel and doesn't add any extensions.
9410 if (Op.getValueType() == MVT::i8 &&
9411 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9412 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9413 if (T1.getValueType() == T2.getValueType() &&
9414 // Blacklist CopyFromReg to avoid partial register stalls.
9415 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9416 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009417 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009418 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9419 }
9420 }
9421
Evan Cheng0488db92007-09-25 01:57:46 +00009422 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9423 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009424 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009425 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009426 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009427}
9428
Evan Cheng370e5342008-12-03 08:38:43 +00009429// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9430// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9431// from the AND / OR.
9432static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9433 Opc = Op.getOpcode();
9434 if (Opc != ISD::OR && Opc != ISD::AND)
9435 return false;
9436 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9437 Op.getOperand(0).hasOneUse() &&
9438 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9439 Op.getOperand(1).hasOneUse());
9440}
9441
Evan Cheng961d6d42009-02-02 08:19:07 +00009442// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9443// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009444static bool isXor1OfSetCC(SDValue Op) {
9445 if (Op.getOpcode() != ISD::XOR)
9446 return false;
9447 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9448 if (N1C && N1C->getAPIntValue() == 1) {
9449 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9450 Op.getOperand(0).hasOneUse();
9451 }
9452 return false;
9453}
9454
Dan Gohmand858e902010-04-17 15:26:15 +00009455SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009456 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009457 SDValue Chain = Op.getOperand(0);
9458 SDValue Cond = Op.getOperand(1);
9459 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009460 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009461 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009462 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009463
Dan Gohman1a492952009-10-20 16:22:37 +00009464 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009465 // Check for setcc([su]{add,sub,mul}o == 0).
9466 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9467 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9468 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9469 Cond.getOperand(0).getResNo() == 1 &&
9470 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9471 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9472 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9473 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9474 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9475 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9476 Inverted = true;
9477 Cond = Cond.getOperand(0);
9478 } else {
9479 SDValue NewCond = LowerSETCC(Cond, DAG);
9480 if (NewCond.getNode())
9481 Cond = NewCond;
9482 }
Dan Gohman1a492952009-10-20 16:22:37 +00009483 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009484#if 0
9485 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009486 else if (Cond.getOpcode() == X86ISD::ADD ||
9487 Cond.getOpcode() == X86ISD::SUB ||
9488 Cond.getOpcode() == X86ISD::SMUL ||
9489 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009490 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009491#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009492
Evan Chengad9c0a32009-12-15 00:53:42 +00009493 // Look pass (and (setcc_carry (cmp ...)), 1).
9494 if (Cond.getOpcode() == ISD::AND &&
9495 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9496 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009497 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009498 Cond = Cond.getOperand(0);
9499 }
9500
Evan Cheng3f41d662007-10-08 22:16:29 +00009501 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9502 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009503 unsigned CondOpcode = Cond.getOpcode();
9504 if (CondOpcode == X86ISD::SETCC ||
9505 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009506 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009507
Dan Gohman475871a2008-07-27 21:46:04 +00009508 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009509 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009510 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009511 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009512 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009513 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009514 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009515 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009516 default: break;
9517 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009518 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009519 // These can only come from an arithmetic instruction with overflow,
9520 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009521 Cond = Cond.getNode()->getOperand(1);
9522 addTest = false;
9523 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009524 }
Evan Cheng0488db92007-09-25 01:57:46 +00009525 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009526 }
9527 CondOpcode = Cond.getOpcode();
9528 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9529 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9530 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9531 Cond.getOperand(0).getValueType() != MVT::i8)) {
9532 SDValue LHS = Cond.getOperand(0);
9533 SDValue RHS = Cond.getOperand(1);
9534 unsigned X86Opcode;
9535 unsigned X86Cond;
9536 SDVTList VTs;
9537 switch (CondOpcode) {
9538 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9539 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9540 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9541 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9542 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9543 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9544 default: llvm_unreachable("unexpected overflowing operator");
9545 }
9546 if (Inverted)
9547 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9548 if (CondOpcode == ISD::UMULO)
9549 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9550 MVT::i32);
9551 else
9552 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9553
9554 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9555
9556 if (CondOpcode == ISD::UMULO)
9557 Cond = X86Op.getValue(2);
9558 else
9559 Cond = X86Op.getValue(1);
9560
9561 CC = DAG.getConstant(X86Cond, MVT::i8);
9562 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009563 } else {
9564 unsigned CondOpc;
9565 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9566 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009567 if (CondOpc == ISD::OR) {
9568 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9569 // two branches instead of an explicit OR instruction with a
9570 // separate test.
9571 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009572 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009573 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009574 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009575 Chain, Dest, CC, Cmp);
9576 CC = Cond.getOperand(1).getOperand(0);
9577 Cond = Cmp;
9578 addTest = false;
9579 }
9580 } else { // ISD::AND
9581 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9582 // two branches instead of an explicit AND instruction with a
9583 // separate test. However, we only do this if this block doesn't
9584 // have a fall-through edge, because this requires an explicit
9585 // jmp when the condition is false.
9586 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009587 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009588 Op.getNode()->hasOneUse()) {
9589 X86::CondCode CCode =
9590 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9591 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009593 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009594 // Look for an unconditional branch following this conditional branch.
9595 // We need this because we need to reverse the successors in order
9596 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009597 if (User->getOpcode() == ISD::BR) {
9598 SDValue FalseBB = User->getOperand(1);
9599 SDNode *NewBR =
9600 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009601 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009602 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009603 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009604
Dale Johannesene4d209d2009-02-03 20:21:25 +00009605 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009606 Chain, Dest, CC, Cmp);
9607 X86::CondCode CCode =
9608 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9609 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009610 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009611 Cond = Cmp;
9612 addTest = false;
9613 }
9614 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009615 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009616 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9617 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9618 // It should be transformed during dag combiner except when the condition
9619 // is set by a arithmetics with overflow node.
9620 X86::CondCode CCode =
9621 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9622 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009623 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009624 Cond = Cond.getOperand(0).getOperand(1);
9625 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009626 } else if (Cond.getOpcode() == ISD::SETCC &&
9627 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9628 // For FCMP_OEQ, we can emit
9629 // two branches instead of an explicit AND instruction with a
9630 // separate test. However, we only do this if this block doesn't
9631 // have a fall-through edge, because this requires an explicit
9632 // jmp when the condition is false.
9633 if (Op.getNode()->hasOneUse()) {
9634 SDNode *User = *Op.getNode()->use_begin();
9635 // Look for an unconditional branch following this conditional branch.
9636 // We need this because we need to reverse the successors in order
9637 // to implement FCMP_OEQ.
9638 if (User->getOpcode() == ISD::BR) {
9639 SDValue FalseBB = User->getOperand(1);
9640 SDNode *NewBR =
9641 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9642 assert(NewBR == User);
9643 (void)NewBR;
9644 Dest = FalseBB;
9645
9646 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9647 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009648 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009649 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9650 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9651 Chain, Dest, CC, Cmp);
9652 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9653 Cond = Cmp;
9654 addTest = false;
9655 }
9656 }
9657 } else if (Cond.getOpcode() == ISD::SETCC &&
9658 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9659 // For FCMP_UNE, we can emit
9660 // two branches instead of an explicit AND instruction with a
9661 // separate test. However, we only do this if this block doesn't
9662 // have a fall-through edge, because this requires an explicit
9663 // jmp when the condition is false.
9664 if (Op.getNode()->hasOneUse()) {
9665 SDNode *User = *Op.getNode()->use_begin();
9666 // Look for an unconditional branch following this conditional branch.
9667 // We need this because we need to reverse the successors in order
9668 // to implement FCMP_UNE.
9669 if (User->getOpcode() == ISD::BR) {
9670 SDValue FalseBB = User->getOperand(1);
9671 SDNode *NewBR =
9672 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9673 assert(NewBR == User);
9674 (void)NewBR;
9675
9676 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9677 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009678 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009679 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9680 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9681 Chain, Dest, CC, Cmp);
9682 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9683 Cond = Cmp;
9684 addTest = false;
9685 Dest = FalseBB;
9686 }
9687 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009688 }
Evan Cheng0488db92007-09-25 01:57:46 +00009689 }
9690
9691 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009692 // Look pass the truncate if the high bits are known zero.
9693 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9694 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009695
9696 // We know the result of AND is compared against zero. Try to match
9697 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009698 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009699 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9700 if (NewSetCC.getNode()) {
9701 CC = NewSetCC.getOperand(0);
9702 Cond = NewSetCC.getOperand(1);
9703 addTest = false;
9704 }
9705 }
9706 }
9707
9708 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009709 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009710 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009711 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009712 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009713 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009714 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009715}
9716
Anton Korobeynikove060b532007-04-17 19:34:00 +00009717
9718// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9719// Calls to _alloca is needed to probe the stack when allocating more than 4k
9720// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9721// that the guard pages used by the OS virtual memory manager are allocated in
9722// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009723SDValue
9724X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009725 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009726 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009727 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009728 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009729 "are being used");
9730 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009731 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009732
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009733 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009734 SDValue Chain = Op.getOperand(0);
9735 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009736 // FIXME: Ensure alignment here
9737
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009738 bool Is64Bit = Subtarget->is64Bit();
9739 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009740
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009741 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009742 MachineFunction &MF = DAG.getMachineFunction();
9743 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009744
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009745 if (Is64Bit) {
9746 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009747 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009748 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009749
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009750 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009751 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009752 if (I->hasNestAttr())
9753 report_fatal_error("Cannot use segmented stacks with functions that "
9754 "have nested arguments.");
9755 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009756
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009757 const TargetRegisterClass *AddrRegClass =
9758 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9759 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9760 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9761 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9762 DAG.getRegister(Vreg, SPTy));
9763 SDValue Ops1[2] = { Value, Chain };
9764 return DAG.getMergeValues(Ops1, 2, dl);
9765 } else {
9766 SDValue Flag;
9767 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009768
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009769 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9770 Flag = Chain.getValue(1);
9771 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009772
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009773 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9774 Flag = Chain.getValue(1);
9775
Michael Liaoc5c970e2012-10-31 04:14:09 +00009776 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
9777 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009778
9779 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9780 return DAG.getMergeValues(Ops1, 2, dl);
9781 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009782}
9783
Dan Gohmand858e902010-04-17 15:26:15 +00009784SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009785 MachineFunction &MF = DAG.getMachineFunction();
9786 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9787
Dan Gohman69de1932008-02-06 22:27:42 +00009788 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009789 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009790
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009791 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009792 // vastart just stores the address of the VarArgsFrameIndex slot into the
9793 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009794 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9795 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009796 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9797 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009798 }
9799
9800 // __va_list_tag:
9801 // gp_offset (0 - 6 * 8)
9802 // fp_offset (48 - 48 + 8 * 16)
9803 // overflow_arg_area (point to parameters coming in memory).
9804 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009805 SmallVector<SDValue, 8> MemOps;
9806 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009807 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009808 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009809 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9810 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009811 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009812 MemOps.push_back(Store);
9813
9814 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009815 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009816 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009817 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009818 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9819 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009820 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009821 MemOps.push_back(Store);
9822
9823 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009824 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009825 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009826 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9827 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009828 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9829 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009830 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009831 MemOps.push_back(Store);
9832
9833 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009834 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009835 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009836 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9837 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009838 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9839 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009840 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009841 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009842 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009843}
9844
Dan Gohmand858e902010-04-17 15:26:15 +00009845SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009846 assert(Subtarget->is64Bit() &&
9847 "LowerVAARG only handles 64-bit va_arg!");
9848 assert((Subtarget->isTargetLinux() ||
9849 Subtarget->isTargetDarwin()) &&
9850 "Unhandled target in LowerVAARG");
9851 assert(Op.getNode()->getNumOperands() == 4);
9852 SDValue Chain = Op.getOperand(0);
9853 SDValue SrcPtr = Op.getOperand(1);
9854 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9855 unsigned Align = Op.getConstantOperandVal(3);
9856 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009857
Dan Gohman320afb82010-10-12 18:00:49 +00009858 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009859 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009860 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009861 uint8_t ArgMode;
9862
9863 // Decide which area this value should be read from.
9864 // TODO: Implement the AMD64 ABI in its entirety. This simple
9865 // selection mechanism works only for the basic types.
9866 if (ArgVT == MVT::f80) {
9867 llvm_unreachable("va_arg for f80 not yet implemented");
9868 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9869 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9870 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9871 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9872 } else {
9873 llvm_unreachable("Unhandled argument type in LowerVAARG");
9874 }
9875
9876 if (ArgMode == 2) {
9877 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009878 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009879 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009880 .getFunction()->getFnAttributes()
9881 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009882 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009883 }
9884
9885 // Insert VAARG_64 node into the DAG
9886 // VAARG_64 returns two values: Variable Argument Address, Chain
9887 SmallVector<SDValue, 11> InstOps;
9888 InstOps.push_back(Chain);
9889 InstOps.push_back(SrcPtr);
9890 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9891 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9892 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9893 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9894 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9895 VTs, &InstOps[0], InstOps.size(),
9896 MVT::i64,
9897 MachinePointerInfo(SV),
9898 /*Align=*/0,
9899 /*Volatile=*/false,
9900 /*ReadMem=*/true,
9901 /*WriteMem=*/true);
9902 Chain = VAARG.getValue(1);
9903
9904 // Load the next argument and return it
9905 return DAG.getLoad(ArgVT, dl,
9906 Chain,
9907 VAARG,
9908 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009909 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009910}
9911
Craig Topper55b24052012-09-11 06:15:32 +00009912static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9913 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009914 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009915 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009916 SDValue Chain = Op.getOperand(0);
9917 SDValue DstPtr = Op.getOperand(1);
9918 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009919 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9920 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009921 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009922
Chris Lattnere72f2022010-09-21 05:40:29 +00009923 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009924 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009925 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009926 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009927}
9928
Craig Topper80e46362012-01-23 06:16:53 +00009929// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9930// may or may not be a constant. Takes immediate version of shift as input.
9931static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9932 SDValue SrcOp, SDValue ShAmt,
9933 SelectionDAG &DAG) {
9934 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9935
9936 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009937 // Constant may be a TargetConstant. Use a regular constant.
9938 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009939 switch (Opc) {
9940 default: llvm_unreachable("Unknown target vector shift node");
9941 case X86ISD::VSHLI:
9942 case X86ISD::VSRLI:
9943 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009944 return DAG.getNode(Opc, dl, VT, SrcOp,
9945 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009946 }
9947 }
9948
9949 // Change opcode to non-immediate version
9950 switch (Opc) {
9951 default: llvm_unreachable("Unknown target vector shift node");
9952 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9953 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9954 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9955 }
9956
9957 // Need to build a vector containing shift amount
9958 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9959 SDValue ShOps[4];
9960 ShOps[0] = ShAmt;
9961 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009962 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009963 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009964
9965 // The return type has to be a 128-bit type with the same element
9966 // type as the input type.
9967 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9968 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9969
9970 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009971 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9972}
9973
Craig Topper55b24052012-09-11 06:15:32 +00009974static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009975 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009976 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009977 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009978 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009979 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009980 case Intrinsic::x86_sse_comieq_ss:
9981 case Intrinsic::x86_sse_comilt_ss:
9982 case Intrinsic::x86_sse_comile_ss:
9983 case Intrinsic::x86_sse_comigt_ss:
9984 case Intrinsic::x86_sse_comige_ss:
9985 case Intrinsic::x86_sse_comineq_ss:
9986 case Intrinsic::x86_sse_ucomieq_ss:
9987 case Intrinsic::x86_sse_ucomilt_ss:
9988 case Intrinsic::x86_sse_ucomile_ss:
9989 case Intrinsic::x86_sse_ucomigt_ss:
9990 case Intrinsic::x86_sse_ucomige_ss:
9991 case Intrinsic::x86_sse_ucomineq_ss:
9992 case Intrinsic::x86_sse2_comieq_sd:
9993 case Intrinsic::x86_sse2_comilt_sd:
9994 case Intrinsic::x86_sse2_comile_sd:
9995 case Intrinsic::x86_sse2_comigt_sd:
9996 case Intrinsic::x86_sse2_comige_sd:
9997 case Intrinsic::x86_sse2_comineq_sd:
9998 case Intrinsic::x86_sse2_ucomieq_sd:
9999 case Intrinsic::x86_sse2_ucomilt_sd:
10000 case Intrinsic::x86_sse2_ucomile_sd:
10001 case Intrinsic::x86_sse2_ucomigt_sd:
10002 case Intrinsic::x86_sse2_ucomige_sd:
10003 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010004 unsigned Opc;
10005 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010006 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010007 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010008 case Intrinsic::x86_sse_comieq_ss:
10009 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010010 Opc = X86ISD::COMI;
10011 CC = ISD::SETEQ;
10012 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010013 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010014 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010015 Opc = X86ISD::COMI;
10016 CC = ISD::SETLT;
10017 break;
10018 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010019 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010020 Opc = X86ISD::COMI;
10021 CC = ISD::SETLE;
10022 break;
10023 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010024 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010025 Opc = X86ISD::COMI;
10026 CC = ISD::SETGT;
10027 break;
10028 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010029 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010030 Opc = X86ISD::COMI;
10031 CC = ISD::SETGE;
10032 break;
10033 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010034 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010035 Opc = X86ISD::COMI;
10036 CC = ISD::SETNE;
10037 break;
10038 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010039 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010040 Opc = X86ISD::UCOMI;
10041 CC = ISD::SETEQ;
10042 break;
10043 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010044 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010045 Opc = X86ISD::UCOMI;
10046 CC = ISD::SETLT;
10047 break;
10048 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010049 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010050 Opc = X86ISD::UCOMI;
10051 CC = ISD::SETLE;
10052 break;
10053 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010054 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010055 Opc = X86ISD::UCOMI;
10056 CC = ISD::SETGT;
10057 break;
10058 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010059 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010060 Opc = X86ISD::UCOMI;
10061 CC = ISD::SETGE;
10062 break;
10063 case Intrinsic::x86_sse_ucomineq_ss:
10064 case Intrinsic::x86_sse2_ucomineq_sd:
10065 Opc = X86ISD::UCOMI;
10066 CC = ISD::SETNE;
10067 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010068 }
Evan Cheng734503b2006-09-11 02:19:56 +000010069
Dan Gohman475871a2008-07-27 21:46:04 +000010070 SDValue LHS = Op.getOperand(1);
10071 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010072 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010073 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010074 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10075 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10076 DAG.getConstant(X86CC, MVT::i8), Cond);
10077 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010078 }
Craig Topper6d688152012-08-14 07:43:25 +000010079
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010080 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010081 case Intrinsic::x86_sse2_pmulu_dq:
10082 case Intrinsic::x86_avx2_pmulu_dq:
10083 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10084 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010085
10086 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010087 case Intrinsic::x86_sse3_hadd_ps:
10088 case Intrinsic::x86_sse3_hadd_pd:
10089 case Intrinsic::x86_avx_hadd_ps_256:
10090 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010091 case Intrinsic::x86_sse3_hsub_ps:
10092 case Intrinsic::x86_sse3_hsub_pd:
10093 case Intrinsic::x86_avx_hsub_ps_256:
10094 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010095 case Intrinsic::x86_ssse3_phadd_w_128:
10096 case Intrinsic::x86_ssse3_phadd_d_128:
10097 case Intrinsic::x86_avx2_phadd_w:
10098 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010099 case Intrinsic::x86_ssse3_phsub_w_128:
10100 case Intrinsic::x86_ssse3_phsub_d_128:
10101 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010102 case Intrinsic::x86_avx2_phsub_d: {
10103 unsigned Opcode;
10104 switch (IntNo) {
10105 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10106 case Intrinsic::x86_sse3_hadd_ps:
10107 case Intrinsic::x86_sse3_hadd_pd:
10108 case Intrinsic::x86_avx_hadd_ps_256:
10109 case Intrinsic::x86_avx_hadd_pd_256:
10110 Opcode = X86ISD::FHADD;
10111 break;
10112 case Intrinsic::x86_sse3_hsub_ps:
10113 case Intrinsic::x86_sse3_hsub_pd:
10114 case Intrinsic::x86_avx_hsub_ps_256:
10115 case Intrinsic::x86_avx_hsub_pd_256:
10116 Opcode = X86ISD::FHSUB;
10117 break;
10118 case Intrinsic::x86_ssse3_phadd_w_128:
10119 case Intrinsic::x86_ssse3_phadd_d_128:
10120 case Intrinsic::x86_avx2_phadd_w:
10121 case Intrinsic::x86_avx2_phadd_d:
10122 Opcode = X86ISD::HADD;
10123 break;
10124 case Intrinsic::x86_ssse3_phsub_w_128:
10125 case Intrinsic::x86_ssse3_phsub_d_128:
10126 case Intrinsic::x86_avx2_phsub_w:
10127 case Intrinsic::x86_avx2_phsub_d:
10128 Opcode = X86ISD::HSUB;
10129 break;
10130 }
10131 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010132 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010133 }
10134
10135 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010136 case Intrinsic::x86_avx2_psllv_d:
10137 case Intrinsic::x86_avx2_psllv_q:
10138 case Intrinsic::x86_avx2_psllv_d_256:
10139 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010140 case Intrinsic::x86_avx2_psrlv_d:
10141 case Intrinsic::x86_avx2_psrlv_q:
10142 case Intrinsic::x86_avx2_psrlv_d_256:
10143 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010144 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010145 case Intrinsic::x86_avx2_psrav_d_256: {
10146 unsigned Opcode;
10147 switch (IntNo) {
10148 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10149 case Intrinsic::x86_avx2_psllv_d:
10150 case Intrinsic::x86_avx2_psllv_q:
10151 case Intrinsic::x86_avx2_psllv_d_256:
10152 case Intrinsic::x86_avx2_psllv_q_256:
10153 Opcode = ISD::SHL;
10154 break;
10155 case Intrinsic::x86_avx2_psrlv_d:
10156 case Intrinsic::x86_avx2_psrlv_q:
10157 case Intrinsic::x86_avx2_psrlv_d_256:
10158 case Intrinsic::x86_avx2_psrlv_q_256:
10159 Opcode = ISD::SRL;
10160 break;
10161 case Intrinsic::x86_avx2_psrav_d:
10162 case Intrinsic::x86_avx2_psrav_d_256:
10163 Opcode = ISD::SRA;
10164 break;
10165 }
10166 return DAG.getNode(Opcode, dl, Op.getValueType(),
10167 Op.getOperand(1), Op.getOperand(2));
10168 }
10169
Craig Topper969ba282012-01-25 06:43:11 +000010170 case Intrinsic::x86_ssse3_pshuf_b_128:
10171 case Intrinsic::x86_avx2_pshuf_b:
10172 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10173 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010174
Craig Topper969ba282012-01-25 06:43:11 +000010175 case Intrinsic::x86_ssse3_psign_b_128:
10176 case Intrinsic::x86_ssse3_psign_w_128:
10177 case Intrinsic::x86_ssse3_psign_d_128:
10178 case Intrinsic::x86_avx2_psign_b:
10179 case Intrinsic::x86_avx2_psign_w:
10180 case Intrinsic::x86_avx2_psign_d:
10181 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10182 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010183
Craig Toppere566cd02012-01-26 07:18:03 +000010184 case Intrinsic::x86_sse41_insertps:
10185 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10186 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010187
Craig Toppere566cd02012-01-26 07:18:03 +000010188 case Intrinsic::x86_avx_vperm2f128_ps_256:
10189 case Intrinsic::x86_avx_vperm2f128_pd_256:
10190 case Intrinsic::x86_avx_vperm2f128_si_256:
10191 case Intrinsic::x86_avx2_vperm2i128:
10192 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10193 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010194
Craig Topperffa6c402012-04-16 07:13:00 +000010195 case Intrinsic::x86_avx2_permd:
10196 case Intrinsic::x86_avx2_permps:
10197 // Operands intentionally swapped. Mask is last operand to intrinsic,
10198 // but second operand for node/intruction.
10199 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10200 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010201
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010202 // ptest and testp intrinsics. The intrinsic these come from are designed to
10203 // return an integer value, not just an instruction so lower it to the ptest
10204 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010205 case Intrinsic::x86_sse41_ptestz:
10206 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010207 case Intrinsic::x86_sse41_ptestnzc:
10208 case Intrinsic::x86_avx_ptestz_256:
10209 case Intrinsic::x86_avx_ptestc_256:
10210 case Intrinsic::x86_avx_ptestnzc_256:
10211 case Intrinsic::x86_avx_vtestz_ps:
10212 case Intrinsic::x86_avx_vtestc_ps:
10213 case Intrinsic::x86_avx_vtestnzc_ps:
10214 case Intrinsic::x86_avx_vtestz_pd:
10215 case Intrinsic::x86_avx_vtestc_pd:
10216 case Intrinsic::x86_avx_vtestnzc_pd:
10217 case Intrinsic::x86_avx_vtestz_ps_256:
10218 case Intrinsic::x86_avx_vtestc_ps_256:
10219 case Intrinsic::x86_avx_vtestnzc_ps_256:
10220 case Intrinsic::x86_avx_vtestz_pd_256:
10221 case Intrinsic::x86_avx_vtestc_pd_256:
10222 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10223 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010224 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010225 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010226 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010227 case Intrinsic::x86_avx_vtestz_ps:
10228 case Intrinsic::x86_avx_vtestz_pd:
10229 case Intrinsic::x86_avx_vtestz_ps_256:
10230 case Intrinsic::x86_avx_vtestz_pd_256:
10231 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010232 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010233 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010234 // ZF = 1
10235 X86CC = X86::COND_E;
10236 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010237 case Intrinsic::x86_avx_vtestc_ps:
10238 case Intrinsic::x86_avx_vtestc_pd:
10239 case Intrinsic::x86_avx_vtestc_ps_256:
10240 case Intrinsic::x86_avx_vtestc_pd_256:
10241 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010242 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010243 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010244 // CF = 1
10245 X86CC = X86::COND_B;
10246 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010247 case Intrinsic::x86_avx_vtestnzc_ps:
10248 case Intrinsic::x86_avx_vtestnzc_pd:
10249 case Intrinsic::x86_avx_vtestnzc_ps_256:
10250 case Intrinsic::x86_avx_vtestnzc_pd_256:
10251 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010252 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010253 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010254 // ZF and CF = 0
10255 X86CC = X86::COND_A;
10256 break;
10257 }
Eric Christopherfd179292009-08-27 18:07:15 +000010258
Eric Christopher71c67532009-07-29 00:28:05 +000010259 SDValue LHS = Op.getOperand(1);
10260 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010261 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10262 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10264 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10265 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010266 }
Evan Cheng5759f972008-05-04 09:15:50 +000010267
Craig Topper80e46362012-01-23 06:16:53 +000010268 // SSE/AVX shift intrinsics
10269 case Intrinsic::x86_sse2_psll_w:
10270 case Intrinsic::x86_sse2_psll_d:
10271 case Intrinsic::x86_sse2_psll_q:
10272 case Intrinsic::x86_avx2_psll_w:
10273 case Intrinsic::x86_avx2_psll_d:
10274 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010275 case Intrinsic::x86_sse2_psrl_w:
10276 case Intrinsic::x86_sse2_psrl_d:
10277 case Intrinsic::x86_sse2_psrl_q:
10278 case Intrinsic::x86_avx2_psrl_w:
10279 case Intrinsic::x86_avx2_psrl_d:
10280 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010281 case Intrinsic::x86_sse2_psra_w:
10282 case Intrinsic::x86_sse2_psra_d:
10283 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010284 case Intrinsic::x86_avx2_psra_d: {
10285 unsigned Opcode;
10286 switch (IntNo) {
10287 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10288 case Intrinsic::x86_sse2_psll_w:
10289 case Intrinsic::x86_sse2_psll_d:
10290 case Intrinsic::x86_sse2_psll_q:
10291 case Intrinsic::x86_avx2_psll_w:
10292 case Intrinsic::x86_avx2_psll_d:
10293 case Intrinsic::x86_avx2_psll_q:
10294 Opcode = X86ISD::VSHL;
10295 break;
10296 case Intrinsic::x86_sse2_psrl_w:
10297 case Intrinsic::x86_sse2_psrl_d:
10298 case Intrinsic::x86_sse2_psrl_q:
10299 case Intrinsic::x86_avx2_psrl_w:
10300 case Intrinsic::x86_avx2_psrl_d:
10301 case Intrinsic::x86_avx2_psrl_q:
10302 Opcode = X86ISD::VSRL;
10303 break;
10304 case Intrinsic::x86_sse2_psra_w:
10305 case Intrinsic::x86_sse2_psra_d:
10306 case Intrinsic::x86_avx2_psra_w:
10307 case Intrinsic::x86_avx2_psra_d:
10308 Opcode = X86ISD::VSRA;
10309 break;
10310 }
10311 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010312 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010313 }
10314
10315 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010316 case Intrinsic::x86_sse2_pslli_w:
10317 case Intrinsic::x86_sse2_pslli_d:
10318 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010319 case Intrinsic::x86_avx2_pslli_w:
10320 case Intrinsic::x86_avx2_pslli_d:
10321 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010322 case Intrinsic::x86_sse2_psrli_w:
10323 case Intrinsic::x86_sse2_psrli_d:
10324 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010325 case Intrinsic::x86_avx2_psrli_w:
10326 case Intrinsic::x86_avx2_psrli_d:
10327 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010328 case Intrinsic::x86_sse2_psrai_w:
10329 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010330 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010331 case Intrinsic::x86_avx2_psrai_d: {
10332 unsigned Opcode;
10333 switch (IntNo) {
10334 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10335 case Intrinsic::x86_sse2_pslli_w:
10336 case Intrinsic::x86_sse2_pslli_d:
10337 case Intrinsic::x86_sse2_pslli_q:
10338 case Intrinsic::x86_avx2_pslli_w:
10339 case Intrinsic::x86_avx2_pslli_d:
10340 case Intrinsic::x86_avx2_pslli_q:
10341 Opcode = X86ISD::VSHLI;
10342 break;
10343 case Intrinsic::x86_sse2_psrli_w:
10344 case Intrinsic::x86_sse2_psrli_d:
10345 case Intrinsic::x86_sse2_psrli_q:
10346 case Intrinsic::x86_avx2_psrli_w:
10347 case Intrinsic::x86_avx2_psrli_d:
10348 case Intrinsic::x86_avx2_psrli_q:
10349 Opcode = X86ISD::VSRLI;
10350 break;
10351 case Intrinsic::x86_sse2_psrai_w:
10352 case Intrinsic::x86_sse2_psrai_d:
10353 case Intrinsic::x86_avx2_psrai_w:
10354 case Intrinsic::x86_avx2_psrai_d:
10355 Opcode = X86ISD::VSRAI;
10356 break;
10357 }
10358 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010359 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010360 }
10361
Craig Topper4feb6472012-08-06 06:22:36 +000010362 case Intrinsic::x86_sse42_pcmpistria128:
10363 case Intrinsic::x86_sse42_pcmpestria128:
10364 case Intrinsic::x86_sse42_pcmpistric128:
10365 case Intrinsic::x86_sse42_pcmpestric128:
10366 case Intrinsic::x86_sse42_pcmpistrio128:
10367 case Intrinsic::x86_sse42_pcmpestrio128:
10368 case Intrinsic::x86_sse42_pcmpistris128:
10369 case Intrinsic::x86_sse42_pcmpestris128:
10370 case Intrinsic::x86_sse42_pcmpistriz128:
10371 case Intrinsic::x86_sse42_pcmpestriz128: {
10372 unsigned Opcode;
10373 unsigned X86CC;
10374 switch (IntNo) {
10375 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10376 case Intrinsic::x86_sse42_pcmpistria128:
10377 Opcode = X86ISD::PCMPISTRI;
10378 X86CC = X86::COND_A;
10379 break;
10380 case Intrinsic::x86_sse42_pcmpestria128:
10381 Opcode = X86ISD::PCMPESTRI;
10382 X86CC = X86::COND_A;
10383 break;
10384 case Intrinsic::x86_sse42_pcmpistric128:
10385 Opcode = X86ISD::PCMPISTRI;
10386 X86CC = X86::COND_B;
10387 break;
10388 case Intrinsic::x86_sse42_pcmpestric128:
10389 Opcode = X86ISD::PCMPESTRI;
10390 X86CC = X86::COND_B;
10391 break;
10392 case Intrinsic::x86_sse42_pcmpistrio128:
10393 Opcode = X86ISD::PCMPISTRI;
10394 X86CC = X86::COND_O;
10395 break;
10396 case Intrinsic::x86_sse42_pcmpestrio128:
10397 Opcode = X86ISD::PCMPESTRI;
10398 X86CC = X86::COND_O;
10399 break;
10400 case Intrinsic::x86_sse42_pcmpistris128:
10401 Opcode = X86ISD::PCMPISTRI;
10402 X86CC = X86::COND_S;
10403 break;
10404 case Intrinsic::x86_sse42_pcmpestris128:
10405 Opcode = X86ISD::PCMPESTRI;
10406 X86CC = X86::COND_S;
10407 break;
10408 case Intrinsic::x86_sse42_pcmpistriz128:
10409 Opcode = X86ISD::PCMPISTRI;
10410 X86CC = X86::COND_E;
10411 break;
10412 case Intrinsic::x86_sse42_pcmpestriz128:
10413 Opcode = X86ISD::PCMPESTRI;
10414 X86CC = X86::COND_E;
10415 break;
10416 }
10417 SmallVector<SDValue, 5> NewOps;
10418 NewOps.append(Op->op_begin()+1, Op->op_end());
10419 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10420 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10421 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10422 DAG.getConstant(X86CC, MVT::i8),
10423 SDValue(PCMP.getNode(), 1));
10424 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10425 }
Craig Topper6d688152012-08-14 07:43:25 +000010426
Craig Topper4feb6472012-08-06 06:22:36 +000010427 case Intrinsic::x86_sse42_pcmpistri128:
10428 case Intrinsic::x86_sse42_pcmpestri128: {
10429 unsigned Opcode;
10430 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10431 Opcode = X86ISD::PCMPISTRI;
10432 else
10433 Opcode = X86ISD::PCMPESTRI;
10434
10435 SmallVector<SDValue, 5> NewOps;
10436 NewOps.append(Op->op_begin()+1, Op->op_end());
10437 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10438 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10439 }
Craig Topper0e292372012-08-24 04:03:22 +000010440 case Intrinsic::x86_fma_vfmadd_ps:
10441 case Intrinsic::x86_fma_vfmadd_pd:
10442 case Intrinsic::x86_fma_vfmsub_ps:
10443 case Intrinsic::x86_fma_vfmsub_pd:
10444 case Intrinsic::x86_fma_vfnmadd_ps:
10445 case Intrinsic::x86_fma_vfnmadd_pd:
10446 case Intrinsic::x86_fma_vfnmsub_ps:
10447 case Intrinsic::x86_fma_vfnmsub_pd:
10448 case Intrinsic::x86_fma_vfmaddsub_ps:
10449 case Intrinsic::x86_fma_vfmaddsub_pd:
10450 case Intrinsic::x86_fma_vfmsubadd_ps:
10451 case Intrinsic::x86_fma_vfmsubadd_pd:
10452 case Intrinsic::x86_fma_vfmadd_ps_256:
10453 case Intrinsic::x86_fma_vfmadd_pd_256:
10454 case Intrinsic::x86_fma_vfmsub_ps_256:
10455 case Intrinsic::x86_fma_vfmsub_pd_256:
10456 case Intrinsic::x86_fma_vfnmadd_ps_256:
10457 case Intrinsic::x86_fma_vfnmadd_pd_256:
10458 case Intrinsic::x86_fma_vfnmsub_ps_256:
10459 case Intrinsic::x86_fma_vfnmsub_pd_256:
10460 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10461 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10462 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10463 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010464 unsigned Opc;
10465 switch (IntNo) {
10466 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10467 case Intrinsic::x86_fma_vfmadd_ps:
10468 case Intrinsic::x86_fma_vfmadd_pd:
10469 case Intrinsic::x86_fma_vfmadd_ps_256:
10470 case Intrinsic::x86_fma_vfmadd_pd_256:
10471 Opc = X86ISD::FMADD;
10472 break;
10473 case Intrinsic::x86_fma_vfmsub_ps:
10474 case Intrinsic::x86_fma_vfmsub_pd:
10475 case Intrinsic::x86_fma_vfmsub_ps_256:
10476 case Intrinsic::x86_fma_vfmsub_pd_256:
10477 Opc = X86ISD::FMSUB;
10478 break;
10479 case Intrinsic::x86_fma_vfnmadd_ps:
10480 case Intrinsic::x86_fma_vfnmadd_pd:
10481 case Intrinsic::x86_fma_vfnmadd_ps_256:
10482 case Intrinsic::x86_fma_vfnmadd_pd_256:
10483 Opc = X86ISD::FNMADD;
10484 break;
10485 case Intrinsic::x86_fma_vfnmsub_ps:
10486 case Intrinsic::x86_fma_vfnmsub_pd:
10487 case Intrinsic::x86_fma_vfnmsub_ps_256:
10488 case Intrinsic::x86_fma_vfnmsub_pd_256:
10489 Opc = X86ISD::FNMSUB;
10490 break;
10491 case Intrinsic::x86_fma_vfmaddsub_ps:
10492 case Intrinsic::x86_fma_vfmaddsub_pd:
10493 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10494 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10495 Opc = X86ISD::FMADDSUB;
10496 break;
10497 case Intrinsic::x86_fma_vfmsubadd_ps:
10498 case Intrinsic::x86_fma_vfmsubadd_pd:
10499 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10500 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10501 Opc = X86ISD::FMSUBADD;
10502 break;
10503 }
10504
10505 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10506 Op.getOperand(2), Op.getOperand(3));
10507 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010508 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010509}
Evan Cheng72261582005-12-20 06:22:03 +000010510
Craig Topper55b24052012-09-11 06:15:32 +000010511static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010512 DebugLoc dl = Op.getDebugLoc();
10513 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10514 switch (IntNo) {
10515 default: return SDValue(); // Don't custom lower most intrinsics.
10516
10517 // RDRAND intrinsics.
10518 case Intrinsic::x86_rdrand_16:
10519 case Intrinsic::x86_rdrand_32:
10520 case Intrinsic::x86_rdrand_64: {
10521 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010522 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10523 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010524
10525 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10526 // return the value from Rand, which is always 0, casted to i32.
10527 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10528 DAG.getConstant(1, Op->getValueType(1)),
10529 DAG.getConstant(X86::COND_B, MVT::i32),
10530 SDValue(Result.getNode(), 1) };
10531 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10532 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10533 Ops, 4);
10534
10535 // Return { result, isValid, chain }.
10536 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010537 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010538 }
10539 }
10540}
10541
Dan Gohmand858e902010-04-17 15:26:15 +000010542SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10543 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010544 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10545 MFI->setReturnAddressIsTaken(true);
10546
Bill Wendling64e87322009-01-16 19:25:27 +000010547 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010548 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010549 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010550
10551 if (Depth > 0) {
10552 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10553 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010554 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10555 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10556 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010557 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010558 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010559 }
10560
10561 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010562 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010563 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010564 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010565}
10566
Dan Gohmand858e902010-04-17 15:26:15 +000010567SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010568 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10569 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010570
Owen Andersone50ed302009-08-10 22:56:29 +000010571 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010572 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010573 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10574 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010575 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010576 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010577 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10578 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010579 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010580 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010581}
10582
Dan Gohman475871a2008-07-27 21:46:04 +000010583SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010584 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010585 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010586}
10587
Dan Gohmand858e902010-04-17 15:26:15 +000010588SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010589 SDValue Chain = Op.getOperand(0);
10590 SDValue Offset = Op.getOperand(1);
10591 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010592 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010593
Dan Gohmand8816272010-08-11 18:14:00 +000010594 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10595 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10596 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010597 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010598
Dan Gohmand8816272010-08-11 18:14:00 +000010599 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010600 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010601 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010602 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10603 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010604 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010605
Dale Johannesene4d209d2009-02-03 20:21:25 +000010606 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010607 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010608 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010609}
10610
Michael Liao6c0e04c2012-10-15 22:39:43 +000010611SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10612 SelectionDAG &DAG) const {
10613 DebugLoc DL = Op.getDebugLoc();
10614 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10615 DAG.getVTList(MVT::i32, MVT::Other),
10616 Op.getOperand(0), Op.getOperand(1));
10617}
10618
10619SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10620 SelectionDAG &DAG) const {
10621 DebugLoc DL = Op.getDebugLoc();
10622 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10623 Op.getOperand(0), Op.getOperand(1));
10624}
10625
Craig Topper55b24052012-09-11 06:15:32 +000010626static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010627 return Op.getOperand(0);
10628}
10629
10630SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10631 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010632 SDValue Root = Op.getOperand(0);
10633 SDValue Trmp = Op.getOperand(1); // trampoline
10634 SDValue FPtr = Op.getOperand(2); // nested function
10635 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010636 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010637
Dan Gohman69de1932008-02-06 22:27:42 +000010638 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010639 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010640
10641 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010642 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010643
10644 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010645 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10646 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010647
Michael Liao7abf67a2012-10-04 19:50:43 +000010648 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10649 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010650
10651 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10652
10653 // Load the pointer to the nested function into R11.
10654 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010655 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010656 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010657 Addr, MachinePointerInfo(TrmpAddr),
10658 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010659
Owen Anderson825b72b2009-08-11 20:47:22 +000010660 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10661 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010662 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10663 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010664 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010665
10666 // Load the 'nest' parameter value into R10.
10667 // R10 is specified in X86CallingConv.td
10668 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010669 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10670 DAG.getConstant(10, MVT::i64));
10671 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010672 Addr, MachinePointerInfo(TrmpAddr, 10),
10673 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010674
Owen Anderson825b72b2009-08-11 20:47:22 +000010675 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10676 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010677 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10678 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010679 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010680
10681 // Jump to the nested function.
10682 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010683 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10684 DAG.getConstant(20, MVT::i64));
10685 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010686 Addr, MachinePointerInfo(TrmpAddr, 20),
10687 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010688
10689 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010690 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10691 DAG.getConstant(22, MVT::i64));
10692 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010693 MachinePointerInfo(TrmpAddr, 22),
10694 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010695
Duncan Sands4a544a72011-09-06 13:37:06 +000010696 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010697 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010698 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010699 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010700 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010701 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010702
10703 switch (CC) {
10704 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010705 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010706 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010707 case CallingConv::X86_StdCall: {
10708 // Pass 'nest' parameter in ECX.
10709 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010710 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010711
10712 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010713 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010714 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010715
Chris Lattner58d74912008-03-12 17:45:29 +000010716 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010717 unsigned InRegCount = 0;
10718 unsigned Idx = 1;
10719
10720 for (FunctionType::param_iterator I = FTy->param_begin(),
10721 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010722 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010723 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010724 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010725
10726 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010727 report_fatal_error("Nest register in use - reduce number of inreg"
10728 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010729 }
10730 }
10731 break;
10732 }
10733 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010734 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010735 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010736 // Pass 'nest' parameter in EAX.
10737 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010738 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010739 break;
10740 }
10741
Dan Gohman475871a2008-07-27 21:46:04 +000010742 SDValue OutChains[4];
10743 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010744
Owen Anderson825b72b2009-08-11 20:47:22 +000010745 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10746 DAG.getConstant(10, MVT::i32));
10747 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010748
Chris Lattnera62fe662010-02-05 19:20:30 +000010749 // This is storing the opcode for MOV32ri.
10750 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010751 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010752 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010753 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010754 Trmp, MachinePointerInfo(TrmpAddr),
10755 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010756
Owen Anderson825b72b2009-08-11 20:47:22 +000010757 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10758 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010759 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10760 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010761 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010762
Chris Lattnera62fe662010-02-05 19:20:30 +000010763 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010764 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10765 DAG.getConstant(5, MVT::i32));
10766 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010767 MachinePointerInfo(TrmpAddr, 5),
10768 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010769
Owen Anderson825b72b2009-08-11 20:47:22 +000010770 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10771 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010772 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10773 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010774 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010775
Duncan Sands4a544a72011-09-06 13:37:06 +000010776 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010777 }
10778}
10779
Dan Gohmand858e902010-04-17 15:26:15 +000010780SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10781 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010782 /*
10783 The rounding mode is in bits 11:10 of FPSR, and has the following
10784 settings:
10785 00 Round to nearest
10786 01 Round to -inf
10787 10 Round to +inf
10788 11 Round to 0
10789
10790 FLT_ROUNDS, on the other hand, expects the following:
10791 -1 Undefined
10792 0 Round to 0
10793 1 Round to nearest
10794 2 Round to +inf
10795 3 Round to -inf
10796
10797 To perform the conversion, we do:
10798 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10799 */
10800
10801 MachineFunction &MF = DAG.getMachineFunction();
10802 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010803 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010804 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010805 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010806 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010807
10808 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010809 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010810 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010811
Michael J. Spencerec38de22010-10-10 22:04:20 +000010812
Chris Lattner2156b792010-09-22 01:11:26 +000010813 MachineMemOperand *MMO =
10814 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10815 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010816
Chris Lattner2156b792010-09-22 01:11:26 +000010817 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10818 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10819 DAG.getVTList(MVT::Other),
10820 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010821
10822 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010823 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010824 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010825
10826 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010827 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010828 DAG.getNode(ISD::SRL, DL, MVT::i16,
10829 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010830 CWD, DAG.getConstant(0x800, MVT::i16)),
10831 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010832 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010833 DAG.getNode(ISD::SRL, DL, MVT::i16,
10834 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010835 CWD, DAG.getConstant(0x400, MVT::i16)),
10836 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010837
Dan Gohman475871a2008-07-27 21:46:04 +000010838 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010839 DAG.getNode(ISD::AND, DL, MVT::i16,
10840 DAG.getNode(ISD::ADD, DL, MVT::i16,
10841 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010842 DAG.getConstant(1, MVT::i16)),
10843 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010844
10845
Duncan Sands83ec4b62008-06-06 12:08:01 +000010846 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010847 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010848}
10849
Craig Topper55b24052012-09-11 06:15:32 +000010850static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010851 EVT VT = Op.getValueType();
10852 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010853 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010854 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010855
10856 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010857 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010858 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010859 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010860 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010861 }
Evan Cheng18efe262007-12-14 02:13:44 +000010862
Evan Cheng152804e2007-12-14 08:30:15 +000010863 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010864 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010865 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010866
10867 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010868 SDValue Ops[] = {
10869 Op,
10870 DAG.getConstant(NumBits+NumBits-1, OpVT),
10871 DAG.getConstant(X86::COND_E, MVT::i8),
10872 Op.getValue(1)
10873 };
10874 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010875
10876 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010877 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010878
Owen Anderson825b72b2009-08-11 20:47:22 +000010879 if (VT == MVT::i8)
10880 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010881 return Op;
10882}
10883
Craig Topper55b24052012-09-11 06:15:32 +000010884static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010885 EVT VT = Op.getValueType();
10886 EVT OpVT = VT;
10887 unsigned NumBits = VT.getSizeInBits();
10888 DebugLoc dl = Op.getDebugLoc();
10889
10890 Op = Op.getOperand(0);
10891 if (VT == MVT::i8) {
10892 // Zero extend to i32 since there is not an i8 bsr.
10893 OpVT = MVT::i32;
10894 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10895 }
10896
10897 // Issue a bsr (scan bits in reverse).
10898 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10899 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10900
10901 // And xor with NumBits-1.
10902 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10903
10904 if (VT == MVT::i8)
10905 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10906 return Op;
10907}
10908
Craig Topper55b24052012-09-11 06:15:32 +000010909static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010910 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010911 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010912 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010913 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010914
10915 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010916 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010917 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010918
10919 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010920 SDValue Ops[] = {
10921 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010922 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010923 DAG.getConstant(X86::COND_E, MVT::i8),
10924 Op.getValue(1)
10925 };
Chandler Carruth77821022011-12-24 12:12:34 +000010926 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010927}
10928
Craig Topper13894fa2011-08-24 06:14:18 +000010929// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10930// ones, and then concatenate the result back.
10931static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010932 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010933
Craig Topper7a9a28b2012-08-12 02:23:29 +000010934 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010935 "Unsupported value type for operation");
10936
Craig Topper66ddd152012-04-27 22:54:43 +000010937 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010938 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010939
10940 // Extract the LHS vectors
10941 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010942 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10943 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010944
10945 // Extract the RHS vectors
10946 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010947 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10948 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010949
10950 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10951 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10952
10953 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10954 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10955 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10956}
10957
Craig Topper55b24052012-09-11 06:15:32 +000010958static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010959 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010960 Op.getValueType().isInteger() &&
10961 "Only handle AVX 256-bit vector integer operation");
10962 return Lower256IntArith(Op, DAG);
10963}
10964
Craig Topper55b24052012-09-11 06:15:32 +000010965static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010966 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010967 Op.getValueType().isInteger() &&
10968 "Only handle AVX 256-bit vector integer operation");
10969 return Lower256IntArith(Op, DAG);
10970}
10971
Craig Topper55b24052012-09-11 06:15:32 +000010972static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10973 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010974 EVT VT = Op.getValueType();
10975
10976 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010977 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010978 return Lower256IntArith(Op, DAG);
10979
Craig Topper5b209e82012-02-05 03:14:49 +000010980 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10981 "Only know how to lower V2I64/V4I64 multiply");
10982
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010983 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010984
Craig Topper5b209e82012-02-05 03:14:49 +000010985 // Ahi = psrlqi(a, 32);
10986 // Bhi = psrlqi(b, 32);
10987 //
10988 // AloBlo = pmuludq(a, b);
10989 // AloBhi = pmuludq(a, Bhi);
10990 // AhiBlo = pmuludq(Ahi, b);
10991
10992 // AloBhi = psllqi(AloBhi, 32);
10993 // AhiBlo = psllqi(AhiBlo, 32);
10994 // return AloBlo + AloBhi + AhiBlo;
10995
Craig Topperaaa643c2011-11-09 07:28:55 +000010996 SDValue A = Op.getOperand(0);
10997 SDValue B = Op.getOperand(1);
10998
Craig Topper5b209e82012-02-05 03:14:49 +000010999 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011000
Craig Topper5b209e82012-02-05 03:14:49 +000011001 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11002 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011003
Craig Topper5b209e82012-02-05 03:14:49 +000011004 // Bit cast to 32-bit vectors for MULUDQ
11005 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11006 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11007 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11008 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11009 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011010
Craig Topper5b209e82012-02-05 03:14:49 +000011011 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11012 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11013 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011014
Craig Topper5b209e82012-02-05 03:14:49 +000011015 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11016 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011017
Dale Johannesene4d209d2009-02-03 20:21:25 +000011018 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011019 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011020}
11021
Nadav Rotem43012222011-05-11 08:12:09 +000011022SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11023
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011024 EVT VT = Op.getValueType();
11025 DebugLoc dl = Op.getDebugLoc();
11026 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011027 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011028 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011029
Craig Topper1accb7e2012-01-10 06:54:16 +000011030 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011031 return SDValue();
11032
Nadav Rotem43012222011-05-11 08:12:09 +000011033 // Optimize shl/srl/sra with constant shift amount.
11034 if (isSplatVector(Amt.getNode())) {
11035 SDValue SclrAmt = Amt->getOperand(0);
11036 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11037 uint64_t ShiftAmt = C->getZExtValue();
11038
Craig Toppered2e13d2012-01-22 19:15:14 +000011039 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
11040 (Subtarget->hasAVX2() &&
11041 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11042 if (Op.getOpcode() == ISD::SHL)
11043 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11044 DAG.getConstant(ShiftAmt, MVT::i32));
11045 if (Op.getOpcode() == ISD::SRL)
11046 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11047 DAG.getConstant(ShiftAmt, MVT::i32));
11048 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11049 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11050 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011051 }
11052
Craig Toppered2e13d2012-01-22 19:15:14 +000011053 if (VT == MVT::v16i8) {
11054 if (Op.getOpcode() == ISD::SHL) {
11055 // Make a large shift.
11056 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11057 DAG.getConstant(ShiftAmt, MVT::i32));
11058 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11059 // Zero out the rightmost bits.
11060 SmallVector<SDValue, 16> V(16,
11061 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11062 MVT::i8));
11063 return DAG.getNode(ISD::AND, dl, VT, SHL,
11064 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011065 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011066 if (Op.getOpcode() == ISD::SRL) {
11067 // Make a large shift.
11068 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11069 DAG.getConstant(ShiftAmt, MVT::i32));
11070 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11071 // Zero out the leftmost bits.
11072 SmallVector<SDValue, 16> V(16,
11073 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11074 MVT::i8));
11075 return DAG.getNode(ISD::AND, dl, VT, SRL,
11076 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11077 }
11078 if (Op.getOpcode() == ISD::SRA) {
11079 if (ShiftAmt == 7) {
11080 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011081 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011082 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011083 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011084
Craig Toppered2e13d2012-01-22 19:15:14 +000011085 // R s>> a === ((R u>> a) ^ m) - m
11086 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11087 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11088 MVT::i8));
11089 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11090 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11091 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11092 return Res;
11093 }
Craig Topper731dfd02012-04-23 03:42:40 +000011094 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011095 }
Craig Topper46154eb2011-11-11 07:39:23 +000011096
Craig Topper0d86d462011-11-20 00:12:05 +000011097 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
11098 if (Op.getOpcode() == ISD::SHL) {
11099 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011100 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11101 DAG.getConstant(ShiftAmt, MVT::i32));
11102 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011103 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011104 SmallVector<SDValue, 32> V(32,
11105 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11106 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011107 return DAG.getNode(ISD::AND, dl, VT, SHL,
11108 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011109 }
Craig Topper0d86d462011-11-20 00:12:05 +000011110 if (Op.getOpcode() == ISD::SRL) {
11111 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011112 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11113 DAG.getConstant(ShiftAmt, MVT::i32));
11114 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011115 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011116 SmallVector<SDValue, 32> V(32,
11117 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11118 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011119 return DAG.getNode(ISD::AND, dl, VT, SRL,
11120 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11121 }
11122 if (Op.getOpcode() == ISD::SRA) {
11123 if (ShiftAmt == 7) {
11124 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011125 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011126 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011127 }
11128
11129 // R s>> a === ((R u>> a) ^ m) - m
11130 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11131 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11132 MVT::i8));
11133 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11134 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11135 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11136 return Res;
11137 }
Craig Topper731dfd02012-04-23 03:42:40 +000011138 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011139 }
Nadav Rotem43012222011-05-11 08:12:09 +000011140 }
11141 }
11142
11143 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011144 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011145 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11146 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011147
Chris Lattner7302d802012-02-06 21:56:39 +000011148 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11149 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011150 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11151 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011152 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011153 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011154
11155 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011156 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011157 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11158 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11159 }
Nadav Rotem43012222011-05-11 08:12:09 +000011160 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011161 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011162
Nate Begeman51409212010-07-28 00:21:48 +000011163 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011164 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11165 DAG.getConstant(5, MVT::i32));
11166 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011167
Lang Hames8b99c1e2011-12-17 01:08:46 +000011168 // Turn 'a' into a mask suitable for VSELECT
11169 SDValue VSelM = DAG.getConstant(0x80, VT);
11170 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011171 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011172
Lang Hames8b99c1e2011-12-17 01:08:46 +000011173 SDValue CM1 = DAG.getConstant(0x0f, VT);
11174 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011175
Lang Hames8b99c1e2011-12-17 01:08:46 +000011176 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11177 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011178 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11179 DAG.getConstant(4, MVT::i32), DAG);
11180 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011181 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11182
Nate Begeman51409212010-07-28 00:21:48 +000011183 // a += a
11184 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011185 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011186 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011187
Lang Hames8b99c1e2011-12-17 01:08:46 +000011188 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11189 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011190 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11191 DAG.getConstant(2, MVT::i32), DAG);
11192 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011193 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11194
Nate Begeman51409212010-07-28 00:21:48 +000011195 // a += a
11196 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011197 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011198 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011199
Lang Hames8b99c1e2011-12-17 01:08:46 +000011200 // return VSELECT(r, r+r, a);
11201 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011202 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011203 return R;
11204 }
Craig Topper46154eb2011-11-11 07:39:23 +000011205
11206 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011207 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011208 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011209 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11210 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11211
11212 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011213 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11214 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011215
11216 // Recreate the shift amount vectors
11217 SDValue Amt1, Amt2;
11218 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11219 // Constant shift amount
11220 SmallVector<SDValue, 4> Amt1Csts;
11221 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011222 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011223 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011224 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011225 Amt2Csts.push_back(Amt->getOperand(i));
11226
11227 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11228 &Amt1Csts[0], NumElems/2);
11229 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11230 &Amt2Csts[0], NumElems/2);
11231 } else {
11232 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011233 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11234 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011235 }
11236
11237 // Issue new vector shifts for the smaller types
11238 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11239 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11240
11241 // Concatenate the result back
11242 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11243 }
11244
Nate Begeman51409212010-07-28 00:21:48 +000011245 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011246}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011247
Craig Topper55b24052012-09-11 06:15:32 +000011248static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011249 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11250 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011251 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11252 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011253 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011254 SDValue LHS = N->getOperand(0);
11255 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011256 unsigned BaseOp = 0;
11257 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011258 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011259 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011260 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011261 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011262 // A subtract of one will be selected as a INC. Note that INC doesn't
11263 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011264 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11265 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011266 BaseOp = X86ISD::INC;
11267 Cond = X86::COND_O;
11268 break;
11269 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011270 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011271 Cond = X86::COND_O;
11272 break;
11273 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011274 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011275 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011276 break;
11277 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011278 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11279 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011280 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11281 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011282 BaseOp = X86ISD::DEC;
11283 Cond = X86::COND_O;
11284 break;
11285 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011286 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011287 Cond = X86::COND_O;
11288 break;
11289 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011290 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011291 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011292 break;
11293 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011294 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011295 Cond = X86::COND_O;
11296 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011297 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11298 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11299 MVT::i32);
11300 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011301
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011302 SDValue SetCC =
11303 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11304 DAG.getConstant(X86::COND_O, MVT::i32),
11305 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011306
Dan Gohman6e5fda22011-07-22 18:45:15 +000011307 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011308 }
Bill Wendling74c37652008-12-09 22:08:41 +000011309 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011310
Bill Wendling61edeb52008-12-02 01:06:39 +000011311 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011312 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011313 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011314
Bill Wendling61edeb52008-12-02 01:06:39 +000011315 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011316 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11317 DAG.getConstant(Cond, MVT::i32),
11318 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011319
Dan Gohman6e5fda22011-07-22 18:45:15 +000011320 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011321}
11322
Chad Rosier30450e82011-12-22 22:35:21 +000011323SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11324 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011325 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011326 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11327 EVT VT = Op.getValueType();
11328
Craig Toppered2e13d2012-01-22 19:15:14 +000011329 if (!Subtarget->hasSSE2() || !VT.isVector())
11330 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011331
Craig Toppered2e13d2012-01-22 19:15:14 +000011332 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11333 ExtraVT.getScalarType().getSizeInBits();
11334 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11335
11336 switch (VT.getSimpleVT().SimpleTy) {
11337 default: return SDValue();
11338 case MVT::v8i32:
11339 case MVT::v16i16:
11340 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011341 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011342 if (!Subtarget->hasAVX2()) {
11343 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011344 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011345
Craig Toppered2e13d2012-01-22 19:15:14 +000011346 // Extract the LHS vectors
11347 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011348 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11349 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011350
Craig Toppered2e13d2012-01-22 19:15:14 +000011351 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11352 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011353
Craig Toppered2e13d2012-01-22 19:15:14 +000011354 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011355 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011356 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11357 ExtraNumElems/2);
11358 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011359
Craig Toppered2e13d2012-01-22 19:15:14 +000011360 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11361 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011362
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011363 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011364 }
11365 // fall through
11366 case MVT::v4i32:
11367 case MVT::v8i16: {
11368 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11369 Op.getOperand(0), ShAmt, DAG);
11370 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011371 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011372 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011373}
11374
11375
Craig Topper55b24052012-09-11 06:15:32 +000011376static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11377 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011378 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011379
Eric Christopher77ed1352011-07-08 00:04:56 +000011380 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11381 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011382 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011383 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011384 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011385 SDValue Ops[] = {
11386 DAG.getRegister(X86::ESP, MVT::i32), // Base
11387 DAG.getTargetConstant(1, MVT::i8), // Scale
11388 DAG.getRegister(0, MVT::i32), // Index
11389 DAG.getTargetConstant(0, MVT::i32), // Disp
11390 DAG.getRegister(0, MVT::i32), // Segment.
11391 Zero,
11392 Chain
11393 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011394 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011395 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11396 array_lengthof(Ops));
11397 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011398 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011399
Eric Christopher9a9d2752010-07-22 02:48:34 +000011400 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011401 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011402 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011403
Chris Lattner132929a2010-08-14 17:26:09 +000011404 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11405 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11406 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11407 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011408
Chris Lattner132929a2010-08-14 17:26:09 +000011409 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11410 if (!Op1 && !Op2 && !Op3 && Op4)
11411 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011412
Chris Lattner132929a2010-08-14 17:26:09 +000011413 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11414 if (Op1 && !Op2 && !Op3 && !Op4)
11415 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011416
11417 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011418 // (MFENCE)>;
11419 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011420}
11421
Craig Topper55b24052012-09-11 06:15:32 +000011422static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11423 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011424 DebugLoc dl = Op.getDebugLoc();
11425 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11426 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11427 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11428 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11429
11430 // The only fence that needs an instruction is a sequentially-consistent
11431 // cross-thread fence.
11432 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11433 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11434 // no-sse2). There isn't any reason to disable it if the target processor
11435 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011436 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011437 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11438
11439 SDValue Chain = Op.getOperand(0);
11440 SDValue Zero = DAG.getConstant(0, MVT::i32);
11441 SDValue Ops[] = {
11442 DAG.getRegister(X86::ESP, MVT::i32), // Base
11443 DAG.getTargetConstant(1, MVT::i8), // Scale
11444 DAG.getRegister(0, MVT::i32), // Index
11445 DAG.getTargetConstant(0, MVT::i32), // Disp
11446 DAG.getRegister(0, MVT::i32), // Segment.
11447 Zero,
11448 Chain
11449 };
11450 SDNode *Res =
11451 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11452 array_lengthof(Ops));
11453 return SDValue(Res, 0);
11454 }
11455
11456 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11457 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11458}
11459
11460
Craig Topper55b24052012-09-11 06:15:32 +000011461static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11462 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011463 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011464 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011465 unsigned Reg = 0;
11466 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011468 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011469 case MVT::i8: Reg = X86::AL; size = 1; break;
11470 case MVT::i16: Reg = X86::AX; size = 2; break;
11471 case MVT::i32: Reg = X86::EAX; size = 4; break;
11472 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011473 assert(Subtarget->is64Bit() && "Node not type legal!");
11474 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011475 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011476 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011477 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011478 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011479 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011480 Op.getOperand(1),
11481 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011482 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011483 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011484 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011485 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11486 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11487 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011488 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011489 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011490 return cpOut;
11491}
11492
Craig Topper55b24052012-09-11 06:15:32 +000011493static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11494 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011495 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011496 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011497 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011498 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011499 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011500 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11501 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011502 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011503 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11504 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011505 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011506 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011507 rdx.getValue(1)
11508 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011509 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011510}
11511
Craig Topper55b24052012-09-11 06:15:32 +000011512SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011513 EVT SrcVT = Op.getOperand(0).getValueType();
11514 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011515 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011516 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011517 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011518 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011519 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011520 // i64 <=> MMX conversions are Legal.
11521 if (SrcVT==MVT::i64 && DstVT.isVector())
11522 return Op;
11523 if (DstVT==MVT::i64 && SrcVT.isVector())
11524 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011525 // MMX <=> MMX conversions are Legal.
11526 if (SrcVT.isVector() && DstVT.isVector())
11527 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011528 // All other conversions need to be expanded.
11529 return SDValue();
11530}
Chris Lattner5b856542010-12-20 00:59:46 +000011531
Craig Topper55b24052012-09-11 06:15:32 +000011532static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011533 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011534 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011535 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011536 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011537 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011538 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011539 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011540 Node->getOperand(0),
11541 Node->getOperand(1), negOp,
11542 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011543 cast<AtomicSDNode>(Node)->getAlignment(),
11544 cast<AtomicSDNode>(Node)->getOrdering(),
11545 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011546}
11547
Eli Friedman327236c2011-08-24 20:50:09 +000011548static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11549 SDNode *Node = Op.getNode();
11550 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011551 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011552
11553 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011554 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11555 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11556 // (The only way to get a 16-byte store is cmpxchg16b)
11557 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11558 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11559 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011560 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11561 cast<AtomicSDNode>(Node)->getMemoryVT(),
11562 Node->getOperand(0),
11563 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011564 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011565 cast<AtomicSDNode>(Node)->getOrdering(),
11566 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011567 return Swap.getValue(1);
11568 }
11569 // Other atomic stores have a simple pattern.
11570 return Op;
11571}
11572
Chris Lattner5b856542010-12-20 00:59:46 +000011573static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11574 EVT VT = Op.getNode()->getValueType(0);
11575
11576 // Let legalize expand this if it isn't a legal type yet.
11577 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11578 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011579
Chris Lattner5b856542010-12-20 00:59:46 +000011580 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011581
Chris Lattner5b856542010-12-20 00:59:46 +000011582 unsigned Opc;
11583 bool ExtraOp = false;
11584 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011585 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011586 case ISD::ADDC: Opc = X86ISD::ADD; break;
11587 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11588 case ISD::SUBC: Opc = X86ISD::SUB; break;
11589 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11590 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011591
Chris Lattner5b856542010-12-20 00:59:46 +000011592 if (!ExtraOp)
11593 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11594 Op.getOperand(1));
11595 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11596 Op.getOperand(1), Op.getOperand(2));
11597}
11598
Evan Cheng0db9fe62006-04-25 20:13:52 +000011599/// LowerOperation - Provide custom lowering hooks for some operations.
11600///
Dan Gohmand858e902010-04-17 15:26:15 +000011601SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011602 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011603 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011604 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011605 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11606 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11607 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011608 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011609 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011610 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011611 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011612 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11613 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11614 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011615 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11616 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011617 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11618 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11619 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011620 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011621 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011622 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011623 case ISD::SHL_PARTS:
11624 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011625 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011626 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011627 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011628 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Michael Liaoa7554632012-10-23 17:36:08 +000011629 case ISD::ZERO_EXTEND: return lowerZERO_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011630 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011631 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011632 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011633 case ISD::FABS: return LowerFABS(Op, DAG);
11634 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011635 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011636 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011637 case ISD::SETCC: return LowerSETCC(Op, DAG);
11638 case ISD::SELECT: return LowerSELECT(Op, DAG);
11639 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011640 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011641 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011642 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011643 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011644 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011645 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011646 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11647 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011648 case ISD::FRAME_TO_ARGS_OFFSET:
11649 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011650 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011651 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011652 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
11653 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011654 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11655 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011656 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011657 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011658 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011659 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011660 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011661 case ISD::SRA:
11662 case ISD::SRL:
11663 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011664 case ISD::SADDO:
11665 case ISD::UADDO:
11666 case ISD::SSUBO:
11667 case ISD::USUBO:
11668 case ISD::SMULO:
11669 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011670 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011671 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011672 case ISD::ADDC:
11673 case ISD::ADDE:
11674 case ISD::SUBC:
11675 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011676 case ISD::ADD: return LowerADD(Op, DAG);
11677 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011678 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011679}
11680
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011681static void ReplaceATOMIC_LOAD(SDNode *Node,
11682 SmallVectorImpl<SDValue> &Results,
11683 SelectionDAG &DAG) {
11684 DebugLoc dl = Node->getDebugLoc();
11685 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11686
11687 // Convert wide load -> cmpxchg8b/cmpxchg16b
11688 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11689 // (The only way to get a 16-byte load is cmpxchg16b)
11690 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011691 SDValue Zero = DAG.getConstant(0, VT);
11692 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011693 Node->getOperand(0),
11694 Node->getOperand(1), Zero, Zero,
11695 cast<AtomicSDNode>(Node)->getMemOperand(),
11696 cast<AtomicSDNode>(Node)->getOrdering(),
11697 cast<AtomicSDNode>(Node)->getSynchScope());
11698 Results.push_back(Swap.getValue(0));
11699 Results.push_back(Swap.getValue(1));
11700}
11701
Craig Topperc0878702012-08-17 06:55:11 +000011702static void
Duncan Sands1607f052008-12-01 11:39:25 +000011703ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011704 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011705 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011706 assert (Node->getValueType(0) == MVT::i64 &&
11707 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011708
11709 SDValue Chain = Node->getOperand(0);
11710 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011711 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011712 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011713 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011714 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011715 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011716 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011717 SDValue Result =
11718 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11719 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011720 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011721 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011722 Results.push_back(Result.getValue(2));
11723}
11724
Duncan Sands126d9072008-07-04 11:47:58 +000011725/// ReplaceNodeResults - Replace a node with an illegal result type
11726/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011727void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11728 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011729 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011730 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011731 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011732 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011733 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011734 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011735 case ISD::ADDC:
11736 case ISD::ADDE:
11737 case ISD::SUBC:
11738 case ISD::SUBE:
11739 // We don't want to expand or promote these.
11740 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011741 case ISD::FP_TO_SINT:
11742 case ISD::FP_TO_UINT: {
11743 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11744
11745 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11746 return;
11747
Eli Friedman948e95a2009-05-23 09:59:16 +000011748 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011749 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011750 SDValue FIST = Vals.first, StackSlot = Vals.second;
11751 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011752 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011753 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011754 if (StackSlot.getNode() != 0)
11755 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11756 MachinePointerInfo(),
11757 false, false, false, 0));
11758 else
11759 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011760 }
11761 return;
11762 }
Michael Liao991b6a22012-10-24 04:09:32 +000011763 case ISD::UINT_TO_FP: {
11764 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
11765 N->getValueType(0) != MVT::v2f32)
11766 return;
11767 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
11768 N->getOperand(0));
11769 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
11770 MVT::f64);
11771 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
11772 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
11773 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
11774 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
11775 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
11776 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
11777 return;
11778 }
Michael Liao44c2d612012-10-10 16:53:28 +000011779 case ISD::FP_ROUND: {
11780 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11781 Results.push_back(V);
11782 return;
11783 }
Duncan Sands1607f052008-12-01 11:39:25 +000011784 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011785 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011786 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011787 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011788 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011789 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011790 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011791 eax.getValue(2));
11792 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11793 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011794 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011795 Results.push_back(edx.getValue(1));
11796 return;
11797 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011798 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011799 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011800 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011801 bool Regs64bit = T == MVT::i128;
11802 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011803 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011804 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11805 DAG.getConstant(0, HalfT));
11806 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11807 DAG.getConstant(1, HalfT));
11808 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11809 Regs64bit ? X86::RAX : X86::EAX,
11810 cpInL, SDValue());
11811 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11812 Regs64bit ? X86::RDX : X86::EDX,
11813 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011814 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011815 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11816 DAG.getConstant(0, HalfT));
11817 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11818 DAG.getConstant(1, HalfT));
11819 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11820 Regs64bit ? X86::RBX : X86::EBX,
11821 swapInL, cpInH.getValue(1));
11822 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011823 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011824 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011825 SDValue Ops[] = { swapInH.getValue(0),
11826 N->getOperand(1),
11827 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011828 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011829 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011830 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11831 X86ISD::LCMPXCHG8_DAG;
11832 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011833 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011834 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11835 Regs64bit ? X86::RAX : X86::EAX,
11836 HalfT, Result.getValue(1));
11837 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11838 Regs64bit ? X86::RDX : X86::EDX,
11839 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011840 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011841 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011842 Results.push_back(cpOutH.getValue(1));
11843 return;
11844 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011845 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011846 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011847 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011848 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011849 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011850 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011851 case ISD::ATOMIC_LOAD_MAX:
11852 case ISD::ATOMIC_LOAD_MIN:
11853 case ISD::ATOMIC_LOAD_UMAX:
11854 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011855 case ISD::ATOMIC_SWAP: {
11856 unsigned Opc;
11857 switch (N->getOpcode()) {
11858 default: llvm_unreachable("Unexpected opcode");
11859 case ISD::ATOMIC_LOAD_ADD:
11860 Opc = X86ISD::ATOMADD64_DAG;
11861 break;
11862 case ISD::ATOMIC_LOAD_AND:
11863 Opc = X86ISD::ATOMAND64_DAG;
11864 break;
11865 case ISD::ATOMIC_LOAD_NAND:
11866 Opc = X86ISD::ATOMNAND64_DAG;
11867 break;
11868 case ISD::ATOMIC_LOAD_OR:
11869 Opc = X86ISD::ATOMOR64_DAG;
11870 break;
11871 case ISD::ATOMIC_LOAD_SUB:
11872 Opc = X86ISD::ATOMSUB64_DAG;
11873 break;
11874 case ISD::ATOMIC_LOAD_XOR:
11875 Opc = X86ISD::ATOMXOR64_DAG;
11876 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011877 case ISD::ATOMIC_LOAD_MAX:
11878 Opc = X86ISD::ATOMMAX64_DAG;
11879 break;
11880 case ISD::ATOMIC_LOAD_MIN:
11881 Opc = X86ISD::ATOMMIN64_DAG;
11882 break;
11883 case ISD::ATOMIC_LOAD_UMAX:
11884 Opc = X86ISD::ATOMUMAX64_DAG;
11885 break;
11886 case ISD::ATOMIC_LOAD_UMIN:
11887 Opc = X86ISD::ATOMUMIN64_DAG;
11888 break;
Craig Topperc0878702012-08-17 06:55:11 +000011889 case ISD::ATOMIC_SWAP:
11890 Opc = X86ISD::ATOMSWAP64_DAG;
11891 break;
11892 }
11893 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011894 return;
Craig Topperc0878702012-08-17 06:55:11 +000011895 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011896 case ISD::ATOMIC_LOAD:
11897 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011898 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011899}
11900
Evan Cheng72261582005-12-20 06:22:03 +000011901const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11902 switch (Opcode) {
11903 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011904 case X86ISD::BSF: return "X86ISD::BSF";
11905 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011906 case X86ISD::SHLD: return "X86ISD::SHLD";
11907 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011908 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011909 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011910 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011911 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011912 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011913 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011914 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11915 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11916 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011917 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011918 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011919 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011920 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011921 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011922 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011923 case X86ISD::COMI: return "X86ISD::COMI";
11924 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011925 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011926 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011927 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11928 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011929 case X86ISD::CMOV: return "X86ISD::CMOV";
11930 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011931 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011932 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11933 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011934 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011935 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011936 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011937 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011938 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011939 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11940 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011941 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011942 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011943 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011944 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011945 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011946 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11947 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11948 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011949 case X86ISD::HADD: return "X86ISD::HADD";
11950 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011951 case X86ISD::FHADD: return "X86ISD::FHADD";
11952 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011953 case X86ISD::FMAX: return "X86ISD::FMAX";
11954 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011955 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11956 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011957 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11958 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011959 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011960 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011961 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000011962 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
11963 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011964 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011965 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011966 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011967 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011968 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11969 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011970 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11971 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11972 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11973 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11974 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11975 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011976 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011977 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011978 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000011979 case X86ISD::VZEXT: return "X86ISD::VZEXT";
11980 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000011981 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000011982 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000011983 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11984 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011985 case X86ISD::VSHL: return "X86ISD::VSHL";
11986 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011987 case X86ISD::VSRA: return "X86ISD::VSRA";
11988 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11989 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11990 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011991 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011992 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11993 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011994 case X86ISD::ADD: return "X86ISD::ADD";
11995 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011996 case X86ISD::ADC: return "X86ISD::ADC";
11997 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011998 case X86ISD::SMUL: return "X86ISD::SMUL";
11999 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012000 case X86ISD::INC: return "X86ISD::INC";
12001 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012002 case X86ISD::OR: return "X86ISD::OR";
12003 case X86ISD::XOR: return "X86ISD::XOR";
12004 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000012005 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000012006 case X86ISD::BLSI: return "X86ISD::BLSI";
12007 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12008 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012009 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012010 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012011 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012012 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12013 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12014 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012015 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012016 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012017 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012018 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012019 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012020 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12021 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012022 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12023 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12024 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012025 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12026 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012027 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12028 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012029 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012030 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012031 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012032 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12033 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012034 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012035 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012036 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012037 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012038 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012039 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012040 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012041 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012042 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012043 case X86ISD::FMADD: return "X86ISD::FMADD";
12044 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12045 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12046 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12047 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12048 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012049 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12050 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012051 }
12052}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012053
Chris Lattnerc9addb72007-03-30 23:15:24 +000012054// isLegalAddressingMode - Return true if the addressing mode represented
12055// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012056bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012057 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012058 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012059 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012060 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012061
Chris Lattnerc9addb72007-03-30 23:15:24 +000012062 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012063 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012064 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012065
Chris Lattnerc9addb72007-03-30 23:15:24 +000012066 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012067 unsigned GVFlags =
12068 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012069
Chris Lattnerdfed4132009-07-10 07:38:24 +000012070 // If a reference to this global requires an extra load, we can't fold it.
12071 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012072 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012073
Chris Lattnerdfed4132009-07-10 07:38:24 +000012074 // If BaseGV requires a register for the PIC base, we cannot also have a
12075 // BaseReg specified.
12076 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012077 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012078
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012079 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012080 if ((M != CodeModel::Small || R != Reloc::Static) &&
12081 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012082 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012083 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012084
Chris Lattnerc9addb72007-03-30 23:15:24 +000012085 switch (AM.Scale) {
12086 case 0:
12087 case 1:
12088 case 2:
12089 case 4:
12090 case 8:
12091 // These scales always work.
12092 break;
12093 case 3:
12094 case 5:
12095 case 9:
12096 // These scales are formed with basereg+scalereg. Only accept if there is
12097 // no basereg yet.
12098 if (AM.HasBaseReg)
12099 return false;
12100 break;
12101 default: // Other stuff never works.
12102 return false;
12103 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012104
Chris Lattnerc9addb72007-03-30 23:15:24 +000012105 return true;
12106}
12107
12108
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012109bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012110 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012111 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012112 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12113 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012114 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000012115 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012116 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012117}
12118
Evan Cheng70e10d32012-07-17 06:53:39 +000012119bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
12120 return Imm == (int32_t)Imm;
12121}
12122
12123bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012124 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000012125 return Imm == (int32_t)Imm;
12126}
12127
Owen Andersone50ed302009-08-10 22:56:29 +000012128bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012129 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012130 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012131 unsigned NumBits1 = VT1.getSizeInBits();
12132 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000012133 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012134 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000012135 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012136}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012137
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012138bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012139 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012140 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012141}
12142
Owen Andersone50ed302009-08-10 22:56:29 +000012143bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012144 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012145 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012146}
12147
Owen Andersone50ed302009-08-10 22:56:29 +000012148bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012149 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012150 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012151}
12152
Evan Cheng60c07e12006-07-05 22:17:51 +000012153/// isShuffleMaskLegal - Targets can use this to indicate that they only
12154/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12155/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12156/// are assumed to be legal.
12157bool
Eric Christopherfd179292009-08-27 18:07:15 +000012158X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012159 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012160 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012161 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012162 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012163
Nate Begemana09008b2009-10-19 02:17:23 +000012164 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012165 return (VT.getVectorNumElements() == 2 ||
12166 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12167 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012168 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012169 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000012170 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
12171 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012172 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000012173 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
12174 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000012175 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
12176 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012177}
12178
Dan Gohman7d8143f2008-04-09 20:09:42 +000012179bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012180X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012181 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012182 unsigned NumElts = VT.getVectorNumElements();
12183 // FIXME: This collection of masks seems suspect.
12184 if (NumElts == 2)
12185 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012186 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012187 return (isMOVLMask(Mask, VT) ||
12188 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000012189 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
12190 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012191 }
12192 return false;
12193}
12194
12195//===----------------------------------------------------------------------===//
12196// X86 Scheduler Hooks
12197//===----------------------------------------------------------------------===//
12198
Michael Liaobe02a902012-11-08 07:28:54 +000012199/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012200static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12201 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012202 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012203
12204 const BasicBlock *BB = MBB->getBasicBlock();
12205 MachineFunction::iterator I = MBB;
12206 ++I;
12207
12208 // For the v = xbegin(), we generate
12209 //
12210 // thisMBB:
12211 // xbegin sinkMBB
12212 //
12213 // mainMBB:
12214 // eax = -1
12215 //
12216 // sinkMBB:
12217 // v = eax
12218
12219 MachineBasicBlock *thisMBB = MBB;
12220 MachineFunction *MF = MBB->getParent();
12221 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12222 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12223 MF->insert(I, mainMBB);
12224 MF->insert(I, sinkMBB);
12225
12226 // Transfer the remainder of BB and its successor edges to sinkMBB.
12227 sinkMBB->splice(sinkMBB->begin(), MBB,
12228 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12229 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12230
12231 // thisMBB:
12232 // xbegin sinkMBB
12233 // # fallthrough to mainMBB
12234 // # abortion to sinkMBB
12235 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12236 thisMBB->addSuccessor(mainMBB);
12237 thisMBB->addSuccessor(sinkMBB);
12238
12239 // mainMBB:
12240 // EAX = -1
12241 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12242 mainMBB->addSuccessor(sinkMBB);
12243
12244 // sinkMBB:
12245 // EAX is live into the sinkMBB
12246 sinkMBB->addLiveIn(X86::EAX);
12247 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12248 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12249 .addReg(X86::EAX);
12250
12251 MI->eraseFromParent();
12252 return sinkMBB;
12253}
12254
Michael Liaob118a072012-09-20 03:06:15 +000012255// Get CMPXCHG opcode for the specified data type.
12256static unsigned getCmpXChgOpcode(EVT VT) {
12257 switch (VT.getSimpleVT().SimpleTy) {
12258 case MVT::i8: return X86::LCMPXCHG8;
12259 case MVT::i16: return X86::LCMPXCHG16;
12260 case MVT::i32: return X86::LCMPXCHG32;
12261 case MVT::i64: return X86::LCMPXCHG64;
12262 default:
12263 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012264 }
Michael Liaob118a072012-09-20 03:06:15 +000012265 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012266}
12267
Michael Liaob118a072012-09-20 03:06:15 +000012268// Get LOAD opcode for the specified data type.
12269static unsigned getLoadOpcode(EVT VT) {
12270 switch (VT.getSimpleVT().SimpleTy) {
12271 case MVT::i8: return X86::MOV8rm;
12272 case MVT::i16: return X86::MOV16rm;
12273 case MVT::i32: return X86::MOV32rm;
12274 case MVT::i64: return X86::MOV64rm;
12275 default:
12276 break;
12277 }
12278 llvm_unreachable("Invalid operand size!");
12279}
12280
12281// Get opcode of the non-atomic one from the specified atomic instruction.
12282static unsigned getNonAtomicOpcode(unsigned Opc) {
12283 switch (Opc) {
12284 case X86::ATOMAND8: return X86::AND8rr;
12285 case X86::ATOMAND16: return X86::AND16rr;
12286 case X86::ATOMAND32: return X86::AND32rr;
12287 case X86::ATOMAND64: return X86::AND64rr;
12288 case X86::ATOMOR8: return X86::OR8rr;
12289 case X86::ATOMOR16: return X86::OR16rr;
12290 case X86::ATOMOR32: return X86::OR32rr;
12291 case X86::ATOMOR64: return X86::OR64rr;
12292 case X86::ATOMXOR8: return X86::XOR8rr;
12293 case X86::ATOMXOR16: return X86::XOR16rr;
12294 case X86::ATOMXOR32: return X86::XOR32rr;
12295 case X86::ATOMXOR64: return X86::XOR64rr;
12296 }
12297 llvm_unreachable("Unhandled atomic-load-op opcode!");
12298}
12299
12300// Get opcode of the non-atomic one from the specified atomic instruction with
12301// extra opcode.
12302static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12303 unsigned &ExtraOpc) {
12304 switch (Opc) {
12305 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12306 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12307 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12308 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012309 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012310 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12311 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12312 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012313 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012314 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12315 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12316 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012317 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012318 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12319 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12320 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012321 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012322 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12323 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12324 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12325 }
12326 llvm_unreachable("Unhandled atomic-load-op opcode!");
12327}
12328
12329// Get opcode of the non-atomic one from the specified atomic instruction for
12330// 64-bit data type on 32-bit target.
12331static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12332 switch (Opc) {
12333 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12334 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12335 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12336 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12337 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12338 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012339 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12340 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12341 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12342 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012343 }
12344 llvm_unreachable("Unhandled atomic-load-op opcode!");
12345}
12346
12347// Get opcode of the non-atomic one from the specified atomic instruction for
12348// 64-bit data type on 32-bit target with extra opcode.
12349static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12350 unsigned &HiOpc,
12351 unsigned &ExtraOpc) {
12352 switch (Opc) {
12353 case X86::ATOMNAND6432:
12354 ExtraOpc = X86::NOT32r;
12355 HiOpc = X86::AND32rr;
12356 return X86::AND32rr;
12357 }
12358 llvm_unreachable("Unhandled atomic-load-op opcode!");
12359}
12360
12361// Get pseudo CMOV opcode from the specified data type.
12362static unsigned getPseudoCMOVOpc(EVT VT) {
12363 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012364 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012365 case MVT::i16: return X86::CMOV_GR16;
12366 case MVT::i32: return X86::CMOV_GR32;
12367 default:
12368 break;
12369 }
12370 llvm_unreachable("Unknown CMOV opcode!");
12371}
12372
12373// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12374// They will be translated into a spin-loop or compare-exchange loop from
12375//
12376// ...
12377// dst = atomic-fetch-op MI.addr, MI.val
12378// ...
12379//
12380// to
12381//
12382// ...
12383// EAX = LOAD MI.addr
12384// loop:
12385// t1 = OP MI.val, EAX
12386// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12387// JNE loop
12388// sink:
12389// dst = EAX
12390// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012391MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012392X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12393 MachineBasicBlock *MBB) const {
12394 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12395 DebugLoc DL = MI->getDebugLoc();
12396
12397 MachineFunction *MF = MBB->getParent();
12398 MachineRegisterInfo &MRI = MF->getRegInfo();
12399
12400 const BasicBlock *BB = MBB->getBasicBlock();
12401 MachineFunction::iterator I = MBB;
12402 ++I;
12403
12404 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12405 "Unexpected number of operands");
12406
12407 assert(MI->hasOneMemOperand() &&
12408 "Expected atomic-load-op to have one memoperand");
12409
12410 // Memory Reference
12411 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12412 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12413
12414 unsigned DstReg, SrcReg;
12415 unsigned MemOpndSlot;
12416
12417 unsigned CurOp = 0;
12418
12419 DstReg = MI->getOperand(CurOp++).getReg();
12420 MemOpndSlot = CurOp;
12421 CurOp += X86::AddrNumOperands;
12422 SrcReg = MI->getOperand(CurOp++).getReg();
12423
12424 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012425 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012426 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12427
12428 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12429 unsigned LOADOpc = getLoadOpcode(VT);
12430
12431 // For the atomic load-arith operator, we generate
12432 //
12433 // thisMBB:
12434 // EAX = LOAD [MI.addr]
12435 // mainMBB:
12436 // t1 = OP MI.val, EAX
12437 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12438 // JNE mainMBB
12439 // sinkMBB:
12440
12441 MachineBasicBlock *thisMBB = MBB;
12442 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12443 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12444 MF->insert(I, mainMBB);
12445 MF->insert(I, sinkMBB);
12446
12447 MachineInstrBuilder MIB;
12448
12449 // Transfer the remainder of BB and its successor edges to sinkMBB.
12450 sinkMBB->splice(sinkMBB->begin(), MBB,
12451 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12452 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12453
12454 // thisMBB:
12455 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12456 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12457 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12458 MIB.setMemRefs(MMOBegin, MMOEnd);
12459
12460 thisMBB->addSuccessor(mainMBB);
12461
12462 // mainMBB:
12463 MachineBasicBlock *origMainMBB = mainMBB;
12464 mainMBB->addLiveIn(AccPhyReg);
12465
12466 // Copy AccPhyReg as it is used more than once.
12467 unsigned AccReg = MRI.createVirtualRegister(RC);
12468 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12469 .addReg(AccPhyReg);
12470
12471 unsigned t1 = MRI.createVirtualRegister(RC);
12472 unsigned Opc = MI->getOpcode();
12473 switch (Opc) {
12474 default:
12475 llvm_unreachable("Unhandled atomic-load-op opcode!");
12476 case X86::ATOMAND8:
12477 case X86::ATOMAND16:
12478 case X86::ATOMAND32:
12479 case X86::ATOMAND64:
12480 case X86::ATOMOR8:
12481 case X86::ATOMOR16:
12482 case X86::ATOMOR32:
12483 case X86::ATOMOR64:
12484 case X86::ATOMXOR8:
12485 case X86::ATOMXOR16:
12486 case X86::ATOMXOR32:
12487 case X86::ATOMXOR64: {
12488 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12489 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12490 .addReg(AccReg);
12491 break;
12492 }
12493 case X86::ATOMNAND8:
12494 case X86::ATOMNAND16:
12495 case X86::ATOMNAND32:
12496 case X86::ATOMNAND64: {
12497 unsigned t2 = MRI.createVirtualRegister(RC);
12498 unsigned NOTOpc;
12499 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12500 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12501 .addReg(AccReg);
12502 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12503 break;
12504 }
Michael Liao08382492012-09-21 03:00:17 +000012505 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012506 case X86::ATOMMAX16:
12507 case X86::ATOMMAX32:
12508 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012509 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012510 case X86::ATOMMIN16:
12511 case X86::ATOMMIN32:
12512 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012513 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012514 case X86::ATOMUMAX16:
12515 case X86::ATOMUMAX32:
12516 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012517 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012518 case X86::ATOMUMIN16:
12519 case X86::ATOMUMIN32:
12520 case X86::ATOMUMIN64: {
12521 unsigned CMPOpc;
12522 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12523
12524 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12525 .addReg(SrcReg)
12526 .addReg(AccReg);
12527
12528 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012529 if (VT != MVT::i8) {
12530 // Native support
12531 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12532 .addReg(SrcReg)
12533 .addReg(AccReg);
12534 } else {
12535 // Promote i8 to i32 to use CMOV32
12536 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12537 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12538 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12539 unsigned t2 = MRI.createVirtualRegister(RC32);
12540
12541 unsigned Undef = MRI.createVirtualRegister(RC32);
12542 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12543
12544 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12545 .addReg(Undef)
12546 .addReg(SrcReg)
12547 .addImm(X86::sub_8bit);
12548 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12549 .addReg(Undef)
12550 .addReg(AccReg)
12551 .addImm(X86::sub_8bit);
12552
12553 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12554 .addReg(SrcReg32)
12555 .addReg(AccReg32);
12556
12557 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12558 .addReg(t2, 0, X86::sub_8bit);
12559 }
Michael Liaob118a072012-09-20 03:06:15 +000012560 } else {
12561 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012562 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012563 "Invalid atomic-load-op transformation!");
12564 unsigned SelOpc = getPseudoCMOVOpc(VT);
12565 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12566 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12567 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12568 .addReg(SrcReg).addReg(AccReg)
12569 .addImm(CC);
12570 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12571 }
12572 break;
12573 }
12574 }
12575
12576 // Copy AccPhyReg back from virtual register.
12577 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12578 .addReg(AccReg);
12579
12580 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12581 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12582 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12583 MIB.addReg(t1);
12584 MIB.setMemRefs(MMOBegin, MMOEnd);
12585
12586 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12587
12588 mainMBB->addSuccessor(origMainMBB);
12589 mainMBB->addSuccessor(sinkMBB);
12590
12591 // sinkMBB:
12592 sinkMBB->addLiveIn(AccPhyReg);
12593
12594 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12595 TII->get(TargetOpcode::COPY), DstReg)
12596 .addReg(AccPhyReg);
12597
12598 MI->eraseFromParent();
12599 return sinkMBB;
12600}
12601
12602// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12603// instructions. They will be translated into a spin-loop or compare-exchange
12604// loop from
12605//
12606// ...
12607// dst = atomic-fetch-op MI.addr, MI.val
12608// ...
12609//
12610// to
12611//
12612// ...
12613// EAX = LOAD [MI.addr + 0]
12614// EDX = LOAD [MI.addr + 4]
12615// loop:
12616// EBX = OP MI.val.lo, EAX
12617// ECX = OP MI.val.hi, EDX
12618// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12619// JNE loop
12620// sink:
12621// dst = EDX:EAX
12622// ...
12623MachineBasicBlock *
12624X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12625 MachineBasicBlock *MBB) const {
12626 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12627 DebugLoc DL = MI->getDebugLoc();
12628
12629 MachineFunction *MF = MBB->getParent();
12630 MachineRegisterInfo &MRI = MF->getRegInfo();
12631
12632 const BasicBlock *BB = MBB->getBasicBlock();
12633 MachineFunction::iterator I = MBB;
12634 ++I;
12635
12636 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12637 "Unexpected number of operands");
12638
12639 assert(MI->hasOneMemOperand() &&
12640 "Expected atomic-load-op32 to have one memoperand");
12641
12642 // Memory Reference
12643 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12644 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12645
12646 unsigned DstLoReg, DstHiReg;
12647 unsigned SrcLoReg, SrcHiReg;
12648 unsigned MemOpndSlot;
12649
12650 unsigned CurOp = 0;
12651
12652 DstLoReg = MI->getOperand(CurOp++).getReg();
12653 DstHiReg = MI->getOperand(CurOp++).getReg();
12654 MemOpndSlot = CurOp;
12655 CurOp += X86::AddrNumOperands;
12656 SrcLoReg = MI->getOperand(CurOp++).getReg();
12657 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012658
Craig Topperc9099502012-04-20 06:31:50 +000012659 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012660 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012661
Michael Liaob118a072012-09-20 03:06:15 +000012662 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12663 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012664
Michael Liaob118a072012-09-20 03:06:15 +000012665 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012666 //
Michael Liaob118a072012-09-20 03:06:15 +000012667 // thisMBB:
12668 // EAX = LOAD [MI.addr + 0]
12669 // EDX = LOAD [MI.addr + 4]
12670 // mainMBB:
12671 // EBX = OP MI.vallo, EAX
12672 // ECX = OP MI.valhi, EDX
12673 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12674 // JNE mainMBB
12675 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012676
Mon P Wang63307c32008-05-05 19:05:59 +000012677 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012678 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12679 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12680 MF->insert(I, mainMBB);
12681 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012682
Michael Liaob118a072012-09-20 03:06:15 +000012683 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012684
Michael Liaob118a072012-09-20 03:06:15 +000012685 // Transfer the remainder of BB and its successor edges to sinkMBB.
12686 sinkMBB->splice(sinkMBB->begin(), MBB,
12687 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12688 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012689
Michael Liaob118a072012-09-20 03:06:15 +000012690 // thisMBB:
12691 // Lo
12692 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12693 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12694 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12695 MIB.setMemRefs(MMOBegin, MMOEnd);
12696 // Hi
12697 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12698 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012699 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012700 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012701 else
Michael Liaob118a072012-09-20 03:06:15 +000012702 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12703 }
12704 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012705
Michael Liaob118a072012-09-20 03:06:15 +000012706 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012707
Michael Liaob118a072012-09-20 03:06:15 +000012708 // mainMBB:
12709 MachineBasicBlock *origMainMBB = mainMBB;
12710 mainMBB->addLiveIn(X86::EAX);
12711 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012712
Michael Liaob118a072012-09-20 03:06:15 +000012713 // Copy EDX:EAX as they are used more than once.
12714 unsigned LoReg = MRI.createVirtualRegister(RC);
12715 unsigned HiReg = MRI.createVirtualRegister(RC);
12716 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12717 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012718
Michael Liaob118a072012-09-20 03:06:15 +000012719 unsigned t1L = MRI.createVirtualRegister(RC);
12720 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012721
Michael Liaob118a072012-09-20 03:06:15 +000012722 unsigned Opc = MI->getOpcode();
12723 switch (Opc) {
12724 default:
12725 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12726 case X86::ATOMAND6432:
12727 case X86::ATOMOR6432:
12728 case X86::ATOMXOR6432:
12729 case X86::ATOMADD6432:
12730 case X86::ATOMSUB6432: {
12731 unsigned HiOpc;
12732 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000012733 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
12734 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000012735 break;
12736 }
12737 case X86::ATOMNAND6432: {
12738 unsigned HiOpc, NOTOpc;
12739 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12740 unsigned t2L = MRI.createVirtualRegister(RC);
12741 unsigned t2H = MRI.createVirtualRegister(RC);
12742 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12743 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12744 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12745 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12746 break;
12747 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012748 case X86::ATOMMAX6432:
12749 case X86::ATOMMIN6432:
12750 case X86::ATOMUMAX6432:
12751 case X86::ATOMUMIN6432: {
12752 unsigned HiOpc;
12753 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12754 unsigned cL = MRI.createVirtualRegister(RC8);
12755 unsigned cH = MRI.createVirtualRegister(RC8);
12756 unsigned cL32 = MRI.createVirtualRegister(RC);
12757 unsigned cH32 = MRI.createVirtualRegister(RC);
12758 unsigned cc = MRI.createVirtualRegister(RC);
12759 // cl := cmp src_lo, lo
12760 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12761 .addReg(SrcLoReg).addReg(LoReg);
12762 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12763 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12764 // ch := cmp src_hi, hi
12765 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12766 .addReg(SrcHiReg).addReg(HiReg);
12767 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12768 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12769 // cc := if (src_hi == hi) ? cl : ch;
12770 if (Subtarget->hasCMov()) {
12771 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12772 .addReg(cH32).addReg(cL32);
12773 } else {
12774 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12775 .addReg(cH32).addReg(cL32)
12776 .addImm(X86::COND_E);
12777 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12778 }
12779 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12780 if (Subtarget->hasCMov()) {
12781 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12782 .addReg(SrcLoReg).addReg(LoReg);
12783 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12784 .addReg(SrcHiReg).addReg(HiReg);
12785 } else {
12786 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12787 .addReg(SrcLoReg).addReg(LoReg)
12788 .addImm(X86::COND_NE);
12789 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12790 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12791 .addReg(SrcHiReg).addReg(HiReg)
12792 .addImm(X86::COND_NE);
12793 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12794 }
12795 break;
12796 }
Michael Liaob118a072012-09-20 03:06:15 +000012797 case X86::ATOMSWAP6432: {
12798 unsigned HiOpc;
12799 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12800 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12801 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12802 break;
12803 }
12804 }
Mon P Wang63307c32008-05-05 19:05:59 +000012805
Michael Liaob118a072012-09-20 03:06:15 +000012806 // Copy EDX:EAX back from HiReg:LoReg
12807 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12808 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12809 // Copy ECX:EBX from t1H:t1L
12810 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12811 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012812
Michael Liaob118a072012-09-20 03:06:15 +000012813 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12814 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12815 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12816 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012817
Michael Liaob118a072012-09-20 03:06:15 +000012818 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012819
Michael Liaob118a072012-09-20 03:06:15 +000012820 mainMBB->addSuccessor(origMainMBB);
12821 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012822
Michael Liaob118a072012-09-20 03:06:15 +000012823 // sinkMBB:
12824 sinkMBB->addLiveIn(X86::EAX);
12825 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012826
Michael Liaob118a072012-09-20 03:06:15 +000012827 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12828 TII->get(TargetOpcode::COPY), DstLoReg)
12829 .addReg(X86::EAX);
12830 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12831 TII->get(TargetOpcode::COPY), DstHiReg)
12832 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012833
Michael Liaob118a072012-09-20 03:06:15 +000012834 MI->eraseFromParent();
12835 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012836}
12837
Eric Christopherf83a5de2009-08-27 18:08:16 +000012838// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012839// or XMM0_V32I8 in AVX all of this code can be replaced with that
12840// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000012841static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
12842 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000012843 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012844 switch (MI->getOpcode()) {
12845 default: llvm_unreachable("illegal opcode!");
12846 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
12847 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
12848 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
12849 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
12850 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
12851 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
12852 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
12853 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012854 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012855
Craig Topper8aae8dd2012-11-10 08:57:41 +000012856 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000012857 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000012858
Craig Topper52ea2452012-11-10 09:25:36 +000012859 unsigned NumArgs = MI->getNumOperands();
12860 for (unsigned i = 1; i < NumArgs; ++i) {
12861 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000012862 if (!(Op.isReg() && Op.isImplicit()))
12863 MIB.addOperand(Op);
12864 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000012865 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000012866 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
12867
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012868 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012869 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012870 .addReg(X86::XMM0);
12871
Dan Gohman14152b42010-07-06 20:24:04 +000012872 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012873 return BB;
12874}
12875
Craig Topper9c7ae012012-11-10 01:23:36 +000012876// FIXME: Custom handling because TableGen doesn't support multiple implicit
12877// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000012878static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
12879 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000012880 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000012881 switch (MI->getOpcode()) {
12882 default: llvm_unreachable("illegal opcode!");
12883 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
12884 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
12885 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
12886 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
12887 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
12888 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
12889 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
12890 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000012891 }
12892
Craig Topper8aae8dd2012-11-10 08:57:41 +000012893 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000012894 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000012895
Craig Topper52ea2452012-11-10 09:25:36 +000012896 unsigned NumArgs = MI->getNumOperands(); // remove the results
12897 for (unsigned i = 1; i < NumArgs; ++i) {
12898 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000012899 if (!(Op.isReg() && Op.isImplicit()))
12900 MIB.addOperand(Op);
12901 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000012902 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000012903 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
12904
12905 BuildMI(*BB, MI, dl,
12906 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12907 .addReg(X86::ECX);
12908
12909 MI->eraseFromParent();
12910 return BB;
12911}
12912
Craig Topper2da36912012-11-11 22:45:02 +000012913static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
12914 const TargetInstrInfo *TII,
12915 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000012916 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012917
Eric Christopher228232b2010-11-30 07:20:12 +000012918 // Address into RAX/EAX, other two args into ECX, EDX.
12919 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12920 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12921 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12922 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012923 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012924
Eric Christopher228232b2010-11-30 07:20:12 +000012925 unsigned ValOps = X86::AddrNumOperands;
12926 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12927 .addReg(MI->getOperand(ValOps).getReg());
12928 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12929 .addReg(MI->getOperand(ValOps+1).getReg());
12930
12931 // The instruction doesn't actually take any operands though.
12932 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012933
Eric Christopher228232b2010-11-30 07:20:12 +000012934 MI->eraseFromParent(); // The pseudo is gone now.
12935 return BB;
12936}
12937
12938MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012939X86TargetLowering::EmitVAARG64WithCustomInserter(
12940 MachineInstr *MI,
12941 MachineBasicBlock *MBB) const {
12942 // Emit va_arg instruction on X86-64.
12943
12944 // Operands to this pseudo-instruction:
12945 // 0 ) Output : destination address (reg)
12946 // 1-5) Input : va_list address (addr, i64mem)
12947 // 6 ) ArgSize : Size (in bytes) of vararg type
12948 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12949 // 8 ) Align : Alignment of type
12950 // 9 ) EFLAGS (implicit-def)
12951
12952 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12953 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12954
12955 unsigned DestReg = MI->getOperand(0).getReg();
12956 MachineOperand &Base = MI->getOperand(1);
12957 MachineOperand &Scale = MI->getOperand(2);
12958 MachineOperand &Index = MI->getOperand(3);
12959 MachineOperand &Disp = MI->getOperand(4);
12960 MachineOperand &Segment = MI->getOperand(5);
12961 unsigned ArgSize = MI->getOperand(6).getImm();
12962 unsigned ArgMode = MI->getOperand(7).getImm();
12963 unsigned Align = MI->getOperand(8).getImm();
12964
12965 // Memory Reference
12966 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12967 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12968 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12969
12970 // Machine Information
12971 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12972 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12973 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12974 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12975 DebugLoc DL = MI->getDebugLoc();
12976
12977 // struct va_list {
12978 // i32 gp_offset
12979 // i32 fp_offset
12980 // i64 overflow_area (address)
12981 // i64 reg_save_area (address)
12982 // }
12983 // sizeof(va_list) = 24
12984 // alignment(va_list) = 8
12985
12986 unsigned TotalNumIntRegs = 6;
12987 unsigned TotalNumXMMRegs = 8;
12988 bool UseGPOffset = (ArgMode == 1);
12989 bool UseFPOffset = (ArgMode == 2);
12990 unsigned MaxOffset = TotalNumIntRegs * 8 +
12991 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12992
12993 /* Align ArgSize to a multiple of 8 */
12994 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12995 bool NeedsAlign = (Align > 8);
12996
12997 MachineBasicBlock *thisMBB = MBB;
12998 MachineBasicBlock *overflowMBB;
12999 MachineBasicBlock *offsetMBB;
13000 MachineBasicBlock *endMBB;
13001
13002 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13003 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13004 unsigned OffsetReg = 0;
13005
13006 if (!UseGPOffset && !UseFPOffset) {
13007 // If we only pull from the overflow region, we don't create a branch.
13008 // We don't need to alter control flow.
13009 OffsetDestReg = 0; // unused
13010 OverflowDestReg = DestReg;
13011
13012 offsetMBB = NULL;
13013 overflowMBB = thisMBB;
13014 endMBB = thisMBB;
13015 } else {
13016 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13017 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13018 // If not, pull from overflow_area. (branch to overflowMBB)
13019 //
13020 // thisMBB
13021 // | .
13022 // | .
13023 // offsetMBB overflowMBB
13024 // | .
13025 // | .
13026 // endMBB
13027
13028 // Registers for the PHI in endMBB
13029 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13030 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13031
13032 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13033 MachineFunction *MF = MBB->getParent();
13034 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13035 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13036 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13037
13038 MachineFunction::iterator MBBIter = MBB;
13039 ++MBBIter;
13040
13041 // Insert the new basic blocks
13042 MF->insert(MBBIter, offsetMBB);
13043 MF->insert(MBBIter, overflowMBB);
13044 MF->insert(MBBIter, endMBB);
13045
13046 // Transfer the remainder of MBB and its successor edges to endMBB.
13047 endMBB->splice(endMBB->begin(), thisMBB,
13048 llvm::next(MachineBasicBlock::iterator(MI)),
13049 thisMBB->end());
13050 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13051
13052 // Make offsetMBB and overflowMBB successors of thisMBB
13053 thisMBB->addSuccessor(offsetMBB);
13054 thisMBB->addSuccessor(overflowMBB);
13055
13056 // endMBB is a successor of both offsetMBB and overflowMBB
13057 offsetMBB->addSuccessor(endMBB);
13058 overflowMBB->addSuccessor(endMBB);
13059
13060 // Load the offset value into a register
13061 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13062 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13063 .addOperand(Base)
13064 .addOperand(Scale)
13065 .addOperand(Index)
13066 .addDisp(Disp, UseFPOffset ? 4 : 0)
13067 .addOperand(Segment)
13068 .setMemRefs(MMOBegin, MMOEnd);
13069
13070 // Check if there is enough room left to pull this argument.
13071 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13072 .addReg(OffsetReg)
13073 .addImm(MaxOffset + 8 - ArgSizeA8);
13074
13075 // Branch to "overflowMBB" if offset >= max
13076 // Fall through to "offsetMBB" otherwise
13077 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13078 .addMBB(overflowMBB);
13079 }
13080
13081 // In offsetMBB, emit code to use the reg_save_area.
13082 if (offsetMBB) {
13083 assert(OffsetReg != 0);
13084
13085 // Read the reg_save_area address.
13086 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13087 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13088 .addOperand(Base)
13089 .addOperand(Scale)
13090 .addOperand(Index)
13091 .addDisp(Disp, 16)
13092 .addOperand(Segment)
13093 .setMemRefs(MMOBegin, MMOEnd);
13094
13095 // Zero-extend the offset
13096 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13097 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13098 .addImm(0)
13099 .addReg(OffsetReg)
13100 .addImm(X86::sub_32bit);
13101
13102 // Add the offset to the reg_save_area to get the final address.
13103 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13104 .addReg(OffsetReg64)
13105 .addReg(RegSaveReg);
13106
13107 // Compute the offset for the next argument
13108 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13109 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13110 .addReg(OffsetReg)
13111 .addImm(UseFPOffset ? 16 : 8);
13112
13113 // Store it back into the va_list.
13114 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13115 .addOperand(Base)
13116 .addOperand(Scale)
13117 .addOperand(Index)
13118 .addDisp(Disp, UseFPOffset ? 4 : 0)
13119 .addOperand(Segment)
13120 .addReg(NextOffsetReg)
13121 .setMemRefs(MMOBegin, MMOEnd);
13122
13123 // Jump to endMBB
13124 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13125 .addMBB(endMBB);
13126 }
13127
13128 //
13129 // Emit code to use overflow area
13130 //
13131
13132 // Load the overflow_area address into a register.
13133 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13134 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13135 .addOperand(Base)
13136 .addOperand(Scale)
13137 .addOperand(Index)
13138 .addDisp(Disp, 8)
13139 .addOperand(Segment)
13140 .setMemRefs(MMOBegin, MMOEnd);
13141
13142 // If we need to align it, do so. Otherwise, just copy the address
13143 // to OverflowDestReg.
13144 if (NeedsAlign) {
13145 // Align the overflow address
13146 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13147 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13148
13149 // aligned_addr = (addr + (align-1)) & ~(align-1)
13150 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13151 .addReg(OverflowAddrReg)
13152 .addImm(Align-1);
13153
13154 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13155 .addReg(TmpReg)
13156 .addImm(~(uint64_t)(Align-1));
13157 } else {
13158 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13159 .addReg(OverflowAddrReg);
13160 }
13161
13162 // Compute the next overflow address after this argument.
13163 // (the overflow address should be kept 8-byte aligned)
13164 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13165 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13166 .addReg(OverflowDestReg)
13167 .addImm(ArgSizeA8);
13168
13169 // Store the new overflow address.
13170 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13171 .addOperand(Base)
13172 .addOperand(Scale)
13173 .addOperand(Index)
13174 .addDisp(Disp, 8)
13175 .addOperand(Segment)
13176 .addReg(NextAddrReg)
13177 .setMemRefs(MMOBegin, MMOEnd);
13178
13179 // If we branched, emit the PHI to the front of endMBB.
13180 if (offsetMBB) {
13181 BuildMI(*endMBB, endMBB->begin(), DL,
13182 TII->get(X86::PHI), DestReg)
13183 .addReg(OffsetDestReg).addMBB(offsetMBB)
13184 .addReg(OverflowDestReg).addMBB(overflowMBB);
13185 }
13186
13187 // Erase the pseudo instruction
13188 MI->eraseFromParent();
13189
13190 return endMBB;
13191}
13192
13193MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013194X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13195 MachineInstr *MI,
13196 MachineBasicBlock *MBB) const {
13197 // Emit code to save XMM registers to the stack. The ABI says that the
13198 // number of registers to save is given in %al, so it's theoretically
13199 // possible to do an indirect jump trick to avoid saving all of them,
13200 // however this code takes a simpler approach and just executes all
13201 // of the stores if %al is non-zero. It's less code, and it's probably
13202 // easier on the hardware branch predictor, and stores aren't all that
13203 // expensive anyway.
13204
13205 // Create the new basic blocks. One block contains all the XMM stores,
13206 // and one block is the final destination regardless of whether any
13207 // stores were performed.
13208 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13209 MachineFunction *F = MBB->getParent();
13210 MachineFunction::iterator MBBIter = MBB;
13211 ++MBBIter;
13212 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13213 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13214 F->insert(MBBIter, XMMSaveMBB);
13215 F->insert(MBBIter, EndMBB);
13216
Dan Gohman14152b42010-07-06 20:24:04 +000013217 // Transfer the remainder of MBB and its successor edges to EndMBB.
13218 EndMBB->splice(EndMBB->begin(), MBB,
13219 llvm::next(MachineBasicBlock::iterator(MI)),
13220 MBB->end());
13221 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13222
Dan Gohmand6708ea2009-08-15 01:38:56 +000013223 // The original block will now fall through to the XMM save block.
13224 MBB->addSuccessor(XMMSaveMBB);
13225 // The XMMSaveMBB will fall through to the end block.
13226 XMMSaveMBB->addSuccessor(EndMBB);
13227
13228 // Now add the instructions.
13229 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13230 DebugLoc DL = MI->getDebugLoc();
13231
13232 unsigned CountReg = MI->getOperand(0).getReg();
13233 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13234 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13235
13236 if (!Subtarget->isTargetWin64()) {
13237 // If %al is 0, branch around the XMM save block.
13238 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013239 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013240 MBB->addSuccessor(EndMBB);
13241 }
13242
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013243 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013244 // In the XMM save block, save all the XMM argument registers.
13245 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13246 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013247 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013248 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013249 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013250 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013251 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013252 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013253 .addFrameIndex(RegSaveFrameIndex)
13254 .addImm(/*Scale=*/1)
13255 .addReg(/*IndexReg=*/0)
13256 .addImm(/*Disp=*/Offset)
13257 .addReg(/*Segment=*/0)
13258 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013259 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013260 }
13261
Dan Gohman14152b42010-07-06 20:24:04 +000013262 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013263
13264 return EndMBB;
13265}
Mon P Wang63307c32008-05-05 19:05:59 +000013266
Lang Hames6e3f7e42012-02-03 01:13:49 +000013267// The EFLAGS operand of SelectItr might be missing a kill marker
13268// because there were multiple uses of EFLAGS, and ISel didn't know
13269// which to mark. Figure out whether SelectItr should have had a
13270// kill marker, and set it if it should. Returns the correct kill
13271// marker value.
13272static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13273 MachineBasicBlock* BB,
13274 const TargetRegisterInfo* TRI) {
13275 // Scan forward through BB for a use/def of EFLAGS.
13276 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13277 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013278 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013279 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013280 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013281 if (mi.definesRegister(X86::EFLAGS))
13282 break; // Should have kill-flag - update below.
13283 }
13284
13285 // If we hit the end of the block, check whether EFLAGS is live into a
13286 // successor.
13287 if (miI == BB->end()) {
13288 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13289 sEnd = BB->succ_end();
13290 sItr != sEnd; ++sItr) {
13291 MachineBasicBlock* succ = *sItr;
13292 if (succ->isLiveIn(X86::EFLAGS))
13293 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013294 }
13295 }
13296
Lang Hames6e3f7e42012-02-03 01:13:49 +000013297 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13298 // out. SelectMI should have a kill flag on EFLAGS.
13299 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013300 return true;
13301}
13302
Evan Cheng60c07e12006-07-05 22:17:51 +000013303MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013304X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013305 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013306 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13307 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013308
Chris Lattner52600972009-09-02 05:57:00 +000013309 // To "insert" a SELECT_CC instruction, we actually have to insert the
13310 // diamond control-flow pattern. The incoming instruction knows the
13311 // destination vreg to set, the condition code register to branch on, the
13312 // true/false values to select between, and a branch opcode to use.
13313 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13314 MachineFunction::iterator It = BB;
13315 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013316
Chris Lattner52600972009-09-02 05:57:00 +000013317 // thisMBB:
13318 // ...
13319 // TrueVal = ...
13320 // cmpTY ccX, r1, r2
13321 // bCC copy1MBB
13322 // fallthrough --> copy0MBB
13323 MachineBasicBlock *thisMBB = BB;
13324 MachineFunction *F = BB->getParent();
13325 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13326 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013327 F->insert(It, copy0MBB);
13328 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013329
Bill Wendling730c07e2010-06-25 20:48:10 +000013330 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13331 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013332 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13333 if (!MI->killsRegister(X86::EFLAGS) &&
13334 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13335 copy0MBB->addLiveIn(X86::EFLAGS);
13336 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013337 }
13338
Dan Gohman14152b42010-07-06 20:24:04 +000013339 // Transfer the remainder of BB and its successor edges to sinkMBB.
13340 sinkMBB->splice(sinkMBB->begin(), BB,
13341 llvm::next(MachineBasicBlock::iterator(MI)),
13342 BB->end());
13343 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13344
13345 // Add the true and fallthrough blocks as its successors.
13346 BB->addSuccessor(copy0MBB);
13347 BB->addSuccessor(sinkMBB);
13348
13349 // Create the conditional branch instruction.
13350 unsigned Opc =
13351 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13352 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13353
Chris Lattner52600972009-09-02 05:57:00 +000013354 // copy0MBB:
13355 // %FalseValue = ...
13356 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013357 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013358
Chris Lattner52600972009-09-02 05:57:00 +000013359 // sinkMBB:
13360 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13361 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013362 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13363 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013364 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13365 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13366
Dan Gohman14152b42010-07-06 20:24:04 +000013367 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013368 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013369}
13370
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013371MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013372X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13373 bool Is64Bit) const {
13374 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13375 DebugLoc DL = MI->getDebugLoc();
13376 MachineFunction *MF = BB->getParent();
13377 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13378
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013379 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013380
13381 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13382 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13383
13384 // BB:
13385 // ... [Till the alloca]
13386 // If stacklet is not large enough, jump to mallocMBB
13387 //
13388 // bumpMBB:
13389 // Allocate by subtracting from RSP
13390 // Jump to continueMBB
13391 //
13392 // mallocMBB:
13393 // Allocate by call to runtime
13394 //
13395 // continueMBB:
13396 // ...
13397 // [rest of original BB]
13398 //
13399
13400 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13401 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13402 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13403
13404 MachineRegisterInfo &MRI = MF->getRegInfo();
13405 const TargetRegisterClass *AddrRegClass =
13406 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13407
13408 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13409 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13410 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013411 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013412 sizeVReg = MI->getOperand(1).getReg(),
13413 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13414
13415 MachineFunction::iterator MBBIter = BB;
13416 ++MBBIter;
13417
13418 MF->insert(MBBIter, bumpMBB);
13419 MF->insert(MBBIter, mallocMBB);
13420 MF->insert(MBBIter, continueMBB);
13421
13422 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13423 (MachineBasicBlock::iterator(MI)), BB->end());
13424 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13425
13426 // Add code to the main basic block to check if the stack limit has been hit,
13427 // and if so, jump to mallocMBB otherwise to bumpMBB.
13428 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013429 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013430 .addReg(tmpSPVReg).addReg(sizeVReg);
13431 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013432 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013433 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013434 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13435
13436 // bumpMBB simply decreases the stack pointer, since we know the current
13437 // stacklet has enough space.
13438 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013439 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013440 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013441 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013442 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13443
13444 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013445 const uint32_t *RegMask =
13446 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013447 if (Is64Bit) {
13448 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13449 .addReg(sizeVReg);
13450 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013451 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013452 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013453 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013454 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013455 } else {
13456 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13457 .addImm(12);
13458 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13459 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013460 .addExternalSymbol("__morestack_allocate_stack_space")
13461 .addRegMask(RegMask)
13462 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013463 }
13464
13465 if (!Is64Bit)
13466 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13467 .addImm(16);
13468
13469 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13470 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13471 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13472
13473 // Set up the CFG correctly.
13474 BB->addSuccessor(bumpMBB);
13475 BB->addSuccessor(mallocMBB);
13476 mallocMBB->addSuccessor(continueMBB);
13477 bumpMBB->addSuccessor(continueMBB);
13478
13479 // Take care of the PHI nodes.
13480 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13481 MI->getOperand(0).getReg())
13482 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13483 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13484
13485 // Delete the original pseudo instruction.
13486 MI->eraseFromParent();
13487
13488 // And we're done.
13489 return continueMBB;
13490}
13491
13492MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013493X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013494 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013495 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13496 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013497
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013498 assert(!Subtarget->isTargetEnvMacho());
13499
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013500 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13501 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013502
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013503 if (Subtarget->isTargetWin64()) {
13504 if (Subtarget->isTargetCygMing()) {
13505 // ___chkstk(Mingw64):
13506 // Clobbers R10, R11, RAX and EFLAGS.
13507 // Updates RSP.
13508 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13509 .addExternalSymbol("___chkstk")
13510 .addReg(X86::RAX, RegState::Implicit)
13511 .addReg(X86::RSP, RegState::Implicit)
13512 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13513 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13514 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13515 } else {
13516 // __chkstk(MSVCRT): does not update stack pointer.
13517 // Clobbers R10, R11 and EFLAGS.
13518 // FIXME: RAX(allocated size) might be reused and not killed.
13519 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13520 .addExternalSymbol("__chkstk")
13521 .addReg(X86::RAX, RegState::Implicit)
13522 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13523 // RAX has the offset to subtracted from RSP.
13524 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13525 .addReg(X86::RSP)
13526 .addReg(X86::RAX);
13527 }
13528 } else {
13529 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013530 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13531
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013532 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13533 .addExternalSymbol(StackProbeSymbol)
13534 .addReg(X86::EAX, RegState::Implicit)
13535 .addReg(X86::ESP, RegState::Implicit)
13536 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13537 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13538 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13539 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013540
Dan Gohman14152b42010-07-06 20:24:04 +000013541 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013542 return BB;
13543}
Chris Lattner52600972009-09-02 05:57:00 +000013544
13545MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013546X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13547 MachineBasicBlock *BB) const {
13548 // This is pretty easy. We're taking the value that we received from
13549 // our load from the relocation, sticking it in either RDI (x86-64)
13550 // or EAX and doing an indirect call. The return value will then
13551 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013552 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013553 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013554 DebugLoc DL = MI->getDebugLoc();
13555 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013556
13557 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013558 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013559
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013560 // Get a register mask for the lowered call.
13561 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13562 // proper register mask.
13563 const uint32_t *RegMask =
13564 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013565 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013566 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13567 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013568 .addReg(X86::RIP)
13569 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013570 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013571 MI->getOperand(3).getTargetFlags())
13572 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013573 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013574 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013575 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013576 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013577 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13578 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013579 .addReg(0)
13580 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013581 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013582 MI->getOperand(3).getTargetFlags())
13583 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013584 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013585 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013586 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013587 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013588 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13589 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013590 .addReg(TII->getGlobalBaseReg(F))
13591 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013592 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013593 MI->getOperand(3).getTargetFlags())
13594 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013595 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013596 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013597 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013598 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013599
Dan Gohman14152b42010-07-06 20:24:04 +000013600 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013601 return BB;
13602}
13603
13604MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013605X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13606 MachineBasicBlock *MBB) const {
13607 DebugLoc DL = MI->getDebugLoc();
13608 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13609
13610 MachineFunction *MF = MBB->getParent();
13611 MachineRegisterInfo &MRI = MF->getRegInfo();
13612
13613 const BasicBlock *BB = MBB->getBasicBlock();
13614 MachineFunction::iterator I = MBB;
13615 ++I;
13616
13617 // Memory Reference
13618 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13619 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13620
13621 unsigned DstReg;
13622 unsigned MemOpndSlot = 0;
13623
13624 unsigned CurOp = 0;
13625
13626 DstReg = MI->getOperand(CurOp++).getReg();
13627 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
13628 assert(RC->hasType(MVT::i32) && "Invalid destination!");
13629 unsigned mainDstReg = MRI.createVirtualRegister(RC);
13630 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
13631
13632 MemOpndSlot = CurOp;
13633
13634 MVT PVT = getPointerTy();
13635 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13636 "Invalid Pointer Size!");
13637
13638 // For v = setjmp(buf), we generate
13639 //
13640 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013641 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000013642 // SjLjSetup restoreMBB
13643 //
13644 // mainMBB:
13645 // v_main = 0
13646 //
13647 // sinkMBB:
13648 // v = phi(main, restore)
13649 //
13650 // restoreMBB:
13651 // v_restore = 1
13652
13653 MachineBasicBlock *thisMBB = MBB;
13654 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13655 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13656 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
13657 MF->insert(I, mainMBB);
13658 MF->insert(I, sinkMBB);
13659 MF->push_back(restoreMBB);
13660
13661 MachineInstrBuilder MIB;
13662
13663 // Transfer the remainder of BB and its successor edges to sinkMBB.
13664 sinkMBB->splice(sinkMBB->begin(), MBB,
13665 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13666 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13667
13668 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000013669 unsigned PtrStoreOpc = 0;
13670 unsigned LabelReg = 0;
13671 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13672 Reloc::Model RM = getTargetMachine().getRelocationModel();
13673 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
13674 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013675
Michael Liao281ae5a2012-10-17 02:22:27 +000013676 // Prepare IP either in reg or imm.
13677 if (!UseImmLabel) {
13678 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
13679 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
13680 LabelReg = MRI.createVirtualRegister(PtrRC);
13681 if (Subtarget->is64Bit()) {
13682 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
13683 .addReg(X86::RIP)
13684 .addImm(0)
13685 .addReg(0)
13686 .addMBB(restoreMBB)
13687 .addReg(0);
13688 } else {
13689 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
13690 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
13691 .addReg(XII->getGlobalBaseReg(MF))
13692 .addImm(0)
13693 .addReg(0)
13694 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
13695 .addReg(0);
13696 }
13697 } else
13698 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000013699 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000013700 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000013701 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13702 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013703 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013704 else
13705 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13706 }
Michael Liao281ae5a2012-10-17 02:22:27 +000013707 if (!UseImmLabel)
13708 MIB.addReg(LabelReg);
13709 else
13710 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013711 MIB.setMemRefs(MMOBegin, MMOEnd);
13712 // Setup
13713 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
13714 .addMBB(restoreMBB);
13715 MIB.addRegMask(RegInfo->getNoPreservedMask());
13716 thisMBB->addSuccessor(mainMBB);
13717 thisMBB->addSuccessor(restoreMBB);
13718
13719 // mainMBB:
13720 // EAX = 0
13721 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
13722 mainMBB->addSuccessor(sinkMBB);
13723
13724 // sinkMBB:
13725 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13726 TII->get(X86::PHI), DstReg)
13727 .addReg(mainDstReg).addMBB(mainMBB)
13728 .addReg(restoreDstReg).addMBB(restoreMBB);
13729
13730 // restoreMBB:
13731 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
13732 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
13733 restoreMBB->addSuccessor(sinkMBB);
13734
13735 MI->eraseFromParent();
13736 return sinkMBB;
13737}
13738
13739MachineBasicBlock *
13740X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
13741 MachineBasicBlock *MBB) const {
13742 DebugLoc DL = MI->getDebugLoc();
13743 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13744
13745 MachineFunction *MF = MBB->getParent();
13746 MachineRegisterInfo &MRI = MF->getRegInfo();
13747
13748 // Memory Reference
13749 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13750 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13751
13752 MVT PVT = getPointerTy();
13753 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
13754 "Invalid Pointer Size!");
13755
13756 const TargetRegisterClass *RC =
13757 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
13758 unsigned Tmp = MRI.createVirtualRegister(RC);
13759 // Since FP is only updated here but NOT referenced, it's treated as GPR.
13760 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
13761 unsigned SP = RegInfo->getStackRegister();
13762
13763 MachineInstrBuilder MIB;
13764
Michael Liao281ae5a2012-10-17 02:22:27 +000013765 const int64_t LabelOffset = 1 * PVT.getStoreSize();
13766 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000013767
13768 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
13769 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
13770
13771 // Reload FP
13772 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
13773 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13774 MIB.addOperand(MI->getOperand(i));
13775 MIB.setMemRefs(MMOBegin, MMOEnd);
13776 // Reload IP
13777 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
13778 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13779 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013780 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013781 else
13782 MIB.addOperand(MI->getOperand(i));
13783 }
13784 MIB.setMemRefs(MMOBegin, MMOEnd);
13785 // Reload SP
13786 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
13787 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13788 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000013789 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000013790 else
13791 MIB.addOperand(MI->getOperand(i));
13792 }
13793 MIB.setMemRefs(MMOBegin, MMOEnd);
13794 // Jump
13795 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
13796
13797 MI->eraseFromParent();
13798 return MBB;
13799}
13800
13801MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013802X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013803 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013804 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013805 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013806 case X86::TAILJMPd64:
13807 case X86::TAILJMPr64:
13808 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013809 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013810 case X86::TCRETURNdi64:
13811 case X86::TCRETURNri64:
13812 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013813 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013814 case X86::WIN_ALLOCA:
13815 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013816 case X86::SEG_ALLOCA_32:
13817 return EmitLoweredSegAlloca(MI, BB, false);
13818 case X86::SEG_ALLOCA_64:
13819 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013820 case X86::TLSCall_32:
13821 case X86::TLSCall_64:
13822 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013823 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013824 case X86::CMOV_FR32:
13825 case X86::CMOV_FR64:
13826 case X86::CMOV_V4F32:
13827 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013828 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013829 case X86::CMOV_V8F32:
13830 case X86::CMOV_V4F64:
13831 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013832 case X86::CMOV_GR16:
13833 case X86::CMOV_GR32:
13834 case X86::CMOV_RFP32:
13835 case X86::CMOV_RFP64:
13836 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013837 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013838
Dale Johannesen849f2142007-07-03 00:53:03 +000013839 case X86::FP32_TO_INT16_IN_MEM:
13840 case X86::FP32_TO_INT32_IN_MEM:
13841 case X86::FP32_TO_INT64_IN_MEM:
13842 case X86::FP64_TO_INT16_IN_MEM:
13843 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013844 case X86::FP64_TO_INT64_IN_MEM:
13845 case X86::FP80_TO_INT16_IN_MEM:
13846 case X86::FP80_TO_INT32_IN_MEM:
13847 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013848 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13849 DebugLoc DL = MI->getDebugLoc();
13850
Evan Cheng60c07e12006-07-05 22:17:51 +000013851 // Change the floating point control register to use "round towards zero"
13852 // mode when truncating to an integer value.
13853 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013854 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013855 addFrameReference(BuildMI(*BB, MI, DL,
13856 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013857
13858 // Load the old value of the high byte of the control word...
13859 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013860 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013861 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013862 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013863
13864 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013865 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013866 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013867
13868 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013869 addFrameReference(BuildMI(*BB, MI, DL,
13870 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013871
13872 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013873 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013874 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013875
13876 // Get the X86 opcode to use.
13877 unsigned Opc;
13878 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013879 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013880 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13881 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13882 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13883 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13884 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13885 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013886 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13887 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13888 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013889 }
13890
13891 X86AddressMode AM;
13892 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013893 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013894 AM.BaseType = X86AddressMode::RegBase;
13895 AM.Base.Reg = Op.getReg();
13896 } else {
13897 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013898 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013899 }
13900 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013901 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013902 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013903 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013904 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013905 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013906 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013907 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013908 AM.GV = Op.getGlobal();
13909 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013910 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013911 }
Dan Gohman14152b42010-07-06 20:24:04 +000013912 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013913 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013914
13915 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013916 addFrameReference(BuildMI(*BB, MI, DL,
13917 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013918
Dan Gohman14152b42010-07-06 20:24:04 +000013919 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013920 return BB;
13921 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013922 // String/text processing lowering.
13923 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013924 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013925 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013926 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013927 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013928 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013929 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000013930 case X86::VPCMPESTRM128MEM:
13931 assert(Subtarget->hasSSE42() &&
13932 "Target must have SSE4.2 or AVX features enabled");
13933 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000013934
13935 // String/text processing lowering.
13936 case X86::PCMPISTRIREG:
13937 case X86::VPCMPISTRIREG:
13938 case X86::PCMPISTRIMEM:
13939 case X86::VPCMPISTRIMEM:
13940 case X86::PCMPESTRIREG:
13941 case X86::VPCMPESTRIREG:
13942 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000013943 case X86::VPCMPESTRIMEM:
13944 assert(Subtarget->hasSSE42() &&
13945 "Target must have SSE4.2 or AVX features enabled");
13946 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000013947
Craig Topper8aae8dd2012-11-10 08:57:41 +000013948 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000013949 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000013950 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000013951
Michael Liaobe02a902012-11-08 07:28:54 +000013952 // xbegin
13953 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000013954 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000013955
Craig Topper8aae8dd2012-11-10 08:57:41 +000013956 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013957 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013958 case X86::ATOMAND16:
13959 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013960 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013961 // Fall through
13962 case X86::ATOMOR8:
13963 case X86::ATOMOR16:
13964 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013965 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013966 // Fall through
13967 case X86::ATOMXOR16:
13968 case X86::ATOMXOR8:
13969 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013970 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013971 // Fall through
13972 case X86::ATOMNAND8:
13973 case X86::ATOMNAND16:
13974 case X86::ATOMNAND32:
13975 case X86::ATOMNAND64:
13976 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013977 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013978 case X86::ATOMMAX16:
13979 case X86::ATOMMAX32:
13980 case X86::ATOMMAX64:
13981 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013982 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013983 case X86::ATOMMIN16:
13984 case X86::ATOMMIN32:
13985 case X86::ATOMMIN64:
13986 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013987 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013988 case X86::ATOMUMAX16:
13989 case X86::ATOMUMAX32:
13990 case X86::ATOMUMAX64:
13991 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013992 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013993 case X86::ATOMUMIN16:
13994 case X86::ATOMUMIN32:
13995 case X86::ATOMUMIN64:
13996 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013997
13998 // This group does 64-bit operations on a 32-bit host.
13999 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014000 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014001 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014002 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014003 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014004 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014005 case X86::ATOMMAX6432:
14006 case X86::ATOMMIN6432:
14007 case X86::ATOMUMAX6432:
14008 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014009 case X86::ATOMSWAP6432:
14010 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014011
Dan Gohmand6708ea2009-08-15 01:38:56 +000014012 case X86::VASTART_SAVE_XMM_REGS:
14013 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014014
14015 case X86::VAARG_64:
14016 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014017
14018 case X86::EH_SjLj_SetJmp32:
14019 case X86::EH_SjLj_SetJmp64:
14020 return emitEHSjLjSetJmp(MI, BB);
14021
14022 case X86::EH_SjLj_LongJmp32:
14023 case X86::EH_SjLj_LongJmp64:
14024 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014025 }
14026}
14027
14028//===----------------------------------------------------------------------===//
14029// X86 Optimization Hooks
14030//===----------------------------------------------------------------------===//
14031
Dan Gohman475871a2008-07-27 21:46:04 +000014032void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014033 APInt &KnownZero,
14034 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014035 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014036 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014037 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014038 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014039 assert((Opc >= ISD::BUILTIN_OP_END ||
14040 Opc == ISD::INTRINSIC_WO_CHAIN ||
14041 Opc == ISD::INTRINSIC_W_CHAIN ||
14042 Opc == ISD::INTRINSIC_VOID) &&
14043 "Should use MaskedValueIsZero if you don't know whether Op"
14044 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014045
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014046 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014047 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014048 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014049 case X86ISD::ADD:
14050 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014051 case X86ISD::ADC:
14052 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014053 case X86ISD::SMUL:
14054 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014055 case X86ISD::INC:
14056 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014057 case X86ISD::OR:
14058 case X86ISD::XOR:
14059 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014060 // These nodes' second result is a boolean.
14061 if (Op.getResNo() == 0)
14062 break;
14063 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014064 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014065 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014066 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014067 case ISD::INTRINSIC_WO_CHAIN: {
14068 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14069 unsigned NumLoBits = 0;
14070 switch (IntId) {
14071 default: break;
14072 case Intrinsic::x86_sse_movmsk_ps:
14073 case Intrinsic::x86_avx_movmsk_ps_256:
14074 case Intrinsic::x86_sse2_movmsk_pd:
14075 case Intrinsic::x86_avx_movmsk_pd_256:
14076 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014077 case Intrinsic::x86_sse2_pmovmskb_128:
14078 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014079 // High bits of movmskp{s|d}, pmovmskb are known zero.
14080 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014081 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014082 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14083 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14084 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14085 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14086 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14087 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014088 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014089 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014090 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014091 break;
14092 }
14093 }
14094 break;
14095 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014096 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014097}
Chris Lattner259e97c2006-01-31 19:43:35 +000014098
Owen Andersonbc146b02010-09-21 20:42:50 +000014099unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14100 unsigned Depth) const {
14101 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14102 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14103 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014104
Owen Andersonbc146b02010-09-21 20:42:50 +000014105 // Fallback case.
14106 return 1;
14107}
14108
Evan Cheng206ee9d2006-07-07 08:33:52 +000014109/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014110/// node is a GlobalAddress + offset.
14111bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014112 const GlobalValue* &GA,
14113 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014114 if (N->getOpcode() == X86ISD::Wrapper) {
14115 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014116 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014117 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014118 return true;
14119 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014120 }
Evan Chengad4196b2008-05-12 19:56:52 +000014121 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014122}
14123
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014124/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14125/// same as extracting the high 128-bit part of 256-bit vector and then
14126/// inserting the result into the low part of a new 256-bit vector
14127static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14128 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014129 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014130
14131 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014132 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014133 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14134 SVOp->getMaskElt(j) >= 0)
14135 return false;
14136
14137 return true;
14138}
14139
14140/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14141/// same as extracting the low 128-bit part of 256-bit vector and then
14142/// inserting the result into the high part of a new 256-bit vector
14143static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14144 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014145 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014146
14147 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014148 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014149 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14150 SVOp->getMaskElt(j) >= 0)
14151 return false;
14152
14153 return true;
14154}
14155
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014156/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14157static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014158 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014159 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014160 DebugLoc dl = N->getDebugLoc();
14161 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14162 SDValue V1 = SVOp->getOperand(0);
14163 SDValue V2 = SVOp->getOperand(1);
14164 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014165 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014166
14167 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14168 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14169 //
14170 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014171 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014172 // V UNDEF BUILD_VECTOR UNDEF
14173 // \ / \ /
14174 // CONCAT_VECTOR CONCAT_VECTOR
14175 // \ /
14176 // \ /
14177 // RESULT: V + zero extended
14178 //
14179 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14180 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14181 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14182 return SDValue();
14183
14184 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14185 return SDValue();
14186
14187 // To match the shuffle mask, the first half of the mask should
14188 // be exactly the first vector, and all the rest a splat with the
14189 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014190 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014191 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14192 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14193 return SDValue();
14194
Chad Rosier3d1161e2012-01-03 21:05:52 +000014195 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14196 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014197 if (Ld->hasNUsesOfValue(1, 0)) {
14198 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14199 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14200 SDValue ResNode =
14201 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14202 Ld->getMemoryVT(),
14203 Ld->getPointerInfo(),
14204 Ld->getAlignment(),
14205 false/*isVolatile*/, true/*ReadMem*/,
14206 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014207
14208 // Make sure the newly-created LOAD is in the same position as Ld in
14209 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14210 // and update uses of Ld's output chain to use the TokenFactor.
14211 if (Ld->hasAnyUseOfValue(1)) {
14212 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14213 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14214 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14215 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14216 SDValue(ResNode.getNode(), 1));
14217 }
14218
Chad Rosier42726832012-05-07 18:47:44 +000014219 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14220 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014221 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014222
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014223 // Emit a zeroed vector and insert the desired subvector on its
14224 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014225 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014226 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014227 return DCI.CombineTo(N, InsV);
14228 }
14229
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014230 //===--------------------------------------------------------------------===//
14231 // Combine some shuffles into subvector extracts and inserts:
14232 //
14233
14234 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14235 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014236 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14237 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014238 return DCI.CombineTo(N, InsV);
14239 }
14240
14241 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14242 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014243 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14244 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014245 return DCI.CombineTo(N, InsV);
14246 }
14247
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014248 return SDValue();
14249}
14250
14251/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014252static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014253 TargetLowering::DAGCombinerInfo &DCI,
14254 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014255 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014256 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014257
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014258 // Don't create instructions with illegal types after legalize types has run.
14259 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14260 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14261 return SDValue();
14262
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014263 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000014264 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014265 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014266 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014267
14268 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014269 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014270 return SDValue();
14271
14272 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14273 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14274 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014275 SmallVector<SDValue, 16> Elts;
14276 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014277 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014278
Nate Begemanfdea31a2010-03-24 20:49:50 +000014279 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014280}
Evan Chengd880b972008-05-09 21:53:03 +000014281
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014282
Craig Topper55b24052012-09-11 06:15:32 +000014283/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014284/// a sequence of vector shuffle operations.
14285/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014286static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14287 TargetLowering::DAGCombinerInfo &DCI,
14288 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014289 if (!DCI.isBeforeLegalizeOps())
14290 return SDValue();
14291
Craig Topper3ef43cf2012-04-24 06:36:35 +000014292 if (!Subtarget->hasAVX())
14293 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014294
14295 EVT VT = N->getValueType(0);
14296 SDValue Op = N->getOperand(0);
14297 EVT OpVT = Op.getValueType();
14298 DebugLoc dl = N->getDebugLoc();
14299
14300 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
14301
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014302 if (Subtarget->hasAVX2()) {
14303 // AVX2: v4i64 -> v4i32
14304
14305 // VPERMD
14306 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
14307
14308 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
14309 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
14310 ShufMask);
14311
Craig Topperd63fa652012-04-22 18:51:37 +000014312 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
14313 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014314 }
14315
14316 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014317 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014318 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014319
14320 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014321 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014322
14323 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14324 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14325
14326 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000014327 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014328
Craig Toppercacafd42012-08-14 08:18:43 +000014329 SDValue Undef = DAG.getUNDEF(VT);
14330 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
14331 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014332
14333 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014334 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014335
Elena Demikhovsky73252572012-02-01 10:33:05 +000014336 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014337 }
Craig Topperd63fa652012-04-22 18:51:37 +000014338
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014339 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
14340
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014341 if (Subtarget->hasAVX2()) {
14342 // AVX2: v8i32 -> v8i16
14343
14344 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000014345
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014346 // PSHUFB
14347 SmallVector<SDValue,32> pshufbMask;
14348 for (unsigned i = 0; i < 2; ++i) {
14349 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
14350 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
14351 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
14352 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
14353 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
14354 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
14355 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
14356 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
14357 for (unsigned j = 0; j < 8; ++j)
14358 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
14359 }
Craig Topperd63fa652012-04-22 18:51:37 +000014360 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
14361 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014362 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
14363
14364 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
14365
14366 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000014367 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014368 &ShufMask[0]);
14369
Craig Topperd63fa652012-04-22 18:51:37 +000014370 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
14371 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000014372
14373 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
14374 }
14375
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014376 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014377 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014378
14379 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000014380 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014381
14382 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
14383 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
14384
14385 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000014386 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
14387 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014388
Craig Toppercacafd42012-08-14 08:18:43 +000014389 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
14390 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
14391 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014392
14393 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
14394 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
14395
14396 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000014397 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014398
Elena Demikhovsky73252572012-02-01 10:33:05 +000014399 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014400 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014401 }
14402
14403 return SDValue();
14404}
14405
Craig Topper89f4e662012-03-20 07:17:59 +000014406/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14407/// specific shuffle of a load can be folded into a single element load.
14408/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14409/// shuffles have been customed lowered so we need to handle those here.
14410static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14411 TargetLowering::DAGCombinerInfo &DCI) {
14412 if (DCI.isBeforeLegalizeOps())
14413 return SDValue();
14414
14415 SDValue InVec = N->getOperand(0);
14416 SDValue EltNo = N->getOperand(1);
14417
14418 if (!isa<ConstantSDNode>(EltNo))
14419 return SDValue();
14420
14421 EVT VT = InVec.getValueType();
14422
14423 bool HasShuffleIntoBitcast = false;
14424 if (InVec.getOpcode() == ISD::BITCAST) {
14425 // Don't duplicate a load with other uses.
14426 if (!InVec.hasOneUse())
14427 return SDValue();
14428 EVT BCVT = InVec.getOperand(0).getValueType();
14429 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14430 return SDValue();
14431 InVec = InVec.getOperand(0);
14432 HasShuffleIntoBitcast = true;
14433 }
14434
14435 if (!isTargetShuffle(InVec.getOpcode()))
14436 return SDValue();
14437
14438 // Don't duplicate a load with other uses.
14439 if (!InVec.hasOneUse())
14440 return SDValue();
14441
14442 SmallVector<int, 16> ShuffleMask;
14443 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014444 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14445 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014446 return SDValue();
14447
14448 // Select the input vector, guarding against out of range extract vector.
14449 unsigned NumElems = VT.getVectorNumElements();
14450 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14451 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14452 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14453 : InVec.getOperand(1);
14454
14455 // If inputs to shuffle are the same for both ops, then allow 2 uses
14456 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14457
14458 if (LdNode.getOpcode() == ISD::BITCAST) {
14459 // Don't duplicate a load with other uses.
14460 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14461 return SDValue();
14462
14463 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14464 LdNode = LdNode.getOperand(0);
14465 }
14466
14467 if (!ISD::isNormalLoad(LdNode.getNode()))
14468 return SDValue();
14469
14470 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14471
14472 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14473 return SDValue();
14474
14475 if (HasShuffleIntoBitcast) {
14476 // If there's a bitcast before the shuffle, check if the load type and
14477 // alignment is valid.
14478 unsigned Align = LN0->getAlignment();
14479 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014480 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014481 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14482
14483 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14484 return SDValue();
14485 }
14486
14487 // All checks match so transform back to vector_shuffle so that DAG combiner
14488 // can finish the job
14489 DebugLoc dl = N->getDebugLoc();
14490
14491 // Create shuffle node taking into account the case that its a unary shuffle
14492 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14493 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14494 InVec.getOperand(0), Shuffle,
14495 &ShuffleMask[0]);
14496 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14497 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14498 EltNo);
14499}
14500
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014501/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14502/// generation and convert it from being a bunch of shuffles and extracts
14503/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014504static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014505 TargetLowering::DAGCombinerInfo &DCI) {
14506 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14507 if (NewOp.getNode())
14508 return NewOp;
14509
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014510 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014511 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14512 // from mmx to v2i32 has a single usage.
14513 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14514 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14515 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14516 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14517 N->getValueType(0),
14518 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014519
14520 // Only operate on vectors of 4 elements, where the alternative shuffling
14521 // gets to be more expensive.
14522 if (InputVector.getValueType() != MVT::v4i32)
14523 return SDValue();
14524
14525 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14526 // single use which is a sign-extend or zero-extend, and all elements are
14527 // used.
14528 SmallVector<SDNode *, 4> Uses;
14529 unsigned ExtractedElements = 0;
14530 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14531 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14532 if (UI.getUse().getResNo() != InputVector.getResNo())
14533 return SDValue();
14534
14535 SDNode *Extract = *UI;
14536 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14537 return SDValue();
14538
14539 if (Extract->getValueType(0) != MVT::i32)
14540 return SDValue();
14541 if (!Extract->hasOneUse())
14542 return SDValue();
14543 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14544 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14545 return SDValue();
14546 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14547 return SDValue();
14548
14549 // Record which element was extracted.
14550 ExtractedElements |=
14551 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14552
14553 Uses.push_back(Extract);
14554 }
14555
14556 // If not all the elements were used, this may not be worthwhile.
14557 if (ExtractedElements != 15)
14558 return SDValue();
14559
14560 // Ok, we've now decided to do the transformation.
14561 DebugLoc dl = InputVector.getDebugLoc();
14562
14563 // Store the value to a temporary stack slot.
14564 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014565 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14566 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014567
14568 // Replace each use (extract) with a load of the appropriate element.
14569 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14570 UE = Uses.end(); UI != UE; ++UI) {
14571 SDNode *Extract = *UI;
14572
Nadav Rotem86694292011-05-17 08:31:57 +000014573 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014574 SDValue Idx = Extract->getOperand(1);
14575 unsigned EltSize =
14576 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14577 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014578 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014579 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14580
Nadav Rotem86694292011-05-17 08:31:57 +000014581 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014582 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014583
14584 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014585 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014586 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014587 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014588
14589 // Replace the exact with the load.
14590 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14591 }
14592
14593 // The replacement was made in place; don't return anything.
14594 return SDValue();
14595}
14596
Duncan Sands6bcd2192011-09-17 16:49:39 +000014597/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14598/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014599static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014600 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014601 const X86Subtarget *Subtarget) {
14602 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014603 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014604 // Get the LHS/RHS of the select.
14605 SDValue LHS = N->getOperand(1);
14606 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014607 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014608
Dan Gohman670e5392009-09-21 18:03:22 +000014609 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014610 // instructions match the semantics of the common C idiom x<y?x:y but not
14611 // x<=y?x:y, because of how they handle negative zero (which can be
14612 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014613 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14614 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014615 (Subtarget->hasSSE2() ||
14616 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014617 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014618
Chris Lattner47b4ce82009-03-11 05:48:52 +000014619 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014620 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014621 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14622 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014623 switch (CC) {
14624 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014625 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014626 // Converting this to a min would handle NaNs incorrectly, and swapping
14627 // the operands would cause it to handle comparisons between positive
14628 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014629 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014630 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014631 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14632 break;
14633 std::swap(LHS, RHS);
14634 }
Dan Gohman670e5392009-09-21 18:03:22 +000014635 Opcode = X86ISD::FMIN;
14636 break;
14637 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014638 // Converting this to a min would handle comparisons between positive
14639 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014640 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014641 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14642 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014643 Opcode = X86ISD::FMIN;
14644 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014645 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014646 // Converting this to a min would handle both negative zeros and NaNs
14647 // incorrectly, but we can swap the operands to fix both.
14648 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014649 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014650 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014651 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014652 Opcode = X86ISD::FMIN;
14653 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014654
Dan Gohman670e5392009-09-21 18:03:22 +000014655 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014656 // Converting this to a max would handle comparisons between positive
14657 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014658 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014659 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014660 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014661 Opcode = X86ISD::FMAX;
14662 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014663 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014664 // Converting this to a max would handle NaNs incorrectly, and swapping
14665 // the operands would cause it to handle comparisons between positive
14666 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014667 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014668 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014669 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14670 break;
14671 std::swap(LHS, RHS);
14672 }
Dan Gohman670e5392009-09-21 18:03:22 +000014673 Opcode = X86ISD::FMAX;
14674 break;
14675 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014676 // Converting this to a max would handle both negative zeros and NaNs
14677 // incorrectly, but we can swap the operands to fix both.
14678 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014679 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014680 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014681 case ISD::SETGE:
14682 Opcode = X86ISD::FMAX;
14683 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014684 }
Dan Gohman670e5392009-09-21 18:03:22 +000014685 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014686 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14687 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014688 switch (CC) {
14689 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014690 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014691 // Converting this to a min would handle comparisons between positive
14692 // and negative zero incorrectly, and swapping the operands would
14693 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014694 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014695 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014696 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014697 break;
14698 std::swap(LHS, RHS);
14699 }
Dan Gohman670e5392009-09-21 18:03:22 +000014700 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014701 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014702 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014703 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014704 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014705 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14706 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014707 Opcode = X86ISD::FMIN;
14708 break;
14709 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014710 // Converting this to a min would handle both negative zeros and NaNs
14711 // incorrectly, but we can swap the operands to fix both.
14712 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014713 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014714 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014715 case ISD::SETGE:
14716 Opcode = X86ISD::FMIN;
14717 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014718
Dan Gohman670e5392009-09-21 18:03:22 +000014719 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014720 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014721 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014722 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014723 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014724 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014725 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014726 // Converting this to a max would handle comparisons between positive
14727 // and negative zero incorrectly, and swapping the operands would
14728 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014729 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014730 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014731 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014732 break;
14733 std::swap(LHS, RHS);
14734 }
Dan Gohman670e5392009-09-21 18:03:22 +000014735 Opcode = X86ISD::FMAX;
14736 break;
14737 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014738 // Converting this to a max would handle both negative zeros and NaNs
14739 // incorrectly, but we can swap the operands to fix both.
14740 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014741 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014742 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014743 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014744 Opcode = X86ISD::FMAX;
14745 break;
14746 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014747 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014748
Chris Lattner47b4ce82009-03-11 05:48:52 +000014749 if (Opcode)
14750 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014751 }
Eric Christopherfd179292009-08-27 18:07:15 +000014752
Chris Lattnerd1980a52009-03-12 06:52:53 +000014753 // If this is a select between two integer constants, try to do some
14754 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014755 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14756 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014757 // Don't do this for crazy integer types.
14758 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14759 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014760 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014761 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014762
Chris Lattnercee56e72009-03-13 05:53:31 +000014763 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014764 // Efficiently invertible.
14765 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14766 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14767 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14768 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014769 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014770 }
Eric Christopherfd179292009-08-27 18:07:15 +000014771
Chris Lattnerd1980a52009-03-12 06:52:53 +000014772 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014773 if (FalseC->getAPIntValue() == 0 &&
14774 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014775 if (NeedsCondInvert) // Invert the condition if needed.
14776 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14777 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014778
Chris Lattnerd1980a52009-03-12 06:52:53 +000014779 // Zero extend the condition if needed.
14780 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014781
Chris Lattnercee56e72009-03-13 05:53:31 +000014782 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014783 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014784 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014785 }
Eric Christopherfd179292009-08-27 18:07:15 +000014786
Chris Lattner97a29a52009-03-13 05:22:11 +000014787 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014788 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014789 if (NeedsCondInvert) // Invert the condition if needed.
14790 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14791 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014792
Chris Lattner97a29a52009-03-13 05:22:11 +000014793 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014794 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14795 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014796 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014797 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014798 }
Eric Christopherfd179292009-08-27 18:07:15 +000014799
Chris Lattnercee56e72009-03-13 05:53:31 +000014800 // Optimize cases that will turn into an LEA instruction. This requires
14801 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014802 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014803 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014804 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014805
Chris Lattnercee56e72009-03-13 05:53:31 +000014806 bool isFastMultiplier = false;
14807 if (Diff < 10) {
14808 switch ((unsigned char)Diff) {
14809 default: break;
14810 case 1: // result = add base, cond
14811 case 2: // result = lea base( , cond*2)
14812 case 3: // result = lea base(cond, cond*2)
14813 case 4: // result = lea base( , cond*4)
14814 case 5: // result = lea base(cond, cond*4)
14815 case 8: // result = lea base( , cond*8)
14816 case 9: // result = lea base(cond, cond*8)
14817 isFastMultiplier = true;
14818 break;
14819 }
14820 }
Eric Christopherfd179292009-08-27 18:07:15 +000014821
Chris Lattnercee56e72009-03-13 05:53:31 +000014822 if (isFastMultiplier) {
14823 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14824 if (NeedsCondInvert) // Invert the condition if needed.
14825 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14826 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014827
Chris Lattnercee56e72009-03-13 05:53:31 +000014828 // Zero extend the condition if needed.
14829 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14830 Cond);
14831 // Scale the condition by the difference.
14832 if (Diff != 1)
14833 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14834 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014835
Chris Lattnercee56e72009-03-13 05:53:31 +000014836 // Add the base if non-zero.
14837 if (FalseC->getAPIntValue() != 0)
14838 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14839 SDValue(FalseC, 0));
14840 return Cond;
14841 }
Eric Christopherfd179292009-08-27 18:07:15 +000014842 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014843 }
14844 }
Eric Christopherfd179292009-08-27 18:07:15 +000014845
Evan Cheng56f582d2012-01-04 01:41:39 +000014846 // Canonicalize max and min:
14847 // (x > y) ? x : y -> (x >= y) ? x : y
14848 // (x < y) ? x : y -> (x <= y) ? x : y
14849 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14850 // the need for an extra compare
14851 // against zero. e.g.
14852 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14853 // subl %esi, %edi
14854 // testl %edi, %edi
14855 // movl $0, %eax
14856 // cmovgl %edi, %eax
14857 // =>
14858 // xorl %eax, %eax
14859 // subl %esi, $edi
14860 // cmovsl %eax, %edi
14861 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14862 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14863 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14864 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14865 switch (CC) {
14866 default: break;
14867 case ISD::SETLT:
14868 case ISD::SETGT: {
14869 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14870 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14871 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14872 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14873 }
14874 }
14875 }
14876
Nadav Rotemcc616562012-01-15 19:27:55 +000014877 // If we know that this node is legal then we know that it is going to be
14878 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14879 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14880 // to simplify previous instructions.
14881 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14882 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014883 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014884 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014885
14886 // Don't optimize vector selects that map to mask-registers.
14887 if (BitWidth == 1)
14888 return SDValue();
14889
Nadav Rotemcc616562012-01-15 19:27:55 +000014890 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14891 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14892
14893 APInt KnownZero, KnownOne;
14894 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14895 DCI.isBeforeLegalizeOps());
14896 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14897 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14898 DCI.CommitTargetLoweringOpt(TLO);
14899 }
14900
Dan Gohman475871a2008-07-27 21:46:04 +000014901 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014902}
14903
Michael Liao2a33cec2012-08-10 19:58:13 +000014904// Check whether a boolean test is testing a boolean value generated by
14905// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14906// code.
14907//
14908// Simplify the following patterns:
14909// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14910// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14911// to (Op EFLAGS Cond)
14912//
14913// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14914// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14915// to (Op EFLAGS !Cond)
14916//
14917// where Op could be BRCOND or CMOV.
14918//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014919static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014920 // Quit if not CMP and SUB with its value result used.
14921 if (Cmp.getOpcode() != X86ISD::CMP &&
14922 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14923 return SDValue();
14924
14925 // Quit if not used as a boolean value.
14926 if (CC != X86::COND_E && CC != X86::COND_NE)
14927 return SDValue();
14928
14929 // Check CMP operands. One of them should be 0 or 1 and the other should be
14930 // an SetCC or extended from it.
14931 SDValue Op1 = Cmp.getOperand(0);
14932 SDValue Op2 = Cmp.getOperand(1);
14933
14934 SDValue SetCC;
14935 const ConstantSDNode* C = 0;
14936 bool needOppositeCond = (CC == X86::COND_E);
14937
14938 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14939 SetCC = Op2;
14940 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14941 SetCC = Op1;
14942 else // Quit if all operands are not constants.
14943 return SDValue();
14944
14945 if (C->getZExtValue() == 1)
14946 needOppositeCond = !needOppositeCond;
14947 else if (C->getZExtValue() != 0)
14948 // Quit if the constant is neither 0 or 1.
14949 return SDValue();
14950
14951 // Skip 'zext' node.
14952 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14953 SetCC = SetCC.getOperand(0);
14954
Michael Liao7fdc66b2012-09-10 16:36:16 +000014955 switch (SetCC.getOpcode()) {
14956 case X86ISD::SETCC:
14957 // Set the condition code or opposite one if necessary.
14958 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14959 if (needOppositeCond)
14960 CC = X86::GetOppositeBranchCondition(CC);
14961 return SetCC.getOperand(1);
14962 case X86ISD::CMOV: {
14963 // Check whether false/true value has canonical one, i.e. 0 or 1.
14964 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14965 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14966 // Quit if true value is not a constant.
14967 if (!TVal)
14968 return SDValue();
14969 // Quit if false value is not a constant.
14970 if (!FVal) {
14971 // A special case for rdrand, where 0 is set if false cond is found.
14972 SDValue Op = SetCC.getOperand(0);
14973 if (Op.getOpcode() != X86ISD::RDRAND)
14974 return SDValue();
14975 }
14976 // Quit if false value is not the constant 0 or 1.
14977 bool FValIsFalse = true;
14978 if (FVal && FVal->getZExtValue() != 0) {
14979 if (FVal->getZExtValue() != 1)
14980 return SDValue();
14981 // If FVal is 1, opposite cond is needed.
14982 needOppositeCond = !needOppositeCond;
14983 FValIsFalse = false;
14984 }
14985 // Quit if TVal is not the constant opposite of FVal.
14986 if (FValIsFalse && TVal->getZExtValue() != 1)
14987 return SDValue();
14988 if (!FValIsFalse && TVal->getZExtValue() != 0)
14989 return SDValue();
14990 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14991 if (needOppositeCond)
14992 CC = X86::GetOppositeBranchCondition(CC);
14993 return SetCC.getOperand(3);
14994 }
14995 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014996
Michael Liao7fdc66b2012-09-10 16:36:16 +000014997 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014998}
14999
Chris Lattnerd1980a52009-03-12 06:52:53 +000015000/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15001static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015002 TargetLowering::DAGCombinerInfo &DCI,
15003 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015004 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015005
Chris Lattnerd1980a52009-03-12 06:52:53 +000015006 // If the flag operand isn't dead, don't touch this CMOV.
15007 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15008 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015009
Evan Chengb5a55d92011-05-24 01:48:22 +000015010 SDValue FalseOp = N->getOperand(0);
15011 SDValue TrueOp = N->getOperand(1);
15012 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15013 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015014
Evan Chengb5a55d92011-05-24 01:48:22 +000015015 if (CC == X86::COND_E || CC == X86::COND_NE) {
15016 switch (Cond.getOpcode()) {
15017 default: break;
15018 case X86ISD::BSR:
15019 case X86ISD::BSF:
15020 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15021 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15022 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15023 }
15024 }
15025
Michael Liao2a33cec2012-08-10 19:58:13 +000015026 SDValue Flags;
15027
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015028 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015029 if (Flags.getNode() &&
15030 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015031 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015032 SDValue Ops[] = { FalseOp, TrueOp,
15033 DAG.getConstant(CC, MVT::i8), Flags };
15034 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15035 Ops, array_lengthof(Ops));
15036 }
15037
Chris Lattnerd1980a52009-03-12 06:52:53 +000015038 // If this is a select between two integer constants, try to do some
15039 // optimizations. Note that the operands are ordered the opposite of SELECT
15040 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015041 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15042 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015043 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15044 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015045 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15046 CC = X86::GetOppositeBranchCondition(CC);
15047 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015048 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015049 }
Eric Christopherfd179292009-08-27 18:07:15 +000015050
Chris Lattnerd1980a52009-03-12 06:52:53 +000015051 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015052 // This is efficient for any integer data type (including i8/i16) and
15053 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015054 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015055 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15056 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015057
Chris Lattnerd1980a52009-03-12 06:52:53 +000015058 // Zero extend the condition if needed.
15059 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015060
Chris Lattnerd1980a52009-03-12 06:52:53 +000015061 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15062 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015063 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015064 if (N->getNumValues() == 2) // Dead flag value?
15065 return DCI.CombineTo(N, Cond, SDValue());
15066 return Cond;
15067 }
Eric Christopherfd179292009-08-27 18:07:15 +000015068
Chris Lattnercee56e72009-03-13 05:53:31 +000015069 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15070 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015071 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015072 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15073 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015074
Chris Lattner97a29a52009-03-13 05:22:11 +000015075 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015076 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15077 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015078 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15079 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015080
Chris Lattner97a29a52009-03-13 05:22:11 +000015081 if (N->getNumValues() == 2) // Dead flag value?
15082 return DCI.CombineTo(N, Cond, SDValue());
15083 return Cond;
15084 }
Eric Christopherfd179292009-08-27 18:07:15 +000015085
Chris Lattnercee56e72009-03-13 05:53:31 +000015086 // Optimize cases that will turn into an LEA instruction. This requires
15087 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015088 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015089 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015090 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015091
Chris Lattnercee56e72009-03-13 05:53:31 +000015092 bool isFastMultiplier = false;
15093 if (Diff < 10) {
15094 switch ((unsigned char)Diff) {
15095 default: break;
15096 case 1: // result = add base, cond
15097 case 2: // result = lea base( , cond*2)
15098 case 3: // result = lea base(cond, cond*2)
15099 case 4: // result = lea base( , cond*4)
15100 case 5: // result = lea base(cond, cond*4)
15101 case 8: // result = lea base( , cond*8)
15102 case 9: // result = lea base(cond, cond*8)
15103 isFastMultiplier = true;
15104 break;
15105 }
15106 }
Eric Christopherfd179292009-08-27 18:07:15 +000015107
Chris Lattnercee56e72009-03-13 05:53:31 +000015108 if (isFastMultiplier) {
15109 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015110 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15111 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015112 // Zero extend the condition if needed.
15113 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15114 Cond);
15115 // Scale the condition by the difference.
15116 if (Diff != 1)
15117 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15118 DAG.getConstant(Diff, Cond.getValueType()));
15119
15120 // Add the base if non-zero.
15121 if (FalseC->getAPIntValue() != 0)
15122 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15123 SDValue(FalseC, 0));
15124 if (N->getNumValues() == 2) // Dead flag value?
15125 return DCI.CombineTo(N, Cond, SDValue());
15126 return Cond;
15127 }
Eric Christopherfd179292009-08-27 18:07:15 +000015128 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015129 }
15130 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015131
15132 // Handle these cases:
15133 // (select (x != c), e, c) -> select (x != c), e, x),
15134 // (select (x == c), c, e) -> select (x == c), x, e)
15135 // where the c is an integer constant, and the "select" is the combination
15136 // of CMOV and CMP.
15137 //
15138 // The rationale for this change is that the conditional-move from a constant
15139 // needs two instructions, however, conditional-move from a register needs
15140 // only one instruction.
15141 //
15142 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15143 // some instruction-combining opportunities. This opt needs to be
15144 // postponed as late as possible.
15145 //
15146 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15147 // the DCI.xxxx conditions are provided to postpone the optimization as
15148 // late as possible.
15149
15150 ConstantSDNode *CmpAgainst = 0;
15151 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15152 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15153 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15154
15155 if (CC == X86::COND_NE &&
15156 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15157 CC = X86::GetOppositeBranchCondition(CC);
15158 std::swap(TrueOp, FalseOp);
15159 }
15160
15161 if (CC == X86::COND_E &&
15162 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15163 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15164 DAG.getConstant(CC, MVT::i8), Cond };
15165 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15166 array_lengthof(Ops));
15167 }
15168 }
15169 }
15170
Chris Lattnerd1980a52009-03-12 06:52:53 +000015171 return SDValue();
15172}
15173
15174
Evan Cheng0b0cd912009-03-28 05:57:29 +000015175/// PerformMulCombine - Optimize a single multiply with constant into two
15176/// in order to implement it with two cheaper instructions, e.g.
15177/// LEA + SHL, LEA + LEA.
15178static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15179 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015180 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15181 return SDValue();
15182
Owen Andersone50ed302009-08-10 22:56:29 +000015183 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015184 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015185 return SDValue();
15186
15187 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15188 if (!C)
15189 return SDValue();
15190 uint64_t MulAmt = C->getZExtValue();
15191 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15192 return SDValue();
15193
15194 uint64_t MulAmt1 = 0;
15195 uint64_t MulAmt2 = 0;
15196 if ((MulAmt % 9) == 0) {
15197 MulAmt1 = 9;
15198 MulAmt2 = MulAmt / 9;
15199 } else if ((MulAmt % 5) == 0) {
15200 MulAmt1 = 5;
15201 MulAmt2 = MulAmt / 5;
15202 } else if ((MulAmt % 3) == 0) {
15203 MulAmt1 = 3;
15204 MulAmt2 = MulAmt / 3;
15205 }
15206 if (MulAmt2 &&
15207 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15208 DebugLoc DL = N->getDebugLoc();
15209
15210 if (isPowerOf2_64(MulAmt2) &&
15211 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15212 // If second multiplifer is pow2, issue it first. We want the multiply by
15213 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15214 // is an add.
15215 std::swap(MulAmt1, MulAmt2);
15216
15217 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015218 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015219 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015220 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015221 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015222 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015223 DAG.getConstant(MulAmt1, VT));
15224
Eric Christopherfd179292009-08-27 18:07:15 +000015225 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015226 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015227 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015228 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015229 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015230 DAG.getConstant(MulAmt2, VT));
15231
15232 // Do not add new nodes to DAG combiner worklist.
15233 DCI.CombineTo(N, NewMul, false);
15234 }
15235 return SDValue();
15236}
15237
Evan Chengad9c0a32009-12-15 00:53:42 +000015238static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15239 SDValue N0 = N->getOperand(0);
15240 SDValue N1 = N->getOperand(1);
15241 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15242 EVT VT = N0.getValueType();
15243
15244 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15245 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015246 if (VT.isInteger() && !VT.isVector() &&
15247 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015248 N0.getOperand(1).getOpcode() == ISD::Constant) {
15249 SDValue N00 = N0.getOperand(0);
15250 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15251 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15252 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15253 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15254 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15255 APInt ShAmt = N1C->getAPIntValue();
15256 Mask = Mask.shl(ShAmt);
15257 if (Mask != 0)
15258 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15259 N00, DAG.getConstant(Mask, VT));
15260 }
15261 }
15262
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015263
15264 // Hardware support for vector shifts is sparse which makes us scalarize the
15265 // vector operations in many cases. Also, on sandybridge ADD is faster than
15266 // shl.
15267 // (shl V, 1) -> add V,V
15268 if (isSplatVector(N1.getNode())) {
15269 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15270 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15271 // We shift all of the values by one. In many cases we do not have
15272 // hardware support for this operation. This is better expressed as an ADD
15273 // of two values.
15274 if (N1C && (1 == N1C->getZExtValue())) {
15275 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15276 }
15277 }
15278
Evan Chengad9c0a32009-12-15 00:53:42 +000015279 return SDValue();
15280}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015281
Nate Begeman740ab032009-01-26 00:52:55 +000015282/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15283/// when possible.
15284static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015285 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015286 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015287 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015288 if (N->getOpcode() == ISD::SHL) {
15289 SDValue V = PerformSHLCombine(N, DAG);
15290 if (V.getNode()) return V;
15291 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015292
Nate Begeman740ab032009-01-26 00:52:55 +000015293 // On X86 with SSE2 support, we can transform this to a vector shift if
15294 // all elements are shifted by the same amount. We can't do this in legalize
15295 // because the a constant vector is typically transformed to a constant pool
15296 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015297 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015298 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015299
Craig Topper7be5dfd2011-11-12 09:58:49 +000015300 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
15301 (!Subtarget->hasAVX2() ||
15302 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015303 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015304
Mon P Wang3becd092009-01-28 08:12:05 +000015305 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015306 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015307 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015308 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015309 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15310 unsigned NumElts = VT.getVectorNumElements();
15311 unsigned i = 0;
15312 for (; i != NumElts; ++i) {
15313 SDValue Arg = ShAmtOp.getOperand(i);
15314 if (Arg.getOpcode() == ISD::UNDEF) continue;
15315 BaseShAmt = Arg;
15316 break;
15317 }
Craig Topper37c26772012-01-17 04:44:50 +000015318 // Handle the case where the build_vector is all undef
15319 // FIXME: Should DAG allow this?
15320 if (i == NumElts)
15321 return SDValue();
15322
Mon P Wang3becd092009-01-28 08:12:05 +000015323 for (; i != NumElts; ++i) {
15324 SDValue Arg = ShAmtOp.getOperand(i);
15325 if (Arg.getOpcode() == ISD::UNDEF) continue;
15326 if (Arg != BaseShAmt) {
15327 return SDValue();
15328 }
15329 }
15330 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015331 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015332 SDValue InVec = ShAmtOp.getOperand(0);
15333 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15334 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15335 unsigned i = 0;
15336 for (; i != NumElts; ++i) {
15337 SDValue Arg = InVec.getOperand(i);
15338 if (Arg.getOpcode() == ISD::UNDEF) continue;
15339 BaseShAmt = Arg;
15340 break;
15341 }
15342 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15343 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015344 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015345 if (C->getZExtValue() == SplatIdx)
15346 BaseShAmt = InVec.getOperand(1);
15347 }
15348 }
Mon P Wang845b1892012-02-01 22:15:20 +000015349 if (BaseShAmt.getNode() == 0) {
15350 // Don't create instructions with illegal types after legalize
15351 // types has run.
15352 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15353 !DCI.isBeforeLegalize())
15354 return SDValue();
15355
Mon P Wangefa42202009-09-03 19:56:25 +000015356 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15357 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015358 }
Mon P Wang3becd092009-01-28 08:12:05 +000015359 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015360 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015361
Mon P Wangefa42202009-09-03 19:56:25 +000015362 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015363 if (EltVT.bitsGT(MVT::i32))
15364 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15365 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015366 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015367
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015368 // The shift amount is identical so we can do a vector shift.
15369 SDValue ValOp = N->getOperand(0);
15370 switch (N->getOpcode()) {
15371 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015372 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015373 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015374 switch (VT.getSimpleVT().SimpleTy) {
15375 default: return SDValue();
15376 case MVT::v2i64:
15377 case MVT::v4i32:
15378 case MVT::v8i16:
15379 case MVT::v4i64:
15380 case MVT::v8i32:
15381 case MVT::v16i16:
15382 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15383 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015384 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015385 switch (VT.getSimpleVT().SimpleTy) {
15386 default: return SDValue();
15387 case MVT::v4i32:
15388 case MVT::v8i16:
15389 case MVT::v8i32:
15390 case MVT::v16i16:
15391 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15392 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015393 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015394 switch (VT.getSimpleVT().SimpleTy) {
15395 default: return SDValue();
15396 case MVT::v2i64:
15397 case MVT::v4i32:
15398 case MVT::v8i16:
15399 case MVT::v4i64:
15400 case MVT::v8i32:
15401 case MVT::v16i16:
15402 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15403 }
Nate Begeman740ab032009-01-26 00:52:55 +000015404 }
Nate Begeman740ab032009-01-26 00:52:55 +000015405}
15406
Nate Begemanb65c1752010-12-17 22:55:37 +000015407
Stuart Hastings865f0932011-06-03 23:53:54 +000015408// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15409// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15410// and friends. Likewise for OR -> CMPNEQSS.
15411static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15412 TargetLowering::DAGCombinerInfo &DCI,
15413 const X86Subtarget *Subtarget) {
15414 unsigned opcode;
15415
15416 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15417 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015418 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015419 SDValue N0 = N->getOperand(0);
15420 SDValue N1 = N->getOperand(1);
15421 SDValue CMP0 = N0->getOperand(1);
15422 SDValue CMP1 = N1->getOperand(1);
15423 DebugLoc DL = N->getDebugLoc();
15424
15425 // The SETCCs should both refer to the same CMP.
15426 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15427 return SDValue();
15428
15429 SDValue CMP00 = CMP0->getOperand(0);
15430 SDValue CMP01 = CMP0->getOperand(1);
15431 EVT VT = CMP00.getValueType();
15432
15433 if (VT == MVT::f32 || VT == MVT::f64) {
15434 bool ExpectingFlags = false;
15435 // Check for any users that want flags:
15436 for (SDNode::use_iterator UI = N->use_begin(),
15437 UE = N->use_end();
15438 !ExpectingFlags && UI != UE; ++UI)
15439 switch (UI->getOpcode()) {
15440 default:
15441 case ISD::BR_CC:
15442 case ISD::BRCOND:
15443 case ISD::SELECT:
15444 ExpectingFlags = true;
15445 break;
15446 case ISD::CopyToReg:
15447 case ISD::SIGN_EXTEND:
15448 case ISD::ZERO_EXTEND:
15449 case ISD::ANY_EXTEND:
15450 break;
15451 }
15452
15453 if (!ExpectingFlags) {
15454 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15455 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15456
15457 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15458 X86::CondCode tmp = cc0;
15459 cc0 = cc1;
15460 cc1 = tmp;
15461 }
15462
15463 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15464 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15465 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15466 X86ISD::NodeType NTOperator = is64BitFP ?
15467 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15468 // FIXME: need symbolic constants for these magic numbers.
15469 // See X86ATTInstPrinter.cpp:printSSECC().
15470 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15471 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15472 DAG.getConstant(x86cc, MVT::i8));
15473 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15474 OnesOrZeroesF);
15475 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15476 DAG.getConstant(1, MVT::i32));
15477 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15478 return OneBitOfTruth;
15479 }
15480 }
15481 }
15482 }
15483 return SDValue();
15484}
15485
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015486/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15487/// so it can be folded inside ANDNP.
15488static bool CanFoldXORWithAllOnes(const SDNode *N) {
15489 EVT VT = N->getValueType(0);
15490
15491 // Match direct AllOnes for 128 and 256-bit vectors
15492 if (ISD::isBuildVectorAllOnes(N))
15493 return true;
15494
15495 // Look through a bit convert.
15496 if (N->getOpcode() == ISD::BITCAST)
15497 N = N->getOperand(0).getNode();
15498
15499 // Sometimes the operand may come from a insert_subvector building a 256-bit
15500 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015501 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015502 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15503 SDValue V1 = N->getOperand(0);
15504 SDValue V2 = N->getOperand(1);
15505
15506 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15507 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15508 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15509 ISD::isBuildVectorAllOnes(V2.getNode()))
15510 return true;
15511 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015512
15513 return false;
15514}
15515
Nate Begemanb65c1752010-12-17 22:55:37 +000015516static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15517 TargetLowering::DAGCombinerInfo &DCI,
15518 const X86Subtarget *Subtarget) {
15519 if (DCI.isBeforeLegalizeOps())
15520 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015521
Stuart Hastings865f0932011-06-03 23:53:54 +000015522 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15523 if (R.getNode())
15524 return R;
15525
Craig Topper54a11172011-10-14 07:06:56 +000015526 EVT VT = N->getValueType(0);
15527
Craig Topperb4c94572011-10-21 06:55:01 +000015528 // Create ANDN, BLSI, and BLSR instructions
15529 // BLSI is X & (-X)
15530 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000015531 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
15532 SDValue N0 = N->getOperand(0);
15533 SDValue N1 = N->getOperand(1);
15534 DebugLoc DL = N->getDebugLoc();
15535
15536 // Check LHS for not
15537 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
15538 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
15539 // Check RHS for not
15540 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
15541 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
15542
Craig Topperb4c94572011-10-21 06:55:01 +000015543 // Check LHS for neg
15544 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
15545 isZero(N0.getOperand(0)))
15546 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
15547
15548 // Check RHS for neg
15549 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
15550 isZero(N1.getOperand(0)))
15551 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
15552
15553 // Check LHS for X-1
15554 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15555 isAllOnes(N0.getOperand(1)))
15556 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
15557
15558 // Check RHS for X-1
15559 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15560 isAllOnes(N1.getOperand(1)))
15561 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
15562
Craig Topper54a11172011-10-14 07:06:56 +000015563 return SDValue();
15564 }
15565
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015566 // Want to form ANDNP nodes:
15567 // 1) In the hopes of then easily combining them with OR and AND nodes
15568 // to form PBLEND/PSIGN.
15569 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000015570 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000015571 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015572
Nate Begemanb65c1752010-12-17 22:55:37 +000015573 SDValue N0 = N->getOperand(0);
15574 SDValue N1 = N->getOperand(1);
15575 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015576
Nate Begemanb65c1752010-12-17 22:55:37 +000015577 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015578 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015579 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
15580 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015581 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000015582
15583 // Check RHS for vnot
15584 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015585 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
15586 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000015587 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015588
Nate Begemanb65c1752010-12-17 22:55:37 +000015589 return SDValue();
15590}
15591
Evan Cheng760d1942010-01-04 21:22:48 +000015592static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000015593 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000015594 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000015595 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000015596 return SDValue();
15597
Stuart Hastings865f0932011-06-03 23:53:54 +000015598 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
15599 if (R.getNode())
15600 return R;
15601
Evan Cheng760d1942010-01-04 21:22:48 +000015602 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000015603
Evan Cheng760d1942010-01-04 21:22:48 +000015604 SDValue N0 = N->getOperand(0);
15605 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015606
Nate Begemanb65c1752010-12-17 22:55:37 +000015607 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000015608 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000015609 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000015610 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
15611 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015612
Craig Topper1666cb62011-11-19 07:07:26 +000015613 // Canonicalize pandn to RHS
15614 if (N0.getOpcode() == X86ISD::ANDNP)
15615 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000015616 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000015617 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
15618 SDValue Mask = N1.getOperand(0);
15619 SDValue X = N1.getOperand(1);
15620 SDValue Y;
15621 if (N0.getOperand(0) == Mask)
15622 Y = N0.getOperand(1);
15623 if (N0.getOperand(1) == Mask)
15624 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015625
Craig Topper1666cb62011-11-19 07:07:26 +000015626 // Check to see if the mask appeared in both the AND and ANDNP and
15627 if (!Y.getNode())
15628 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015629
Craig Topper1666cb62011-11-19 07:07:26 +000015630 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000015631 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000015632 if (Mask.getOpcode() == ISD::BITCAST)
15633 Mask = Mask.getOperand(0);
15634 if (X.getOpcode() == ISD::BITCAST)
15635 X = X.getOperand(0);
15636 if (Y.getOpcode() == ISD::BITCAST)
15637 Y = Y.getOperand(0);
15638
Craig Topper1666cb62011-11-19 07:07:26 +000015639 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015640
Craig Toppered2e13d2012-01-22 19:15:14 +000015641 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000015642 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
15643 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015644 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000015645 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000015646
15647 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015648 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000015649 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
15650 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
15651 if ((SraAmt + 1) != EltBits)
15652 return SDValue();
15653
15654 DebugLoc DL = N->getDebugLoc();
15655
15656 // Now we know we at least have a plendvb with the mask val. See if
15657 // we can form a psignb/w/d.
15658 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015659 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15660 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015661 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15662 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15663 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015664 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015665 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015666 }
15667 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015668 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015669 return SDValue();
15670
15671 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15672
15673 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15674 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15675 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015676 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015677 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015678 }
15679 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015680
Craig Topper1666cb62011-11-19 07:07:26 +000015681 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15682 return SDValue();
15683
Nate Begemanb65c1752010-12-17 22:55:37 +000015684 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015685 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15686 std::swap(N0, N1);
15687 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15688 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015689 if (!N0.hasOneUse() || !N1.hasOneUse())
15690 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015691
15692 SDValue ShAmt0 = N0.getOperand(1);
15693 if (ShAmt0.getValueType() != MVT::i8)
15694 return SDValue();
15695 SDValue ShAmt1 = N1.getOperand(1);
15696 if (ShAmt1.getValueType() != MVT::i8)
15697 return SDValue();
15698 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15699 ShAmt0 = ShAmt0.getOperand(0);
15700 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15701 ShAmt1 = ShAmt1.getOperand(0);
15702
15703 DebugLoc DL = N->getDebugLoc();
15704 unsigned Opc = X86ISD::SHLD;
15705 SDValue Op0 = N0.getOperand(0);
15706 SDValue Op1 = N1.getOperand(0);
15707 if (ShAmt0.getOpcode() == ISD::SUB) {
15708 Opc = X86ISD::SHRD;
15709 std::swap(Op0, Op1);
15710 std::swap(ShAmt0, ShAmt1);
15711 }
15712
Evan Cheng8b1190a2010-04-28 01:18:01 +000015713 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015714 if (ShAmt1.getOpcode() == ISD::SUB) {
15715 SDValue Sum = ShAmt1.getOperand(0);
15716 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015717 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15718 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15719 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15720 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015721 return DAG.getNode(Opc, DL, VT,
15722 Op0, Op1,
15723 DAG.getNode(ISD::TRUNCATE, DL,
15724 MVT::i8, ShAmt0));
15725 }
15726 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15727 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15728 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015729 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015730 return DAG.getNode(Opc, DL, VT,
15731 N0.getOperand(0), N1.getOperand(0),
15732 DAG.getNode(ISD::TRUNCATE, DL,
15733 MVT::i8, ShAmt0));
15734 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015735
Evan Cheng760d1942010-01-04 21:22:48 +000015736 return SDValue();
15737}
15738
Manman Ren92363622012-06-07 22:39:10 +000015739// Generate NEG and CMOV for integer abs.
15740static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15741 EVT VT = N->getValueType(0);
15742
15743 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15744 // 8-bit integer abs to NEG and CMOV.
15745 if (VT.isInteger() && VT.getSizeInBits() == 8)
15746 return SDValue();
15747
15748 SDValue N0 = N->getOperand(0);
15749 SDValue N1 = N->getOperand(1);
15750 DebugLoc DL = N->getDebugLoc();
15751
15752 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15753 // and change it to SUB and CMOV.
15754 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15755 N0.getOpcode() == ISD::ADD &&
15756 N0.getOperand(1) == N1 &&
15757 N1.getOpcode() == ISD::SRA &&
15758 N1.getOperand(0) == N0.getOperand(0))
15759 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15760 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15761 // Generate SUB & CMOV.
15762 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15763 DAG.getConstant(0, VT), N0.getOperand(0));
15764
15765 SDValue Ops[] = { N0.getOperand(0), Neg,
15766 DAG.getConstant(X86::COND_GE, MVT::i8),
15767 SDValue(Neg.getNode(), 1) };
15768 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15769 Ops, array_lengthof(Ops));
15770 }
15771 return SDValue();
15772}
15773
Craig Topper3738ccd2011-12-27 06:27:23 +000015774// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015775static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15776 TargetLowering::DAGCombinerInfo &DCI,
15777 const X86Subtarget *Subtarget) {
15778 if (DCI.isBeforeLegalizeOps())
15779 return SDValue();
15780
Manman Ren45d53b82012-06-08 18:58:26 +000015781 if (Subtarget->hasCMov()) {
15782 SDValue RV = performIntegerAbsCombine(N, DAG);
15783 if (RV.getNode())
15784 return RV;
15785 }
Manman Ren92363622012-06-07 22:39:10 +000015786
15787 // Try forming BMI if it is available.
15788 if (!Subtarget->hasBMI())
15789 return SDValue();
15790
Craig Topperb4c94572011-10-21 06:55:01 +000015791 EVT VT = N->getValueType(0);
15792
15793 if (VT != MVT::i32 && VT != MVT::i64)
15794 return SDValue();
15795
Craig Topper3738ccd2011-12-27 06:27:23 +000015796 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15797
Craig Topperb4c94572011-10-21 06:55:01 +000015798 // Create BLSMSK instructions by finding X ^ (X-1)
15799 SDValue N0 = N->getOperand(0);
15800 SDValue N1 = N->getOperand(1);
15801 DebugLoc DL = N->getDebugLoc();
15802
15803 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15804 isAllOnes(N0.getOperand(1)))
15805 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15806
15807 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15808 isAllOnes(N1.getOperand(1)))
15809 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15810
15811 return SDValue();
15812}
15813
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015814/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15815static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015816 TargetLowering::DAGCombinerInfo &DCI,
15817 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015818 LoadSDNode *Ld = cast<LoadSDNode>(N);
15819 EVT RegVT = Ld->getValueType(0);
15820 EVT MemVT = Ld->getMemoryVT();
15821 DebugLoc dl = Ld->getDebugLoc();
15822 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15823
15824 ISD::LoadExtType Ext = Ld->getExtensionType();
15825
Nadav Rotemca6f2962011-09-18 19:00:23 +000015826 // If this is a vector EXT Load then attempt to optimize it using a
Michael Liao35a56402012-10-17 03:59:18 +000015827 // shuffle. We need SSSE3 shuffles.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015828 // TODO: It is possible to support ZExt by zeroing the undef values
15829 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015830 if (RegVT.isVector() && RegVT.isInteger() &&
Michael Liao35a56402012-10-17 03:59:18 +000015831 Ext == ISD::EXTLOAD && Subtarget->hasSSSE3()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015832 assert(MemVT != RegVT && "Cannot extend to the same type");
15833 assert(MemVT.isVector() && "Must load a vector from memory");
15834
15835 unsigned NumElems = RegVT.getVectorNumElements();
15836 unsigned RegSz = RegVT.getSizeInBits();
15837 unsigned MemSz = MemVT.getSizeInBits();
15838 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015839
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015840 // All sizes must be a power of two.
15841 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15842 return SDValue();
15843
15844 // Attempt to load the original value using scalar loads.
15845 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015846 MVT SclrLoadTy = MVT::i8;
15847 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15848 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15849 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015850 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015851 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015852 }
15853 }
15854
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015855 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15856 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15857 (64 <= MemSz))
15858 SclrLoadTy = MVT::f64;
15859
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015860 // Calculate the number of scalar loads that we need to perform
15861 // in order to load our vector from memory.
15862 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015863
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015864 // Represent our vector as a sequence of elements which are the
15865 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015866 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15867 RegSz/SclrLoadTy.getSizeInBits());
15868
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015869 // Represent the data using the same element type that is stored in
15870 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015871 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15872 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015873
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015874 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15875 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015876
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015877 // We can't shuffle using an illegal type.
15878 if (!TLI.isTypeLegal(WideVecVT))
15879 return SDValue();
15880
15881 SmallVector<SDValue, 8> Chains;
15882 SDValue Ptr = Ld->getBasePtr();
15883 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15884 TLI.getPointerTy());
15885 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15886
15887 for (unsigned i = 0; i < NumLoads; ++i) {
15888 // Perform a single load.
15889 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15890 Ptr, Ld->getPointerInfo(),
15891 Ld->isVolatile(), Ld->isNonTemporal(),
15892 Ld->isInvariant(), Ld->getAlignment());
15893 Chains.push_back(ScalarLoad.getValue(1));
15894 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15895 // another round of DAGCombining.
15896 if (i == 0)
15897 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15898 else
15899 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15900 ScalarLoad, DAG.getIntPtrConstant(i));
15901
15902 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15903 }
15904
15905 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15906 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015907
15908 // Bitcast the loaded value to a vector of the original element type, in
15909 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015910 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015911 unsigned SizeRatio = RegSz/MemSz;
15912
15913 // Redistribute the loaded elements into the different locations.
15914 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015915 for (unsigned i = 0; i != NumElems; ++i)
15916 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015917
15918 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015919 DAG.getUNDEF(WideVecVT),
15920 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015921
15922 // Bitcast to the requested type.
15923 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15924 // Replace the original load with the new sequence
15925 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015926 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015927 }
15928
15929 return SDValue();
15930}
15931
Chris Lattner149a4e52008-02-22 02:09:43 +000015932/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015933static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015934 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015935 StoreSDNode *St = cast<StoreSDNode>(N);
15936 EVT VT = St->getValue().getValueType();
15937 EVT StVT = St->getMemoryVT();
15938 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015939 SDValue StoredVal = St->getOperand(1);
15940 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15941
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015942 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015943 // On Sandy Bridge, 256-bit memory operations are executed by two
15944 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15945 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015946 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015947 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15948 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015949 SDValue Value0 = StoredVal.getOperand(0);
15950 SDValue Value1 = StoredVal.getOperand(1);
15951
15952 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15953 SDValue Ptr0 = St->getBasePtr();
15954 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15955
15956 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15957 St->getPointerInfo(), St->isVolatile(),
15958 St->isNonTemporal(), St->getAlignment());
15959 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15960 St->getPointerInfo(), St->isVolatile(),
15961 St->isNonTemporal(), St->getAlignment());
15962 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15963 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015964
15965 // Optimize trunc store (of multiple scalars) to shuffle and store.
15966 // First, pack all of the elements in one place. Next, store to memory
15967 // in fewer chunks.
15968 if (St->isTruncatingStore() && VT.isVector()) {
15969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15970 unsigned NumElems = VT.getVectorNumElements();
15971 assert(StVT != VT && "Cannot truncate to the same type");
15972 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15973 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15974
15975 // From, To sizes and ElemCount must be pow of two
15976 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015977 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015978 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015979 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015980
Nadav Rotem614061b2011-08-10 19:30:14 +000015981 unsigned SizeRatio = FromSz / ToSz;
15982
15983 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15984
15985 // Create a type on which we perform the shuffle
15986 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15987 StVT.getScalarType(), NumElems*SizeRatio);
15988
15989 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15990
15991 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15992 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015993 for (unsigned i = 0; i != NumElems; ++i)
15994 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015995
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015996 // Can't shuffle using an illegal type.
15997 if (!TLI.isTypeLegal(WideVecVT))
15998 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015999
16000 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016001 DAG.getUNDEF(WideVecVT),
16002 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016003 // At this point all of the data is stored at the bottom of the
16004 // register. We now need to save it to mem.
16005
16006 // Find the largest store unit
16007 MVT StoreType = MVT::i8;
16008 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16009 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16010 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016011 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016012 StoreType = Tp;
16013 }
16014
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016015 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16016 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16017 (64 <= NumElems * ToSz))
16018 StoreType = MVT::f64;
16019
Nadav Rotem614061b2011-08-10 19:30:14 +000016020 // Bitcast the original vector into a vector of store-size units
16021 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016022 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016023 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16024 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16025 SmallVector<SDValue, 8> Chains;
16026 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16027 TLI.getPointerTy());
16028 SDValue Ptr = St->getBasePtr();
16029
16030 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016031 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016032 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16033 StoreType, ShuffWide,
16034 DAG.getIntPtrConstant(i));
16035 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16036 St->getPointerInfo(), St->isVolatile(),
16037 St->isNonTemporal(), St->getAlignment());
16038 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16039 Chains.push_back(Ch);
16040 }
16041
16042 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16043 Chains.size());
16044 }
16045
16046
Chris Lattner149a4e52008-02-22 02:09:43 +000016047 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16048 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016049 // A preferable solution to the general problem is to figure out the right
16050 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016051
16052 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016053 if (VT.getSizeInBits() != 64)
16054 return SDValue();
16055
Devang Patel578efa92009-06-05 21:57:13 +000016056 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000016057 bool NoImplicitFloatOps = F->getFnAttributes().
16058 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016059 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016060 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016061 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016062 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016063 isa<LoadSDNode>(St->getValue()) &&
16064 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16065 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016066 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016067 LoadSDNode *Ld = 0;
16068 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016069 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016070 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016071 // Must be a store of a load. We currently handle two cases: the load
16072 // is a direct child, and it's under an intervening TokenFactor. It is
16073 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016074 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016075 Ld = cast<LoadSDNode>(St->getChain());
16076 else if (St->getValue().hasOneUse() &&
16077 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016078 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016079 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016080 TokenFactorIndex = i;
16081 Ld = cast<LoadSDNode>(St->getValue());
16082 } else
16083 Ops.push_back(ChainVal->getOperand(i));
16084 }
16085 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016086
Evan Cheng536e6672009-03-12 05:59:15 +000016087 if (!Ld || !ISD::isNormalLoad(Ld))
16088 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016089
Evan Cheng536e6672009-03-12 05:59:15 +000016090 // If this is not the MMX case, i.e. we are just turning i64 load/store
16091 // into f64 load/store, avoid the transformation if there are multiple
16092 // uses of the loaded value.
16093 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16094 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016095
Evan Cheng536e6672009-03-12 05:59:15 +000016096 DebugLoc LdDL = Ld->getDebugLoc();
16097 DebugLoc StDL = N->getDebugLoc();
16098 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16099 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16100 // pair instead.
16101 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016102 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016103 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16104 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016105 Ld->isNonTemporal(), Ld->isInvariant(),
16106 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016107 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016108 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016109 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016110 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016111 Ops.size());
16112 }
Evan Cheng536e6672009-03-12 05:59:15 +000016113 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016114 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016115 St->isVolatile(), St->isNonTemporal(),
16116 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016117 }
Evan Cheng536e6672009-03-12 05:59:15 +000016118
16119 // Otherwise, lower to two pairs of 32-bit loads / stores.
16120 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016121 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16122 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016123
Owen Anderson825b72b2009-08-11 20:47:22 +000016124 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016125 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016126 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016127 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016128 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016129 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016130 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016131 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016132 MinAlign(Ld->getAlignment(), 4));
16133
16134 SDValue NewChain = LoLd.getValue(1);
16135 if (TokenFactorIndex != -1) {
16136 Ops.push_back(LoLd);
16137 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016138 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016139 Ops.size());
16140 }
16141
16142 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016143 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16144 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016145
16146 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016147 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016148 St->isVolatile(), St->isNonTemporal(),
16149 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016150 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016151 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016152 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016153 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016154 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016155 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016156 }
Dan Gohman475871a2008-07-27 21:46:04 +000016157 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016158}
16159
Duncan Sands17470be2011-09-22 20:15:48 +000016160/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16161/// and return the operands for the horizontal operation in LHS and RHS. A
16162/// horizontal operation performs the binary operation on successive elements
16163/// of its first operand, then on successive elements of its second operand,
16164/// returning the resulting values in a vector. For example, if
16165/// A = < float a0, float a1, float a2, float a3 >
16166/// and
16167/// B = < float b0, float b1, float b2, float b3 >
16168/// then the result of doing a horizontal operation on A and B is
16169/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16170/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16171/// A horizontal-op B, for some already available A and B, and if so then LHS is
16172/// set to A, RHS to B, and the routine returns 'true'.
16173/// Note that the binary operation should have the property that if one of the
16174/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016175static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016176 // Look for the following pattern: if
16177 // A = < float a0, float a1, float a2, float a3 >
16178 // B = < float b0, float b1, float b2, float b3 >
16179 // and
16180 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16181 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16182 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16183 // which is A horizontal-op B.
16184
16185 // At least one of the operands should be a vector shuffle.
16186 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16187 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16188 return false;
16189
16190 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016191
16192 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16193 "Unsupported vector type for horizontal add/sub");
16194
16195 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16196 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016197 unsigned NumElts = VT.getVectorNumElements();
16198 unsigned NumLanes = VT.getSizeInBits()/128;
16199 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016200 assert((NumLaneElts % 2 == 0) &&
16201 "Vector type should have an even number of elements in each lane");
16202 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016203
16204 // View LHS in the form
16205 // LHS = VECTOR_SHUFFLE A, B, LMask
16206 // If LHS is not a shuffle then pretend it is the shuffle
16207 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16208 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16209 // type VT.
16210 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016211 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016212 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16213 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16214 A = LHS.getOperand(0);
16215 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16216 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016217 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16218 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016219 } else {
16220 if (LHS.getOpcode() != ISD::UNDEF)
16221 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016222 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016223 LMask[i] = i;
16224 }
16225
16226 // Likewise, view RHS in the form
16227 // RHS = VECTOR_SHUFFLE C, D, RMask
16228 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016229 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016230 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16231 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16232 C = RHS.getOperand(0);
16233 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16234 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016235 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16236 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016237 } else {
16238 if (RHS.getOpcode() != ISD::UNDEF)
16239 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016240 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016241 RMask[i] = i;
16242 }
16243
16244 // Check that the shuffles are both shuffling the same vectors.
16245 if (!(A == C && B == D) && !(A == D && B == C))
16246 return false;
16247
16248 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16249 if (!A.getNode() && !B.getNode())
16250 return false;
16251
16252 // If A and B occur in reverse order in RHS, then "swap" them (which means
16253 // rewriting the mask).
16254 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016255 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016256
16257 // At this point LHS and RHS are equivalent to
16258 // LHS = VECTOR_SHUFFLE A, B, LMask
16259 // RHS = VECTOR_SHUFFLE A, B, RMask
16260 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016261 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016262 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016263
Craig Topperf8363302011-12-02 08:18:41 +000016264 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016265 if (LIdx < 0 || RIdx < 0 ||
16266 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16267 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016268 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016269
Craig Topperf8363302011-12-02 08:18:41 +000016270 // Check that successive elements are being operated on. If not, this is
16271 // not a horizontal operation.
16272 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16273 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016274 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016275 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016276 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016277 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016278 }
16279
16280 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16281 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16282 return true;
16283}
16284
16285/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16286static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16287 const X86Subtarget *Subtarget) {
16288 EVT VT = N->getValueType(0);
16289 SDValue LHS = N->getOperand(0);
16290 SDValue RHS = N->getOperand(1);
16291
16292 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016293 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016294 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016295 isHorizontalBinOp(LHS, RHS, true))
16296 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16297 return SDValue();
16298}
16299
16300/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16301static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16302 const X86Subtarget *Subtarget) {
16303 EVT VT = N->getValueType(0);
16304 SDValue LHS = N->getOperand(0);
16305 SDValue RHS = N->getOperand(1);
16306
16307 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016308 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000016309 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016310 isHorizontalBinOp(LHS, RHS, false))
16311 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16312 return SDValue();
16313}
16314
Chris Lattner6cf73262008-01-25 06:14:17 +000016315/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16316/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016317static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016318 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16319 // F[X]OR(0.0, x) -> x
16320 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016321 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16322 if (C->getValueAPF().isPosZero())
16323 return N->getOperand(1);
16324 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16325 if (C->getValueAPF().isPosZero())
16326 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016327 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016328}
16329
Nadav Rotemd60cb112012-08-19 13:06:16 +000016330/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16331/// X86ISD::FMAX nodes.
16332static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16333 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16334
16335 // Only perform optimizations if UnsafeMath is used.
16336 if (!DAG.getTarget().Options.UnsafeFPMath)
16337 return SDValue();
16338
16339 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016340 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016341 unsigned NewOp = 0;
16342 switch (N->getOpcode()) {
16343 default: llvm_unreachable("unknown opcode");
16344 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16345 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16346 }
16347
16348 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16349 N->getOperand(0), N->getOperand(1));
16350}
16351
16352
Chris Lattneraf723b92008-01-25 05:46:26 +000016353/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016354static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016355 // FAND(0.0, x) -> 0.0
16356 // FAND(x, 0.0) -> 0.0
16357 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16358 if (C->getValueAPF().isPosZero())
16359 return N->getOperand(0);
16360 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16361 if (C->getValueAPF().isPosZero())
16362 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016363 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016364}
16365
Dan Gohmane5af2d32009-01-29 01:59:02 +000016366static SDValue PerformBTCombine(SDNode *N,
16367 SelectionDAG &DAG,
16368 TargetLowering::DAGCombinerInfo &DCI) {
16369 // BT ignores high bits in the bit index operand.
16370 SDValue Op1 = N->getOperand(1);
16371 if (Op1.hasOneUse()) {
16372 unsigned BitWidth = Op1.getValueSizeInBits();
16373 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16374 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016375 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16376 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016377 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016378 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16379 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16380 DCI.CommitTargetLoweringOpt(TLO);
16381 }
16382 return SDValue();
16383}
Chris Lattner83e6c992006-10-04 06:57:07 +000016384
Eli Friedman7a5e5552009-06-07 06:52:44 +000016385static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16386 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016387 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016388 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016389 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016390 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016391 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016392 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016393 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016394 }
16395 return SDValue();
16396}
16397
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016398static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16399 TargetLowering::DAGCombinerInfo &DCI,
16400 const X86Subtarget *Subtarget) {
16401 if (!DCI.isBeforeLegalizeOps())
16402 return SDValue();
16403
Craig Topper3ef43cf2012-04-24 06:36:35 +000016404 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016405 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016406
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016407 EVT VT = N->getValueType(0);
16408 SDValue Op = N->getOperand(0);
16409 EVT OpVT = Op.getValueType();
16410 DebugLoc dl = N->getDebugLoc();
16411
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016412 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
16413 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016414
Craig Topper3ef43cf2012-04-24 06:36:35 +000016415 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016416 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016417
16418 // Optimize vectors in AVX mode
16419 // Sign extend v8i16 to v8i32 and
16420 // v4i32 to v4i64
16421 //
16422 // Divide input vector into two parts
16423 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
16424 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
16425 // concat the vectors to original VT
16426
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016427 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000016428 SDValue Undef = DAG.getUNDEF(OpVT);
16429
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016430 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016431 for (unsigned i = 0; i != NumElems/2; ++i)
16432 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016433
Craig Toppercacafd42012-08-14 08:18:43 +000016434 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016435
16436 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000016437 for (unsigned i = 0; i != NumElems/2; ++i)
16438 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016439
Craig Toppercacafd42012-08-14 08:18:43 +000016440 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016441
Craig Topper3ef43cf2012-04-24 06:36:35 +000016442 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016443 VT.getVectorNumElements()/2);
16444
Craig Topper3ef43cf2012-04-24 06:36:35 +000016445 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016446 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
16447
16448 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
16449 }
16450 return SDValue();
16451}
16452
Michael Liaof6c24ee2012-08-10 14:39:24 +000016453static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016454 const X86Subtarget* Subtarget) {
16455 DebugLoc dl = N->getDebugLoc();
16456 EVT VT = N->getValueType(0);
16457
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016458 // Let legalize expand this if it isn't a legal type yet.
16459 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16460 return SDValue();
16461
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016462 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016463 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16464 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016465 return SDValue();
16466
16467 SDValue A = N->getOperand(0);
16468 SDValue B = N->getOperand(1);
16469 SDValue C = N->getOperand(2);
16470
16471 bool NegA = (A.getOpcode() == ISD::FNEG);
16472 bool NegB = (B.getOpcode() == ISD::FNEG);
16473 bool NegC = (C.getOpcode() == ISD::FNEG);
16474
Michael Liaof6c24ee2012-08-10 14:39:24 +000016475 // Negative multiplication when NegA xor NegB
16476 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016477 if (NegA)
16478 A = A.getOperand(0);
16479 if (NegB)
16480 B = B.getOperand(0);
16481 if (NegC)
16482 C = C.getOperand(0);
16483
16484 unsigned Opcode;
16485 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016486 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016487 else
Craig Topperbf404372012-08-31 15:40:30 +000016488 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16489
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016490 return DAG.getNode(Opcode, dl, VT, A, B, C);
16491}
16492
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016493static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016494 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016495 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016496 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16497 // (and (i32 x86isd::setcc_carry), 1)
16498 // This eliminates the zext. This transformation is necessary because
16499 // ISD::SETCC is always legalized to i8.
16500 DebugLoc dl = N->getDebugLoc();
16501 SDValue N0 = N->getOperand(0);
16502 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016503 EVT OpVT = N0.getValueType();
16504
Evan Cheng2e489c42009-12-16 00:53:11 +000016505 if (N0.getOpcode() == ISD::AND &&
16506 N0.hasOneUse() &&
16507 N0.getOperand(0).hasOneUse()) {
16508 SDValue N00 = N0.getOperand(0);
16509 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
16510 return SDValue();
16511 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16512 if (!C || C->getZExtValue() != 1)
16513 return SDValue();
16514 return DAG.getNode(ISD::AND, dl, VT,
16515 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
16516 N00.getOperand(0), N00.getOperand(1)),
16517 DAG.getConstant(1, VT));
16518 }
Craig Topperd0cf5652012-04-21 18:13:35 +000016519
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016520 // Optimize vectors in AVX mode:
16521 //
16522 // v8i16 -> v8i32
16523 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
16524 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
16525 // Concat upper and lower parts.
16526 //
16527 // v4i32 -> v4i64
16528 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
16529 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
16530 // Concat upper and lower parts.
16531 //
Craig Topperc16f8512012-04-25 06:39:39 +000016532 if (!DCI.isBeforeLegalizeOps())
16533 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016534
Craig Topperc16f8512012-04-25 06:39:39 +000016535 if (!Subtarget->hasAVX())
16536 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016537
Craig Topperc16f8512012-04-25 06:39:39 +000016538 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
16539 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016540
Craig Topperc16f8512012-04-25 06:39:39 +000016541 if (Subtarget->hasAVX2())
16542 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016543
Craig Topperc16f8512012-04-25 06:39:39 +000016544 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
16545 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
16546 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016547
Craig Topperc16f8512012-04-25 06:39:39 +000016548 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
16549 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016550
Craig Topperc16f8512012-04-25 06:39:39 +000016551 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
16552 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
16553
16554 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016555 }
16556
Evan Cheng2e489c42009-12-16 00:53:11 +000016557 return SDValue();
16558}
16559
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016560// Optimize x == -y --> x+y == 0
16561// x != -y --> x+y != 0
16562static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
16563 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16564 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000016565 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016566
16567 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
16568 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
16569 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
16570 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16571 LHS.getValueType(), RHS, LHS.getOperand(1));
16572 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16573 addV, DAG.getConstant(0, addV.getValueType()), CC);
16574 }
16575 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
16576 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
16577 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
16578 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
16579 RHS.getValueType(), LHS, RHS.getOperand(1));
16580 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
16581 addV, DAG.getConstant(0, addV.getValueType()), CC);
16582 }
16583 return SDValue();
16584}
16585
Shuxin Yanga5526a92012-10-31 23:11:48 +000016586// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
16587// as "sbb reg,reg", since it can be extended without zext and produces
16588// an all-ones bit which is more useful than 0/1 in some cases.
16589static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
16590 return DAG.getNode(ISD::AND, DL, MVT::i8,
16591 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
16592 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
16593 DAG.getConstant(1, MVT::i8));
16594}
16595
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016596// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016597static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
16598 TargetLowering::DAGCombinerInfo &DCI,
16599 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016600 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000016601 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
16602 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016603
Shuxin Yanga5526a92012-10-31 23:11:48 +000016604 if (CC == X86::COND_A) {
16605 // Try to convert COND_A into COND_B in an attempt to facilitate
16606 // materializing "setb reg".
16607 //
16608 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
16609 // cannot take an immediate as its first operand.
16610 //
16611 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
16612 EFLAGS.getValueType().isInteger() &&
16613 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
16614 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
16615 EFLAGS.getNode()->getVTList(),
16616 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
16617 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
16618 return MaterializeSETB(DL, NewEFLAGS, DAG);
16619 }
16620 }
16621
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016622 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
16623 // a zext and produces an all-ones bit which is more useful than 0/1 in some
16624 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000016625 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000016626 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016627
Michael Liao2a33cec2012-08-10 19:58:13 +000016628 SDValue Flags;
16629
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016630 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16631 if (Flags.getNode()) {
16632 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16633 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
16634 }
16635
Michael Liao2a33cec2012-08-10 19:58:13 +000016636 return SDValue();
16637}
16638
16639// Optimize branch condition evaluation.
16640//
16641static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
16642 TargetLowering::DAGCombinerInfo &DCI,
16643 const X86Subtarget *Subtarget) {
16644 DebugLoc DL = N->getDebugLoc();
16645 SDValue Chain = N->getOperand(0);
16646 SDValue Dest = N->getOperand(1);
16647 SDValue EFLAGS = N->getOperand(3);
16648 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
16649
16650 SDValue Flags;
16651
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016652 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
16653 if (Flags.getNode()) {
16654 SDValue Cond = DAG.getConstant(CC, MVT::i8);
16655 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
16656 Flags);
16657 }
16658
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016659 return SDValue();
16660}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016661
Benjamin Kramer1396c402011-06-18 11:09:41 +000016662static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
16663 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016664 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016665 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016666
16667 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016668 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016669 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016670 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016671 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16672 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16673 }
16674
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016675 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16676 // a 32-bit target where SSE doesn't support i64->FP operations.
16677 if (Op0.getOpcode() == ISD::LOAD) {
16678 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16679 EVT VT = Ld->getValueType(0);
16680 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16681 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16682 !XTLI->getSubtarget()->is64Bit() &&
16683 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016684 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16685 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016686 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16687 return FILDChain;
16688 }
16689 }
16690 return SDValue();
16691}
16692
Chris Lattner23a01992010-12-20 01:37:09 +000016693// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16694static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16695 X86TargetLowering::DAGCombinerInfo &DCI) {
16696 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16697 // the result is either zero or one (depending on the input carry bit).
16698 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16699 if (X86::isZeroNode(N->getOperand(0)) &&
16700 X86::isZeroNode(N->getOperand(1)) &&
16701 // We don't have a good way to replace an EFLAGS use, so only do this when
16702 // dead right now.
16703 SDValue(N, 1).use_empty()) {
16704 DebugLoc DL = N->getDebugLoc();
16705 EVT VT = N->getValueType(0);
16706 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16707 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16708 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16709 DAG.getConstant(X86::COND_B,MVT::i8),
16710 N->getOperand(2)),
16711 DAG.getConstant(1, VT));
16712 return DCI.CombineTo(N, Res1, CarryOut);
16713 }
16714
16715 return SDValue();
16716}
16717
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016718// fold (add Y, (sete X, 0)) -> adc 0, Y
16719// (add Y, (setne X, 0)) -> sbb -1, Y
16720// (sub (sete X, 0), Y) -> sbb 0, Y
16721// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016722static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016723 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016724
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016725 // Look through ZExts.
16726 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16727 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16728 return SDValue();
16729
16730 SDValue SetCC = Ext.getOperand(0);
16731 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16732 return SDValue();
16733
16734 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16735 if (CC != X86::COND_E && CC != X86::COND_NE)
16736 return SDValue();
16737
16738 SDValue Cmp = SetCC.getOperand(1);
16739 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016740 !X86::isZeroNode(Cmp.getOperand(1)) ||
16741 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016742 return SDValue();
16743
16744 SDValue CmpOp0 = Cmp.getOperand(0);
16745 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16746 DAG.getConstant(1, CmpOp0.getValueType()));
16747
16748 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16749 if (CC == X86::COND_NE)
16750 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16751 DL, OtherVal.getValueType(), OtherVal,
16752 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16753 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16754 DL, OtherVal.getValueType(), OtherVal,
16755 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16756}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016757
Craig Topper54f952a2011-11-19 09:02:40 +000016758/// PerformADDCombine - Do target-specific dag combines on integer adds.
16759static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16760 const X86Subtarget *Subtarget) {
16761 EVT VT = N->getValueType(0);
16762 SDValue Op0 = N->getOperand(0);
16763 SDValue Op1 = N->getOperand(1);
16764
16765 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016766 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016767 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016768 isHorizontalBinOp(Op0, Op1, true))
16769 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16770
16771 return OptimizeConditionalInDecrement(N, DAG);
16772}
16773
16774static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16775 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016776 SDValue Op0 = N->getOperand(0);
16777 SDValue Op1 = N->getOperand(1);
16778
16779 // X86 can't encode an immediate LHS of a sub. See if we can push the
16780 // negation into a preceding instruction.
16781 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016782 // If the RHS of the sub is a XOR with one use and a constant, invert the
16783 // immediate. Then add one to the LHS of the sub so we can turn
16784 // X-Y -> X+~Y+1, saving one register.
16785 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16786 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016787 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016788 EVT VT = Op0.getValueType();
16789 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16790 Op1.getOperand(0),
16791 DAG.getConstant(~XorC, VT));
16792 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016793 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016794 }
16795 }
16796
Craig Topper54f952a2011-11-19 09:02:40 +000016797 // Try to synthesize horizontal adds from adds of shuffles.
16798 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016799 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016800 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16801 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016802 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16803
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016804 return OptimizeConditionalInDecrement(N, DAG);
16805}
16806
Michael Liaod9d09602012-10-23 17:34:00 +000016807/// performVZEXTCombine - Performs build vector combines
16808static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
16809 TargetLowering::DAGCombinerInfo &DCI,
16810 const X86Subtarget *Subtarget) {
16811 // (vzext (bitcast (vzext (x)) -> (vzext x)
16812 SDValue In = N->getOperand(0);
16813 while (In.getOpcode() == ISD::BITCAST)
16814 In = In.getOperand(0);
16815
16816 if (In.getOpcode() != X86ISD::VZEXT)
16817 return SDValue();
16818
16819 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
16820}
16821
Dan Gohman475871a2008-07-27 21:46:04 +000016822SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016823 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016824 SelectionDAG &DAG = DCI.DAG;
16825 switch (N->getOpcode()) {
16826 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016827 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016828 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016829 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016830 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016831 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016832 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16833 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016834 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016835 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016836 case ISD::SHL:
16837 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016838 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016839 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016840 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016841 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016842 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016843 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016844 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000016845 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16846 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016847 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016848 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016849 case X86ISD::FMIN:
16850 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016851 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016852 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016853 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016854 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016855 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016856 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016857 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016858 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016859 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016860 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000016861 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016862 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016863 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016864 case X86ISD::UNPCKH:
16865 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016866 case X86ISD::MOVHLPS:
16867 case X86ISD::MOVLHPS:
16868 case X86ISD::PSHUFD:
16869 case X86ISD::PSHUFHW:
16870 case X86ISD::PSHUFLW:
16871 case X86ISD::MOVSS:
16872 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016873 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016874 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016875 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016876 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016877 }
16878
Dan Gohman475871a2008-07-27 21:46:04 +000016879 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016880}
16881
Evan Chenge5b51ac2010-04-17 06:13:15 +000016882/// isTypeDesirableForOp - Return true if the target has native support for
16883/// the specified value type and it is 'desirable' to use the type for the
16884/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16885/// instruction encodings are longer and some i16 instructions are slow.
16886bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16887 if (!isTypeLegal(VT))
16888 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016889 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016890 return true;
16891
16892 switch (Opc) {
16893 default:
16894 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016895 case ISD::LOAD:
16896 case ISD::SIGN_EXTEND:
16897 case ISD::ZERO_EXTEND:
16898 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016899 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016900 case ISD::SRL:
16901 case ISD::SUB:
16902 case ISD::ADD:
16903 case ISD::MUL:
16904 case ISD::AND:
16905 case ISD::OR:
16906 case ISD::XOR:
16907 return false;
16908 }
16909}
16910
16911/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016912/// beneficial for dag combiner to promote the specified node. If true, it
16913/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016914bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016915 EVT VT = Op.getValueType();
16916 if (VT != MVT::i16)
16917 return false;
16918
Evan Cheng4c26e932010-04-19 19:29:22 +000016919 bool Promote = false;
16920 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016921 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016922 default: break;
16923 case ISD::LOAD: {
16924 LoadSDNode *LD = cast<LoadSDNode>(Op);
16925 // If the non-extending load has a single use and it's not live out, then it
16926 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016927 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16928 Op.hasOneUse()*/) {
16929 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16930 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16931 // The only case where we'd want to promote LOAD (rather then it being
16932 // promoted as an operand is when it's only use is liveout.
16933 if (UI->getOpcode() != ISD::CopyToReg)
16934 return false;
16935 }
16936 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016937 Promote = true;
16938 break;
16939 }
16940 case ISD::SIGN_EXTEND:
16941 case ISD::ZERO_EXTEND:
16942 case ISD::ANY_EXTEND:
16943 Promote = true;
16944 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016945 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016946 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016947 SDValue N0 = Op.getOperand(0);
16948 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016949 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016950 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016951 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016952 break;
16953 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016954 case ISD::ADD:
16955 case ISD::MUL:
16956 case ISD::AND:
16957 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016958 case ISD::XOR:
16959 Commute = true;
16960 // fallthrough
16961 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016962 SDValue N0 = Op.getOperand(0);
16963 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016964 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016965 return false;
16966 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016967 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016968 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016969 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016970 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016971 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016972 }
16973 }
16974
16975 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016976 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016977}
16978
Evan Cheng60c07e12006-07-05 22:17:51 +000016979//===----------------------------------------------------------------------===//
16980// X86 Inline Assembly Support
16981//===----------------------------------------------------------------------===//
16982
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016983namespace {
16984 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016985 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016986 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016987
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016988 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016989 StringRef piece(*args[i]);
16990 if (!s.startswith(piece)) // Check if the piece matches.
16991 return false;
16992
16993 s = s.substr(piece.size());
16994 StringRef::size_type pos = s.find_first_not_of(" \t");
16995 if (pos == 0) // We matched a prefix.
16996 return false;
16997
16998 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016999 }
17000
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017001 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017002 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017003 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017004}
17005
Chris Lattnerb8105652009-07-20 17:51:36 +000017006bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17007 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017008
17009 std::string AsmStr = IA->getAsmString();
17010
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017011 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17012 if (!Ty || Ty->getBitWidth() % 16 != 0)
17013 return false;
17014
Chris Lattnerb8105652009-07-20 17:51:36 +000017015 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017016 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017017 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017018
17019 switch (AsmPieces.size()) {
17020 default: return false;
17021 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017022 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017023 // we will turn this bswap into something that will be lowered to logical
17024 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17025 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017026 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017027 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17028 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17029 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17030 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17031 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17032 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017033 // No need to check constraints, nothing other than the equivalent of
17034 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017035 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017036 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017037
Chris Lattnerb8105652009-07-20 17:51:36 +000017038 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017039 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017040 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017041 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17042 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017043 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017044 const std::string &ConstraintsStr = IA->getConstraintString();
17045 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017046 std::sort(AsmPieces.begin(), AsmPieces.end());
17047 if (AsmPieces.size() == 4 &&
17048 AsmPieces[0] == "~{cc}" &&
17049 AsmPieces[1] == "~{dirflag}" &&
17050 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017051 AsmPieces[3] == "~{fpsr}")
17052 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017053 }
17054 break;
17055 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017056 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017057 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017058 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17059 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17060 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017061 AsmPieces.clear();
17062 const std::string &ConstraintsStr = IA->getConstraintString();
17063 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17064 std::sort(AsmPieces.begin(), AsmPieces.end());
17065 if (AsmPieces.size() == 4 &&
17066 AsmPieces[0] == "~{cc}" &&
17067 AsmPieces[1] == "~{dirflag}" &&
17068 AsmPieces[2] == "~{flags}" &&
17069 AsmPieces[3] == "~{fpsr}")
17070 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017071 }
Evan Cheng55d42002011-01-08 01:24:27 +000017072
17073 if (CI->getType()->isIntegerTy(64)) {
17074 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17075 if (Constraints.size() >= 2 &&
17076 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17077 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17078 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017079 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17080 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17081 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017082 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017083 }
17084 }
17085 break;
17086 }
17087 return false;
17088}
17089
17090
17091
Chris Lattnerf4dff842006-07-11 02:54:03 +000017092/// getConstraintType - Given a constraint letter, return the type of
17093/// constraint it is for this target.
17094X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017095X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17096 if (Constraint.size() == 1) {
17097 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017098 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017099 case 'q':
17100 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017101 case 'f':
17102 case 't':
17103 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017104 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017105 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017106 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017107 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017108 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017109 case 'a':
17110 case 'b':
17111 case 'c':
17112 case 'd':
17113 case 'S':
17114 case 'D':
17115 case 'A':
17116 return C_Register;
17117 case 'I':
17118 case 'J':
17119 case 'K':
17120 case 'L':
17121 case 'M':
17122 case 'N':
17123 case 'G':
17124 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017125 case 'e':
17126 case 'Z':
17127 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017128 default:
17129 break;
17130 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017131 }
Chris Lattner4234f572007-03-25 02:14:49 +000017132 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017133}
17134
John Thompson44ab89e2010-10-29 17:29:13 +000017135/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017136/// This object must already have been set up with the operand type
17137/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017138TargetLowering::ConstraintWeight
17139 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017140 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017141 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017142 Value *CallOperandVal = info.CallOperandVal;
17143 // If we don't have a value, we can't do a match,
17144 // but allow it at the lowest weight.
17145 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017146 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017147 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017148 // Look at the constraint type.
17149 switch (*constraint) {
17150 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017151 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17152 case 'R':
17153 case 'q':
17154 case 'Q':
17155 case 'a':
17156 case 'b':
17157 case 'c':
17158 case 'd':
17159 case 'S':
17160 case 'D':
17161 case 'A':
17162 if (CallOperandVal->getType()->isIntegerTy())
17163 weight = CW_SpecificReg;
17164 break;
17165 case 'f':
17166 case 't':
17167 case 'u':
17168 if (type->isFloatingPointTy())
17169 weight = CW_SpecificReg;
17170 break;
17171 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000017172 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000017173 weight = CW_SpecificReg;
17174 break;
17175 case 'x':
17176 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017177 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000017178 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000017179 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017180 break;
17181 case 'I':
17182 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17183 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017184 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017185 }
17186 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017187 case 'J':
17188 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17189 if (C->getZExtValue() <= 63)
17190 weight = CW_Constant;
17191 }
17192 break;
17193 case 'K':
17194 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17195 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17196 weight = CW_Constant;
17197 }
17198 break;
17199 case 'L':
17200 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17201 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17202 weight = CW_Constant;
17203 }
17204 break;
17205 case 'M':
17206 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17207 if (C->getZExtValue() <= 3)
17208 weight = CW_Constant;
17209 }
17210 break;
17211 case 'N':
17212 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17213 if (C->getZExtValue() <= 0xff)
17214 weight = CW_Constant;
17215 }
17216 break;
17217 case 'G':
17218 case 'C':
17219 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17220 weight = CW_Constant;
17221 }
17222 break;
17223 case 'e':
17224 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17225 if ((C->getSExtValue() >= -0x80000000LL) &&
17226 (C->getSExtValue() <= 0x7fffffffLL))
17227 weight = CW_Constant;
17228 }
17229 break;
17230 case 'Z':
17231 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17232 if (C->getZExtValue() <= 0xffffffff)
17233 weight = CW_Constant;
17234 }
17235 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017236 }
17237 return weight;
17238}
17239
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017240/// LowerXConstraint - try to replace an X constraint, which matches anything,
17241/// with another that has more specific requirements based on the type of the
17242/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017243const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017244LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017245 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17246 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017247 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017248 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017249 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017250 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017251 return "x";
17252 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017253
Chris Lattner5e764232008-04-26 23:02:14 +000017254 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017255}
17256
Chris Lattner48884cd2007-08-25 00:47:38 +000017257/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17258/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017259void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017260 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017261 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017262 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017263 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017264
Eric Christopher100c8332011-06-02 23:16:42 +000017265 // Only support length 1 constraints for now.
17266 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017267
Eric Christopher100c8332011-06-02 23:16:42 +000017268 char ConstraintLetter = Constraint[0];
17269 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017270 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017271 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017272 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017273 if (C->getZExtValue() <= 31) {
17274 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017275 break;
17276 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017277 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017278 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017279 case 'J':
17280 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017281 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017282 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17283 break;
17284 }
17285 }
17286 return;
17287 case 'K':
17288 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017289 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017290 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17291 break;
17292 }
17293 }
17294 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017295 case 'N':
17296 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017297 if (C->getZExtValue() <= 255) {
17298 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017299 break;
17300 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017301 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017302 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017303 case 'e': {
17304 // 32-bit signed value
17305 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017306 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17307 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017308 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017309 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017310 break;
17311 }
17312 // FIXME gcc accepts some relocatable values here too, but only in certain
17313 // memory models; it's complicated.
17314 }
17315 return;
17316 }
17317 case 'Z': {
17318 // 32-bit unsigned value
17319 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017320 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17321 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017322 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17323 break;
17324 }
17325 }
17326 // FIXME gcc accepts some relocatable values here too, but only in certain
17327 // memory models; it's complicated.
17328 return;
17329 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017330 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017331 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017332 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017333 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017334 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017335 break;
17336 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017337
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017338 // In any sort of PIC mode addresses need to be computed at runtime by
17339 // adding in a register or some sort of table lookup. These can't
17340 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017341 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017342 return;
17343
Chris Lattnerdc43a882007-05-03 16:52:29 +000017344 // If we are in non-pic codegen mode, we allow the address of a global (with
17345 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017346 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017347 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017348
Chris Lattner49921962009-05-08 18:23:14 +000017349 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17350 while (1) {
17351 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17352 Offset += GA->getOffset();
17353 break;
17354 } else if (Op.getOpcode() == ISD::ADD) {
17355 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17356 Offset += C->getZExtValue();
17357 Op = Op.getOperand(0);
17358 continue;
17359 }
17360 } else if (Op.getOpcode() == ISD::SUB) {
17361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17362 Offset += -C->getZExtValue();
17363 Op = Op.getOperand(0);
17364 continue;
17365 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017366 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017367
Chris Lattner49921962009-05-08 18:23:14 +000017368 // Otherwise, this isn't something we can handle, reject it.
17369 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017370 }
Eric Christopherfd179292009-08-27 18:07:15 +000017371
Dan Gohman46510a72010-04-15 01:51:59 +000017372 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017373 // If we require an extra load to get this address, as in PIC mode, we
17374 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017375 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17376 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017377 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017378
Devang Patel0d881da2010-07-06 22:08:15 +000017379 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17380 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017381 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017382 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017383 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017384
Gabor Greifba36cb52008-08-28 21:40:38 +000017385 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017386 Ops.push_back(Result);
17387 return;
17388 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017389 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017390}
17391
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017392std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017393X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017394 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017395 // First, see if this is a constraint that directly corresponds to an LLVM
17396 // register class.
17397 if (Constraint.size() == 1) {
17398 // GCC Constraint Letters
17399 switch (Constraint[0]) {
17400 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017401 // TODO: Slight differences here in allocation order and leaving
17402 // RIP in the class. Do they matter any more here than they do
17403 // in the normal allocation?
17404 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17405 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017406 if (VT == MVT::i32 || VT == MVT::f32)
17407 return std::make_pair(0U, &X86::GR32RegClass);
17408 if (VT == MVT::i16)
17409 return std::make_pair(0U, &X86::GR16RegClass);
17410 if (VT == MVT::i8 || VT == MVT::i1)
17411 return std::make_pair(0U, &X86::GR8RegClass);
17412 if (VT == MVT::i64 || VT == MVT::f64)
17413 return std::make_pair(0U, &X86::GR64RegClass);
17414 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017415 }
17416 // 32-bit fallthrough
17417 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017418 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017419 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17420 if (VT == MVT::i16)
17421 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17422 if (VT == MVT::i8 || VT == MVT::i1)
17423 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17424 if (VT == MVT::i64)
17425 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017426 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017427 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017428 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017429 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017430 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017431 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017432 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017433 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017434 return std::make_pair(0U, &X86::GR32RegClass);
17435 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017436 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017437 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017438 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017439 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017440 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017441 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017442 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17443 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017444 case 'f': // FP Stack registers.
17445 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17446 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017447 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017448 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017449 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017450 return std::make_pair(0U, &X86::RFP64RegClass);
17451 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017452 case 'y': // MMX_REGS if MMX allowed.
17453 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017454 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017455 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017456 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017457 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017458 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017459 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017460
Owen Anderson825b72b2009-08-11 20:47:22 +000017461 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017462 default: break;
17463 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017464 case MVT::f32:
17465 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017466 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017467 case MVT::f64:
17468 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017469 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017470 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017471 case MVT::v16i8:
17472 case MVT::v8i16:
17473 case MVT::v4i32:
17474 case MVT::v2i64:
17475 case MVT::v4f32:
17476 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017477 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017478 // AVX types.
17479 case MVT::v32i8:
17480 case MVT::v16i16:
17481 case MVT::v8i32:
17482 case MVT::v4i64:
17483 case MVT::v8f32:
17484 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017485 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017486 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017487 break;
17488 }
17489 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017490
Chris Lattnerf76d1802006-07-31 23:26:50 +000017491 // Use the default implementation in TargetLowering to convert the register
17492 // constraint into a member of a register class.
17493 std::pair<unsigned, const TargetRegisterClass*> Res;
17494 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017495
17496 // Not found as a standard register?
17497 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017498 // Map st(0) -> st(7) -> ST0
17499 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17500 tolower(Constraint[1]) == 's' &&
17501 tolower(Constraint[2]) == 't' &&
17502 Constraint[3] == '(' &&
17503 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17504 Constraint[5] == ')' &&
17505 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017506
Chris Lattner56d77c72009-09-13 22:41:48 +000017507 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017508 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017509 return Res;
17510 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017511
Chris Lattner56d77c72009-09-13 22:41:48 +000017512 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017513 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017514 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017515 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017516 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017517 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017518
17519 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017520 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017521 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017522 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017523 return Res;
17524 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017525
Dale Johannesen330169f2008-11-13 21:52:36 +000017526 // 'A' means EAX + EDX.
17527 if (Constraint == "A") {
17528 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017529 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017530 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017531 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017532 return Res;
17533 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017534
Chris Lattnerf76d1802006-07-31 23:26:50 +000017535 // Otherwise, check to see if this is a register class of the wrong value
17536 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17537 // turn into {ax},{dx}.
17538 if (Res.second->hasType(VT))
17539 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017540
Chris Lattnerf76d1802006-07-31 23:26:50 +000017541 // All of the single-register GCC register classes map their values onto
17542 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17543 // really want an 8-bit or 32-bit register, map to the appropriate register
17544 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017545 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017546 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017547 unsigned DestReg = 0;
17548 switch (Res.first) {
17549 default: break;
17550 case X86::AX: DestReg = X86::AL; break;
17551 case X86::DX: DestReg = X86::DL; break;
17552 case X86::CX: DestReg = X86::CL; break;
17553 case X86::BX: DestReg = X86::BL; break;
17554 }
17555 if (DestReg) {
17556 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017557 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017558 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017559 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017560 unsigned DestReg = 0;
17561 switch (Res.first) {
17562 default: break;
17563 case X86::AX: DestReg = X86::EAX; break;
17564 case X86::DX: DestReg = X86::EDX; break;
17565 case X86::CX: DestReg = X86::ECX; break;
17566 case X86::BX: DestReg = X86::EBX; break;
17567 case X86::SI: DestReg = X86::ESI; break;
17568 case X86::DI: DestReg = X86::EDI; break;
17569 case X86::BP: DestReg = X86::EBP; break;
17570 case X86::SP: DestReg = X86::ESP; break;
17571 }
17572 if (DestReg) {
17573 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017574 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017575 }
Owen Anderson825b72b2009-08-11 20:47:22 +000017576 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017577 unsigned DestReg = 0;
17578 switch (Res.first) {
17579 default: break;
17580 case X86::AX: DestReg = X86::RAX; break;
17581 case X86::DX: DestReg = X86::RDX; break;
17582 case X86::CX: DestReg = X86::RCX; break;
17583 case X86::BX: DestReg = X86::RBX; break;
17584 case X86::SI: DestReg = X86::RSI; break;
17585 case X86::DI: DestReg = X86::RDI; break;
17586 case X86::BP: DestReg = X86::RBP; break;
17587 case X86::SP: DestReg = X86::RSP; break;
17588 }
17589 if (DestReg) {
17590 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000017591 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000017592 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000017593 }
Craig Topperc9099502012-04-20 06:31:50 +000017594 } else if (Res.second == &X86::FR32RegClass ||
17595 Res.second == &X86::FR64RegClass ||
17596 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017597 // Handle references to XMM physical registers that got mapped into the
17598 // wrong class. This can happen with constraints like {xmm0} where the
17599 // target independent register mapper will just pick the first match it can
17600 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000017601
17602 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000017603 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017604 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000017605 Res.second = &X86::FR64RegClass;
17606 else if (X86::VR128RegClass.hasType(VT))
17607 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000017608 else if (X86::VR256RegClass.hasType(VT))
17609 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000017610 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017611
Chris Lattnerf76d1802006-07-31 23:26:50 +000017612 return Res;
17613}
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017614
Nadav Roteme6237022012-11-05 19:32:46 +000017615//===----------------------------------------------------------------------===//
17616//
17617// X86 cost model.
17618//
17619//===----------------------------------------------------------------------===//
17620
17621struct X86CostTblEntry {
17622 int ISD;
17623 MVT Type;
17624 unsigned Cost;
17625};
17626
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017627static int
17628FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017629 for (unsigned int i = 0; i < len; ++i)
17630 if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
17631 return i;
17632
17633 // Could not find an entry.
17634 return -1;
17635}
17636
Nadav Rotemb0428682012-11-06 19:33:53 +000017637struct X86TypeConversionCostTblEntry {
17638 int ISD;
17639 MVT Dst;
17640 MVT Src;
17641 unsigned Cost;
17642};
17643
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000017644static int
17645FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
17646 int ISD, MVT Dst, MVT Src) {
Nadav Rotemb0428682012-11-06 19:33:53 +000017647 for (unsigned int i = 0; i < len; ++i)
17648 if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
17649 return i;
17650
17651 // Could not find an entry.
17652 return -1;
17653}
17654
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017655unsigned
17656X86VectorTargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
17657 Type *Ty) const {
Nadav Roteme6237022012-11-05 19:32:46 +000017658 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017659 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Roteme6237022012-11-05 19:32:46 +000017660
17661 int ISD = InstructionOpcodeToISD(Opcode);
17662 assert(ISD && "Invalid opcode");
17663
Nadav Rotemb0428682012-11-06 19:33:53 +000017664 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017665
Nadav Roteme6237022012-11-05 19:32:46 +000017666 static const X86CostTblEntry AVX1CostTable[] = {
17667 // We don't have to scalarize unsupported ops. We can issue two half-sized
17668 // operations and we only need to extract the upper YMM half.
17669 // Two ops + 1 extract + 1 insert = 4.
17670 { ISD::MUL, MVT::v8i32, 4 },
17671 { ISD::SUB, MVT::v8i32, 4 },
17672 { ISD::ADD, MVT::v8i32, 4 },
17673 { ISD::MUL, MVT::v4i64, 4 },
17674 { ISD::SUB, MVT::v4i64, 4 },
17675 { ISD::ADD, MVT::v4i64, 4 },
17676 };
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017677
Nadav Roteme6237022012-11-05 19:32:46 +000017678 // Look for AVX1 lowering tricks.
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017679 if (ST.hasAVX()) {
17680 int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
17681 LT.second);
17682 if (Idx != -1)
17683 return LT.first * AVX1CostTable[Idx].Cost;
17684 }
Nadav Roteme6237022012-11-05 19:32:46 +000017685 // Fallback to the default implementation.
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017686 return VectorTargetTransformImpl::getArithmeticInstrCost(Opcode, Ty);
17687}
17688
17689unsigned
17690X86VectorTargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
Richard Smithe010eb32012-11-05 22:01:44 +000017691 unsigned Index) const {
Nadav Rotema4ab5292012-11-05 21:12:13 +000017692 assert(Val->isVectorTy() && "This must be a vector type");
17693
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017694 if (Index != -1U) {
Nadav Rotema4ab5292012-11-05 21:12:13 +000017695 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017696 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Val);
Nadav Rotema4ab5292012-11-05 21:12:13 +000017697
17698 // This type is legalized to a scalar type.
17699 if (!LT.second.isVector())
17700 return 0;
17701
17702 // The type may be split. Normalize the index to the new type.
17703 unsigned Width = LT.second.getVectorNumElements();
17704 Index = Index % Width;
17705
17706 // Floating point scalars are already located in index #0.
17707 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
17708 return 0;
17709 }
17710
Nadav Rotemb4b04c32012-11-03 00:39:56 +000017711 return VectorTargetTransformImpl::getVectorInstrCost(Opcode, Val, Index);
17712}
17713
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017714unsigned X86VectorTargetTransformInfo::getCmpSelInstrCost(unsigned Opcode,
17715 Type *ValTy,
17716 Type *CondTy) const {
17717 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000017718 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017719
17720 MVT MTy = LT.second;
17721
17722 int ISD = InstructionOpcodeToISD(Opcode);
17723 assert(ISD && "Invalid opcode");
17724
17725 const X86Subtarget &ST =
17726 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17727
17728 static const X86CostTblEntry SSE42CostTbl[] = {
17729 { ISD::SETCC, MVT::v2f64, 1 },
17730 { ISD::SETCC, MVT::v4f32, 1 },
17731 { ISD::SETCC, MVT::v2i64, 1 },
17732 { ISD::SETCC, MVT::v4i32, 1 },
17733 { ISD::SETCC, MVT::v8i16, 1 },
17734 { ISD::SETCC, MVT::v16i8, 1 },
17735 };
17736
17737 static const X86CostTblEntry AVX1CostTbl[] = {
17738 { ISD::SETCC, MVT::v4f64, 1 },
17739 { ISD::SETCC, MVT::v8f32, 1 },
17740 // AVX1 does not support 8-wide integer compare.
17741 { ISD::SETCC, MVT::v4i64, 4 },
17742 { ISD::SETCC, MVT::v8i32, 4 },
17743 { ISD::SETCC, MVT::v16i16, 4 },
17744 { ISD::SETCC, MVT::v32i8, 4 },
17745 };
17746
17747 static const X86CostTblEntry AVX2CostTbl[] = {
17748 { ISD::SETCC, MVT::v4i64, 1 },
17749 { ISD::SETCC, MVT::v8i32, 1 },
17750 { ISD::SETCC, MVT::v16i16, 1 },
17751 { ISD::SETCC, MVT::v32i8, 1 },
17752 };
17753
17754 if (ST.hasSSE42()) {
17755 int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
17756 if (Idx != -1)
17757 return LT.first * SSE42CostTbl[Idx].Cost;
17758 }
17759
17760 if (ST.hasAVX()) {
17761 int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
17762 if (Idx != -1)
17763 return LT.first * AVX1CostTbl[Idx].Cost;
17764 }
17765
17766 if (ST.hasAVX2()) {
17767 int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
17768 if (Idx != -1)
17769 return LT.first * AVX2CostTbl[Idx].Cost;
17770 }
17771
17772 return VectorTargetTransformImpl::getCmpSelInstrCost(Opcode, ValTy, CondTy);
17773}
17774
Nadav Rotemb0428682012-11-06 19:33:53 +000017775unsigned X86VectorTargetTransformInfo::getCastInstrCost(unsigned Opcode,
17776 Type *Dst,
17777 Type *Src) const {
17778 int ISD = InstructionOpcodeToISD(Opcode);
17779 assert(ISD && "Invalid opcode");
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017780
Nadav Rotemb0428682012-11-06 19:33:53 +000017781 EVT SrcTy = TLI->getValueType(Src);
17782 EVT DstTy = TLI->getValueType(Dst);
17783
17784 if (!SrcTy.isSimple() || !DstTy.isSimple())
17785 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
17786
17787 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
17788
17789 static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
17790 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
17791 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
17792 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
17793 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
17794 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1 },
17795 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1 },
17796 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
17797 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
17798 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
17799 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
Nadav Rotemb14a5f52012-11-09 07:02:24 +000017800 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
17801 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Nadav Rotemb0428682012-11-06 19:33:53 +000017802 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 6 },
17803 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 9 },
Nadav Rotema6fb97a2012-11-06 21:17:17 +000017804 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 3 },
Nadav Rotemb0428682012-11-06 19:33:53 +000017805 };
17806
17807 if (ST.hasAVX()) {
17808 int Idx = FindInConvertTable(AVXConversionTbl,
17809 array_lengthof(AVXConversionTbl),
17810 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
17811 if (Idx != -1)
17812 return AVXConversionTbl[Idx].Cost;
17813 }
17814
17815 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
17816}
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000017817