blob: 311971070e69585c4e8a14c4248c2cdde3a1a8b7 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Craig Topper1bf724b2012-02-19 07:15:48 +000016#include "X86ISelLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000018#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +0000161 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000162
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000163 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000164 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000165
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000167 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000170 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000171 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000173
Eric Christopherde5e1012011-03-11 01:05:58 +0000174 // For 64-bit since we have so many registers use the ILP scheduler, for
175 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000176 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000177 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000179 else if (Subtarget->is64Bit())
180 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000181 else
182 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000184
Preston Gurd2e2efd92012-09-04 18:22:17 +0000185 // Bypass i32 with i8 on Atom when compiling with O2
186 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
187 addBypassSlowDivType(Type::getInt32Ty(getGlobalContext()), Type::getInt8Ty(getGlobalContext()));
188
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000189 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000190 // Setup Windows compiler runtime calls.
191 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000192 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000193 setLibcallName(RTLIB::SREM_I64, "_allrem");
194 setLibcallName(RTLIB::UREM_I64, "_aullrem");
195 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
200 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000201
202 // The _ftol2 runtime function has an unusual calling conv, which
203 // is modeled by a special pseudo-instruction.
204 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
206 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
207 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000208 }
209
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000211 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 setUseUnderscoreSetJmp(false);
213 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000214 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 // MS runtime is weird: it exports _setjmp, but longjmp!
216 setUseUnderscoreSetJmp(true);
217 setUseUnderscoreLongJmp(false);
218 } else {
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(true);
221 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000224 addRegisterClass(MVT::i8, &X86::GR8RegClass);
225 addRegisterClass(MVT::i16, &X86::GR16RegClass);
226 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000228 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000231
Scott Michelfdc40a02009-02-17 22:15:04 +0000232 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000234 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
238 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000239
240 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
243 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
249 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
252 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000253
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000257 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 // We have an algorithm for SSE2->double, and we turn this into a
259 // 64-bit FILD followed by conditional FADD for other targets.
260 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000261 // We have an algorithm for SSE2, and we turn this into a 64-bit
262 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
266 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
267 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
269 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000271 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 // SSE has no i16 to fp conversion, only i32
273 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000281 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285
Dale Johannesen73328d12007-09-19 23:55:34 +0000286 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
287 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
289 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000290
Evan Cheng02568ff2006-01-30 22:13:22 +0000291 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
292 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
294 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000296 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000298 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000300 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000303 }
304
305 // Handle FP_TO_UINT by promoting the destination to a larger signed
306 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
313 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000314 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000315 // Since AVX is a superset of SSE3, only check for SSE here.
316 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 // Expand FP_TO_UINT into a select.
318 // FIXME: We would like to use a Custom expander here eventually to do
319 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000322 // With SSE3 we can use fisttpll to convert to a signed i64; without
323 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000327 if (isTargetFTOL()) {
328 // Use the _ftol2 runtime function, which has a pseudo-instruction
329 // to handle its weird calling convention.
330 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
331 }
332
Chris Lattner399610a2006-12-05 18:22:22 +0000333 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000334 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
336 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000337 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000338 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000339 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000340 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000341 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000342 }
Chris Lattner21f66852005-12-23 05:15:23 +0000343
Dan Gohmanb00ee212008-02-18 19:34:53 +0000344 // Scalar integer divide and remainder are lowered to use operations that
345 // produce two results, to match the available instructions. This exposes
346 // the two-result form to trivial CSE, which is able to combine x/y and x%y
347 // into a single instruction.
348 //
349 // Scalar integer multiply-high is also lowered to use two-result
350 // operations, to match the available instructions. However, plain multiply
351 // (low) operations are left as Legal, as there are single-result
352 // instructions for this in x86. Using the two-result multiply instructions
353 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000354 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000355 MVT VT = IntVTs[i];
356 setOperationAction(ISD::MULHS, VT, Expand);
357 setOperationAction(ISD::MULHU, VT, Expand);
358 setOperationAction(ISD::SDIV, VT, Expand);
359 setOperationAction(ISD::UDIV, VT, Expand);
360 setOperationAction(ISD::SREM, VT, Expand);
361 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000363 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000364 setOperationAction(ISD::ADDC, VT, Custom);
365 setOperationAction(ISD::ADDE, VT, Custom);
366 setOperationAction(ISD::SUBC, VT, Custom);
367 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000368 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000369
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
371 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
372 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
373 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000374 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
378 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
379 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f32 , Expand);
381 setOperationAction(ISD::FREM , MVT::f64 , Expand);
382 setOperationAction(ISD::FREM , MVT::f80 , Expand);
383 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000384
Chandler Carruth77821022011-12-24 12:12:34 +0000385 // Promote the i8 variants and force them on up to i32 which has a shorter
386 // encoding.
387 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
388 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
389 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
390 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000391 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
393 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
394 if (Subtarget->is64Bit())
395 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000396 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000397 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
398 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
401 }
Craig Topper37f21672011-10-11 06:44:02 +0000402
403 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000404 // When promoting the i8 variants, force them to i32 for a shorter
405 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000406 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000407 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
408 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
409 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
411 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
412 if (Subtarget->is64Bit())
413 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000414 } else {
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
420 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
421 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
424 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 }
426
Benjamin Kramer1292c222010-12-04 20:32:23 +0000427 if (Subtarget->hasPOPCNT()) {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
429 } else {
430 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
432 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
433 if (Subtarget->is64Bit())
434 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
435 }
436
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
438 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000439
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000443 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000444 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
448 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
449 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000450 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000457 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000458 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000460
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000461 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
463 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
464 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
465 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000466 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
468 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000469 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000470 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
472 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
473 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
474 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000475 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000476 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000477 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
479 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
480 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000481 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
483 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
484 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000485 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486
Craig Topper1accb7e2012-01-10 06:54:16 +0000487 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000489
Eric Christopher9a9d2752010-07-22 02:48:34 +0000490 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000491 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000492
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000493 // On X86 and X86-64, atomic operations are lowered to locked instructions.
494 // Locked instructions, in turn, have implicit fence semantics (all memory
495 // operations are flushed before issuing the locked instruction, and they
496 // are not buffered), so we can fold away the common pattern of
497 // fence-atomic-fence.
498 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000499
Mon P Wang63307c32008-05-05 19:05:59 +0000500 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000501 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 MVT VT = IntVTs[i];
503 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000505 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000506 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000507
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000509 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
513 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
515 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
516 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000517 }
518
Eli Friedman43f51ae2011-08-26 21:21:21 +0000519 if (Subtarget->hasCmpxchg16b()) {
520 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
521 }
522
Evan Cheng3c992d22006-03-07 02:02:57 +0000523 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000524 if (!Subtarget->isTargetDarwin() &&
525 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000526 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000528 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000529
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
531 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
532 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
533 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000534 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000535 setExceptionPointerRegister(X86::RAX);
536 setExceptionSelectorRegister(X86::RDX);
537 } else {
538 setExceptionPointerRegister(X86::EAX);
539 setExceptionSelectorRegister(X86::EDX);
540 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
542 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000543
Duncan Sands4a544a72011-09-06 13:37:06 +0000544 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
545 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000546
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000548
Nate Begemanacc398c2006-01-25 18:21:52 +0000549 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::VASTART , MVT::Other, Custom);
551 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000552 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::VAARG , MVT::Other, Custom);
554 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000555 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::VAARG , MVT::Other, Expand);
557 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000558 }
Evan Chengae642192007-03-02 23:16:35 +0000559
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
561 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000562
563 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
564 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
565 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000566 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000567 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
568 MVT::i64 : MVT::i32, Custom);
569 else
570 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
571 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000572
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000573 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000574 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000576 addRegisterClass(MVT::f32, &X86::FR32RegClass);
577 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000578
Evan Cheng223547a2006-01-31 22:28:30 +0000579 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FABS , MVT::f64, Custom);
581 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000582
583 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FNEG , MVT::f64, Custom);
585 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000586
Evan Cheng68c47cb2007-01-05 07:55:56 +0000587 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
589 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000590
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000591 // Lower this to FGETSIGNx86 plus an AND.
592 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
593 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
594
Evan Chengd25e9e82006-02-02 00:28:23 +0000595 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FSIN , MVT::f64, Expand);
597 setOperationAction(ISD::FCOS , MVT::f64, Expand);
598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000600
Chris Lattnera54aa942006-01-29 06:26:08 +0000601 // Expand FP immediates into loads from the stack, except for the special
602 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0)); // xorpd
604 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000605 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606 // Use SSE for f32, x87 for f64.
607 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000608 addRegisterClass(MVT::f32, &X86::FR32RegClass);
609 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610
611 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613
614 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
619 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f32, Expand);
625 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
Nate Begemane1795842008-02-14 08:57:00 +0000627 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628 addLegalFPImmediate(APFloat(+0.0f)); // xorps
629 addLegalFPImmediate(APFloat(+0.0)); // FLD0
630 addLegalFPImmediate(APFloat(+1.0)); // FLD1
631 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
632 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
633
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000634 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
636 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000637 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000638 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000640 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000641 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
642 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000643
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
645 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
647 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000648
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000649 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
651 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000653 addLegalFPImmediate(APFloat(+0.0)); // FLD0
654 addLegalFPImmediate(APFloat(+1.0)); // FLD1
655 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
656 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000657 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
658 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
659 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
660 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000661 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000662
Cameron Zwarich33390842011-07-08 21:39:21 +0000663 // We don't support FMA.
664 setOperationAction(ISD::FMA, MVT::f64, Expand);
665 setOperationAction(ISD::FMA, MVT::f32, Expand);
666
Dale Johannesen59a58732007-08-05 18:49:15 +0000667 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000668 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000669 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
671 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000673 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000674 addLegalFPImmediate(TmpFlt); // FLD0
675 TmpFlt.changeSign();
676 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000677
678 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000679 APFloat TmpFlt2(+1.0);
680 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
681 &ignored);
682 addLegalFPImmediate(TmpFlt2); // FLD1
683 TmpFlt2.changeSign();
684 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
685 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000687 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
689 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000690 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000691
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000692 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
693 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
694 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
695 setOperationAction(ISD::FRINT, MVT::f80, Expand);
696 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000697 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000698 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000699
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000700 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
702 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
703 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000704
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FLOG, MVT::f80, Expand);
706 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
707 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
708 setOperationAction(ISD::FEXP, MVT::f80, Expand);
709 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000710
Mon P Wangf007a8b2008-11-06 05:31:54 +0000711 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000712 // (for widening) or expand (for scalarization). Then we will selectively
713 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000714 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
715 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000732 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
733 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000738 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000742 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000750 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000752 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000759 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
765 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000769 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000770 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000774 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000775 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
776 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000777 setTruncStoreAction((MVT::SimpleValueType)VT,
778 (MVT::SimpleValueType)InnerVT, Expand);
779 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
780 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
781 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000782 }
783
Evan Chengc7ce29b2009-02-13 22:36:38 +0000784 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
785 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000786 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000787 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000789 }
790
Dale Johannesen0488fb62010-09-30 23:57:10 +0000791 // MMX-sized vectors (other than x86mmx) are expected to be expanded
792 // into smaller operations.
793 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
794 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
795 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
796 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
797 setOperationAction(ISD::AND, MVT::v8i8, Expand);
798 setOperationAction(ISD::AND, MVT::v4i16, Expand);
799 setOperationAction(ISD::AND, MVT::v2i32, Expand);
800 setOperationAction(ISD::AND, MVT::v1i64, Expand);
801 setOperationAction(ISD::OR, MVT::v8i8, Expand);
802 setOperationAction(ISD::OR, MVT::v4i16, Expand);
803 setOperationAction(ISD::OR, MVT::v2i32, Expand);
804 setOperationAction(ISD::OR, MVT::v1i64, Expand);
805 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
806 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
807 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
808 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
809 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
810 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
811 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
812 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
813 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
814 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
815 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
816 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
817 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000818 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
819 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
820 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
821 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000822
Craig Topper1accb7e2012-01-10 06:54:16 +0000823 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000824 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000825
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
827 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
828 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
829 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
831 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000832 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
834 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
835 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
836 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
837 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000838 }
839
Craig Topper1accb7e2012-01-10 06:54:16 +0000840 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000841 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000842
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000843 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
844 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000845 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
846 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
847 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
848 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000849
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
851 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
852 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
853 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
854 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
855 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
856 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
857 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
858 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
859 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
860 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
861 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
862 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
863 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
864 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
865 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000866 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000867
Nadav Rotem354efd82011-09-18 14:57:03 +0000868 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000869 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
870 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
871 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000872
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
874 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
876 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
877 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000878
Evan Cheng2c3ae372006-04-12 21:21:57 +0000879 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000880 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000881 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000882 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000883 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000884 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000885 // Do not attempt to custom lower non-128-bit vectors
886 if (!VT.is128BitVector())
887 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000888 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
889 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
890 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000891 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
894 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
895 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
896 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
898 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000899
Nate Begemancdd1eec2008-02-12 22:51:28 +0000900 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
902 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000903 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000904
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000905 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000906 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000907 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000908
909 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000910 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000911 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000912
Craig Topper0d1f1762012-08-12 00:34:56 +0000913 setOperationAction(ISD::AND, VT, Promote);
914 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
915 setOperationAction(ISD::OR, VT, Promote);
916 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
917 setOperationAction(ISD::XOR, VT, Promote);
918 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
919 setOperationAction(ISD::LOAD, VT, Promote);
920 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
921 setOperationAction(ISD::SELECT, VT, Promote);
922 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000923 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000926
Evan Cheng2c3ae372006-04-12 21:21:57 +0000927 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
929 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
930 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
931 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000932
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
934 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000935
936 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000937 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000938
Craig Topperd0a31172012-01-10 06:37:29 +0000939 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000940 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
941 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
942 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
943 setOperationAction(ISD::FRINT, MVT::f32, Legal);
944 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
945 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
946 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
947 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
948 setOperationAction(ISD::FRINT, MVT::f64, Legal);
949 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
950
Craig Topper12fb5c62012-09-08 17:42:27 +0000951 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
952 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
953
Nate Begeman14d12ca2008-02-11 04:19:36 +0000954 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000956
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000957 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
958 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
959 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
960 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
961 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000962
Nate Begeman14d12ca2008-02-11 04:19:36 +0000963 // i8 and i16 vectors are custom , because the source register and source
964 // source memory operand types are not the same width. f32 vectors are
965 // custom since the immediate controlling the insert encodes additional
966 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
968 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
969 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
970 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000971
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
973 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
974 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
975 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000976
Pete Coopera77214a2011-11-14 19:38:42 +0000977 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000978 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000979 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000980 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
981 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000982 }
983 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000984
Craig Topper1accb7e2012-01-10 06:54:16 +0000985 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000986 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000987 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000988
Nadav Rotem43012222011-05-11 08:12:09 +0000989 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000990 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000991
Nadav Rotem43012222011-05-11 08:12:09 +0000992 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000993 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000994
995 if (Subtarget->hasAVX2()) {
996 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
997 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
998
999 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1000 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1001
1002 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1003 } else {
1004 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1005 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1006
1007 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1008 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1009
1010 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1011 }
Nadav Rotem43012222011-05-11 08:12:09 +00001012 }
1013
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001014 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001015 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1016 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1017 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1018 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1019 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1020 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001021
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001023 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1024 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001025
Owen Anderson825b72b2009-08-11 20:47:22 +00001026 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1027 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1028 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1029 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1030 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001031 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001033 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001034
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1036 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1037 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1038 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1039 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001040 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001042 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001043
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001044 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1045 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001046 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001047
Michael Liaob8150d82012-09-10 18:33:51 +00001048 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1049
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001050 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1051 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1052
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001053 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1054 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1055
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001056 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001057 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001058
Duncan Sands28b77e92011-09-06 19:07:46 +00001059 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1060 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1061 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1062 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001063
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001064 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1065 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1066 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1067
Craig Topperaaa643c2011-11-09 07:28:55 +00001068 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1069 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1070 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1071 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001072
Craig Topperbf404372012-08-31 15:40:30 +00001073 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001074 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1075 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1076 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1077 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1078 setOperationAction(ISD::FMA, MVT::f32, Custom);
1079 setOperationAction(ISD::FMA, MVT::f64, Custom);
1080 }
Craig Topper880ef452012-08-11 22:34:26 +00001081
Craig Topperaaa643c2011-11-09 07:28:55 +00001082 if (Subtarget->hasAVX2()) {
1083 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1084 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1085 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1086 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001087
Craig Topperaaa643c2011-11-09 07:28:55 +00001088 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1089 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1090 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1091 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001092
Craig Topperaaa643c2011-11-09 07:28:55 +00001093 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1094 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1095 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001096 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001097
1098 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001099
1100 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1101 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1102
1103 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1104 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1105
1106 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001107 } else {
1108 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1109 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1110 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1111 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1112
1113 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1114 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1115 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1116 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1117
1118 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1119 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1120 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1121 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001122
1123 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1124 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1125
1126 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1127 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1128
1129 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001130 }
Craig Topper13894fa2011-08-24 06:14:18 +00001131
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001132 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001133 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1134 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001135 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001136
1137 // Extract subvector is special because the value type
1138 // (result) is 128-bit but the source is 256-bit wide.
1139 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001140 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001141
1142 // Do not attempt to custom lower other non-256-bit vectors
1143 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001144 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001145
Craig Topper0d1f1762012-08-12 00:34:56 +00001146 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1147 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1148 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1149 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1150 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1151 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1152 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001153 }
1154
David Greene54d8eba2011-01-27 22:38:56 +00001155 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001156 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001157 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001158
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001159 // Do not attempt to promote non-256-bit vectors
1160 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001161 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001162
Craig Topper0d1f1762012-08-12 00:34:56 +00001163 setOperationAction(ISD::AND, VT, Promote);
1164 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1165 setOperationAction(ISD::OR, VT, Promote);
1166 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1167 setOperationAction(ISD::XOR, VT, Promote);
1168 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1169 setOperationAction(ISD::LOAD, VT, Promote);
1170 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1171 setOperationAction(ISD::SELECT, VT, Promote);
1172 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001173 }
David Greene9b9838d2009-06-29 16:47:10 +00001174 }
1175
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001176 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1177 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001178 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1179 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001180 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1181 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001182 }
1183
Evan Cheng6be2c582006-04-05 23:38:46 +00001184 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001185 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001186 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001187
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001188
Eli Friedman962f5492010-06-02 19:35:46 +00001189 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1190 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001191 //
Eli Friedman962f5492010-06-02 19:35:46 +00001192 // FIXME: We really should do custom legalization for addition and
1193 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1194 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001195 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1196 // Add/Sub/Mul with overflow operations are custom lowered.
1197 MVT VT = IntVTs[i];
1198 setOperationAction(ISD::SADDO, VT, Custom);
1199 setOperationAction(ISD::UADDO, VT, Custom);
1200 setOperationAction(ISD::SSUBO, VT, Custom);
1201 setOperationAction(ISD::USUBO, VT, Custom);
1202 setOperationAction(ISD::SMULO, VT, Custom);
1203 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001204 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001205
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001206 // There are no 8-bit 3-address imul/mul instructions
1207 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1208 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001209
Evan Chengd54f2d52009-03-31 19:38:51 +00001210 if (!Subtarget->is64Bit()) {
1211 // These libcalls are not available in 32-bit.
1212 setLibcallName(RTLIB::SHL_I128, 0);
1213 setLibcallName(RTLIB::SRL_I128, 0);
1214 setLibcallName(RTLIB::SRA_I128, 0);
1215 }
1216
Evan Cheng206ee9d2006-07-07 08:33:52 +00001217 // We have target-specific dag combine patterns for the following nodes:
1218 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001219 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001220 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001221 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001222 setTargetDAGCombine(ISD::SHL);
1223 setTargetDAGCombine(ISD::SRA);
1224 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001225 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001226 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001227 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001228 setTargetDAGCombine(ISD::FADD);
1229 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001230 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001231 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001232 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001233 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001234 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001235 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001236 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001237 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001238 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001239 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001240 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001241 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001242 if (Subtarget->is64Bit())
1243 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001244 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001245
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001246 computeRegisterProperties();
1247
Evan Cheng05219282011-01-06 06:52:41 +00001248 // On Darwin, -Os means optimize for size without hurting performance,
1249 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001250 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001251 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001252 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001253 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1254 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1255 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001256 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001257 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001258
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001259 // Predictable cmov don't hurt on atom because it's in-order.
1260 predictableSelectIsExpensive = !Subtarget->isAtom();
1261
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001262 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001263}
1264
Scott Michel5b8f82e2008-03-10 15:42:14 +00001265
Duncan Sands28b77e92011-09-06 19:07:46 +00001266EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1267 if (!VT.isVector()) return MVT::i8;
1268 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001269}
1270
1271
Evan Cheng29286502008-01-23 23:17:41 +00001272/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1273/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001274static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001275 if (MaxAlign == 16)
1276 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001277 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001278 if (VTy->getBitWidth() == 128)
1279 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001280 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001281 unsigned EltAlign = 0;
1282 getMaxByValAlign(ATy->getElementType(), EltAlign);
1283 if (EltAlign > MaxAlign)
1284 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001285 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001286 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1287 unsigned EltAlign = 0;
1288 getMaxByValAlign(STy->getElementType(i), EltAlign);
1289 if (EltAlign > MaxAlign)
1290 MaxAlign = EltAlign;
1291 if (MaxAlign == 16)
1292 break;
1293 }
1294 }
Evan Cheng29286502008-01-23 23:17:41 +00001295}
1296
1297/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1298/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001299/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1300/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001301unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001302 if (Subtarget->is64Bit()) {
1303 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001304 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001305 if (TyAlign > 8)
1306 return TyAlign;
1307 return 8;
1308 }
1309
Evan Cheng29286502008-01-23 23:17:41 +00001310 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001311 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001312 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001313 return Align;
1314}
Chris Lattner2b02a442007-02-25 08:29:00 +00001315
Evan Chengf0df0312008-05-15 08:39:06 +00001316/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001317/// and store operations as a result of memset, memcpy, and memmove
1318/// lowering. If DstAlign is zero that means it's safe to destination
1319/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1320/// means there isn't a need to check it against alignment requirement,
1321/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001322/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001323/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1324/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1325/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001326/// It returns EVT::Other if the type should be determined using generic
1327/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001328EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001329X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1330 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001331 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001332 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001333 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001334 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1335 // linux. This is because the stack realignment code can't handle certain
1336 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001337 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001338 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001339 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001340 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001341 (Subtarget->isUnalignedMemAccessFast() ||
1342 ((DstAlign == 0 || DstAlign >= 16) &&
1343 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001344 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001345 if (Subtarget->getStackAlignment() >= 32) {
1346 if (Subtarget->hasAVX2())
1347 return MVT::v8i32;
1348 if (Subtarget->hasAVX())
1349 return MVT::v8f32;
1350 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001351 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001352 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001353 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001354 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001355 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001356 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001357 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001358 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001359 // Do not use f64 to lower memcpy if source is string constant. It's
1360 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001361 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001362 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001363 }
Evan Chengf0df0312008-05-15 08:39:06 +00001364 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001365 return MVT::i64;
1366 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001367}
1368
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001369/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1370/// current function. The returned value is a member of the
1371/// MachineJumpTableInfo::JTEntryKind enum.
1372unsigned X86TargetLowering::getJumpTableEncoding() const {
1373 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1374 // symbol.
1375 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1376 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001377 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001378
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001379 // Otherwise, use the normal jump table encoding heuristics.
1380 return TargetLowering::getJumpTableEncoding();
1381}
1382
Chris Lattnerc64daab2010-01-26 05:02:42 +00001383const MCExpr *
1384X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1385 const MachineBasicBlock *MBB,
1386 unsigned uid,MCContext &Ctx) const{
1387 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1388 Subtarget->isPICStyleGOT());
1389 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1390 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001391 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1392 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001393}
1394
Evan Chengcc415862007-11-09 01:32:10 +00001395/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1396/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001397SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001398 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001399 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001400 // This doesn't have DebugLoc associated with it, but is not really the
1401 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001402 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001403 return Table;
1404}
1405
Chris Lattner589c6f62010-01-26 06:28:43 +00001406/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1407/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1408/// MCExpr.
1409const MCExpr *X86TargetLowering::
1410getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1411 MCContext &Ctx) const {
1412 // X86-64 uses RIP relative addressing based on the jump table label.
1413 if (Subtarget->isPICStyleRIPRel())
1414 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1415
1416 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001417 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001418}
1419
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001420// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001421std::pair<const TargetRegisterClass*, uint8_t>
1422X86TargetLowering::findRepresentativeClass(EVT VT) const{
1423 const TargetRegisterClass *RRC = 0;
1424 uint8_t Cost = 1;
1425 switch (VT.getSimpleVT().SimpleTy) {
1426 default:
1427 return TargetLowering::findRepresentativeClass(VT);
1428 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001429 RRC = Subtarget->is64Bit() ?
1430 (const TargetRegisterClass*)&X86::GR64RegClass :
1431 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001432 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001433 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001434 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001435 break;
1436 case MVT::f32: case MVT::f64:
1437 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1438 case MVT::v4f32: case MVT::v2f64:
1439 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1440 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001441 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001442 break;
1443 }
1444 return std::make_pair(RRC, Cost);
1445}
1446
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001447bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1448 unsigned &Offset) const {
1449 if (!Subtarget->isTargetLinux())
1450 return false;
1451
1452 if (Subtarget->is64Bit()) {
1453 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1454 Offset = 0x28;
1455 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1456 AddressSpace = 256;
1457 else
1458 AddressSpace = 257;
1459 } else {
1460 // %gs:0x14 on i386
1461 Offset = 0x14;
1462 AddressSpace = 256;
1463 }
1464 return true;
1465}
1466
1467
Chris Lattner2b02a442007-02-25 08:29:00 +00001468//===----------------------------------------------------------------------===//
1469// Return Value Calling Convention Implementation
1470//===----------------------------------------------------------------------===//
1471
Chris Lattner59ed56b2007-02-28 04:55:35 +00001472#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001473
Michael J. Spencerec38de22010-10-10 22:04:20 +00001474bool
Eric Christopher471e4222011-06-08 23:55:35 +00001475X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001476 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001477 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001478 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001479 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001480 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001481 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001482 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001483}
1484
Dan Gohman98ca4f22009-08-05 01:29:28 +00001485SDValue
1486X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001487 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001489 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001490 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001491 MachineFunction &MF = DAG.getMachineFunction();
1492 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001493
Chris Lattner9774c912007-02-27 05:28:59 +00001494 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001495 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001496 RVLocs, *DAG.getContext());
1497 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001498
Evan Chengdcea1632010-02-04 02:40:39 +00001499 // Add the regs to the liveout set for the function.
1500 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1501 for (unsigned i = 0; i != RVLocs.size(); ++i)
1502 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1503 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001504
Dan Gohman475871a2008-07-27 21:46:04 +00001505 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001506
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001508 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1509 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001510 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1511 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001512
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001513 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001514 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1515 CCValAssign &VA = RVLocs[i];
1516 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001517 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001518 EVT ValVT = ValToCopy.getValueType();
1519
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001520 // Promote values to the appropriate types
1521 if (VA.getLocInfo() == CCValAssign::SExt)
1522 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1523 else if (VA.getLocInfo() == CCValAssign::ZExt)
1524 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1525 else if (VA.getLocInfo() == CCValAssign::AExt)
1526 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1527 else if (VA.getLocInfo() == CCValAssign::BCvt)
1528 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1529
Dale Johannesenc4510512010-09-24 19:05:48 +00001530 // If this is x86-64, and we disabled SSE, we can't return FP values,
1531 // or SSE or MMX vectors.
1532 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1533 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001534 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001535 report_fatal_error("SSE register return with SSE disabled");
1536 }
1537 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1538 // llvm-gcc has never done it right and no one has noticed, so this
1539 // should be OK for now.
1540 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001541 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001542 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Chris Lattner447ff682008-03-11 03:23:40 +00001544 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1545 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001546 if (VA.getLocReg() == X86::ST0 ||
1547 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001548 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1549 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001550 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001552 RetOps.push_back(ValToCopy);
1553 // Don't emit a copytoreg.
1554 continue;
1555 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001556
Evan Cheng242b38b2009-02-23 09:03:22 +00001557 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1558 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001559 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001560 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001561 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001562 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001563 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1564 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001565 // If we don't have SSE2 available, convert to v4f32 so the generated
1566 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001567 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001569 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001570 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001571 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001572
Dale Johannesendd64c412009-02-04 00:33:20 +00001573 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001574 Flag = Chain.getValue(1);
1575 }
Dan Gohman61a92132008-04-21 23:59:07 +00001576
1577 // The x86-64 ABI for returning structs by value requires that we copy
1578 // the sret argument into %rax for the return. We saved the argument into
1579 // a virtual register in the entry block, so now we copy the value out
1580 // and into %rax.
1581 if (Subtarget->is64Bit() &&
1582 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1583 MachineFunction &MF = DAG.getMachineFunction();
1584 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1585 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001586 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001587 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001588 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001589
Dale Johannesendd64c412009-02-04 00:33:20 +00001590 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001591 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001592
1593 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001594 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001595 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001596
Chris Lattner447ff682008-03-11 03:23:40 +00001597 RetOps[0] = Chain; // Update chain.
1598
1599 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001600 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001601 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001602
1603 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001604 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001605}
1606
Evan Chengbf010eb2012-04-10 01:51:00 +00001607bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001608 if (N->getNumValues() != 1)
1609 return false;
1610 if (!N->hasNUsesOfValue(1, 0))
1611 return false;
1612
Evan Chengbf010eb2012-04-10 01:51:00 +00001613 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001614 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001615 if (Copy->getOpcode() == ISD::CopyToReg) {
1616 // If the copy has a glue operand, we conservatively assume it isn't safe to
1617 // perform a tail call.
1618 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1619 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001620 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001621 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001622 return false;
1623
Evan Cheng1bf891a2010-12-01 22:59:46 +00001624 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001625 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001626 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001627 if (UI->getOpcode() != X86ISD::RET_FLAG)
1628 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001629 HasRet = true;
1630 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001631
Evan Chengbf010eb2012-04-10 01:51:00 +00001632 if (!HasRet)
1633 return false;
1634
1635 Chain = TCChain;
1636 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001637}
1638
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001639EVT
1640X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001641 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001642 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001643 // TODO: Is this also valid on 32-bit?
1644 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001645 ReturnMVT = MVT::i8;
1646 else
1647 ReturnMVT = MVT::i32;
1648
1649 EVT MinVT = getRegisterType(Context, ReturnMVT);
1650 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001651}
1652
Dan Gohman98ca4f22009-08-05 01:29:28 +00001653/// LowerCallResult - Lower the result values of a call into the
1654/// appropriate copies out of appropriate physical registers.
1655///
1656SDValue
1657X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001658 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 const SmallVectorImpl<ISD::InputArg> &Ins,
1660 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001661 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001662
Chris Lattnere32bbf62007-02-28 07:09:55 +00001663 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001664 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001665 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001666 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001667 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001668 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001669
Chris Lattner3085e152007-02-25 08:59:22 +00001670 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001671 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001672 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001673 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001674
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001677 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001678 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001679 }
1680
Evan Cheng79fb3b42009-02-20 20:43:02 +00001681 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001682
1683 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001684 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001685 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001686 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001687 // instead.
1688 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1689 // If we prefer to use the value in xmm registers, copy it out as f80 and
1690 // use a truncate to move it from fp stack reg to xmm reg.
1691 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001692 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001693 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1694 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001695 Val = Chain.getValue(0);
1696
1697 // Round the f80 to the right size, which also moves it to the appropriate
1698 // xmm register.
1699 if (CopyVT != VA.getValVT())
1700 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1701 // This truncation won't change the value.
1702 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001703 } else {
1704 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1705 CopyVT, InFlag).getValue(1);
1706 Val = Chain.getValue(0);
1707 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001708 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001709 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001710 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001711
Dan Gohman98ca4f22009-08-05 01:29:28 +00001712 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001713}
1714
1715
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001716//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001717// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001718//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001719// StdCall calling convention seems to be standard for many Windows' API
1720// routines and around. It differs from C calling convention just a little:
1721// callee should clean up the stack, not caller. Symbols should be also
1722// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001723// For info on fast calling convention see Fast Calling Convention (tail call)
1724// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001725
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001727/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001728enum StructReturnType {
1729 NotStructReturn,
1730 RegStructReturn,
1731 StackStructReturn
1732};
1733static StructReturnType
1734callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001736 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001737
Rafael Espindola1cee7102012-07-25 13:41:10 +00001738 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1739 if (!Flags.isSRet())
1740 return NotStructReturn;
1741 if (Flags.isInReg())
1742 return RegStructReturn;
1743 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001744}
1745
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001746/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001747/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001748static StructReturnType
1749argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001751 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001752
Rafael Espindola1cee7102012-07-25 13:41:10 +00001753 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1754 if (!Flags.isSRet())
1755 return NotStructReturn;
1756 if (Flags.isInReg())
1757 return RegStructReturn;
1758 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001759}
1760
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001761/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1762/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001763/// the specific parameter attribute. The copy will be passed as a byval
1764/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001765static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001766CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001767 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1768 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001769 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001770
Dale Johannesendd64c412009-02-04 00:33:20 +00001771 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001772 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001773 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001774}
1775
Chris Lattner29689432010-03-11 00:22:57 +00001776/// IsTailCallConvention - Return true if the calling convention is one that
1777/// supports tail call optimization.
1778static bool IsTailCallConvention(CallingConv::ID CC) {
1779 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1780}
1781
Evan Cheng485fafc2011-03-21 01:19:09 +00001782bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001783 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001784 return false;
1785
1786 CallSite CS(CI);
1787 CallingConv::ID CalleeCC = CS.getCallingConv();
1788 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1789 return false;
1790
1791 return true;
1792}
1793
Evan Cheng0c439eb2010-01-27 00:07:07 +00001794/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1795/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001796static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1797 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001798 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001799}
1800
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801SDValue
1802X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001803 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001804 const SmallVectorImpl<ISD::InputArg> &Ins,
1805 DebugLoc dl, SelectionDAG &DAG,
1806 const CCValAssign &VA,
1807 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001808 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001809 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001810 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001811 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1812 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001813 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001814 EVT ValVT;
1815
1816 // If value is passed by pointer we have address passed instead of the value
1817 // itself.
1818 if (VA.getLocInfo() == CCValAssign::Indirect)
1819 ValVT = VA.getLocVT();
1820 else
1821 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001822
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001823 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001824 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001825 // In case of tail call optimization mark all arguments mutable. Since they
1826 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001827 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001828 unsigned Bytes = Flags.getByValSize();
1829 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1830 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001831 return DAG.getFrameIndex(FI, getPointerTy());
1832 } else {
1833 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001834 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001835 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1836 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001837 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001838 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001839 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001840}
1841
Dan Gohman475871a2008-07-27 21:46:04 +00001842SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001844 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 bool isVarArg,
1846 const SmallVectorImpl<ISD::InputArg> &Ins,
1847 DebugLoc dl,
1848 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001849 SmallVectorImpl<SDValue> &InVals)
1850 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001851 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001853
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 const Function* Fn = MF.getFunction();
1855 if (Fn->hasExternalLinkage() &&
1856 Subtarget->isTargetCygMing() &&
1857 Fn->getName() == "main")
1858 FuncInfo->setForceFramePointer(true);
1859
Evan Cheng1bc78042006-04-26 01:20:17 +00001860 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001862 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001863 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001864
Chris Lattner29689432010-03-11 00:22:57 +00001865 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1866 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001867
Chris Lattner638402b2007-02-28 07:00:42 +00001868 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001869 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001870 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001871 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001872
1873 // Allocate shadow area for Win64
1874 if (IsWin64) {
1875 CCInfo.AllocateStack(32, 8);
1876 }
1877
Duncan Sands45907662010-10-31 13:21:44 +00001878 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001879
Chris Lattnerf39f7712007-02-28 05:46:49 +00001880 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001881 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001882 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1883 CCValAssign &VA = ArgLocs[i];
1884 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1885 // places.
1886 assert(VA.getValNo() != LastVal &&
1887 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001888 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001889 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001890
Chris Lattnerf39f7712007-02-28 05:46:49 +00001891 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001892 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001893 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001895 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001897 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001899 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001901 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001902 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001903 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001904 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001905 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001906 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001907 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001908 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001909 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001910
Devang Patel68e6bee2011-02-21 23:21:26 +00001911 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001912 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001913
Chris Lattnerf39f7712007-02-28 05:46:49 +00001914 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1915 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1916 // right size.
1917 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001918 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001919 DAG.getValueType(VA.getValVT()));
1920 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001921 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001922 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001923 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001924 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001925
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001926 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001927 // Handle MMX values passed in XMM regs.
1928 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001929 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1930 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001931 } else
1932 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001933 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001934 } else {
1935 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001936 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001937 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001938
1939 // If value is passed via pointer - do a load.
1940 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001941 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001942 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001943
Dan Gohman98ca4f22009-08-05 01:29:28 +00001944 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001945 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001946
Dan Gohman61a92132008-04-21 23:59:07 +00001947 // The x86-64 ABI for returning structs by value requires that we copy
1948 // the sret argument into %rax for the return. Save the argument into
1949 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001950 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001951 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1952 unsigned Reg = FuncInfo->getSRetReturnReg();
1953 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001955 FuncInfo->setSRetReturnReg(Reg);
1956 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001957 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001958 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001959 }
1960
Chris Lattnerf39f7712007-02-28 05:46:49 +00001961 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001962 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001963 if (FuncIsMadeTailCallSafe(CallConv,
1964 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001965 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001966
Evan Cheng1bc78042006-04-26 01:20:17 +00001967 // If the function takes variable number of arguments, make a frame index for
1968 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001969 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001970 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1971 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001972 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001973 }
1974 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001975 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1976
1977 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001978 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001979 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001980 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001981 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001982 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1983 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001984 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001985 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1986 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1987 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001988 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001989 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001990
1991 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001992 // The XMM registers which might contain var arg parameters are shadowed
1993 // in their paired GPR. So we only need to save the GPR to their home
1994 // slots.
1995 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001996 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001997 } else {
1998 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1999 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002000
Chad Rosier30450e82011-12-22 22:35:21 +00002001 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2002 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002003 }
2004 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2005 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002006
Devang Patel578efa92009-06-05 21:57:13 +00002007 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002008 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002009 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002010 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2011 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002012 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002013 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002014 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002015 // Kernel mode asks for SSE to be disabled, so don't push them
2016 // on the stack.
2017 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002018
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002019 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002020 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002021 // Get to the caller-allocated home save location. Add 8 to account
2022 // for the return address.
2023 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002024 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002025 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002026 // Fixup to set vararg frame on shadow area (4 x i64).
2027 if (NumIntRegs < 4)
2028 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002029 } else {
2030 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002031 // registers, then we must store them to their spots on the stack so
2032 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002033 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2034 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2035 FuncInfo->setRegSaveFrameIndex(
2036 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002037 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002038 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002039
Gordon Henriksen86737662008-01-05 16:56:59 +00002040 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002042 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2043 getPointerTy());
2044 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002045 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002046 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2047 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002048 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002049 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002051 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002052 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002053 MachinePointerInfo::getFixedStack(
2054 FuncInfo->getRegSaveFrameIndex(), Offset),
2055 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002057 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002058 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002059
Dan Gohmanface41a2009-08-16 21:24:25 +00002060 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2061 // Now store the XMM (fp + vector) parameter registers.
2062 SmallVector<SDValue, 11> SaveXMMOps;
2063 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002064
Craig Topperc9099502012-04-20 06:31:50 +00002065 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002066 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2067 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002068
Dan Gohman1e93df62010-04-17 14:41:14 +00002069 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2070 FuncInfo->getRegSaveFrameIndex()));
2071 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2072 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002073
Dan Gohmanface41a2009-08-16 21:24:25 +00002074 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002075 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002076 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002077 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2078 SaveXMMOps.push_back(Val);
2079 }
2080 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2081 MVT::Other,
2082 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002083 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002084
2085 if (!MemOps.empty())
2086 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2087 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002088 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002089 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002090
Gordon Henriksen86737662008-01-05 16:56:59 +00002091 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002092 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2093 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002094 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002095 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002096 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002097 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002098 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002099 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002100 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002101 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002102
Gordon Henriksen86737662008-01-05 16:56:59 +00002103 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002104 // RegSaveFrameIndex is X86-64 only.
2105 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002106 if (CallConv == CallingConv::X86_FastCall ||
2107 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002108 // fastcc functions can't have varargs.
2109 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002110 }
Evan Cheng25caf632006-05-23 21:06:34 +00002111
Rafael Espindola76927d752011-08-30 19:39:58 +00002112 FuncInfo->setArgumentStackSize(StackSize);
2113
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002115}
2116
Dan Gohman475871a2008-07-27 21:46:04 +00002117SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002118X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2119 SDValue StackPtr, SDValue Arg,
2120 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002121 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002122 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002123 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002125 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002126 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002127 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002128
2129 return DAG.getStore(Chain, dl, Arg, PtrOff,
2130 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002131 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002132}
2133
Bill Wendling64e87322009-01-16 19:25:27 +00002134/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002135/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002136SDValue
2137X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002138 SDValue &OutRetAddr, SDValue Chain,
2139 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002140 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002141 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002142 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002144
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002145 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002146 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002147 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002148 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002149}
2150
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002151/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002152/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002153static SDValue
2154EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002156 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002157 // Store the return address to the appropriate stack slot.
2158 if (!FPDiff) return Chain;
2159 // Calculate the new stack slot for the return address.
2160 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002161 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002162 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002164 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002165 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002166 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002167 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002168 return Chain;
2169}
2170
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002172X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002173 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002174 SelectionDAG &DAG = CLI.DAG;
2175 DebugLoc &dl = CLI.DL;
2176 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2177 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2178 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2179 SDValue Chain = CLI.Chain;
2180 SDValue Callee = CLI.Callee;
2181 CallingConv::ID CallConv = CLI.CallConv;
2182 bool &isTailCall = CLI.IsTailCall;
2183 bool isVarArg = CLI.IsVarArg;
2184
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 MachineFunction &MF = DAG.getMachineFunction();
2186 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002187 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002188 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002189 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002190 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191
Nick Lewycky22de16d2012-01-19 00:34:10 +00002192 if (MF.getTarget().Options.DisableTailCalls)
2193 isTailCall = false;
2194
Evan Cheng5f941932010-02-05 02:21:12 +00002195 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002196 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002197 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002198 isVarArg, SR != NotStructReturn,
2199 MF.getFunction()->hasStructRetAttr(),
2200 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002201
2202 // Sibcalls are automatically detected tailcalls which do not require
2203 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002204 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002205 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002206
2207 if (isTailCall)
2208 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002209 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002210
Chris Lattner29689432010-03-11 00:22:57 +00002211 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2212 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002213
Chris Lattner638402b2007-02-28 07:00:42 +00002214 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002215 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002216 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002217 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002218
2219 // Allocate shadow area for Win64
2220 if (IsWin64) {
2221 CCInfo.AllocateStack(32, 8);
2222 }
2223
Duncan Sands45907662010-10-31 13:21:44 +00002224 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002225
Chris Lattner423c5f42007-02-28 05:31:48 +00002226 // Get a count of how many bytes are to be pushed on the stack.
2227 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002228 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002229 // This is a sibcall. The memory operands are available in caller's
2230 // own caller's stack.
2231 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002232 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2233 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002234 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002235
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002237 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002239 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002240 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2241 FPDiff = NumBytesCallerPushed - NumBytes;
2242
2243 // Set the delta of movement of the returnaddr stackslot.
2244 // But only set if delta is greater than previous delta.
2245 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2246 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2247 }
2248
Evan Chengf22f9b32010-02-06 03:28:46 +00002249 if (!IsSibcall)
2250 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002251
Dan Gohman475871a2008-07-27 21:46:04 +00002252 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002253 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002254 if (isTailCall && FPDiff)
2255 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2256 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002257
Dan Gohman475871a2008-07-27 21:46:04 +00002258 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2259 SmallVector<SDValue, 8> MemOpChains;
2260 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002261
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002262 // Walk the register/memloc assignments, inserting copies/loads. In the case
2263 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002264 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2265 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002266 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002267 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002268 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002269 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002270
Chris Lattner423c5f42007-02-28 05:31:48 +00002271 // Promote the value if needed.
2272 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002273 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002274 case CCValAssign::Full: break;
2275 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002276 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002277 break;
2278 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002279 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002280 break;
2281 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002282 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002283 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002284 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2286 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002287 } else
2288 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2289 break;
2290 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002291 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002292 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002293 case CCValAssign::Indirect: {
2294 // Store the argument.
2295 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002296 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002297 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002298 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002299 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002300 Arg = SpillSlot;
2301 break;
2302 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002303 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002304
Chris Lattner423c5f42007-02-28 05:31:48 +00002305 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002306 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2307 if (isVarArg && IsWin64) {
2308 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2309 // shadow reg if callee is a varargs function.
2310 unsigned ShadowReg = 0;
2311 switch (VA.getLocReg()) {
2312 case X86::XMM0: ShadowReg = X86::RCX; break;
2313 case X86::XMM1: ShadowReg = X86::RDX; break;
2314 case X86::XMM2: ShadowReg = X86::R8; break;
2315 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002316 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002317 if (ShadowReg)
2318 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002319 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002320 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002321 assert(VA.isMemLoc());
2322 if (StackPtr.getNode() == 0)
2323 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2324 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2325 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002326 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002327 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002328
Evan Cheng32fe1032006-05-25 00:59:30 +00002329 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002331 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002332
Chris Lattner88e1fd52009-07-09 04:24:46 +00002333 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002334 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2335 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002337 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2338 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002339 } else {
2340 // If we are tail calling and generating PIC/GOT style code load the
2341 // address of the callee into ECX. The value in ecx is used as target of
2342 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2343 // for tail calls on PIC/GOT architectures. Normally we would just put the
2344 // address of GOT into ebx and then call target@PLT. But for tail calls
2345 // ebx would be restored (since ebx is callee saved) before jumping to the
2346 // target@PLT.
2347
2348 // Note: The actual moving to ECX is done further down.
2349 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2350 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2351 !G->getGlobal()->hasProtectedVisibility())
2352 Callee = LowerGlobalAddress(Callee, DAG);
2353 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002354 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002355 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002356 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002357
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002358 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002359 // From AMD64 ABI document:
2360 // For calls that may call functions that use varargs or stdargs
2361 // (prototype-less calls or calls to functions containing ellipsis (...) in
2362 // the declaration) %al is used as hidden argument to specify the number
2363 // of SSE registers used. The contents of %al do not need to match exactly
2364 // the number of registers, but must be an ubound on the number of SSE
2365 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002366
Gordon Henriksen86737662008-01-05 16:56:59 +00002367 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002368 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2370 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2371 };
2372 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002373 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002374 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002375
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002376 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2377 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 }
2379
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002380 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002381 if (isTailCall) {
2382 // Force all the incoming stack arguments to be loaded from the stack
2383 // before any new outgoing arguments are stored to the stack, because the
2384 // outgoing stack slots may alias the incoming argument stack slots, and
2385 // the alias isn't otherwise explicit. This is slightly more conservative
2386 // than necessary, because it means that each store effectively depends
2387 // on every argument instead of just those arguments it would clobber.
2388 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2389
Dan Gohman475871a2008-07-27 21:46:04 +00002390 SmallVector<SDValue, 8> MemOpChains2;
2391 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002392 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002393 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002394 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2395 CCValAssign &VA = ArgLocs[i];
2396 if (VA.isRegLoc())
2397 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002398 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002399 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002400 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002401 // Create frame index.
2402 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002403 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002404 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002405 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002406
Duncan Sands276dcbd2008-03-21 09:14:45 +00002407 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002408 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002409 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002410 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002411 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002412 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002413 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002414
Dan Gohman98ca4f22009-08-05 01:29:28 +00002415 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2416 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002417 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002418 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002419 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002420 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002421 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002422 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002423 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002424 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002425 }
2426 }
2427
2428 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002429 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002430 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002431
2432 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002433 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002434 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002435 }
2436
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002437 // Build a sequence of copy-to-reg nodes chained together with token chain
2438 // and flag operands which copy the outgoing args into registers.
2439 SDValue InFlag;
2440 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2441 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2442 RegsToPass[i].second, InFlag);
2443 InFlag = Chain.getValue(1);
2444 }
2445
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002446 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2447 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2448 // In the 64-bit large code model, we have to make all calls
2449 // through a register, since the call instruction's 32-bit
2450 // pc-relative offset may not be large enough to hold the whole
2451 // address.
2452 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002453 // If the callee is a GlobalAddress node (quite common, every direct call
2454 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2455 // it.
2456
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002457 // We should use extra load for direct calls to dllimported functions in
2458 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002459 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002460 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002461 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002462 bool ExtraLoad = false;
2463 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002464
Chris Lattner48a7d022009-07-09 05:02:21 +00002465 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2466 // external symbols most go through the PLT in PIC mode. If the symbol
2467 // has hidden or protected visibility, or if it is static or local, then
2468 // we don't need to use the PLT - we can directly call it.
2469 if (Subtarget->isTargetELF() &&
2470 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002471 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002472 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002473 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002474 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002475 (!Subtarget->getTargetTriple().isMacOSX() ||
2476 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002477 // PC-relative references to external symbols should go through $stub,
2478 // unless we're building with the leopard linker or later, which
2479 // automatically synthesizes these stubs.
2480 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002481 } else if (Subtarget->isPICStyleRIPRel() &&
2482 isa<Function>(GV) &&
2483 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2484 // If the function is marked as non-lazy, generate an indirect call
2485 // which loads from the GOT directly. This avoids runtime overhead
2486 // at the cost of eager binding (and one extra byte of encoding).
2487 OpFlags = X86II::MO_GOTPCREL;
2488 WrapperKind = X86ISD::WrapperRIP;
2489 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002490 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002491
Devang Patel0d881da2010-07-06 22:08:15 +00002492 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002493 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002494
2495 // Add a wrapper if needed.
2496 if (WrapperKind != ISD::DELETED_NODE)
2497 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2498 // Add extra indirection if needed.
2499 if (ExtraLoad)
2500 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2501 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002502 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002503 }
Bill Wendling056292f2008-09-16 21:48:12 +00002504 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002505 unsigned char OpFlags = 0;
2506
Evan Cheng1bf891a2010-12-01 22:59:46 +00002507 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2508 // external symbols should go through the PLT.
2509 if (Subtarget->isTargetELF() &&
2510 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2511 OpFlags = X86II::MO_PLT;
2512 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002513 (!Subtarget->getTargetTriple().isMacOSX() ||
2514 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002515 // PC-relative references to external symbols should go through $stub,
2516 // unless we're building with the leopard linker or later, which
2517 // automatically synthesizes these stubs.
2518 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002519 }
Eric Christopherfd179292009-08-27 18:07:15 +00002520
Chris Lattner48a7d022009-07-09 05:02:21 +00002521 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2522 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002523 }
2524
Chris Lattnerd96d0722007-02-25 06:40:16 +00002525 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002526 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002527 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002528
Evan Chengf22f9b32010-02-06 03:28:46 +00002529 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002530 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2531 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002532 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002533 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002534
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002535 Ops.push_back(Chain);
2536 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002537
Dan Gohman98ca4f22009-08-05 01:29:28 +00002538 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002539 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002540
Gordon Henriksen86737662008-01-05 16:56:59 +00002541 // Add argument registers to the end of the list so that they are known live
2542 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002543 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2544 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2545 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002546
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002547 // Add a register mask operand representing the call-preserved registers.
2548 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2549 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2550 assert(Mask && "Missing call preserved mask for calling convention");
2551 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002552
Gabor Greifba36cb52008-08-28 21:40:38 +00002553 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002554 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002555
Dan Gohman98ca4f22009-08-05 01:29:28 +00002556 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002557 // We used to do:
2558 //// If this is the first return lowered for this function, add the regs
2559 //// to the liveout set for the function.
2560 // This isn't right, although it's probably harmless on x86; liveouts
2561 // should be computed from returns not tail calls. Consider a void
2562 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002563 return DAG.getNode(X86ISD::TC_RETURN, dl,
2564 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002565 }
2566
Dale Johannesenace16102009-02-03 19:33:06 +00002567 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002568 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002569
Chris Lattner2d297092006-05-23 18:50:38 +00002570 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002571 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002572 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2573 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002574 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002575 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002576 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002577 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002578 // pops the hidden struct pointer, so we have to push it back.
2579 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002580 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002581 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002582 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002583 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002584
Gordon Henriksenae636f82008-01-03 16:47:34 +00002585 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002586 if (!IsSibcall) {
2587 Chain = DAG.getCALLSEQ_END(Chain,
2588 DAG.getIntPtrConstant(NumBytes, true),
2589 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2590 true),
2591 InFlag);
2592 InFlag = Chain.getValue(1);
2593 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002594
Chris Lattner3085e152007-02-25 08:59:22 +00002595 // Handle result values, copying them out of physregs into vregs that we
2596 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002597 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2598 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002599}
2600
Evan Cheng25ab6902006-09-08 06:48:29 +00002601
2602//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002603// Fast Calling Convention (tail call) implementation
2604//===----------------------------------------------------------------------===//
2605
2606// Like std call, callee cleans arguments, convention except that ECX is
2607// reserved for storing the tail called function address. Only 2 registers are
2608// free for argument passing (inreg). Tail call optimization is performed
2609// provided:
2610// * tailcallopt is enabled
2611// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002612// On X86_64 architecture with GOT-style position independent code only local
2613// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002614// To keep the stack aligned according to platform abi the function
2615// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2616// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002617// If a tail called function callee has more arguments than the caller the
2618// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002619// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002620// original REtADDR, but before the saved framepointer or the spilled registers
2621// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2622// stack layout:
2623// arg1
2624// arg2
2625// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002626// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002627// move area ]
2628// (possible EBP)
2629// ESI
2630// EDI
2631// local1 ..
2632
2633/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2634/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002635unsigned
2636X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2637 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002638 MachineFunction &MF = DAG.getMachineFunction();
2639 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002640 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002641 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002642 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002643 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002644 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002645 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2646 // Number smaller than 12 so just add the difference.
2647 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2648 } else {
2649 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002650 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002651 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002652 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002653 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002654}
2655
Evan Cheng5f941932010-02-05 02:21:12 +00002656/// MatchingStackOffset - Return true if the given stack call argument is
2657/// already available in the same position (relatively) of the caller's
2658/// incoming argument stack.
2659static
2660bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2661 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2662 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002663 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2664 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002665 if (Arg.getOpcode() == ISD::CopyFromReg) {
2666 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002667 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002668 return false;
2669 MachineInstr *Def = MRI->getVRegDef(VR);
2670 if (!Def)
2671 return false;
2672 if (!Flags.isByVal()) {
2673 if (!TII->isLoadFromStackSlot(Def, FI))
2674 return false;
2675 } else {
2676 unsigned Opcode = Def->getOpcode();
2677 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2678 Def->getOperand(1).isFI()) {
2679 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002680 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002681 } else
2682 return false;
2683 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002684 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2685 if (Flags.isByVal())
2686 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002687 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002688 // define @foo(%struct.X* %A) {
2689 // tail call @bar(%struct.X* byval %A)
2690 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002691 return false;
2692 SDValue Ptr = Ld->getBasePtr();
2693 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2694 if (!FINode)
2695 return false;
2696 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002697 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002698 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002699 FI = FINode->getIndex();
2700 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002701 } else
2702 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002703
Evan Cheng4cae1332010-03-05 08:38:04 +00002704 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002705 if (!MFI->isFixedObjectIndex(FI))
2706 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002707 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002708}
2709
Dan Gohman98ca4f22009-08-05 01:29:28 +00002710/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2711/// for tail call optimization. Targets which want to do tail call
2712/// optimization should implement this function.
2713bool
2714X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002715 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002716 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002717 bool isCalleeStructRet,
2718 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002719 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002720 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002721 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002722 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002723 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002724 CalleeCC != CallingConv::C)
2725 return false;
2726
Evan Cheng7096ae42010-01-29 06:45:59 +00002727 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002728 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002729 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002730 CallingConv::ID CallerCC = CallerF->getCallingConv();
2731 bool CCMatch = CallerCC == CalleeCC;
2732
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002733 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002734 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002735 return true;
2736 return false;
2737 }
2738
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002739 // Look for obvious safe cases to perform tail call optimization that do not
2740 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002741
Evan Cheng2c12cb42010-03-26 16:26:03 +00002742 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2743 // emit a special epilogue.
2744 if (RegInfo->needsStackRealignment(MF))
2745 return false;
2746
Evan Chenga375d472010-03-15 18:54:48 +00002747 // Also avoid sibcall optimization if either caller or callee uses struct
2748 // return semantics.
2749 if (isCalleeStructRet || isCallerStructRet)
2750 return false;
2751
Chad Rosier2416da32011-06-24 21:15:36 +00002752 // An stdcall caller is expected to clean up its arguments; the callee
2753 // isn't going to do that.
2754 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2755 return false;
2756
Chad Rosier871f6642011-05-18 19:59:50 +00002757 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002758 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002759 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002760
2761 // Optimizing for varargs on Win64 is unlikely to be safe without
2762 // additional testing.
2763 if (Subtarget->isTargetWin64())
2764 return false;
2765
Chad Rosier871f6642011-05-18 19:59:50 +00002766 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002767 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002768 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002769
Chad Rosier871f6642011-05-18 19:59:50 +00002770 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2771 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2772 if (!ArgLocs[i].isRegLoc())
2773 return false;
2774 }
2775
Chad Rosier30450e82011-12-22 22:35:21 +00002776 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2777 // stack. Therefore, if it's not used by the call it is not safe to optimize
2778 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002779 bool Unused = false;
2780 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2781 if (!Ins[i].Used) {
2782 Unused = true;
2783 break;
2784 }
2785 }
2786 if (Unused) {
2787 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002788 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002789 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002790 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002791 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002792 CCValAssign &VA = RVLocs[i];
2793 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2794 return false;
2795 }
2796 }
2797
Evan Cheng13617962010-04-30 01:12:32 +00002798 // If the calling conventions do not match, then we'd better make sure the
2799 // results are returned in the same way as what the caller expects.
2800 if (!CCMatch) {
2801 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002802 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002803 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002804 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2805
2806 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002807 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002808 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002809 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2810
2811 if (RVLocs1.size() != RVLocs2.size())
2812 return false;
2813 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2814 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2815 return false;
2816 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2817 return false;
2818 if (RVLocs1[i].isRegLoc()) {
2819 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2820 return false;
2821 } else {
2822 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2823 return false;
2824 }
2825 }
2826 }
2827
Evan Chenga6bff982010-01-30 01:22:00 +00002828 // If the callee takes no arguments then go on to check the results of the
2829 // call.
2830 if (!Outs.empty()) {
2831 // Check if stack adjustment is needed. For now, do not do this if any
2832 // argument is passed on the stack.
2833 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002834 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002835 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002836
2837 // Allocate shadow area for Win64
2838 if (Subtarget->isTargetWin64()) {
2839 CCInfo.AllocateStack(32, 8);
2840 }
2841
Duncan Sands45907662010-10-31 13:21:44 +00002842 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002843 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002844 MachineFunction &MF = DAG.getMachineFunction();
2845 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2846 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002847
2848 // Check if the arguments are already laid out in the right way as
2849 // the caller's fixed stack objects.
2850 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002851 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2852 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002853 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002854 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2855 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002856 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002857 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002858 if (VA.getLocInfo() == CCValAssign::Indirect)
2859 return false;
2860 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002861 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2862 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002863 return false;
2864 }
2865 }
2866 }
Evan Cheng9c044672010-05-29 01:35:22 +00002867
2868 // If the tailcall address may be in a register, then make sure it's
2869 // possible to register allocate for it. In 32-bit, the call address can
2870 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002871 // callee-saved registers are restored. These happen to be the same
2872 // registers used to pass 'inreg' arguments so watch out for those.
2873 if (!Subtarget->is64Bit() &&
2874 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002875 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002876 unsigned NumInRegs = 0;
2877 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2878 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002879 if (!VA.isRegLoc())
2880 continue;
2881 unsigned Reg = VA.getLocReg();
2882 switch (Reg) {
2883 default: break;
2884 case X86::EAX: case X86::EDX: case X86::ECX:
2885 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002886 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002887 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002888 }
2889 }
2890 }
Evan Chenga6bff982010-01-30 01:22:00 +00002891 }
Evan Chengb1712452010-01-27 06:25:16 +00002892
Evan Cheng86809cc2010-02-03 03:28:02 +00002893 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002894}
2895
Dan Gohman3df24e62008-09-03 23:12:08 +00002896FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002897X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2898 const TargetLibraryInfo *libInfo) const {
2899 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002900}
2901
2902
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002903//===----------------------------------------------------------------------===//
2904// Other Lowering Hooks
2905//===----------------------------------------------------------------------===//
2906
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002907static bool MayFoldLoad(SDValue Op) {
2908 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2909}
2910
2911static bool MayFoldIntoStore(SDValue Op) {
2912 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2913}
2914
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002915static bool isTargetShuffle(unsigned Opcode) {
2916 switch(Opcode) {
2917 default: return false;
2918 case X86ISD::PSHUFD:
2919 case X86ISD::PSHUFHW:
2920 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002921 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002922 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002923 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002924 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002925 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002926 case X86ISD::MOVLPS:
2927 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002928 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002929 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002930 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002931 case X86ISD::MOVSS:
2932 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002933 case X86ISD::UNPCKL:
2934 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002935 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002936 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002937 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002938 return true;
2939 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002940}
2941
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002942static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002943 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002944 switch(Opc) {
2945 default: llvm_unreachable("Unknown x86 shuffle node");
2946 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002947 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002948 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002949 return DAG.getNode(Opc, dl, VT, V1);
2950 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002951}
2952
2953static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002954 SDValue V1, unsigned TargetMask,
2955 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002956 switch(Opc) {
2957 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002958 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002959 case X86ISD::PSHUFHW:
2960 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002961 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002962 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002963 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2964 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002965}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002966
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002967static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002968 SDValue V1, SDValue V2, unsigned TargetMask,
2969 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002970 switch(Opc) {
2971 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002972 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002973 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002974 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002975 return DAG.getNode(Opc, dl, VT, V1, V2,
2976 DAG.getConstant(TargetMask, MVT::i8));
2977 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002978}
2979
2980static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2981 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2982 switch(Opc) {
2983 default: llvm_unreachable("Unknown x86 shuffle node");
2984 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002985 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002986 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002987 case X86ISD::MOVLPS:
2988 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002989 case X86ISD::MOVSS:
2990 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002991 case X86ISD::UNPCKL:
2992 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002993 return DAG.getNode(Opc, dl, VT, V1, V2);
2994 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002995}
2996
Dan Gohmand858e902010-04-17 15:26:15 +00002997SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002998 MachineFunction &MF = DAG.getMachineFunction();
2999 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3000 int ReturnAddrIndex = FuncInfo->getRAIndex();
3001
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003002 if (ReturnAddrIndex == 0) {
3003 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00003004 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00003005 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003006 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003007 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003008 }
3009
Evan Cheng25ab6902006-09-08 06:48:29 +00003010 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003011}
3012
3013
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003014bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3015 bool hasSymbolicDisplacement) {
3016 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003017 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003018 return false;
3019
3020 // If we don't have a symbolic displacement - we don't have any extra
3021 // restrictions.
3022 if (!hasSymbolicDisplacement)
3023 return true;
3024
3025 // FIXME: Some tweaks might be needed for medium code model.
3026 if (M != CodeModel::Small && M != CodeModel::Kernel)
3027 return false;
3028
3029 // For small code model we assume that latest object is 16MB before end of 31
3030 // bits boundary. We may also accept pretty large negative constants knowing
3031 // that all objects are in the positive half of address space.
3032 if (M == CodeModel::Small && Offset < 16*1024*1024)
3033 return true;
3034
3035 // For kernel code model we know that all object resist in the negative half
3036 // of 32bits address space. We may not accept negative offsets, since they may
3037 // be just off and we may accept pretty large positive ones.
3038 if (M == CodeModel::Kernel && Offset > 0)
3039 return true;
3040
3041 return false;
3042}
3043
Evan Chengef41ff62011-06-23 17:54:54 +00003044/// isCalleePop - Determines whether the callee is required to pop its
3045/// own arguments. Callee pop is necessary to support tail calls.
3046bool X86::isCalleePop(CallingConv::ID CallingConv,
3047 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3048 if (IsVarArg)
3049 return false;
3050
3051 switch (CallingConv) {
3052 default:
3053 return false;
3054 case CallingConv::X86_StdCall:
3055 return !is64Bit;
3056 case CallingConv::X86_FastCall:
3057 return !is64Bit;
3058 case CallingConv::X86_ThisCall:
3059 return !is64Bit;
3060 case CallingConv::Fast:
3061 return TailCallOpt;
3062 case CallingConv::GHC:
3063 return TailCallOpt;
3064 }
3065}
3066
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003067/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3068/// specific condition code, returning the condition code and the LHS/RHS of the
3069/// comparison to make.
3070static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3071 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003072 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003073 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3074 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3075 // X > -1 -> X == 0, jump !sign.
3076 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003077 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003078 }
3079 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003080 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003081 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003082 }
3083 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003084 // X < 1 -> X <= 0
3085 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003086 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003087 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003088 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003089
Evan Chengd9558e02006-01-06 00:43:03 +00003090 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003091 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003092 case ISD::SETEQ: return X86::COND_E;
3093 case ISD::SETGT: return X86::COND_G;
3094 case ISD::SETGE: return X86::COND_GE;
3095 case ISD::SETLT: return X86::COND_L;
3096 case ISD::SETLE: return X86::COND_LE;
3097 case ISD::SETNE: return X86::COND_NE;
3098 case ISD::SETULT: return X86::COND_B;
3099 case ISD::SETUGT: return X86::COND_A;
3100 case ISD::SETULE: return X86::COND_BE;
3101 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003102 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003103 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003104
Chris Lattner4c78e022008-12-23 23:42:27 +00003105 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003106
Chris Lattner4c78e022008-12-23 23:42:27 +00003107 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003108 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3109 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003110 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3111 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003112 }
3113
Chris Lattner4c78e022008-12-23 23:42:27 +00003114 switch (SetCCOpcode) {
3115 default: break;
3116 case ISD::SETOLT:
3117 case ISD::SETOLE:
3118 case ISD::SETUGT:
3119 case ISD::SETUGE:
3120 std::swap(LHS, RHS);
3121 break;
3122 }
3123
3124 // On a floating point condition, the flags are set as follows:
3125 // ZF PF CF op
3126 // 0 | 0 | 0 | X > Y
3127 // 0 | 0 | 1 | X < Y
3128 // 1 | 0 | 0 | X == Y
3129 // 1 | 1 | 1 | unordered
3130 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003131 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003132 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003133 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003134 case ISD::SETOLT: // flipped
3135 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003136 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003137 case ISD::SETOLE: // flipped
3138 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003139 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003140 case ISD::SETUGT: // flipped
3141 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003142 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003143 case ISD::SETUGE: // flipped
3144 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003145 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003146 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003147 case ISD::SETNE: return X86::COND_NE;
3148 case ISD::SETUO: return X86::COND_P;
3149 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003150 case ISD::SETOEQ:
3151 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003152 }
Evan Chengd9558e02006-01-06 00:43:03 +00003153}
3154
Evan Cheng4a460802006-01-11 00:33:36 +00003155/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3156/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003157/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003158static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003159 switch (X86CC) {
3160 default:
3161 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003162 case X86::COND_B:
3163 case X86::COND_BE:
3164 case X86::COND_E:
3165 case X86::COND_P:
3166 case X86::COND_A:
3167 case X86::COND_AE:
3168 case X86::COND_NE:
3169 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003170 return true;
3171 }
3172}
3173
Evan Chengeb2f9692009-10-27 19:56:55 +00003174/// isFPImmLegal - Returns true if the target can instruction select the
3175/// specified FP immediate natively. If false, the legalizer will
3176/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003177bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003178 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3179 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3180 return true;
3181 }
3182 return false;
3183}
3184
Nate Begeman9008ca62009-04-27 18:41:29 +00003185/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3186/// the specified range (L, H].
3187static bool isUndefOrInRange(int Val, int Low, int Hi) {
3188 return (Val < 0) || (Val >= Low && Val < Hi);
3189}
3190
3191/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3192/// specified value.
3193static bool isUndefOrEqual(int Val, int CmpVal) {
3194 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003195 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003197}
3198
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003199/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003200/// from position Pos and ending in Pos+Size, falls within the specified
3201/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003202static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003203 unsigned Pos, unsigned Size, int Low) {
3204 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003205 if (!isUndefOrEqual(Mask[i], Low))
3206 return false;
3207 return true;
3208}
3209
Nate Begeman9008ca62009-04-27 18:41:29 +00003210/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3211/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3212/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003213static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003214 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003216 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 return (Mask[0] < 2 && Mask[1] < 2);
3218 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003219}
3220
Nate Begeman9008ca62009-04-27 18:41:29 +00003221/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3222/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003223static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3224 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003225 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003226
Nate Begeman9008ca62009-04-27 18:41:29 +00003227 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003228 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3229 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003230
Evan Cheng506d3df2006-03-29 23:07:14 +00003231 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003232 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003233 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003234 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003235
Craig Toppera9a568a2012-05-02 08:03:44 +00003236 if (VT == MVT::v16i16) {
3237 // Lower quadword copied in order or undef.
3238 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3239 return false;
3240
3241 // Upper quadword shuffled.
3242 for (unsigned i = 12; i != 16; ++i)
3243 if (!isUndefOrInRange(Mask[i], 12, 16))
3244 return false;
3245 }
3246
Evan Cheng506d3df2006-03-29 23:07:14 +00003247 return true;
3248}
3249
Nate Begeman9008ca62009-04-27 18:41:29 +00003250/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3251/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003252static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3253 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003254 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003255
Rafael Espindola15684b22009-04-24 12:40:33 +00003256 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003257 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3258 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003259
Rafael Espindola15684b22009-04-24 12:40:33 +00003260 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003261 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003262 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003263 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003264
Craig Toppera9a568a2012-05-02 08:03:44 +00003265 if (VT == MVT::v16i16) {
3266 // Upper quadword copied in order.
3267 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3268 return false;
3269
3270 // Lower quadword shuffled.
3271 for (unsigned i = 8; i != 12; ++i)
3272 if (!isUndefOrInRange(Mask[i], 8, 12))
3273 return false;
3274 }
3275
Rafael Espindola15684b22009-04-24 12:40:33 +00003276 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003277}
3278
Nate Begemana09008b2009-10-19 02:17:23 +00003279/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3280/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003281static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3282 const X86Subtarget *Subtarget) {
3283 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3284 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003285 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003286
Craig Topper0e2037b2012-01-20 05:53:00 +00003287 unsigned NumElts = VT.getVectorNumElements();
3288 unsigned NumLanes = VT.getSizeInBits()/128;
3289 unsigned NumLaneElts = NumElts/NumLanes;
3290
3291 // Do not handle 64-bit element shuffles with palignr.
3292 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003293 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003294
Craig Topper0e2037b2012-01-20 05:53:00 +00003295 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3296 unsigned i;
3297 for (i = 0; i != NumLaneElts; ++i) {
3298 if (Mask[i+l] >= 0)
3299 break;
3300 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003301
Craig Topper0e2037b2012-01-20 05:53:00 +00003302 // Lane is all undef, go to next lane
3303 if (i == NumLaneElts)
3304 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003305
Craig Topper0e2037b2012-01-20 05:53:00 +00003306 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003307
Craig Topper0e2037b2012-01-20 05:53:00 +00003308 // Make sure its in this lane in one of the sources
3309 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3310 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003311 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003312
3313 // If not lane 0, then we must match lane 0
3314 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3315 return false;
3316
3317 // Correct second source to be contiguous with first source
3318 if (Start >= (int)NumElts)
3319 Start -= NumElts - NumLaneElts;
3320
3321 // Make sure we're shifting in the right direction.
3322 if (Start <= (int)(i+l))
3323 return false;
3324
3325 Start -= i;
3326
3327 // Check the rest of the elements to see if they are consecutive.
3328 for (++i; i != NumLaneElts; ++i) {
3329 int Idx = Mask[i+l];
3330
3331 // Make sure its in this lane
3332 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3333 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3334 return false;
3335
3336 // If not lane 0, then we must match lane 0
3337 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3338 return false;
3339
3340 if (Idx >= (int)NumElts)
3341 Idx -= NumElts - NumLaneElts;
3342
3343 if (!isUndefOrEqual(Idx, Start+i))
3344 return false;
3345
3346 }
Nate Begemana09008b2009-10-19 02:17:23 +00003347 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003348
Nate Begemana09008b2009-10-19 02:17:23 +00003349 return true;
3350}
3351
Craig Topper1a7700a2012-01-19 08:19:12 +00003352/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3353/// the two vector operands have swapped position.
3354static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3355 unsigned NumElems) {
3356 for (unsigned i = 0; i != NumElems; ++i) {
3357 int idx = Mask[i];
3358 if (idx < 0)
3359 continue;
3360 else if (idx < (int)NumElems)
3361 Mask[i] = idx + NumElems;
3362 else
3363 Mask[i] = idx - NumElems;
3364 }
3365}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003366
Craig Topper1a7700a2012-01-19 08:19:12 +00003367/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3368/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3369/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3370/// reverse of what x86 shuffles want.
3371static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3372 bool Commuted = false) {
3373 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003374 return false;
3375
Craig Topper1a7700a2012-01-19 08:19:12 +00003376 unsigned NumElems = VT.getVectorNumElements();
3377 unsigned NumLanes = VT.getSizeInBits()/128;
3378 unsigned NumLaneElems = NumElems/NumLanes;
3379
3380 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003381 return false;
3382
3383 // VSHUFPSY divides the resulting vector into 4 chunks.
3384 // The sources are also splitted into 4 chunks, and each destination
3385 // chunk must come from a different source chunk.
3386 //
3387 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3388 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3389 //
3390 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3391 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3392 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003393 // VSHUFPDY divides the resulting vector into 4 chunks.
3394 // The sources are also splitted into 4 chunks, and each destination
3395 // chunk must come from a different source chunk.
3396 //
3397 // SRC1 => X3 X2 X1 X0
3398 // SRC2 => Y3 Y2 Y1 Y0
3399 //
3400 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3401 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003402 unsigned HalfLaneElems = NumLaneElems/2;
3403 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3404 for (unsigned i = 0; i != NumLaneElems; ++i) {
3405 int Idx = Mask[i+l];
3406 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3407 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3408 return false;
3409 // For VSHUFPSY, the mask of the second half must be the same as the
3410 // first but with the appropriate offsets. This works in the same way as
3411 // VPERMILPS works with masks.
3412 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3413 continue;
3414 if (!isUndefOrEqual(Idx, Mask[i]+l))
3415 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003416 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003417 }
3418
3419 return true;
3420}
3421
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003422/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3423/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003424static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003425 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003426 return false;
3427
Craig Topper7a9a28b2012-08-12 02:23:29 +00003428 unsigned NumElems = VT.getVectorNumElements();
3429
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003430 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003431 return false;
3432
Evan Cheng2064a2b2006-03-28 06:50:32 +00003433 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003434 return isUndefOrEqual(Mask[0], 6) &&
3435 isUndefOrEqual(Mask[1], 7) &&
3436 isUndefOrEqual(Mask[2], 2) &&
3437 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003438}
3439
Nate Begeman0b10b912009-11-07 23:17:15 +00003440/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3441/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3442/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003443static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003444 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003445 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003446
Craig Topper7a9a28b2012-08-12 02:23:29 +00003447 unsigned NumElems = VT.getVectorNumElements();
3448
Nate Begeman0b10b912009-11-07 23:17:15 +00003449 if (NumElems != 4)
3450 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003451
Craig Topperdd637ae2012-02-19 05:41:45 +00003452 return isUndefOrEqual(Mask[0], 2) &&
3453 isUndefOrEqual(Mask[1], 3) &&
3454 isUndefOrEqual(Mask[2], 2) &&
3455 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003456}
3457
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3459/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003460static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003461 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003462 return false;
3463
Craig Topperdd637ae2012-02-19 05:41:45 +00003464 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003465
Evan Cheng5ced1d82006-04-06 23:23:56 +00003466 if (NumElems != 2 && NumElems != 4)
3467 return false;
3468
Chad Rosier238ae312012-04-30 17:47:15 +00003469 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003470 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003471 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003472
Chad Rosier238ae312012-04-30 17:47:15 +00003473 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003474 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003475 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003476
3477 return true;
3478}
3479
Nate Begeman0b10b912009-11-07 23:17:15 +00003480/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3481/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003482static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003483 if (!VT.is128BitVector())
3484 return false;
3485
Craig Topperdd637ae2012-02-19 05:41:45 +00003486 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003487
Craig Topper7a9a28b2012-08-12 02:23:29 +00003488 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003489 return false;
3490
Chad Rosier238ae312012-04-30 17:47:15 +00003491 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003492 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003493 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003494
Chad Rosier238ae312012-04-30 17:47:15 +00003495 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3496 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003497 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003498
3499 return true;
3500}
3501
Elena Demikhovsky15963732012-06-26 08:04:10 +00003502//
3503// Some special combinations that can be optimized.
3504//
3505static
3506SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3507 SelectionDAG &DAG) {
3508 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003509 DebugLoc dl = SVOp->getDebugLoc();
3510
3511 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3512 return SDValue();
3513
3514 ArrayRef<int> Mask = SVOp->getMask();
3515
3516 // These are the special masks that may be optimized.
3517 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3518 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3519 bool MatchEvenMask = true;
3520 bool MatchOddMask = true;
3521 for (int i=0; i<8; ++i) {
3522 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3523 MatchEvenMask = false;
3524 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3525 MatchOddMask = false;
3526 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003527
Elena Demikhovsky32510202012-09-04 12:49:02 +00003528 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003529 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003530
Elena Demikhovsky15963732012-06-26 08:04:10 +00003531 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3532
Elena Demikhovsky32510202012-09-04 12:49:02 +00003533 SDValue Op0 = SVOp->getOperand(0);
3534 SDValue Op1 = SVOp->getOperand(1);
3535
3536 if (MatchEvenMask) {
3537 // Shift the second operand right to 32 bits.
3538 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3539 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3540 } else {
3541 // Shift the first operand left to 32 bits.
3542 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3543 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3544 }
3545 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3546 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003547}
3548
Evan Cheng0038e592006-03-28 00:39:58 +00003549/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3550/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003551static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003552 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003553 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003554
3555 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3556 "Unsupported vector type for unpckh");
3557
Craig Topper6347e862011-11-21 06:57:39 +00003558 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003559 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003560 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003561
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003562 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3563 // independently on 128-bit lanes.
3564 unsigned NumLanes = VT.getSizeInBits()/128;
3565 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003566
Craig Topper94438ba2011-12-16 08:06:31 +00003567 for (unsigned l = 0; l != NumLanes; ++l) {
3568 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3569 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003570 i += 2, ++j) {
3571 int BitI = Mask[i];
3572 int BitI1 = Mask[i+1];
3573 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003574 return false;
David Greenea20244d2011-03-02 17:23:43 +00003575 if (V2IsSplat) {
3576 if (!isUndefOrEqual(BitI1, NumElts))
3577 return false;
3578 } else {
3579 if (!isUndefOrEqual(BitI1, j + NumElts))
3580 return false;
3581 }
Evan Cheng39623da2006-04-20 08:58:49 +00003582 }
Evan Cheng0038e592006-03-28 00:39:58 +00003583 }
David Greenea20244d2011-03-02 17:23:43 +00003584
Evan Cheng0038e592006-03-28 00:39:58 +00003585 return true;
3586}
3587
Evan Cheng4fcb9222006-03-28 02:43:26 +00003588/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3589/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003590static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003591 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003592 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003593
3594 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3595 "Unsupported vector type for unpckh");
3596
Craig Topper6347e862011-11-21 06:57:39 +00003597 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003598 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003599 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003600
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003601 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3602 // independently on 128-bit lanes.
3603 unsigned NumLanes = VT.getSizeInBits()/128;
3604 unsigned NumLaneElts = NumElts/NumLanes;
3605
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003606 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003607 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3608 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003609 int BitI = Mask[i];
3610 int BitI1 = Mask[i+1];
3611 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003612 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003613 if (V2IsSplat) {
3614 if (isUndefOrEqual(BitI1, NumElts))
3615 return false;
3616 } else {
3617 if (!isUndefOrEqual(BitI1, j+NumElts))
3618 return false;
3619 }
Evan Cheng39623da2006-04-20 08:58:49 +00003620 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003621 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003622 return true;
3623}
3624
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003625/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3626/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3627/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003628static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003629 bool HasAVX2) {
3630 unsigned NumElts = VT.getVectorNumElements();
3631
3632 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3633 "Unsupported vector type for unpckh");
3634
3635 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3636 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003637 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003638
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003639 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3640 // FIXME: Need a better way to get rid of this, there's no latency difference
3641 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3642 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003643 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003644 return false;
3645
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003646 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3647 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003648 unsigned NumLanes = VT.getSizeInBits()/128;
3649 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003650
Craig Topper94438ba2011-12-16 08:06:31 +00003651 for (unsigned l = 0; l != NumLanes; ++l) {
3652 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3653 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003654 i += 2, ++j) {
3655 int BitI = Mask[i];
3656 int BitI1 = Mask[i+1];
3657
3658 if (!isUndefOrEqual(BitI, j))
3659 return false;
3660 if (!isUndefOrEqual(BitI1, j))
3661 return false;
3662 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003663 }
David Greenea20244d2011-03-02 17:23:43 +00003664
Rafael Espindola15684b22009-04-24 12:40:33 +00003665 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003666}
3667
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003668/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3669/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3670/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003671static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003672 unsigned NumElts = VT.getVectorNumElements();
3673
3674 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3675 "Unsupported vector type for unpckh");
3676
3677 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3678 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003679 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003680
Craig Topper94438ba2011-12-16 08:06:31 +00003681 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3682 // independently on 128-bit lanes.
3683 unsigned NumLanes = VT.getSizeInBits()/128;
3684 unsigned NumLaneElts = NumElts/NumLanes;
3685
3686 for (unsigned l = 0; l != NumLanes; ++l) {
3687 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3688 i != (l+1)*NumLaneElts; i += 2, ++j) {
3689 int BitI = Mask[i];
3690 int BitI1 = Mask[i+1];
3691 if (!isUndefOrEqual(BitI, j))
3692 return false;
3693 if (!isUndefOrEqual(BitI1, j))
3694 return false;
3695 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003696 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003697 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003698}
3699
Evan Cheng017dcc62006-04-21 01:05:10 +00003700/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3701/// specifies a shuffle of elements that is suitable for input to MOVSS,
3702/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003703static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003704 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003705 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003706 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003707 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003708
Craig Topperc612d792012-01-02 09:17:37 +00003709 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003710
Nate Begeman9008ca62009-04-27 18:41:29 +00003711 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003712 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003713
Craig Topperc612d792012-01-02 09:17:37 +00003714 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003715 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003716 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003717
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003718 return true;
3719}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003720
Craig Topper70b883b2011-11-28 10:14:51 +00003721/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003722/// as permutations between 128-bit chunks or halves. As an example: this
3723/// shuffle bellow:
3724/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3725/// The first half comes from the second half of V1 and the second half from the
3726/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003727static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003728 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003729 return false;
3730
3731 // The shuffle result is divided into half A and half B. In total the two
3732 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3733 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003734 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003735 bool MatchA = false, MatchB = false;
3736
3737 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003738 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003739 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3740 MatchA = true;
3741 break;
3742 }
3743 }
3744
3745 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003746 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003747 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3748 MatchB = true;
3749 break;
3750 }
3751 }
3752
3753 return MatchA && MatchB;
3754}
3755
Craig Topper70b883b2011-11-28 10:14:51 +00003756/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3757/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003758static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003759 EVT VT = SVOp->getValueType(0);
3760
Craig Topperc612d792012-01-02 09:17:37 +00003761 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003762
Craig Topperc612d792012-01-02 09:17:37 +00003763 unsigned FstHalf = 0, SndHalf = 0;
3764 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003765 if (SVOp->getMaskElt(i) > 0) {
3766 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3767 break;
3768 }
3769 }
Craig Topperc612d792012-01-02 09:17:37 +00003770 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003771 if (SVOp->getMaskElt(i) > 0) {
3772 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3773 break;
3774 }
3775 }
3776
3777 return (FstHalf | (SndHalf << 4));
3778}
3779
Craig Topper70b883b2011-11-28 10:14:51 +00003780/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003781/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3782/// Note that VPERMIL mask matching is different depending whether theunderlying
3783/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3784/// to the same elements of the low, but to the higher half of the source.
3785/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003786/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003787static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003788 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003789 return false;
3790
Craig Topperc612d792012-01-02 09:17:37 +00003791 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003792 // Only match 256-bit with 32/64-bit types
3793 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003794 return false;
3795
Craig Topperc612d792012-01-02 09:17:37 +00003796 unsigned NumLanes = VT.getSizeInBits()/128;
3797 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003798 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003799 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003800 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003801 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003802 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003803 continue;
3804 // VPERMILPS handling
3805 if (Mask[i] < 0)
3806 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003807 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003808 return false;
3809 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003810 }
3811
3812 return true;
3813}
3814
Craig Topper5aaffa82012-02-19 02:53:47 +00003815/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003816/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003817/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003818static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003819 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003820 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003821 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003822
3823 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003824 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003825 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003826
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003828 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003829
Craig Topperc612d792012-01-02 09:17:37 +00003830 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3832 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3833 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003834 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003835
Evan Cheng39623da2006-04-20 08:58:49 +00003836 return true;
3837}
3838
Evan Chengd9539472006-04-14 21:59:03 +00003839/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3840/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003841/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003842static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003843 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003844 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003845 return false;
3846
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003847 unsigned NumElems = VT.getVectorNumElements();
3848
3849 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3850 (VT.getSizeInBits() == 256 && NumElems != 8))
3851 return false;
3852
3853 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003854 for (unsigned i = 0; i != NumElems; i += 2)
3855 if (!isUndefOrEqual(Mask[i], i+1) ||
3856 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003857 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003858
3859 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003860}
3861
3862/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3863/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003864/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003865static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003866 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003867 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003868 return false;
3869
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003870 unsigned NumElems = VT.getVectorNumElements();
3871
3872 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3873 (VT.getSizeInBits() == 256 && NumElems != 8))
3874 return false;
3875
3876 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003877 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003878 if (!isUndefOrEqual(Mask[i], i) ||
3879 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003880 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003881
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003882 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003883}
3884
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003885/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3886/// specifies a shuffle of elements that is suitable for input to 256-bit
3887/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003888static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003889 if (!HasAVX || !VT.is256BitVector())
3890 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003891
Craig Topper7a9a28b2012-08-12 02:23:29 +00003892 unsigned NumElts = VT.getVectorNumElements();
3893 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003894 return false;
3895
Craig Topperc612d792012-01-02 09:17:37 +00003896 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003897 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003898 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003899 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003900 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003901 return false;
3902 return true;
3903}
3904
Evan Cheng0b457f02008-09-25 20:50:48 +00003905/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003906/// specifies a shuffle of elements that is suitable for input to 128-bit
3907/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003908static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003909 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003910 return false;
3911
Craig Topperc612d792012-01-02 09:17:37 +00003912 unsigned e = VT.getVectorNumElements() / 2;
3913 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003914 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003915 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003916 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003917 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003918 return false;
3919 return true;
3920}
3921
David Greenec38a03e2011-02-03 15:50:00 +00003922/// isVEXTRACTF128Index - Return true if the specified
3923/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3924/// suitable for input to VEXTRACTF128.
3925bool X86::isVEXTRACTF128Index(SDNode *N) {
3926 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3927 return false;
3928
3929 // The index should be aligned on a 128-bit boundary.
3930 uint64_t Index =
3931 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3932
3933 unsigned VL = N->getValueType(0).getVectorNumElements();
3934 unsigned VBits = N->getValueType(0).getSizeInBits();
3935 unsigned ElSize = VBits / VL;
3936 bool Result = (Index * ElSize) % 128 == 0;
3937
3938 return Result;
3939}
3940
David Greeneccacdc12011-02-04 16:08:29 +00003941/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3942/// operand specifies a subvector insert that is suitable for input to
3943/// VINSERTF128.
3944bool X86::isVINSERTF128Index(SDNode *N) {
3945 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3946 return false;
3947
3948 // The index should be aligned on a 128-bit boundary.
3949 uint64_t Index =
3950 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3951
3952 unsigned VL = N->getValueType(0).getVectorNumElements();
3953 unsigned VBits = N->getValueType(0).getSizeInBits();
3954 unsigned ElSize = VBits / VL;
3955 bool Result = (Index * ElSize) % 128 == 0;
3956
3957 return Result;
3958}
3959
Evan Cheng63d33002006-03-22 08:01:21 +00003960/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003961/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003962/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003963static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003964 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003965
Craig Topper1a7700a2012-01-19 08:19:12 +00003966 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3967 "Unsupported vector type for PSHUF/SHUFP");
3968
3969 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3970 // independently on 128-bit lanes.
3971 unsigned NumElts = VT.getVectorNumElements();
3972 unsigned NumLanes = VT.getSizeInBits()/128;
3973 unsigned NumLaneElts = NumElts/NumLanes;
3974
3975 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3976 "Only supports 2 or 4 elements per lane");
3977
3978 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003979 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003980 for (unsigned i = 0; i != NumElts; ++i) {
3981 int Elt = N->getMaskElt(i);
3982 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003983 Elt &= NumLaneElts - 1;
3984 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003985 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003986 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003987
Evan Cheng63d33002006-03-22 08:01:21 +00003988 return Mask;
3989}
3990
Evan Cheng506d3df2006-03-29 23:07:14 +00003991/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003992/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003993static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003994 EVT VT = N->getValueType(0);
3995
3996 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3997 "Unsupported vector type for PSHUFHW");
3998
3999 unsigned NumElts = VT.getVectorNumElements();
4000
Evan Cheng506d3df2006-03-29 23:07:14 +00004001 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004002 for (unsigned l = 0; l != NumElts; l += 8) {
4003 // 8 nodes per lane, but we only care about the last 4.
4004 for (unsigned i = 0; i < 4; ++i) {
4005 int Elt = N->getMaskElt(l+i+4);
4006 if (Elt < 0) continue;
4007 Elt &= 0x3; // only 2-bits.
4008 Mask |= Elt << (i * 2);
4009 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004010 }
Craig Topper6b28d352012-05-03 07:12:59 +00004011
Evan Cheng506d3df2006-03-29 23:07:14 +00004012 return Mask;
4013}
4014
4015/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004016/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004017static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004018 EVT VT = N->getValueType(0);
4019
4020 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4021 "Unsupported vector type for PSHUFHW");
4022
4023 unsigned NumElts = VT.getVectorNumElements();
4024
Evan Cheng506d3df2006-03-29 23:07:14 +00004025 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004026 for (unsigned l = 0; l != NumElts; l += 8) {
4027 // 8 nodes per lane, but we only care about the first 4.
4028 for (unsigned i = 0; i < 4; ++i) {
4029 int Elt = N->getMaskElt(l+i);
4030 if (Elt < 0) continue;
4031 Elt &= 0x3; // only 2-bits
4032 Mask |= Elt << (i * 2);
4033 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004034 }
Craig Topper6b28d352012-05-03 07:12:59 +00004035
Evan Cheng506d3df2006-03-29 23:07:14 +00004036 return Mask;
4037}
4038
Nate Begemana09008b2009-10-19 02:17:23 +00004039/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4040/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004041static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4042 EVT VT = SVOp->getValueType(0);
4043 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004044
Craig Topper0e2037b2012-01-20 05:53:00 +00004045 unsigned NumElts = VT.getVectorNumElements();
4046 unsigned NumLanes = VT.getSizeInBits()/128;
4047 unsigned NumLaneElts = NumElts/NumLanes;
4048
4049 int Val = 0;
4050 unsigned i;
4051 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004052 Val = SVOp->getMaskElt(i);
4053 if (Val >= 0)
4054 break;
4055 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004056 if (Val >= (int)NumElts)
4057 Val -= NumElts - NumLaneElts;
4058
Eli Friedman63f8dde2011-07-25 21:36:45 +00004059 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004060 return (Val - i) * EltSize;
4061}
4062
David Greenec38a03e2011-02-03 15:50:00 +00004063/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4064/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4065/// instructions.
4066unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4067 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4068 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4069
4070 uint64_t Index =
4071 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4072
4073 EVT VecVT = N->getOperand(0).getValueType();
4074 EVT ElVT = VecVT.getVectorElementType();
4075
4076 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004077 return Index / NumElemsPerChunk;
4078}
4079
David Greeneccacdc12011-02-04 16:08:29 +00004080/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4081/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4082/// instructions.
4083unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4084 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4085 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4086
4087 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004088 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004089
4090 EVT VecVT = N->getValueType(0);
4091 EVT ElVT = VecVT.getVectorElementType();
4092
4093 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004094 return Index / NumElemsPerChunk;
4095}
4096
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004097/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4098/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4099/// Handles 256-bit.
4100static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4101 EVT VT = N->getValueType(0);
4102
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004103 unsigned NumElts = VT.getVectorNumElements();
4104
Craig Topper095c5282012-04-15 23:48:57 +00004105 assert((VT.is256BitVector() && NumElts == 4) &&
4106 "Unsupported vector type for VPERMQ/VPERMPD");
4107
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004108 unsigned Mask = 0;
4109 for (unsigned i = 0; i != NumElts; ++i) {
4110 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004111 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004112 continue;
4113 Mask |= Elt << (i*2);
4114 }
4115
4116 return Mask;
4117}
Evan Cheng37b73872009-07-30 08:33:02 +00004118/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4119/// constant +0.0.
4120bool X86::isZeroNode(SDValue Elt) {
4121 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004122 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004123 (isa<ConstantFPSDNode>(Elt) &&
4124 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4125}
4126
Nate Begeman9008ca62009-04-27 18:41:29 +00004127/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4128/// their permute mask.
4129static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4130 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004131 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004132 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004133 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004134
Nate Begeman5a5ca152009-04-29 05:20:52 +00004135 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004136 int Idx = SVOp->getMaskElt(i);
4137 if (Idx >= 0) {
4138 if (Idx < (int)NumElems)
4139 Idx += NumElems;
4140 else
4141 Idx -= NumElems;
4142 }
4143 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004144 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004145 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4146 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004147}
4148
Evan Cheng533a0aa2006-04-19 20:35:22 +00004149/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4150/// match movhlps. The lower half elements should come from upper half of
4151/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004152/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004153static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004154 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004155 return false;
4156 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004157 return false;
4158 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004159 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004160 return false;
4161 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004162 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004163 return false;
4164 return true;
4165}
4166
Evan Cheng5ced1d82006-04-06 23:23:56 +00004167/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004168/// is promoted to a vector. It also returns the LoadSDNode by reference if
4169/// required.
4170static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004171 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4172 return false;
4173 N = N->getOperand(0).getNode();
4174 if (!ISD::isNON_EXTLoad(N))
4175 return false;
4176 if (LD)
4177 *LD = cast<LoadSDNode>(N);
4178 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004179}
4180
Dan Gohman65fd6562011-11-03 21:49:52 +00004181// Test whether the given value is a vector value which will be legalized
4182// into a load.
4183static bool WillBeConstantPoolLoad(SDNode *N) {
4184 if (N->getOpcode() != ISD::BUILD_VECTOR)
4185 return false;
4186
4187 // Check for any non-constant elements.
4188 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4189 switch (N->getOperand(i).getNode()->getOpcode()) {
4190 case ISD::UNDEF:
4191 case ISD::ConstantFP:
4192 case ISD::Constant:
4193 break;
4194 default:
4195 return false;
4196 }
4197
4198 // Vectors of all-zeros and all-ones are materialized with special
4199 // instructions rather than being loaded.
4200 return !ISD::isBuildVectorAllZeros(N) &&
4201 !ISD::isBuildVectorAllOnes(N);
4202}
4203
Evan Cheng533a0aa2006-04-19 20:35:22 +00004204/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4205/// match movlp{s|d}. The lower half elements should come from lower half of
4206/// V1 (and in order), and the upper half elements should come from the upper
4207/// half of V2 (and in order). And since V1 will become the source of the
4208/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004209static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004210 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004211 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004212 return false;
4213
Evan Cheng466685d2006-10-09 20:57:25 +00004214 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004215 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004216 // Is V2 is a vector load, don't do this transformation. We will try to use
4217 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004218 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004219 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004220
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004221 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004222
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223 if (NumElems != 2 && NumElems != 4)
4224 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004225 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004226 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004227 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004228 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004229 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004230 return false;
4231 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004232}
4233
Evan Cheng39623da2006-04-20 08:58:49 +00004234/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4235/// all the same.
4236static bool isSplatVector(SDNode *N) {
4237 if (N->getOpcode() != ISD::BUILD_VECTOR)
4238 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004239
Dan Gohman475871a2008-07-27 21:46:04 +00004240 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004241 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4242 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004243 return false;
4244 return true;
4245}
4246
Evan Cheng213d2cf2007-05-17 18:45:50 +00004247/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004248/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004249/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004250static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004251 SDValue V1 = N->getOperand(0);
4252 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004253 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4254 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004256 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004258 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4259 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004260 if (Opc != ISD::BUILD_VECTOR ||
4261 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004262 return false;
4263 } else if (Idx >= 0) {
4264 unsigned Opc = V1.getOpcode();
4265 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4266 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004267 if (Opc != ISD::BUILD_VECTOR ||
4268 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004269 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004270 }
4271 }
4272 return true;
4273}
4274
4275/// getZeroVector - Returns a vector of specified type with all zero elements.
4276///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004277static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004278 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004279 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004280 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004281
Dale Johannesen0488fb62010-09-30 23:57:10 +00004282 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004283 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004284 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004285 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004286 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004287 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4288 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4289 } else { // SSE1
4290 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4291 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4292 }
Craig Topper9d352402012-04-23 07:24:41 +00004293 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004294 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004295 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4296 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4297 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4298 } else {
4299 // 256-bit logic and arithmetic instructions in AVX are all
4300 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4301 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4302 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4303 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4304 }
Craig Topper9d352402012-04-23 07:24:41 +00004305 } else
4306 llvm_unreachable("Unexpected vector type");
4307
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004308 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004309}
4310
Chris Lattner8a594482007-11-25 00:24:49 +00004311/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004312/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4313/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4314/// Then bitcast to their original type, ensuring they get CSE'd.
4315static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4316 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004317 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004318 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004319
Owen Anderson825b72b2009-08-11 20:47:22 +00004320 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004321 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004322 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004323 if (HasAVX2) { // AVX2
4324 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4325 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4326 } else { // AVX
4327 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004328 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004329 }
Craig Topper9d352402012-04-23 07:24:41 +00004330 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004331 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004332 } else
4333 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004334
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004335 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004336}
4337
Evan Cheng39623da2006-04-20 08:58:49 +00004338/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4339/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004340static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004341 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004342 if (Mask[i] > (int)NumElems) {
4343 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004344 }
Evan Cheng39623da2006-04-20 08:58:49 +00004345 }
Evan Cheng39623da2006-04-20 08:58:49 +00004346}
4347
Evan Cheng017dcc62006-04-21 01:05:10 +00004348/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4349/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004350static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004351 SDValue V2) {
4352 unsigned NumElems = VT.getVectorNumElements();
4353 SmallVector<int, 8> Mask;
4354 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004355 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004356 Mask.push_back(i);
4357 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004358}
4359
Nate Begeman9008ca62009-04-27 18:41:29 +00004360/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004361static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 SDValue V2) {
4363 unsigned NumElems = VT.getVectorNumElements();
4364 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004365 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 Mask.push_back(i);
4367 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004368 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004370}
4371
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004372/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004373static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 SDValue V2) {
4375 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004377 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004378 Mask.push_back(i + Half);
4379 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004380 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004382}
4383
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004384// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004385// a generic shuffle instruction because the target has no such instructions.
4386// Generate shuffles which repeat i16 and i8 several times until they can be
4387// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004388static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004389 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004392
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 while (NumElems > 4) {
4394 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004395 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004397 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 EltNo -= NumElems/2;
4399 }
4400 NumElems >>= 1;
4401 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004402 return V;
4403}
Eric Christopherfd179292009-08-27 18:07:15 +00004404
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004405/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4406static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4407 EVT VT = V.getValueType();
4408 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004409 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410
Craig Topper9d352402012-04-23 07:24:41 +00004411 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004412 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004413 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004414 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4415 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004416 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004417 // To use VPERMILPS to splat scalars, the second half of indicies must
4418 // refer to the higher part, which is a duplication of the lower one,
4419 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4421 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004422
4423 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4424 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4425 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004426 } else
4427 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004428
4429 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4430}
4431
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004432/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004433static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4434 EVT SrcVT = SV->getValueType(0);
4435 SDValue V1 = SV->getOperand(0);
4436 DebugLoc dl = SV->getDebugLoc();
4437
4438 int EltNo = SV->getSplatIndex();
4439 int NumElems = SrcVT.getVectorNumElements();
4440 unsigned Size = SrcVT.getSizeInBits();
4441
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004442 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4443 "Unknown how to promote splat for type");
4444
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004445 // Extract the 128-bit part containing the splat element and update
4446 // the splat element index when it refers to the higher register.
4447 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004448 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4449 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004450 EltNo -= NumElems/2;
4451 }
4452
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004453 // All i16 and i8 vector types can't be used directly by a generic shuffle
4454 // instruction because the target has no such instruction. Generate shuffles
4455 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004456 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004457 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004458 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004459 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004460
4461 // Recreate the 256-bit vector and place the same 128-bit vector
4462 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004463 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004465 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466 }
4467
4468 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004469}
4470
Evan Chengba05f722006-04-21 23:03:30 +00004471/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004472/// vector of zero or undef vector. This produces a shuffle where the low
4473/// element of V2 is swizzled into the zero/undef vector, landing at element
4474/// 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 +00004475static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004476 bool IsZero,
4477 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004478 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004479 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004480 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004481 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 unsigned NumElems = VT.getVectorNumElements();
4483 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004484 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 // If this is the insertion idx, put the low elt of V2 here.
4486 MaskVec.push_back(i == Idx ? NumElems : i);
4487 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004488}
4489
Craig Toppera1ffc682012-03-20 06:42:26 +00004490/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4491/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004492/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004493static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004494 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004495 unsigned NumElems = VT.getVectorNumElements();
4496 SDValue ImmN;
4497
Craig Topper89f4e662012-03-20 07:17:59 +00004498 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004499 switch(N->getOpcode()) {
4500 case X86ISD::SHUFP:
4501 ImmN = N->getOperand(N->getNumOperands()-1);
4502 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4503 break;
4504 case X86ISD::UNPCKH:
4505 DecodeUNPCKHMask(VT, Mask);
4506 break;
4507 case X86ISD::UNPCKL:
4508 DecodeUNPCKLMask(VT, Mask);
4509 break;
4510 case X86ISD::MOVHLPS:
4511 DecodeMOVHLPSMask(NumElems, Mask);
4512 break;
4513 case X86ISD::MOVLHPS:
4514 DecodeMOVLHPSMask(NumElems, Mask);
4515 break;
4516 case X86ISD::PSHUFD:
4517 case X86ISD::VPERMILP:
4518 ImmN = N->getOperand(N->getNumOperands()-1);
4519 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004520 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004521 break;
4522 case X86ISD::PSHUFHW:
4523 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004524 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004525 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004526 break;
4527 case X86ISD::PSHUFLW:
4528 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004529 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004530 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004531 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004532 case X86ISD::VPERMI:
4533 ImmN = N->getOperand(N->getNumOperands()-1);
4534 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4535 IsUnary = true;
4536 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004537 case X86ISD::MOVSS:
4538 case X86ISD::MOVSD: {
4539 // The index 0 always comes from the first element of the second source,
4540 // this is why MOVSS and MOVSD are used in the first place. The other
4541 // elements come from the other positions of the first source vector
4542 Mask.push_back(NumElems);
4543 for (unsigned i = 1; i != NumElems; ++i) {
4544 Mask.push_back(i);
4545 }
4546 break;
4547 }
4548 case X86ISD::VPERM2X128:
4549 ImmN = N->getOperand(N->getNumOperands()-1);
4550 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004551 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004552 break;
4553 case X86ISD::MOVDDUP:
4554 case X86ISD::MOVLHPD:
4555 case X86ISD::MOVLPD:
4556 case X86ISD::MOVLPS:
4557 case X86ISD::MOVSHDUP:
4558 case X86ISD::MOVSLDUP:
4559 case X86ISD::PALIGN:
4560 // Not yet implemented
4561 return false;
4562 default: llvm_unreachable("unknown target shuffle node");
4563 }
4564
4565 return true;
4566}
4567
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004568/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4569/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004570static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004571 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004572 if (Depth == 6)
4573 return SDValue(); // Limit search depth.
4574
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004575 SDValue V = SDValue(N, 0);
4576 EVT VT = V.getValueType();
4577 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004578
4579 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4580 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004581 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004582
Craig Topper3d092db2012-03-21 02:14:01 +00004583 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004584 return DAG.getUNDEF(VT.getVectorElementType());
4585
Craig Topperd156dc12012-02-06 07:17:51 +00004586 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004587 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4588 : SV->getOperand(1);
4589 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004590 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004591
4592 // Recurse into target specific vector shuffles to find scalars.
4593 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004594 MVT ShufVT = V.getValueType().getSimpleVT();
4595 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004596 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004597 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004598 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004599
Craig Topperd978c542012-05-06 19:46:21 +00004600 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004601 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004602
Craig Topper3d092db2012-03-21 02:14:01 +00004603 int Elt = ShuffleMask[Index];
4604 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004605 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004606
Craig Topper3d092db2012-03-21 02:14:01 +00004607 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004608 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004609 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004610 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004611 }
4612
4613 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004614 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004615 V = V.getOperand(0);
4616 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004617 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004618
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004619 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004620 return SDValue();
4621 }
4622
4623 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4624 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004625 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004626
4627 if (V.getOpcode() == ISD::BUILD_VECTOR)
4628 return V.getOperand(Index);
4629
4630 return SDValue();
4631}
4632
4633/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4634/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004635/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004636static
Craig Topper3d092db2012-03-21 02:14:01 +00004637unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004638 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004639 unsigned i;
4640 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004641 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004642 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643 if (!(Elt.getNode() &&
4644 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4645 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004646 }
4647
4648 return i;
4649}
4650
Craig Topper3d092db2012-03-21 02:14:01 +00004651/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4652/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004653/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4654static
Craig Topper3d092db2012-03-21 02:14:01 +00004655bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4656 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4657 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658 bool SeenV1 = false;
4659 bool SeenV2 = false;
4660
Craig Topper3d092db2012-03-21 02:14:01 +00004661 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004662 int Idx = SVOp->getMaskElt(i);
4663 // Ignore undef indicies
4664 if (Idx < 0)
4665 continue;
4666
Craig Topper3d092db2012-03-21 02:14:01 +00004667 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004668 SeenV1 = true;
4669 else
4670 SeenV2 = true;
4671
4672 // Only accept consecutive elements from the same vector
4673 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4674 return false;
4675 }
4676
4677 OpNum = SeenV1 ? 0 : 1;
4678 return true;
4679}
4680
4681/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4682/// logical left shift of a vector.
4683static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4684 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4685 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4686 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4687 false /* check zeros from right */, DAG);
4688 unsigned OpSrc;
4689
4690 if (!NumZeros)
4691 return false;
4692
4693 // Considering the elements in the mask that are not consecutive zeros,
4694 // check if they consecutively come from only one of the source vectors.
4695 //
4696 // V1 = {X, A, B, C} 0
4697 // \ \ \ /
4698 // vector_shuffle V1, V2 <1, 2, 3, X>
4699 //
4700 if (!isShuffleMaskConsecutive(SVOp,
4701 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004702 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004703 NumZeros, // Where to start looking in the src vector
4704 NumElems, // Number of elements in vector
4705 OpSrc)) // Which source operand ?
4706 return false;
4707
4708 isLeft = false;
4709 ShAmt = NumZeros;
4710 ShVal = SVOp->getOperand(OpSrc);
4711 return true;
4712}
4713
4714/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4715/// logical left shift of a vector.
4716static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4717 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4718 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4719 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4720 true /* check zeros from left */, DAG);
4721 unsigned OpSrc;
4722
4723 if (!NumZeros)
4724 return false;
4725
4726 // Considering the elements in the mask that are not consecutive zeros,
4727 // check if they consecutively come from only one of the source vectors.
4728 //
4729 // 0 { A, B, X, X } = V2
4730 // / \ / /
4731 // vector_shuffle V1, V2 <X, X, 4, 5>
4732 //
4733 if (!isShuffleMaskConsecutive(SVOp,
4734 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004735 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004736 0, // Where to start looking in the src vector
4737 NumElems, // Number of elements in vector
4738 OpSrc)) // Which source operand ?
4739 return false;
4740
4741 isLeft = true;
4742 ShAmt = NumZeros;
4743 ShVal = SVOp->getOperand(OpSrc);
4744 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004745}
4746
4747/// isVectorShift - Returns true if the shuffle can be implemented as a
4748/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004749static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004750 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004751 // Although the logic below support any bitwidth size, there are no
4752 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004753 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004754 return false;
4755
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004756 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4757 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4758 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004759
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004760 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004761}
4762
Evan Chengc78d3b42006-04-24 18:01:45 +00004763/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4764///
Dan Gohman475871a2008-07-27 21:46:04 +00004765static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004766 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004767 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004768 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004769 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004770 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004771 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004772
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004773 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004774 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004775 bool First = true;
4776 for (unsigned i = 0; i < 16; ++i) {
4777 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4778 if (ThisIsNonZero && First) {
4779 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004780 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004781 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004782 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004783 First = false;
4784 }
4785
4786 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004787 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004788 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4789 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004790 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004791 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004792 }
4793 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4795 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4796 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004797 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004798 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004799 } else
4800 ThisElt = LastElt;
4801
Gabor Greifba36cb52008-08-28 21:40:38 +00004802 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004804 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004805 }
4806 }
4807
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004808 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809}
4810
Bill Wendlinga348c562007-03-22 18:42:45 +00004811/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004812///
Dan Gohman475871a2008-07-27 21:46:04 +00004813static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004814 unsigned NumNonZero, unsigned NumZero,
4815 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004816 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004817 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004818 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004819 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004820
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004821 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004822 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 bool First = true;
4824 for (unsigned i = 0; i < 8; ++i) {
4825 bool isNonZero = (NonZeros & (1 << i)) != 0;
4826 if (isNonZero) {
4827 if (First) {
4828 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004829 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004830 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 First = false;
4833 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004834 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004836 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004837 }
4838 }
4839
4840 return V;
4841}
4842
Evan Chengf26ffe92008-05-29 08:22:04 +00004843/// getVShift - Return a vector logical shift node.
4844///
Owen Andersone50ed302009-08-10 22:56:29 +00004845static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004846 unsigned NumBits, SelectionDAG &DAG,
4847 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004848 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004849 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004850 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004851 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4852 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004853 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004854 DAG.getConstant(NumBits,
4855 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004856}
4857
Dan Gohman475871a2008-07-27 21:46:04 +00004858SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004859X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004860 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004861
Evan Chengc3630942009-12-09 21:00:30 +00004862 // Check if the scalar load can be widened into a vector load. And if
4863 // the address is "base + cst" see if the cst can be "absorbed" into
4864 // the shuffle mask.
4865 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4866 SDValue Ptr = LD->getBasePtr();
4867 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4868 return SDValue();
4869 EVT PVT = LD->getValueType(0);
4870 if (PVT != MVT::i32 && PVT != MVT::f32)
4871 return SDValue();
4872
4873 int FI = -1;
4874 int64_t Offset = 0;
4875 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4876 FI = FINode->getIndex();
4877 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004878 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004879 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4880 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4881 Offset = Ptr.getConstantOperandVal(1);
4882 Ptr = Ptr.getOperand(0);
4883 } else {
4884 return SDValue();
4885 }
4886
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004887 // FIXME: 256-bit vector instructions don't require a strict alignment,
4888 // improve this code to support it better.
4889 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004890 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004891 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004892 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004893 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004894 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004895 // Can't change the alignment. FIXME: It's possible to compute
4896 // the exact stack offset and reference FI + adjust offset instead.
4897 // If someone *really* cares about this. That's the way to implement it.
4898 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004899 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004900 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004901 }
4902 }
4903
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004904 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004905 // Ptr + (Offset & ~15).
4906 if (Offset < 0)
4907 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004908 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004909 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004910 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004911 if (StartOffset)
4912 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4913 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4914
4915 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004916 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004917
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4919 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004920 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004921 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004922
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004923 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004924 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004925 Mask.push_back(EltNo);
4926
Craig Toppercc3000632012-01-30 07:50:31 +00004927 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004928 }
4929
4930 return SDValue();
4931}
4932
Michael J. Spencerec38de22010-10-10 22:04:20 +00004933/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4934/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004935/// load which has the same value as a build_vector whose operands are 'elts'.
4936///
4937/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004938///
Nate Begeman1449f292010-03-24 22:19:06 +00004939/// FIXME: we'd also like to handle the case where the last elements are zero
4940/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4941/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004942static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004943 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004944 EVT EltVT = VT.getVectorElementType();
4945 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004946
Nate Begemanfdea31a2010-03-24 20:49:50 +00004947 LoadSDNode *LDBase = NULL;
4948 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004949
Nate Begeman1449f292010-03-24 22:19:06 +00004950 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004951 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004952 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004953 for (unsigned i = 0; i < NumElems; ++i) {
4954 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004955
Nate Begemanfdea31a2010-03-24 20:49:50 +00004956 if (!Elt.getNode() ||
4957 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4958 return SDValue();
4959 if (!LDBase) {
4960 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4961 return SDValue();
4962 LDBase = cast<LoadSDNode>(Elt.getNode());
4963 LastLoadedElt = i;
4964 continue;
4965 }
4966 if (Elt.getOpcode() == ISD::UNDEF)
4967 continue;
4968
4969 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4970 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4971 return SDValue();
4972 LastLoadedElt = i;
4973 }
Nate Begeman1449f292010-03-24 22:19:06 +00004974
4975 // If we have found an entire vector of loads and undefs, then return a large
4976 // load of the entire vector width starting at the base pointer. If we found
4977 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004978 if (LastLoadedElt == NumElems - 1) {
4979 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004980 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004981 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004982 LDBase->isVolatile(), LDBase->isNonTemporal(),
4983 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004984 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004985 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004986 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004987 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004988 }
4989 if (NumElems == 4 && LastLoadedElt == 1 &&
4990 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004991 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4992 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004993 SDValue ResNode =
4994 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4995 LDBase->getPointerInfo(),
4996 LDBase->getAlignment(),
4997 false/*isVolatile*/, true/*ReadMem*/,
4998 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00004999
5000 // Make sure the newly-created LOAD is in the same position as LDBase in
5001 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5002 // update uses of LDBase's output chain to use the TokenFactor.
5003 if (LDBase->hasAnyUseOfValue(1)) {
5004 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5005 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5006 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5007 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5008 SDValue(ResNode.getNode(), 1));
5009 }
5010
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005011 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005012 }
5013 return SDValue();
5014}
5015
Nadav Rotem9d68b062012-04-08 12:54:54 +00005016/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5017/// to generate a splat value for the following cases:
5018/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005019/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005020/// a scalar load, or a constant.
5021/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005022/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005023SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005024X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005025 if (!Subtarget->hasAVX())
5026 return SDValue();
5027
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005028 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005029 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005030
Craig Topper5da8a802012-05-04 05:49:51 +00005031 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5032 "Unsupported vector type for broadcast.");
5033
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005034 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005035 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005036
Nadav Rotem9d68b062012-04-08 12:54:54 +00005037 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005038 default:
5039 // Unknown pattern found.
5040 return SDValue();
5041
5042 case ISD::BUILD_VECTOR: {
5043 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005044 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005045 return SDValue();
5046
Nadav Rotem9d68b062012-04-08 12:54:54 +00005047 Ld = Op.getOperand(0);
5048 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5049 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005050
5051 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005052 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005053 // Constants may have multiple users.
5054 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005055 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005056 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005057 }
5058
5059 case ISD::VECTOR_SHUFFLE: {
5060 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5061
5062 // Shuffles must have a splat mask where the first element is
5063 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005064 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005065 return SDValue();
5066
5067 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005068 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005069 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5070
5071 if (!Subtarget->hasAVX2())
5072 return SDValue();
5073
5074 // Use the register form of the broadcast instruction available on AVX2.
5075 if (VT.is256BitVector())
5076 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5077 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5078 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005079
5080 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005081 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005082 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005083
5084 // The scalar_to_vector node and the suspected
5085 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005086 // Constants may have multiple users.
5087 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005088 return SDValue();
5089 break;
5090 }
5091 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005092
Craig Topper7a9a28b2012-08-12 02:23:29 +00005093 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005094
5095 // Handle the broadcasting a single constant scalar from the constant pool
5096 // into a vector. On Sandybridge it is still better to load a constant vector
5097 // from the constant pool and not to broadcast it from a scalar.
5098 if (ConstSplatVal && Subtarget->hasAVX2()) {
5099 EVT CVT = Ld.getValueType();
5100 assert(!CVT.isVector() && "Must not broadcast a vector type");
5101 unsigned ScalarSize = CVT.getSizeInBits();
5102
Craig Topper5da8a802012-05-04 05:49:51 +00005103 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104 const Constant *C = 0;
5105 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5106 C = CI->getConstantIntValue();
5107 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5108 C = CF->getConstantFPValue();
5109
5110 assert(C && "Invalid constant type");
5111
Nadav Rotem154819d2012-04-09 07:45:58 +00005112 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005113 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005114 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005115 MachinePointerInfo::getConstantPool(),
5116 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005117
Nadav Rotem9d68b062012-04-08 12:54:54 +00005118 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5119 }
5120 }
5121
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005122 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005123 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5124
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005125 // Handle AVX2 in-register broadcasts.
5126 if (!IsLoad && Subtarget->hasAVX2() &&
5127 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5128 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5129
5130 // The scalar source must be a normal load.
5131 if (!IsLoad)
5132 return SDValue();
5133
Craig Topper5da8a802012-05-04 05:49:51 +00005134 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005135 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005136
Craig Toppera9376332012-01-10 08:23:59 +00005137 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005138 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005139 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005140 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005141 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005142 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005143
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005144 // Unsupported broadcast.
5145 return SDValue();
5146}
5147
Michael Liao7091b242012-08-14 21:24:47 +00005148// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5149// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5150// constraint of matching input/output vector elements.
5151SDValue
5152X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5153 DebugLoc DL = Op.getDebugLoc();
5154 SDNode *N = Op.getNode();
5155 EVT VT = Op.getValueType();
5156 unsigned NumElts = Op.getNumOperands();
5157
5158 // Check supported types and sub-targets.
5159 //
5160 // Only v2f32 -> v2f64 needs special handling.
5161 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5162 return SDValue();
5163
5164 SDValue VecIn;
5165 EVT VecInVT;
5166 SmallVector<int, 8> Mask;
5167 EVT SrcVT = MVT::Other;
5168
5169 // Check the patterns could be translated into X86vfpext.
5170 for (unsigned i = 0; i < NumElts; ++i) {
5171 SDValue In = N->getOperand(i);
5172 unsigned Opcode = In.getOpcode();
5173
5174 // Skip if the element is undefined.
5175 if (Opcode == ISD::UNDEF) {
5176 Mask.push_back(-1);
5177 continue;
5178 }
5179
5180 // Quit if one of the elements is not defined from 'fpext'.
5181 if (Opcode != ISD::FP_EXTEND)
5182 return SDValue();
5183
5184 // Check how the source of 'fpext' is defined.
5185 SDValue L2In = In.getOperand(0);
5186 EVT L2InVT = L2In.getValueType();
5187
5188 // Check the original type
5189 if (SrcVT == MVT::Other)
5190 SrcVT = L2InVT;
5191 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5192 return SDValue();
5193
5194 // Check whether the value being 'fpext'ed is extracted from the same
5195 // source.
5196 Opcode = L2In.getOpcode();
5197
5198 // Quit if it's not extracted with a constant index.
5199 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5200 !isa<ConstantSDNode>(L2In.getOperand(1)))
5201 return SDValue();
5202
5203 SDValue ExtractedFromVec = L2In.getOperand(0);
5204
5205 if (VecIn.getNode() == 0) {
5206 VecIn = ExtractedFromVec;
5207 VecInVT = ExtractedFromVec.getValueType();
5208 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5209 return SDValue();
5210
5211 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5212 }
5213
Michael Liao24438b82012-08-20 17:59:18 +00005214 // Quit if all operands of BUILD_VECTOR are undefined.
5215 if (!VecIn.getNode())
5216 return SDValue();
5217
Michael Liao7091b242012-08-14 21:24:47 +00005218 // Fill the remaining mask as undef.
5219 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5220 Mask.push_back(-1);
5221
5222 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5223 DAG.getVectorShuffle(VecInVT, DL,
5224 VecIn, DAG.getUNDEF(VecInVT),
5225 &Mask[0]));
5226}
5227
Evan Chengc3630942009-12-09 21:00:30 +00005228SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005229X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005230 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005231
David Greenef125a292011-02-08 19:04:41 +00005232 EVT VT = Op.getValueType();
5233 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005234 unsigned NumElems = Op.getNumOperands();
5235
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005236 // Vectors containing all zeros can be matched by pxor and xorps later
5237 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5238 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5239 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005240 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005241 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005242
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005243 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005244 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005245
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005246 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005247 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5248 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005249 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005250 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005251 return Op;
5252
Craig Topper07a27622012-01-22 03:07:48 +00005253 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005254 }
5255
Nadav Rotem154819d2012-04-09 07:45:58 +00005256 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005257 if (Broadcast.getNode())
5258 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005259
Michael Liao7091b242012-08-14 21:24:47 +00005260 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5261 if (FpExt.getNode())
5262 return FpExt;
5263
Owen Andersone50ed302009-08-10 22:56:29 +00005264 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005265
Evan Cheng0db9fe62006-04-25 20:13:52 +00005266 unsigned NumZero = 0;
5267 unsigned NumNonZero = 0;
5268 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005269 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005270 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005271 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005272 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005273 if (Elt.getOpcode() == ISD::UNDEF)
5274 continue;
5275 Values.insert(Elt);
5276 if (Elt.getOpcode() != ISD::Constant &&
5277 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005278 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005279 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005280 NumZero++;
5281 else {
5282 NonZeros |= (1 << i);
5283 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005284 }
5285 }
5286
Chris Lattner97a2a562010-08-26 05:24:29 +00005287 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5288 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005289 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005290
Chris Lattner67f453a2008-03-09 05:42:06 +00005291 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005292 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005293 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005294 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005295
Chris Lattner62098042008-03-09 01:05:04 +00005296 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5297 // the value are obviously zero, truncate the value to i32 and do the
5298 // insertion that way. Only do this if the value is non-constant or if the
5299 // value is a constant being inserted into element 0. It is cheaper to do
5300 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005301 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005302 (!IsAllConstants || Idx == 0)) {
5303 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005304 // Handle SSE only.
5305 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5306 EVT VecVT = MVT::v4i32;
5307 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005308
Chris Lattner62098042008-03-09 01:05:04 +00005309 // Truncate the value (which may itself be a constant) to i32, and
5310 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005311 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005312 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005313 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005314
Chris Lattner62098042008-03-09 01:05:04 +00005315 // Now we have our 32-bit value zero extended in the low element of
5316 // a vector. If Idx != 0, swizzle it into place.
5317 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005318 SmallVector<int, 4> Mask;
5319 Mask.push_back(Idx);
5320 for (unsigned i = 1; i != VecElts; ++i)
5321 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005322 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005323 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005324 }
Craig Topper07a27622012-01-22 03:07:48 +00005325 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005326 }
5327 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005328
Chris Lattner19f79692008-03-08 22:59:52 +00005329 // If we have a constant or non-constant insertion into the low element of
5330 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5331 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005332 // depending on what the source datatype is.
5333 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005334 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005335 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005336
5337 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005339 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005340 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005341 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5342 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005343 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005344 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005345 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5346 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005347 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005348 }
5349
5350 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005352 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005353 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005354 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005355 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005356 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005357 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005358 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005359 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005360 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005361 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005362 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005363
5364 // Is it a vector logical left shift?
5365 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005366 X86::isZeroNode(Op.getOperand(0)) &&
5367 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005368 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005369 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005370 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005371 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005372 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005373 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005374
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005375 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005376 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005377
Chris Lattner19f79692008-03-08 22:59:52 +00005378 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5379 // is a non-constant being inserted into an element other than the low one,
5380 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5381 // movd/movss) to move this into the low element, then shuffle it into
5382 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005383 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005384 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005385
Evan Cheng0db9fe62006-04-25 20:13:52 +00005386 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005387 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005388 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005389 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005390 MaskVec.push_back(i == Idx ? 0 : 1);
5391 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005392 }
5393 }
5394
Chris Lattner67f453a2008-03-09 05:42:06 +00005395 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005396 if (Values.size() == 1) {
5397 if (EVTBits == 32) {
5398 // Instead of a shuffle like this:
5399 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5400 // Check if it's possible to issue this instead.
5401 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5402 unsigned Idx = CountTrailingZeros_32(NonZeros);
5403 SDValue Item = Op.getOperand(Idx);
5404 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5405 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5406 }
Dan Gohman475871a2008-07-27 21:46:04 +00005407 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005408 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005409
Dan Gohmana3941172007-07-24 22:55:08 +00005410 // A vector full of immediates; various special cases are already
5411 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005412 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005413 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005414
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005415 // For AVX-length vectors, build the individual 128-bit pieces and use
5416 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005417 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005418 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005419 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005420 V.push_back(Op.getOperand(i));
5421
5422 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5423
5424 // Build both the lower and upper subvector.
5425 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5426 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5427 NumElems/2);
5428
5429 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005430 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005431 }
5432
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005433 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005434 if (EVTBits == 64) {
5435 if (NumNonZero == 1) {
5436 // One half is zero or undef.
5437 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005438 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005439 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005440 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005441 }
Dan Gohman475871a2008-07-27 21:46:04 +00005442 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005443 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005444
5445 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005446 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005447 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005448 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005449 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005450 }
5451
Bill Wendling826f36f2007-03-28 00:57:11 +00005452 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005453 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005454 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005455 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005456 }
5457
5458 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005459 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005460 if (NumElems == 4 && NumZero > 0) {
5461 for (unsigned i = 0; i < 4; ++i) {
5462 bool isZero = !(NonZeros & (1 << i));
5463 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005464 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005465 else
Dale Johannesenace16102009-02-03 19:33:06 +00005466 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005467 }
5468
5469 for (unsigned i = 0; i < 2; ++i) {
5470 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5471 default: break;
5472 case 0:
5473 V[i] = V[i*2]; // Must be a zero vector.
5474 break;
5475 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005476 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477 break;
5478 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005479 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005480 break;
5481 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005482 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483 break;
5484 }
5485 }
5486
Benjamin Kramer9c683542012-01-30 15:16:21 +00005487 bool Reverse1 = (NonZeros & 0x3) == 2;
5488 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5489 int MaskVec[] = {
5490 Reverse1 ? 1 : 0,
5491 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005492 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5493 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005494 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005495 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005496 }
5497
Craig Topper7a9a28b2012-08-12 02:23:29 +00005498 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005499 // Check for a build vector of consecutive loads.
5500 for (unsigned i = 0; i < NumElems; ++i)
5501 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005502
Nate Begemanfdea31a2010-03-24 20:49:50 +00005503 // Check for elements which are consecutive loads.
5504 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5505 if (LD.getNode())
5506 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005507
5508 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005509 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005510 SDValue Result;
5511 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5512 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5513 else
5514 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005515
Chris Lattner24faf612010-08-28 17:59:08 +00005516 for (unsigned i = 1; i < NumElems; ++i) {
5517 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5518 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005519 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005520 }
5521 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005522 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005523
Chris Lattner6e80e442010-08-28 17:15:43 +00005524 // Otherwise, expand into a number of unpckl*, start by extending each of
5525 // our (non-undef) elements to the full vector width with the element in the
5526 // bottom slot of the vector (which generates no code for SSE).
5527 for (unsigned i = 0; i < NumElems; ++i) {
5528 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5529 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5530 else
5531 V[i] = DAG.getUNDEF(VT);
5532 }
5533
5534 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005535 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5536 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5537 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005538 unsigned EltStride = NumElems >> 1;
5539 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005540 for (unsigned i = 0; i < EltStride; ++i) {
5541 // If V[i+EltStride] is undef and this is the first round of mixing,
5542 // then it is safe to just drop this shuffle: V[i] is already in the
5543 // right place, the one element (since it's the first round) being
5544 // inserted as undef can be dropped. This isn't safe for successive
5545 // rounds because they will permute elements within both vectors.
5546 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5547 EltStride == NumElems/2)
5548 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005549
Chris Lattner6e80e442010-08-28 17:15:43 +00005550 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005551 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005552 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005553 }
5554 return V[0];
5555 }
Dan Gohman475871a2008-07-27 21:46:04 +00005556 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005557}
5558
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005559// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5560// to create 256-bit vectors from two other 128-bit ones.
5561static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5562 DebugLoc dl = Op.getDebugLoc();
5563 EVT ResVT = Op.getValueType();
5564
Craig Topper7a9a28b2012-08-12 02:23:29 +00005565 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005566
5567 SDValue V1 = Op.getOperand(0);
5568 SDValue V2 = Op.getOperand(1);
5569 unsigned NumElems = ResVT.getVectorNumElements();
5570
Craig Topper4c7972d2012-04-22 18:15:59 +00005571 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005572}
5573
Craig Topper55b24052012-09-11 06:15:32 +00005574static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005575 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005576
5577 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5578 // from two other 128-bit ones.
5579 return LowerAVXCONCAT_VECTORS(Op, DAG);
5580}
5581
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005582// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005583static SDValue
5584LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5585 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005586 SDValue V1 = SVOp->getOperand(0);
5587 SDValue V2 = SVOp->getOperand(1);
5588 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005589 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005590 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005591
Nadav Roteme6113782012-04-11 06:40:27 +00005592 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005593 return SDValue();
5594
Craig Topper1842ba02012-04-23 06:38:28 +00005595 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005596 MVT OpTy;
5597
Craig Topper708e44f2012-04-23 07:36:33 +00005598 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005599 default: return SDValue();
5600 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005601 ISDNo = X86ISD::BLENDPW;
5602 OpTy = MVT::v8i16;
5603 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005604 case MVT::v4i32:
5605 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005606 ISDNo = X86ISD::BLENDPS;
5607 OpTy = MVT::v4f32;
5608 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005609 case MVT::v2i64:
5610 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005611 ISDNo = X86ISD::BLENDPD;
5612 OpTy = MVT::v2f64;
5613 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005614 case MVT::v8i32:
5615 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005616 if (!Subtarget->hasAVX())
5617 return SDValue();
5618 ISDNo = X86ISD::BLENDPS;
5619 OpTy = MVT::v8f32;
5620 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005621 case MVT::v4i64:
5622 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005623 if (!Subtarget->hasAVX())
5624 return SDValue();
5625 ISDNo = X86ISD::BLENDPD;
5626 OpTy = MVT::v4f64;
5627 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005628 }
5629 assert(ISDNo && "Invalid Op Number");
5630
5631 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005632
Craig Topper1842ba02012-04-23 06:38:28 +00005633 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005634 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005635 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005636 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005637 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005638 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005639 else
5640 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005641 }
5642
Nadav Roteme6113782012-04-11 06:40:27 +00005643 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5644 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5645 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5646 DAG.getConstant(MaskVals, MVT::i32));
5647 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005648}
5649
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650// v8i16 shuffles - Prefer shuffles in the following order:
5651// 1. [all] pshuflw, pshufhw, optional move
5652// 2. [ssse3] 1 x pshufb
5653// 3. [ssse3] 2 x pshufb + 1 x por
5654// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005655static SDValue
5656LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5657 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005658 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005659 SDValue V1 = SVOp->getOperand(0);
5660 SDValue V2 = SVOp->getOperand(1);
5661 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005663
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 // Determine if more than 1 of the words in each of the low and high quadwords
5665 // of the result come from the same quadword of one of the two inputs. Undef
5666 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005667 unsigned LoQuad[] = { 0, 0, 0, 0 };
5668 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005669 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005670 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005671 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005672 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005673 MaskVals.push_back(EltIdx);
5674 if (EltIdx < 0) {
5675 ++Quad[0];
5676 ++Quad[1];
5677 ++Quad[2];
5678 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005679 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 }
5681 ++Quad[EltIdx / 4];
5682 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005683 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005684
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005686 unsigned MaxQuad = 1;
5687 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005688 if (LoQuad[i] > MaxQuad) {
5689 BestLoQuad = i;
5690 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005691 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005692 }
5693
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005695 MaxQuad = 1;
5696 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 if (HiQuad[i] > MaxQuad) {
5698 BestHiQuad = i;
5699 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005700 }
5701 }
5702
Nate Begemanb9a47b82009-02-23 08:49:38 +00005703 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005704 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 // single pshufb instruction is necessary. If There are more than 2 input
5706 // quads, disable the next transformation since it does not help SSSE3.
5707 bool V1Used = InputQuads[0] || InputQuads[1];
5708 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005709 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005710 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005711 BestLoQuad = InputQuads[0] ? 0 : 1;
5712 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005713 }
5714 if (InputQuads.count() > 2) {
5715 BestLoQuad = -1;
5716 BestHiQuad = -1;
5717 }
5718 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005719
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5721 // the shuffle mask. If a quad is scored as -1, that means that it contains
5722 // words from all 4 input quadwords.
5723 SDValue NewV;
5724 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005725 int MaskV[] = {
5726 BestLoQuad < 0 ? 0 : BestLoQuad,
5727 BestHiQuad < 0 ? 1 : BestHiQuad
5728 };
Eric Christopherfd179292009-08-27 18:07:15 +00005729 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005730 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5731 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5732 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005733
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5735 // source words for the shuffle, to aid later transformations.
5736 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005737 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005738 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005740 if (idx != (int)i)
5741 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005743 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 AllWordsInNewV = false;
5745 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005746 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005747
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5749 if (AllWordsInNewV) {
5750 for (int i = 0; i != 8; ++i) {
5751 int idx = MaskVals[i];
5752 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005753 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005754 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 if ((idx != i) && idx < 4)
5756 pshufhw = false;
5757 if ((idx != i) && idx > 3)
5758 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005759 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 V1 = NewV;
5761 V2Used = false;
5762 BestLoQuad = 0;
5763 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005764 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005765
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5767 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005768 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005769 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5770 unsigned TargetMask = 0;
5771 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005773 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5774 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5775 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005776 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005777 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005778 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005779 }
Eric Christopherfd179292009-08-27 18:07:15 +00005780
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 // If we have SSSE3, and all words of the result are from 1 input vector,
5782 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5783 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005784 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005786
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005788 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 // mask, and elements that come from V1 in the V2 mask, so that the two
5790 // results can be OR'd together.
5791 bool TwoInputs = V1Used && V2Used;
5792 for (unsigned i = 0; i != 8; ++i) {
5793 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005794 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5795 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5796 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5797 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005799 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005800 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005801 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005804 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005805
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 // Calculate the shuffle mask for the second input, shuffle it, and
5807 // OR it with the first shuffled input.
5808 pshufbMask.clear();
5809 for (unsigned i = 0; i != 8; ++i) {
5810 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005811 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5812 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5813 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5814 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005816 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005817 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005818 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 MVT::v16i8, &pshufbMask[0], 16));
5820 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005821 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 }
5823
5824 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5825 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005826 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005828 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 for (int i = 0; i != 4; ++i) {
5830 int idx = MaskVals[i];
5831 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 InOrder.set(i);
5833 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005834 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 }
5837 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005838 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005839 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005840
Craig Topperdd637ae2012-02-19 05:41:45 +00005841 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5842 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005843 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005844 NewV.getOperand(0),
5845 getShufflePSHUFLWImmediate(SVOp), DAG);
5846 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 }
Eric Christopherfd179292009-08-27 18:07:15 +00005848
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5850 // and update MaskVals with the new element order.
5851 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005852 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 for (unsigned i = 4; i != 8; ++i) {
5854 int idx = MaskVals[i];
5855 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 InOrder.set(i);
5857 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005858 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005860 }
5861 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005862 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005863 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005864
Craig Topperdd637ae2012-02-19 05:41:45 +00005865 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5866 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005867 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005868 NewV.getOperand(0),
5869 getShufflePSHUFHWImmediate(SVOp), DAG);
5870 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 }
Eric Christopherfd179292009-08-27 18:07:15 +00005872
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 // In case BestHi & BestLo were both -1, which means each quadword has a word
5874 // from each of the four input quadwords, calculate the InOrder bitvector now
5875 // before falling through to the insert/extract cleanup.
5876 if (BestLoQuad == -1 && BestHiQuad == -1) {
5877 NewV = V1;
5878 for (int i = 0; i != 8; ++i)
5879 if (MaskVals[i] < 0 || MaskVals[i] == i)
5880 InOrder.set(i);
5881 }
Eric Christopherfd179292009-08-27 18:07:15 +00005882
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 // The other elements are put in the right place using pextrw and pinsrw.
5884 for (unsigned i = 0; i != 8; ++i) {
5885 if (InOrder[i])
5886 continue;
5887 int EltIdx = MaskVals[i];
5888 if (EltIdx < 0)
5889 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005890 SDValue ExtOp = (EltIdx < 8) ?
5891 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5892 DAG.getIntPtrConstant(EltIdx)) :
5893 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005894 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005895 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 DAG.getIntPtrConstant(i));
5897 }
5898 return NewV;
5899}
5900
5901// v16i8 shuffles - Prefer shuffles in the following order:
5902// 1. [ssse3] 1 x pshufb
5903// 2. [ssse3] 2 x pshufb + 1 x por
5904// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5905static
Nate Begeman9008ca62009-04-27 18:41:29 +00005906SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005907 SelectionDAG &DAG,
5908 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005909 SDValue V1 = SVOp->getOperand(0);
5910 SDValue V2 = SVOp->getOperand(1);
5911 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005912 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005913
Nate Begemanb9a47b82009-02-23 08:49:38 +00005914 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005915 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005916 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005917
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005919 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005921
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005923 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005924 //
5925 // Otherwise, we have elements from both input vectors, and must zero out
5926 // elements that come from V2 in the first mask, and V1 in the second mask
5927 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 for (unsigned i = 0; i != 16; ++i) {
5929 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005930 if (EltIdx < 0 || EltIdx >= 16)
5931 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005933 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005935 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005937
5938 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5939 // the 2nd operand if it's undefined or zero.
5940 if (V2.getOpcode() == ISD::UNDEF ||
5941 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005943
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 // Calculate the shuffle mask for the second input, shuffle it, and
5945 // OR it with the first shuffled input.
5946 pshufbMask.clear();
5947 for (unsigned i = 0; i != 16; ++i) {
5948 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005949 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005950 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005952 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005953 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005954 MVT::v16i8, &pshufbMask[0], 16));
5955 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 }
Eric Christopherfd179292009-08-27 18:07:15 +00005957
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 // No SSSE3 - Calculate in place words and then fix all out of place words
5959 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5960 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005961 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5962 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005963 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 for (int i = 0; i != 8; ++i) {
5965 int Elt0 = MaskVals[i*2];
5966 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005967
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 // This word of the result is all undef, skip it.
5969 if (Elt0 < 0 && Elt1 < 0)
5970 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005971
Nate Begemanb9a47b82009-02-23 08:49:38 +00005972 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005973 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005975
Nate Begemanb9a47b82009-02-23 08:49:38 +00005976 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5977 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5978 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005979
5980 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5981 // using a single extract together, load it and store it.
5982 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005983 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005984 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005985 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005986 DAG.getIntPtrConstant(i));
5987 continue;
5988 }
5989
Nate Begemanb9a47b82009-02-23 08:49:38 +00005990 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005991 // source byte is not also odd, shift the extracted word left 8 bits
5992 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005994 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005995 DAG.getIntPtrConstant(Elt1 / 2));
5996 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005997 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005998 DAG.getConstant(8,
5999 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006000 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6002 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006003 }
6004 // If Elt0 is defined, extract it from the appropriate source. If the
6005 // source byte is not also even, shift the extracted word right 8 bits. If
6006 // Elt1 was also defined, OR the extracted values together before
6007 // inserting them in the result.
6008 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6011 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006012 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006013 DAG.getConstant(8,
6014 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006015 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006016 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6017 DAG.getConstant(0x00FF, MVT::i16));
6018 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006019 : InsElt0;
6020 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006021 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 DAG.getIntPtrConstant(i));
6023 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006024 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006025}
6026
Elena Demikhovsky41789462012-09-06 12:42:01 +00006027// v32i8 shuffles - Translate to VPSHUFB if possible.
6028static
6029SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006030 const X86Subtarget *Subtarget,
6031 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006032 EVT VT = SVOp->getValueType(0);
6033 SDValue V1 = SVOp->getOperand(0);
6034 SDValue V2 = SVOp->getOperand(1);
6035 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006036 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006037
6038 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006039 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6040 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006041
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006042 // VPSHUFB may be generated if
6043 // (1) one of input vector is undefined or zeroinitializer.
6044 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6045 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006046 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006047 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006048 return SDValue();
6049
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006050 if (V1IsAllZero && !V2IsAllZero) {
6051 CommuteVectorShuffleMask(MaskVals, 32);
6052 V1 = V2;
6053 }
6054 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006055 for (unsigned i = 0; i != 32; i++) {
6056 int EltIdx = MaskVals[i];
6057 if (EltIdx < 0 || EltIdx >= 32)
6058 EltIdx = 0x80;
6059 else {
6060 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6061 // Cross lane is not allowed.
6062 return SDValue();
6063 EltIdx &= 0xf;
6064 }
6065 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6066 }
6067 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6068 DAG.getNode(ISD::BUILD_VECTOR, dl,
6069 MVT::v32i8, &pshufbMask[0], 32));
6070}
6071
Evan Cheng7a831ce2007-12-15 03:00:47 +00006072/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006073/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006074/// done when every pair / quad of shuffle mask elements point to elements in
6075/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006076/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006077static
Nate Begeman9008ca62009-04-27 18:41:29 +00006078SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006079 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006080 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006081 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006082 MVT NewVT;
6083 unsigned Scale;
6084 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006085 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006086 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6087 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6088 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6089 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6090 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6091 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006092 }
6093
Nate Begeman9008ca62009-04-27 18:41:29 +00006094 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006095 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006096 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006097 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006098 int EltIdx = SVOp->getMaskElt(i+j);
6099 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006100 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006101 if (StartIdx < 0)
6102 StartIdx = (EltIdx / Scale);
6103 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006104 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006105 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006106 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006107 }
6108
Craig Topper11ac1f82012-05-04 04:08:44 +00006109 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6110 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006111 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006112}
6113
Evan Chengd880b972008-05-09 21:53:03 +00006114/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006115///
Owen Andersone50ed302009-08-10 22:56:29 +00006116static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006117 SDValue SrcOp, SelectionDAG &DAG,
6118 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006119 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006120 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006121 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006122 LD = dyn_cast<LoadSDNode>(SrcOp);
6123 if (!LD) {
6124 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6125 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006126 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006127 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006128 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006129 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006130 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006131 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006132 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006133 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006134 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6135 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6136 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006137 SrcOp.getOperand(0)
6138 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006139 }
6140 }
6141 }
6142
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006143 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006144 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006145 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006146 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006147}
6148
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006149/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6150/// which could not be matched by any known target speficic shuffle
6151static SDValue
6152LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006153
6154 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6155 if (NewOp.getNode())
6156 return NewOp;
6157
Craig Topper8f35c132012-01-20 09:29:03 +00006158 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006159
Craig Topper8f35c132012-01-20 09:29:03 +00006160 unsigned NumElems = VT.getVectorNumElements();
6161 unsigned NumLaneElems = NumElems / 2;
6162
Craig Topper8f35c132012-01-20 09:29:03 +00006163 DebugLoc dl = SVOp->getDebugLoc();
6164 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006165 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006166 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006167
Craig Topper9a2b6e12012-04-06 07:45:23 +00006168 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006169 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006170 // Build a shuffle mask for the output, discovering on the fly which
6171 // input vectors to use as shuffle operands (recorded in InputUsed).
6172 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006173 // out with UseBuildVector set.
6174 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006175 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006176 unsigned LaneStart = l * NumLaneElems;
6177 for (unsigned i = 0; i != NumLaneElems; ++i) {
6178 // The mask element. This indexes into the input.
6179 int Idx = SVOp->getMaskElt(i+LaneStart);
6180 if (Idx < 0) {
6181 // the mask element does not index into any input vector.
6182 Mask.push_back(-1);
6183 continue;
6184 }
Craig Topper8f35c132012-01-20 09:29:03 +00006185
Craig Topper9a2b6e12012-04-06 07:45:23 +00006186 // The input vector this mask element indexes into.
6187 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006188
Craig Topper9a2b6e12012-04-06 07:45:23 +00006189 // Turn the index into an offset from the start of the input vector.
6190 Idx -= Input * NumLaneElems;
6191
6192 // Find or create a shuffle vector operand to hold this input.
6193 unsigned OpNo;
6194 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6195 if (InputUsed[OpNo] == Input)
6196 // This input vector is already an operand.
6197 break;
6198 if (InputUsed[OpNo] < 0) {
6199 // Create a new operand for this input vector.
6200 InputUsed[OpNo] = Input;
6201 break;
6202 }
6203 }
6204
6205 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006206 // More than two input vectors used! Give up on trying to create a
6207 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6208 UseBuildVector = true;
6209 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006210 }
6211
6212 // Add the mask index for the new shuffle vector.
6213 Mask.push_back(Idx + OpNo * NumLaneElems);
6214 }
6215
Craig Topper8ae97ba2012-05-21 06:40:16 +00006216 if (UseBuildVector) {
6217 SmallVector<SDValue, 16> SVOps;
6218 for (unsigned i = 0; i != NumLaneElems; ++i) {
6219 // The mask element. This indexes into the input.
6220 int Idx = SVOp->getMaskElt(i+LaneStart);
6221 if (Idx < 0) {
6222 SVOps.push_back(DAG.getUNDEF(EltVT));
6223 continue;
6224 }
6225
6226 // The input vector this mask element indexes into.
6227 int Input = Idx / NumElems;
6228
6229 // Turn the index into an offset from the start of the input vector.
6230 Idx -= Input * NumElems;
6231
6232 // Extract the vector element by hand.
6233 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6234 SVOp->getOperand(Input),
6235 DAG.getIntPtrConstant(Idx)));
6236 }
6237
6238 // Construct the output using a BUILD_VECTOR.
6239 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6240 SVOps.size());
6241 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006242 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006243 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006244 } else {
6245 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006246 (InputUsed[0] % 2) * NumLaneElems,
6247 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006248 // If only one input was used, use an undefined vector for the other.
6249 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6250 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006251 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006252 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006253 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006254 }
6255
6256 Mask.clear();
6257 }
Craig Topper8f35c132012-01-20 09:29:03 +00006258
6259 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006260 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006261}
6262
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006263/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6264/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006265static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006266LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006267 SDValue V1 = SVOp->getOperand(0);
6268 SDValue V2 = SVOp->getOperand(1);
6269 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006270 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006271
Craig Topper7a9a28b2012-08-12 02:23:29 +00006272 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006273
Benjamin Kramer9c683542012-01-30 15:16:21 +00006274 std::pair<int, int> Locs[4];
6275 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006276 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006277
Evan Chengace3c172008-07-22 21:13:36 +00006278 unsigned NumHi = 0;
6279 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006280 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006281 int Idx = PermMask[i];
6282 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006283 Locs[i] = std::make_pair(-1, -1);
6284 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006285 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6286 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006287 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006288 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006289 NumLo++;
6290 } else {
6291 Locs[i] = std::make_pair(1, NumHi);
6292 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006293 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006294 NumHi++;
6295 }
6296 }
6297 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006298
Evan Chengace3c172008-07-22 21:13:36 +00006299 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006300 // If no more than two elements come from either vector. This can be
6301 // implemented with two shuffles. First shuffle gather the elements.
6302 // The second shuffle, which takes the first shuffle as both of its
6303 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006304 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006305
Benjamin Kramer9c683542012-01-30 15:16:21 +00006306 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006307
Benjamin Kramer9c683542012-01-30 15:16:21 +00006308 for (unsigned i = 0; i != 4; ++i)
6309 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006310 unsigned Idx = (i < 2) ? 0 : 4;
6311 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006312 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006313 }
Evan Chengace3c172008-07-22 21:13:36 +00006314
Nate Begeman9008ca62009-04-27 18:41:29 +00006315 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006316 }
6317
6318 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006319 // Otherwise, we must have three elements from one vector, call it X, and
6320 // one element from the other, call it Y. First, use a shufps to build an
6321 // intermediate vector with the one element from Y and the element from X
6322 // that will be in the same half in the final destination (the indexes don't
6323 // matter). Then, use a shufps to build the final vector, taking the half
6324 // containing the element from Y from the intermediate, and the other half
6325 // from X.
6326 if (NumHi == 3) {
6327 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006328 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006329 std::swap(V1, V2);
6330 }
6331
6332 // Find the element from V2.
6333 unsigned HiIndex;
6334 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006335 int Val = PermMask[HiIndex];
6336 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006337 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006338 if (Val >= 4)
6339 break;
6340 }
6341
Nate Begeman9008ca62009-04-27 18:41:29 +00006342 Mask1[0] = PermMask[HiIndex];
6343 Mask1[1] = -1;
6344 Mask1[2] = PermMask[HiIndex^1];
6345 Mask1[3] = -1;
6346 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006347
6348 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006349 Mask1[0] = PermMask[0];
6350 Mask1[1] = PermMask[1];
6351 Mask1[2] = HiIndex & 1 ? 6 : 4;
6352 Mask1[3] = HiIndex & 1 ? 4 : 6;
6353 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006354 }
Craig Topper69947b92012-04-23 06:57:04 +00006355
6356 Mask1[0] = HiIndex & 1 ? 2 : 0;
6357 Mask1[1] = HiIndex & 1 ? 0 : 2;
6358 Mask1[2] = PermMask[2];
6359 Mask1[3] = PermMask[3];
6360 if (Mask1[2] >= 0)
6361 Mask1[2] += 4;
6362 if (Mask1[3] >= 0)
6363 Mask1[3] += 4;
6364 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006365 }
6366
6367 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006368 int LoMask[] = { -1, -1, -1, -1 };
6369 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006370
Benjamin Kramer9c683542012-01-30 15:16:21 +00006371 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006372 unsigned MaskIdx = 0;
6373 unsigned LoIdx = 0;
6374 unsigned HiIdx = 2;
6375 for (unsigned i = 0; i != 4; ++i) {
6376 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006377 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006378 MaskIdx = 1;
6379 LoIdx = 0;
6380 HiIdx = 2;
6381 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006382 int Idx = PermMask[i];
6383 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006384 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006385 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006386 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006387 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006388 LoIdx++;
6389 } else {
6390 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006391 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006392 HiIdx++;
6393 }
6394 }
6395
Nate Begeman9008ca62009-04-27 18:41:29 +00006396 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6397 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006398 int MaskOps[] = { -1, -1, -1, -1 };
6399 for (unsigned i = 0; i != 4; ++i)
6400 if (Locs[i].first != -1)
6401 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006402 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006403}
6404
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006405static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006406 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006407 V = V.getOperand(0);
6408 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6409 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006410 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6411 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6412 // BUILD_VECTOR (load), undef
6413 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006414 if (MayFoldLoad(V))
6415 return true;
6416 return false;
6417}
6418
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006419// FIXME: the version above should always be used. Since there's
6420// a bug where several vector shuffles can't be folded because the
6421// DAG is not updated during lowering and a node claims to have two
6422// uses while it only has one, use this version, and let isel match
6423// another instruction if the load really happens to have more than
6424// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006425// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006426static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006427 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006428 V = V.getOperand(0);
6429 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6430 V = V.getOperand(0);
6431 if (ISD::isNormalLoad(V.getNode()))
6432 return true;
6433 return false;
6434}
6435
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006436static
Evan Cheng835580f2010-10-07 20:50:20 +00006437SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6438 EVT VT = Op.getValueType();
6439
6440 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006441 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6442 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006443 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6444 V1, DAG));
6445}
6446
6447static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006448SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006449 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006450 SDValue V1 = Op.getOperand(0);
6451 SDValue V2 = Op.getOperand(1);
6452 EVT VT = Op.getValueType();
6453
6454 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6455
Craig Topper1accb7e2012-01-10 06:54:16 +00006456 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006457 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6458
Evan Cheng0899f5c2011-08-31 02:05:24 +00006459 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6460 return DAG.getNode(ISD::BITCAST, dl, VT,
6461 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6462 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6463 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006464}
6465
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006466static
6467SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6468 SDValue V1 = Op.getOperand(0);
6469 SDValue V2 = Op.getOperand(1);
6470 EVT VT = Op.getValueType();
6471
6472 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6473 "unsupported shuffle type");
6474
6475 if (V2.getOpcode() == ISD::UNDEF)
6476 V2 = V1;
6477
6478 // v4i32 or v4f32
6479 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6480}
6481
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006482static
Craig Topper1accb7e2012-01-10 06:54:16 +00006483SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006484 SDValue V1 = Op.getOperand(0);
6485 SDValue V2 = Op.getOperand(1);
6486 EVT VT = Op.getValueType();
6487 unsigned NumElems = VT.getVectorNumElements();
6488
6489 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6490 // operand of these instructions is only memory, so check if there's a
6491 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6492 // same masks.
6493 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006494
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006495 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006496 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006497 CanFoldLoad = true;
6498
6499 // When V1 is a load, it can be folded later into a store in isel, example:
6500 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6501 // turns into:
6502 // (MOVLPSmr addr:$src1, VR128:$src2)
6503 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006504 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006505 CanFoldLoad = true;
6506
Dan Gohman65fd6562011-11-03 21:49:52 +00006507 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006508 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006509 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006510 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6511
6512 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006513 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006514 if (SVOp->getMaskElt(1) != -1)
6515 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006516 }
6517
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006518 // movl and movlp will both match v2i64, but v2i64 is never matched by
6519 // movl earlier because we make it strict to avoid messing with the movlp load
6520 // folding logic (see the code above getMOVLP call). Match it here then,
6521 // this is horrible, but will stay like this until we move all shuffle
6522 // matching to x86 specific nodes. Note that for the 1st condition all
6523 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006524 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006525 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6526 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006527 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006528 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006529 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006530 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006531
6532 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6533
6534 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006535 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006536 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006537}
6538
Nadav Rotem154819d2012-04-09 07:45:58 +00006539SDValue
6540X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006541 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6542 EVT VT = Op.getValueType();
6543 DebugLoc dl = Op.getDebugLoc();
6544 SDValue V1 = Op.getOperand(0);
6545 SDValue V2 = Op.getOperand(1);
6546
6547 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006548 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006549
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006550 // Handle splat operations
6551 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006552 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006553 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006554
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006555 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006556 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006557 if (Broadcast.getNode())
6558 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006559
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006560 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006561 if ((Size == 128 && NumElem <= 4) ||
6562 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006563 return SDValue();
6564
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006565 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006566 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006567 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006568
6569 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6570 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006571 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6572 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006573 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6574 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006575 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006576 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006577 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006578 // FIXME: Figure out a cleaner way to do this.
6579 // Try to make use of movq to zero out the top part.
6580 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6581 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6582 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006583 EVT NewVT = NewOp.getValueType();
6584 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6585 NewVT, true, false))
6586 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006587 DAG, Subtarget, dl);
6588 }
6589 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6590 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006591 if (NewOp.getNode()) {
6592 EVT NewVT = NewOp.getValueType();
6593 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6594 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6595 DAG, Subtarget, dl);
6596 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006597 }
6598 }
6599 return SDValue();
6600}
6601
Dan Gohman475871a2008-07-27 21:46:04 +00006602SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006603X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006604 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SDValue V1 = Op.getOperand(0);
6606 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006607 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006608 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006609 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006610 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006611 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006612 bool V1IsSplat = false;
6613 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006614 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006615 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006616 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006617 MachineFunction &MF = DAG.getMachineFunction();
6618 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006619
Craig Topper3426a3e2011-11-14 06:46:21 +00006620 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006621
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006622 if (V1IsUndef && V2IsUndef)
6623 return DAG.getUNDEF(VT);
6624
6625 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006626
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006627 // Vector shuffle lowering takes 3 steps:
6628 //
6629 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6630 // narrowing and commutation of operands should be handled.
6631 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6632 // shuffle nodes.
6633 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6634 // so the shuffle can be broken into other shuffles and the legalizer can
6635 // try the lowering again.
6636 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006637 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006638 // be matched during isel, all of them must be converted to a target specific
6639 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006640
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006641 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6642 // narrowing and commutation of operands should be handled. The actual code
6643 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006644 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006645 if (NewOp.getNode())
6646 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006647
Craig Topper5aaffa82012-02-19 02:53:47 +00006648 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6649
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006650 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6651 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006652 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006653 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006654 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006655 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006656
Craig Topperdd637ae2012-02-19 05:41:45 +00006657 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006658 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006659 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006660
Craig Topperdd637ae2012-02-19 05:41:45 +00006661 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006662 return getMOVHighToLow(Op, dl, DAG);
6663
6664 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006665 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006666 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006667 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006668
Craig Topper5aaffa82012-02-19 02:53:47 +00006669 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006670 // The actual implementation will match the mask in the if above and then
6671 // during isel it can match several different instructions, not only pshufd
6672 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006673 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6674 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006675
Craig Topper5aaffa82012-02-19 02:53:47 +00006676 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006677
Craig Topperdbd98a42012-02-07 06:28:42 +00006678 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6679 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6680
Craig Topper1accb7e2012-01-10 06:54:16 +00006681 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006682 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6683
Craig Topperb3982da2011-12-31 23:50:21 +00006684 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006685 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006686 }
Eric Christopherfd179292009-08-27 18:07:15 +00006687
Evan Chengf26ffe92008-05-29 08:22:04 +00006688 // Check if this can be converted into a logical shift.
6689 bool isLeft = false;
6690 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006691 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006692 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006693 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006694 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006695 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006696 EVT EltVT = VT.getVectorElementType();
6697 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006698 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006699 }
Eric Christopherfd179292009-08-27 18:07:15 +00006700
Craig Topper5aaffa82012-02-19 02:53:47 +00006701 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006702 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006703 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006704 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006705 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006706 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6707
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006708 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006709 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6710 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006711 }
Eric Christopherfd179292009-08-27 18:07:15 +00006712
Nate Begeman9008ca62009-04-27 18:41:29 +00006713 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006714 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006715 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006716
Craig Topperdd637ae2012-02-19 05:41:45 +00006717 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006718 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006719
Craig Topperdd637ae2012-02-19 05:41:45 +00006720 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006721 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006722
Craig Topperdd637ae2012-02-19 05:41:45 +00006723 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006724 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006725
Craig Topperdd637ae2012-02-19 05:41:45 +00006726 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006727 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006728
Craig Topperdd637ae2012-02-19 05:41:45 +00006729 if (ShouldXformToMOVHLPS(M, VT) ||
6730 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006731 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006732
Evan Chengf26ffe92008-05-29 08:22:04 +00006733 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006734 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006735 EVT EltVT = VT.getVectorElementType();
6736 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006737 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006738 }
Eric Christopherfd179292009-08-27 18:07:15 +00006739
Evan Cheng9eca5e82006-10-25 21:49:50 +00006740 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006741 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6742 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006743 V1IsSplat = isSplatVector(V1.getNode());
6744 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006745
Chris Lattner8a594482007-11-25 00:24:49 +00006746 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006747 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6748 CommuteVectorShuffleMask(M, NumElems);
6749 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006750 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006751 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006752 }
6753
Craig Topperbeabc6c2011-12-05 06:56:46 +00006754 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006755 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006756 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006757 return V1;
6758 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6759 // the instruction selector will not match, so get a canonical MOVL with
6760 // swapped operands to undo the commute.
6761 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006762 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763
Craig Topperbeabc6c2011-12-05 06:56:46 +00006764 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006765 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006766
Craig Topperbeabc6c2011-12-05 06:56:46 +00006767 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006768 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006769
Evan Cheng9bbbb982006-10-25 20:48:19 +00006770 if (V2IsSplat) {
6771 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006772 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006773 // new vector_shuffle with the corrected mask.p
6774 SmallVector<int, 8> NewMask(M.begin(), M.end());
6775 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006776 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006777 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006778 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006779 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006780 }
6781
Evan Cheng9eca5e82006-10-25 21:49:50 +00006782 if (Commuted) {
6783 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006784 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006785 CommuteVectorShuffleMask(M, NumElems);
6786 std::swap(V1, V2);
6787 std::swap(V1IsSplat, V2IsSplat);
6788 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006789
Craig Topper39a9e482012-02-11 06:24:48 +00006790 if (isUNPCKLMask(M, VT, HasAVX2))
6791 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006792
Craig Topper39a9e482012-02-11 06:24:48 +00006793 if (isUNPCKHMask(M, VT, HasAVX2))
6794 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006795 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796
Nate Begeman9008ca62009-04-27 18:41:29 +00006797 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006798 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006799 return CommuteVectorShuffle(SVOp, DAG);
6800
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006801 // The checks below are all present in isShuffleMaskLegal, but they are
6802 // inlined here right now to enable us to directly emit target specific
6803 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006804
Craig Topper0e2037b2012-01-20 05:53:00 +00006805 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006806 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006807 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006808 DAG);
6809
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006810 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6811 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006812 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006813 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006814 }
6815
Craig Toppera9a568a2012-05-02 08:03:44 +00006816 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006817 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006818 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006819 DAG);
6820
Craig Toppera9a568a2012-05-02 08:03:44 +00006821 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006822 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006823 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006824 DAG);
6825
Craig Topper1a7700a2012-01-19 08:19:12 +00006826 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006827 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006828 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006829
Craig Topper94438ba2011-12-16 08:06:31 +00006830 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006831 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006832 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006833 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006834
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006835 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006836 // Generate target specific nodes for 128 or 256-bit shuffles only
6837 // supported in the AVX instruction set.
6838 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006839
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006840 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006841 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006842 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6843
Craig Topper70b883b2011-11-28 10:14:51 +00006844 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006845 if (isVPERMILPMask(M, VT, HasAVX)) {
6846 if (HasAVX2 && VT == MVT::v8i32)
6847 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006848 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006849 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006850 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006851 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006852
Craig Topper70b883b2011-11-28 10:14:51 +00006853 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006854 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006855 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006856 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006857
Craig Topper1842ba02012-04-23 06:38:28 +00006858 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006859 if (BlendOp.getNode())
6860 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006861
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006862 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006863 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006864 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006865 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006866 }
Craig Topper92040742012-04-16 06:43:40 +00006867 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6868 &permclMask[0], 8);
6869 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006870 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006871 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006872 }
Craig Topper095c5282012-04-15 23:48:57 +00006873
Craig Topper8325c112012-04-16 00:41:45 +00006874 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6875 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006876 getShuffleCLImmediate(SVOp), DAG);
6877
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006878
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006879 //===--------------------------------------------------------------------===//
6880 // Since no target specific shuffle was selected for this generic one,
6881 // lower it into other known shuffles. FIXME: this isn't true yet, but
6882 // this is the plan.
6883 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006884
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006885 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6886 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00006887 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006888 if (NewOp.getNode())
6889 return NewOp;
6890 }
6891
6892 if (VT == MVT::v16i8) {
6893 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6894 if (NewOp.getNode())
6895 return NewOp;
6896 }
6897
Elena Demikhovsky41789462012-09-06 12:42:01 +00006898 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00006899 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00006900 if (NewOp.getNode())
6901 return NewOp;
6902 }
6903
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006904 // Handle all 128-bit wide vectors with 4 elements, and match them with
6905 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006906 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006907 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6908
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006909 // Handle general 256-bit shuffles
6910 if (VT.is256BitVector())
6911 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6912
Dan Gohman475871a2008-07-27 21:46:04 +00006913 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006914}
6915
Dan Gohman475871a2008-07-27 21:46:04 +00006916SDValue
6917X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006918 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006919 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006920 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006921
Craig Topper7a9a28b2012-08-12 02:23:29 +00006922 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006923 return SDValue();
6924
Duncan Sands83ec4b62008-06-06 12:08:01 +00006925 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006927 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006929 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006930 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006931 }
6932
6933 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006934 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6935 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6936 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006937 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6938 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006939 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006941 Op.getOperand(0)),
6942 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006944 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006946 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006947 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006948 }
6949
6950 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006951 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6952 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006953 // result has a single use which is a store or a bitcast to i32. And in
6954 // the case of a store, it's not worth it if the index is a constant 0,
6955 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006956 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006957 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006958 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006959 if ((User->getOpcode() != ISD::STORE ||
6960 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6961 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006962 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006963 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006964 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006965 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006966 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006967 Op.getOperand(0)),
6968 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006969 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006970 }
6971
6972 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006973 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006974 if (isa<ConstantSDNode>(Op.getOperand(1)))
6975 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006976 }
Dan Gohman475871a2008-07-27 21:46:04 +00006977 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006978}
6979
6980
Dan Gohman475871a2008-07-27 21:46:04 +00006981SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006982X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6983 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006984 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006985 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006986
David Greene74a579d2011-02-10 16:57:36 +00006987 SDValue Vec = Op.getOperand(0);
6988 EVT VecVT = Vec.getValueType();
6989
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006990 // If this is a 256-bit vector result, first extract the 128-bit vector and
6991 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006992 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006993 DebugLoc dl = Op.getNode()->getDebugLoc();
6994 unsigned NumElems = VecVT.getVectorNumElements();
6995 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006996 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6997
6998 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006999 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007000
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007001 if (IdxVal >= NumElems/2)
7002 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007003 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007004 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007005 }
7006
Craig Topper7a9a28b2012-08-12 02:23:29 +00007007 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007008
Craig Topperd0a31172012-01-10 06:37:29 +00007009 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007010 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007011 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007012 return Res;
7013 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007014
Owen Andersone50ed302009-08-10 22:56:29 +00007015 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007016 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007017 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007018 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007019 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007020 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007021 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007022 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7023 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007024 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007025 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007026 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007028 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007029 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007030 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007031 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007032 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007033 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007034 }
7035
7036 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007037 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007038 if (Idx == 0)
7039 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007040
Evan Cheng0db9fe62006-04-25 20:13:52 +00007041 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007042 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007043 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007044 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007045 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007046 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007047 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007048 }
7049
7050 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007051 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7052 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7053 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007054 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007055 if (Idx == 0)
7056 return Op;
7057
7058 // UNPCKHPD the element to the lowest double word, then movsd.
7059 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7060 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007061 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007062 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007063 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007064 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007065 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007066 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007067 }
7068
Dan Gohman475871a2008-07-27 21:46:04 +00007069 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007070}
7071
Dan Gohman475871a2008-07-27 21:46:04 +00007072SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007073X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7074 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007075 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007076 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007077 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007078
Dan Gohman475871a2008-07-27 21:46:04 +00007079 SDValue N0 = Op.getOperand(0);
7080 SDValue N1 = Op.getOperand(1);
7081 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007082
Craig Topper7a9a28b2012-08-12 02:23:29 +00007083 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007084 return SDValue();
7085
Dan Gohman8a55ce42009-09-23 21:02:20 +00007086 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007087 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007088 unsigned Opc;
7089 if (VT == MVT::v8i16)
7090 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007091 else if (VT == MVT::v16i8)
7092 Opc = X86ISD::PINSRB;
7093 else
7094 Opc = X86ISD::PINSRB;
7095
Nate Begeman14d12ca2008-02-11 04:19:36 +00007096 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7097 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007098 if (N1.getValueType() != MVT::i32)
7099 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7100 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007101 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007102 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007103 }
7104
7105 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007106 // Bits [7:6] of the constant are the source select. This will always be
7107 // zero here. The DAG Combiner may combine an extract_elt index into these
7108 // bits. For example (insert (extract, 3), 2) could be matched by putting
7109 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007110 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007111 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007112 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007113 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007114 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007115 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007116 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007117 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007118 }
7119
7120 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007121 // PINSR* works with constant index.
7122 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007123 }
Dan Gohman475871a2008-07-27 21:46:04 +00007124 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007125}
7126
Dan Gohman475871a2008-07-27 21:46:04 +00007127SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007128X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007129 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007130 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007131
David Greene6b381262011-02-09 15:32:06 +00007132 DebugLoc dl = Op.getDebugLoc();
7133 SDValue N0 = Op.getOperand(0);
7134 SDValue N1 = Op.getOperand(1);
7135 SDValue N2 = Op.getOperand(2);
7136
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007137 // If this is a 256-bit vector result, first extract the 128-bit vector,
7138 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007139 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007140 if (!isa<ConstantSDNode>(N2))
7141 return SDValue();
7142
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007143 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007144 unsigned NumElems = VT.getVectorNumElements();
7145 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007146 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007147
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007148 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007149 bool Upper = IdxVal >= NumElems/2;
7150 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7151 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007152
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007153 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007154 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007155 }
7156
Craig Topperd0a31172012-01-10 06:37:29 +00007157 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007158 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7159
Dan Gohman8a55ce42009-09-23 21:02:20 +00007160 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007161 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007162
Dan Gohman8a55ce42009-09-23 21:02:20 +00007163 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007164 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7165 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007166 if (N1.getValueType() != MVT::i32)
7167 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7168 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007169 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007170 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007171 }
Dan Gohman475871a2008-07-27 21:46:04 +00007172 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007173}
7174
Craig Topper55b24052012-09-11 06:15:32 +00007175static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007176 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007177 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007178 EVT OpVT = Op.getValueType();
7179
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007180 // If this is a 256-bit vector result, first insert into a 128-bit
7181 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007182 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007183 // Insert into a 128-bit vector.
7184 EVT VT128 = EVT::getVectorVT(*Context,
7185 OpVT.getVectorElementType(),
7186 OpVT.getVectorNumElements() / 2);
7187
7188 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7189
7190 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007191 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007192 }
7193
Craig Topperd77d2fe2012-04-29 20:22:05 +00007194 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007195 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007197
Owen Anderson825b72b2009-08-11 20:47:22 +00007198 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007199 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007200 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007201 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007202}
7203
David Greene91585092011-01-26 15:38:49 +00007204// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7205// a simple subregister reference or explicit instructions to grab
7206// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007207static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7208 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007209 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007210 DebugLoc dl = Op.getNode()->getDebugLoc();
7211 SDValue Vec = Op.getNode()->getOperand(0);
7212 SDValue Idx = Op.getNode()->getOperand(1);
7213
Craig Topper7a9a28b2012-08-12 02:23:29 +00007214 if (Op.getNode()->getValueType(0).is128BitVector() &&
7215 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007216 isa<ConstantSDNode>(Idx)) {
7217 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7218 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007219 }
David Greene91585092011-01-26 15:38:49 +00007220 }
7221 return SDValue();
7222}
7223
David Greenecfe33c42011-01-26 19:13:22 +00007224// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7225// simple superregister reference or explicit instructions to insert
7226// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007227static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7228 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007229 if (Subtarget->hasAVX()) {
7230 DebugLoc dl = Op.getNode()->getDebugLoc();
7231 SDValue Vec = Op.getNode()->getOperand(0);
7232 SDValue SubVec = Op.getNode()->getOperand(1);
7233 SDValue Idx = Op.getNode()->getOperand(2);
7234
Craig Topper7a9a28b2012-08-12 02:23:29 +00007235 if (Op.getNode()->getValueType(0).is256BitVector() &&
7236 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007237 isa<ConstantSDNode>(Idx)) {
7238 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7239 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007240 }
7241 }
7242 return SDValue();
7243}
7244
Bill Wendling056292f2008-09-16 21:48:12 +00007245// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7246// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7247// one of the above mentioned nodes. It has to be wrapped because otherwise
7248// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7249// be used to form addressing mode. These wrapped nodes will be selected
7250// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007251SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007252X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007253 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007254
Chris Lattner41621a22009-06-26 19:22:52 +00007255 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7256 // global base reg.
7257 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007258 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007259 CodeModel::Model M = getTargetMachine().getCodeModel();
7260
Chris Lattner4f066492009-07-11 20:29:19 +00007261 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007262 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007263 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007264 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007265 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007266 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007267 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007268
Evan Cheng1606e8e2009-03-13 07:51:59 +00007269 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007270 CP->getAlignment(),
7271 CP->getOffset(), OpFlag);
7272 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007273 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007274 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007275 if (OpFlag) {
7276 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007277 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007278 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007279 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007280 }
7281
7282 return Result;
7283}
7284
Dan Gohmand858e902010-04-17 15:26:15 +00007285SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007286 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007287
Chris Lattner18c59872009-06-27 04:16:01 +00007288 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7289 // global base reg.
7290 unsigned char OpFlag = 0;
7291 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007292 CodeModel::Model M = getTargetMachine().getCodeModel();
7293
Chris Lattner4f066492009-07-11 20:29:19 +00007294 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007295 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007296 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007297 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007298 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007299 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007300 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007301
Chris Lattner18c59872009-06-27 04:16:01 +00007302 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7303 OpFlag);
7304 DebugLoc DL = JT->getDebugLoc();
7305 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007306
Chris Lattner18c59872009-06-27 04:16:01 +00007307 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007308 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007309 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7310 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007311 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007312 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007313
Chris Lattner18c59872009-06-27 04:16:01 +00007314 return Result;
7315}
7316
7317SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007318X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007319 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007320
Chris Lattner18c59872009-06-27 04:16:01 +00007321 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7322 // global base reg.
7323 unsigned char OpFlag = 0;
7324 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007325 CodeModel::Model M = getTargetMachine().getCodeModel();
7326
Chris Lattner4f066492009-07-11 20:29:19 +00007327 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007328 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7329 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7330 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007331 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007332 } else if (Subtarget->isPICStyleGOT()) {
7333 OpFlag = X86II::MO_GOT;
7334 } else if (Subtarget->isPICStyleStubPIC()) {
7335 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7336 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7337 OpFlag = X86II::MO_DARWIN_NONLAZY;
7338 }
Eric Christopherfd179292009-08-27 18:07:15 +00007339
Chris Lattner18c59872009-06-27 04:16:01 +00007340 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007341
Chris Lattner18c59872009-06-27 04:16:01 +00007342 DebugLoc DL = Op.getDebugLoc();
7343 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007344
7345
Chris Lattner18c59872009-06-27 04:16:01 +00007346 // With PIC, the address is actually $g + Offset.
7347 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007348 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007349 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7350 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007351 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007352 Result);
7353 }
Eric Christopherfd179292009-08-27 18:07:15 +00007354
Eli Friedman586272d2011-08-11 01:48:05 +00007355 // For symbols that require a load from a stub to get the address, emit the
7356 // load.
7357 if (isGlobalStubReference(OpFlag))
7358 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007359 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007360
Chris Lattner18c59872009-06-27 04:16:01 +00007361 return Result;
7362}
7363
Dan Gohman475871a2008-07-27 21:46:04 +00007364SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007365X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007366 // Create the TargetBlockAddressAddress node.
7367 unsigned char OpFlags =
7368 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007369 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007370 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007371 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007372 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007373 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7374 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007375
Dan Gohmanf705adb2009-10-30 01:28:02 +00007376 if (Subtarget->isPICStyleRIPRel() &&
7377 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007378 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7379 else
7380 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007381
Dan Gohman29cbade2009-11-20 23:18:13 +00007382 // With PIC, the address is actually $g + Offset.
7383 if (isGlobalRelativeToPICBase(OpFlags)) {
7384 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7385 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7386 Result);
7387 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007388
7389 return Result;
7390}
7391
7392SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007393X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007394 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007395 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007396 // Create the TargetGlobalAddress node, folding in the constant
7397 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007398 unsigned char OpFlags =
7399 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007400 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007401 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007402 if (OpFlags == X86II::MO_NO_FLAG &&
7403 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007404 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007405 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007406 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007407 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007408 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007409 }
Eric Christopherfd179292009-08-27 18:07:15 +00007410
Chris Lattner4f066492009-07-11 20:29:19 +00007411 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007412 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007413 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7414 else
7415 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007416
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007417 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007418 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007419 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7420 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007421 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007422 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007423
Chris Lattner36c25012009-07-10 07:34:39 +00007424 // For globals that require a load from a stub to get the address, emit the
7425 // load.
7426 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007427 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007428 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007429
Dan Gohman6520e202008-10-18 02:06:02 +00007430 // If there was a non-zero offset that we didn't fold, create an explicit
7431 // addition for it.
7432 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007433 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007434 DAG.getConstant(Offset, getPointerTy()));
7435
Evan Cheng0db9fe62006-04-25 20:13:52 +00007436 return Result;
7437}
7438
Evan Chengda43bcf2008-09-24 00:05:32 +00007439SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007440X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007441 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007442 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007443 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007444}
7445
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007446static SDValue
7447GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007448 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007449 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007450 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007451 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007452 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007453 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007454 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007455 GA->getOffset(),
7456 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007457
7458 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7459 : X86ISD::TLSADDR;
7460
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007461 if (InFlag) {
7462 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007463 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007464 } else {
7465 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007466 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007467 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007468
7469 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007470 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007471
Rafael Espindola15f1b662009-04-24 12:59:40 +00007472 SDValue Flag = Chain.getValue(1);
7473 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007474}
7475
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007476// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007477static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007478LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007479 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007480 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007481 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7482 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007483 DAG.getNode(X86ISD::GlobalBaseReg,
7484 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007485 InFlag = Chain.getValue(1);
7486
Chris Lattnerb903bed2009-06-26 21:20:29 +00007487 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007488}
7489
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007490// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007491static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007492LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007493 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007494 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7495 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007496}
7497
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007498static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7499 SelectionDAG &DAG,
7500 const EVT PtrVT,
7501 bool is64Bit) {
7502 DebugLoc dl = GA->getDebugLoc();
7503
7504 // Get the start address of the TLS block for this module.
7505 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7506 .getInfo<X86MachineFunctionInfo>();
7507 MFI->incNumLocalDynamicTLSAccesses();
7508
7509 SDValue Base;
7510 if (is64Bit) {
7511 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7512 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7513 } else {
7514 SDValue InFlag;
7515 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7516 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7517 InFlag = Chain.getValue(1);
7518 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7519 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7520 }
7521
7522 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7523 // of Base.
7524
7525 // Build x@dtpoff.
7526 unsigned char OperandFlags = X86II::MO_DTPOFF;
7527 unsigned WrapperKind = X86ISD::Wrapper;
7528 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7529 GA->getValueType(0),
7530 GA->getOffset(), OperandFlags);
7531 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7532
7533 // Add x@dtpoff with the base.
7534 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7535}
7536
Hans Wennborg228756c2012-05-11 10:11:01 +00007537// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007538static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007539 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007540 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007541 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007542
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007543 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7544 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7545 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007546
Michael J. Spencerec38de22010-10-10 22:04:20 +00007547 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007548 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007549 MachinePointerInfo(Ptr),
7550 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007551
Chris Lattnerb903bed2009-06-26 21:20:29 +00007552 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007553 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7554 // initialexec.
7555 unsigned WrapperKind = X86ISD::Wrapper;
7556 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007557 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007558 } else if (model == TLSModel::InitialExec) {
7559 if (is64Bit) {
7560 OperandFlags = X86II::MO_GOTTPOFF;
7561 WrapperKind = X86ISD::WrapperRIP;
7562 } else {
7563 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7564 }
Chris Lattner18c59872009-06-27 04:16:01 +00007565 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007566 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007567 }
Eric Christopherfd179292009-08-27 18:07:15 +00007568
Hans Wennborg228756c2012-05-11 10:11:01 +00007569 // emit "addl x@ntpoff,%eax" (local exec)
7570 // or "addl x@indntpoff,%eax" (initial exec)
7571 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007572 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007573 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007574 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007575 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007576
Hans Wennborg228756c2012-05-11 10:11:01 +00007577 if (model == TLSModel::InitialExec) {
7578 if (isPIC && !is64Bit) {
7579 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7580 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7581 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007582 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007583
7584 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7585 MachinePointerInfo::getGOT(), false, false, false,
7586 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007587 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007588
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007589 // The address of the thread local variable is the add of the thread
7590 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007591 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007592}
7593
Dan Gohman475871a2008-07-27 21:46:04 +00007594SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007595X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007596
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007597 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007598 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007599
Eric Christopher30ef0e52010-06-03 04:07:48 +00007600 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007601 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007602
Eric Christopher30ef0e52010-06-03 04:07:48 +00007603 switch (model) {
7604 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007605 if (Subtarget->is64Bit())
7606 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7607 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007608 case TLSModel::LocalDynamic:
7609 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7610 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007611 case TLSModel::InitialExec:
7612 case TLSModel::LocalExec:
7613 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007614 Subtarget->is64Bit(),
7615 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007616 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007617 llvm_unreachable("Unknown TLS model.");
7618 }
7619
7620 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007621 // Darwin only has one model of TLS. Lower to that.
7622 unsigned char OpFlag = 0;
7623 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7624 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007625
Eric Christopher30ef0e52010-06-03 04:07:48 +00007626 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7627 // global base reg.
7628 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7629 !Subtarget->is64Bit();
7630 if (PIC32)
7631 OpFlag = X86II::MO_TLVP_PIC_BASE;
7632 else
7633 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007634 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007635 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007636 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007637 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007638 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007639
Eric Christopher30ef0e52010-06-03 04:07:48 +00007640 // With PIC32, the address is actually $g + Offset.
7641 if (PIC32)
7642 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7643 DAG.getNode(X86ISD::GlobalBaseReg,
7644 DebugLoc(), getPointerTy()),
7645 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007646
Eric Christopher30ef0e52010-06-03 04:07:48 +00007647 // Lowering the machine isd will make sure everything is in the right
7648 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007649 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007650 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007651 SDValue Args[] = { Chain, Offset };
7652 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007653
Eric Christopher30ef0e52010-06-03 04:07:48 +00007654 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7655 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7656 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007657
Eric Christopher30ef0e52010-06-03 04:07:48 +00007658 // And our return value (tls address) is in the standard call return value
7659 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007660 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007661 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7662 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007663 }
7664
7665 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007666 // Just use the implicit TLS architecture
7667 // Need to generate someting similar to:
7668 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7669 // ; from TEB
7670 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7671 // mov rcx, qword [rdx+rcx*8]
7672 // mov eax, .tls$:tlsvar
7673 // [rax+rcx] contains the address
7674 // Windows 64bit: gs:0x58
7675 // Windows 32bit: fs:__tls_array
7676
7677 // If GV is an alias then use the aliasee for determining
7678 // thread-localness.
7679 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7680 GV = GA->resolveAliasedGlobal(false);
7681 DebugLoc dl = GA->getDebugLoc();
7682 SDValue Chain = DAG.getEntryNode();
7683
7684 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7685 // %gs:0x58 (64-bit).
7686 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7687 ? Type::getInt8PtrTy(*DAG.getContext(),
7688 256)
7689 : Type::getInt32PtrTy(*DAG.getContext(),
7690 257));
7691
7692 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7693 Subtarget->is64Bit()
7694 ? DAG.getIntPtrConstant(0x58)
7695 : DAG.getExternalSymbol("_tls_array",
7696 getPointerTy()),
7697 MachinePointerInfo(Ptr),
7698 false, false, false, 0);
7699
7700 // Load the _tls_index variable
7701 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7702 if (Subtarget->is64Bit())
7703 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7704 IDX, MachinePointerInfo(), MVT::i32,
7705 false, false, 0);
7706 else
7707 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7708 false, false, false, 0);
7709
7710 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007711 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007712 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7713
7714 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7715 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7716 false, false, false, 0);
7717
7718 // Get the offset of start of .tls section
7719 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7720 GA->getValueType(0),
7721 GA->getOffset(), X86II::MO_SECREL);
7722 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7723
7724 // The address of the thread local variable is the add of the thread
7725 // pointer with the offset of the variable.
7726 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007727 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007728
David Blaikie4d6ccb52012-01-20 21:51:11 +00007729 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007730}
7731
Evan Cheng0db9fe62006-04-25 20:13:52 +00007732
Chad Rosierb90d2a92012-01-03 23:19:12 +00007733/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7734/// and take a 2 x i32 value to shift plus a shift amount.
7735SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007736 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007737 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007738 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007739 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007740 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007741 SDValue ShOpLo = Op.getOperand(0);
7742 SDValue ShOpHi = Op.getOperand(1);
7743 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007744 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007745 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007746 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007747
Dan Gohman475871a2008-07-27 21:46:04 +00007748 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007749 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007750 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7751 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007752 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007753 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7754 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007755 }
Evan Chenge3413162006-01-09 18:33:28 +00007756
Owen Anderson825b72b2009-08-11 20:47:22 +00007757 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7758 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007759 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007761
Dan Gohman475871a2008-07-27 21:46:04 +00007762 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007763 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007764 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7765 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007766
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007767 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007768 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7769 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007770 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007771 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7772 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007773 }
7774
Dan Gohman475871a2008-07-27 21:46:04 +00007775 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007776 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777}
Evan Chenga3195e82006-01-12 22:54:21 +00007778
Dan Gohmand858e902010-04-17 15:26:15 +00007779SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7780 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007781 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007782
Dale Johannesen0488fb62010-09-30 23:57:10 +00007783 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007784 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007785
Owen Anderson825b72b2009-08-11 20:47:22 +00007786 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007787 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007788
Eli Friedman36df4992009-05-27 00:47:34 +00007789 // These are really Legal; return the operand so the caller accepts it as
7790 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007791 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007792 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007793 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007794 Subtarget->is64Bit()) {
7795 return Op;
7796 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007797
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007798 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007799 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007800 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007801 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007802 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007803 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007804 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007805 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007806 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007807 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7808}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007809
Owen Andersone50ed302009-08-10 22:56:29 +00007810SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007811 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007812 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007813 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007814 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007815 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007816 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007817 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007818 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007819 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007820 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007821
Chris Lattner492a43e2010-09-22 01:28:21 +00007822 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007823
Stuart Hastings84be9582011-06-02 15:57:11 +00007824 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7825 MachineMemOperand *MMO;
7826 if (FI) {
7827 int SSFI = FI->getIndex();
7828 MMO =
7829 DAG.getMachineFunction()
7830 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7831 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7832 } else {
7833 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7834 StackSlot = StackSlot.getOperand(1);
7835 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007836 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007837 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7838 X86ISD::FILD, DL,
7839 Tys, Ops, array_lengthof(Ops),
7840 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007841
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007842 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007843 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007844 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007845
7846 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7847 // shouldn't be necessary except that RFP cannot be live across
7848 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007849 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007850 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7851 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007852 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007853 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007854 SDValue Ops[] = {
7855 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7856 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007857 MachineMemOperand *MMO =
7858 DAG.getMachineFunction()
7859 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007860 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007861
Chris Lattner492a43e2010-09-22 01:28:21 +00007862 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7863 Ops, array_lengthof(Ops),
7864 Op.getValueType(), MMO);
7865 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007866 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007867 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007868 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007869
Evan Cheng0db9fe62006-04-25 20:13:52 +00007870 return Result;
7871}
7872
Bill Wendling8b8a6362009-01-17 03:56:04 +00007873// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007874SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7875 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007876 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007877 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007878 movq %rax, %xmm0
7879 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7880 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7881 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007882 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007883 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007884 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007885 addpd %xmm1, %xmm0
7886 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007887 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007888
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007889 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007890 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007891
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007892 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007893 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7894 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007895 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007896
Chris Lattner97484792012-01-25 09:56:22 +00007897 SmallVector<Constant*,2> CV1;
7898 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007899 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007900 CV1.push_back(
7901 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7902 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007903 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007904
Bill Wendling397ae212012-01-05 02:13:20 +00007905 // Load the 64-bit value into an XMM register.
7906 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7907 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007909 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007910 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007911 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7912 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7913 CLod0);
7914
Owen Anderson825b72b2009-08-11 20:47:22 +00007915 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007916 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007917 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007918 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007919 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007920 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007921
Craig Topperd0a31172012-01-10 06:37:29 +00007922 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007923 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7924 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7925 } else {
7926 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7927 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7928 S2F, 0x4E, DAG);
7929 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7930 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7931 Sub);
7932 }
7933
7934 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007935 DAG.getIntPtrConstant(0));
7936}
7937
Bill Wendling8b8a6362009-01-17 03:56:04 +00007938// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007939SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7940 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007941 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007942 // FP constant to bias correct the final result.
7943 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007944 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007945
7946 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007947 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007948 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007949
Eli Friedmanf3704762011-08-29 21:15:46 +00007950 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007951 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007952
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007954 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007955 DAG.getIntPtrConstant(0));
7956
7957 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007958 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007959 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007960 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007962 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007963 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 MVT::v2f64, Bias)));
7965 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007966 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007967 DAG.getIntPtrConstant(0));
7968
7969 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007970 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007971
7972 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007973 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007974
Craig Topper69947b92012-04-23 06:57:04 +00007975 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007976 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007977 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007978 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007979 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007980
7981 // Handle final rounding.
7982 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007983}
7984
Dan Gohmand858e902010-04-17 15:26:15 +00007985SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7986 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007987 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007988 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007989
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007990 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007991 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7992 // the optimization here.
7993 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007994 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007995
Owen Andersone50ed302009-08-10 22:56:29 +00007996 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007997 EVT DstVT = Op.getValueType();
7998 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007999 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008000 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008001 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008002 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008003 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008004
8005 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008006 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008007 if (SrcVT == MVT::i32) {
8008 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8009 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8010 getPointerTy(), StackSlot, WordOff);
8011 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008012 StackSlot, MachinePointerInfo(),
8013 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008014 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008015 OffsetSlot, MachinePointerInfo(),
8016 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008017 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8018 return Fild;
8019 }
8020
8021 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8022 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008023 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008024 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008025 // For i64 source, we need to add the appropriate power of 2 if the input
8026 // was negative. This is the same as the optimization in
8027 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8028 // we must be careful to do the computation in x87 extended precision, not
8029 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008030 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8031 MachineMemOperand *MMO =
8032 DAG.getMachineFunction()
8033 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8034 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008035
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008036 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8037 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008038 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8039 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008040
8041 APInt FF(32, 0x5F800000ULL);
8042
8043 // Check whether the sign bit is set.
8044 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8045 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8046 ISD::SETLT);
8047
8048 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8049 SDValue FudgePtr = DAG.getConstantPool(
8050 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8051 getPointerTy());
8052
8053 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8054 SDValue Zero = DAG.getIntPtrConstant(0);
8055 SDValue Four = DAG.getIntPtrConstant(4);
8056 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8057 Zero, Four);
8058 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8059
8060 // Load the value out, extending it from f32 to f80.
8061 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008062 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008063 FudgePtr, MachinePointerInfo::getConstantPool(),
8064 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008065 // Extend everything to 80 bits to force it to be done on x87.
8066 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8067 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008068}
8069
Dan Gohman475871a2008-07-27 21:46:04 +00008070std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008071FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008072 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008073
Owen Andersone50ed302009-08-10 22:56:29 +00008074 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008075
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008076 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8078 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008079 }
8080
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8082 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008083 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008084
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008085 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008086 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008087 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008088 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008089 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008090 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008091 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008092 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008093
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008094 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8095 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008096 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008097 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008098 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008099 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008100
Evan Cheng0db9fe62006-04-25 20:13:52 +00008101 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008102 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8103 Opc = X86ISD::WIN_FTOL;
8104 else
8105 switch (DstTy.getSimpleVT().SimpleTy) {
8106 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8107 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8108 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8109 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8110 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008111
Dan Gohman475871a2008-07-27 21:46:04 +00008112 SDValue Chain = DAG.getEntryNode();
8113 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008114 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008115 // FIXME This causes a redundant load/store if the SSE-class value is already
8116 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008117 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008118 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008119 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008120 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008121 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008122 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008123 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008124 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008125 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008126
Chris Lattner492a43e2010-09-22 01:28:21 +00008127 MachineMemOperand *MMO =
8128 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8129 MachineMemOperand::MOLoad, MemSize, MemSize);
8130 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8131 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008132 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008133 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008134 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8135 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008136
Chris Lattner07290932010-09-22 01:05:16 +00008137 MachineMemOperand *MMO =
8138 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8139 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008140
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008141 if (Opc != X86ISD::WIN_FTOL) {
8142 // Build the FP_TO_INT*_IN_MEM
8143 SDValue Ops[] = { Chain, Value, StackSlot };
8144 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8145 Ops, 3, DstTy, MMO);
8146 return std::make_pair(FIST, StackSlot);
8147 } else {
8148 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8149 DAG.getVTList(MVT::Other, MVT::Glue),
8150 Chain, Value);
8151 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8152 MVT::i32, ftol.getValue(1));
8153 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8154 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008155 SDValue Ops[] = { eax, edx };
8156 SDValue pair = IsReplace
8157 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8158 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008159 return std::make_pair(pair, SDValue());
8160 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008161}
8162
Dan Gohmand858e902010-04-17 15:26:15 +00008163SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8164 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008165 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008166 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008167
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008168 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8169 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008170 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008171 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8172 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008173
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008174 if (StackSlot.getNode())
8175 // Load the result.
8176 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8177 FIST, StackSlot, MachinePointerInfo(),
8178 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008179
8180 // The node is the result.
8181 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008182}
8183
Dan Gohmand858e902010-04-17 15:26:15 +00008184SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8185 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008186 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8187 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008188 SDValue FIST = Vals.first, StackSlot = Vals.second;
8189 assert(FIST.getNode() && "Unexpected failure");
8190
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008191 if (StackSlot.getNode())
8192 // Load the result.
8193 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8194 FIST, StackSlot, MachinePointerInfo(),
8195 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008196
8197 // The node is the result.
8198 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008199}
8200
Craig Topper43620672012-09-08 07:31:51 +00008201SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008202 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008203 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008204 EVT VT = Op.getValueType();
8205 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008206 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8207 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008208 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008209 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008210 }
Craig Topper43620672012-09-08 07:31:51 +00008211 Constant *C;
8212 if (EltVT == MVT::f64)
8213 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8214 else
8215 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8216 C = ConstantVector::getSplat(NumElts, C);
8217 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8218 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008219 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008220 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008221 false, false, false, Alignment);
8222 if (VT.isVector()) {
8223 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8224 return DAG.getNode(ISD::BITCAST, dl, VT,
8225 DAG.getNode(ISD::AND, dl, ANDVT,
8226 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8227 Op.getOperand(0)),
8228 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8229 }
Dale Johannesenace16102009-02-03 19:33:06 +00008230 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008231}
8232
Dan Gohmand858e902010-04-17 15:26:15 +00008233SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008234 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008235 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008236 EVT VT = Op.getValueType();
8237 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008238 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8239 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008240 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008241 NumElts = VT.getVectorNumElements();
8242 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008243 Constant *C;
8244 if (EltVT == MVT::f64)
8245 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8246 else
8247 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8248 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008249 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8250 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008251 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008252 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008253 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008254 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008255 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008256 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008257 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008258 DAG.getNode(ISD::BITCAST, dl, XORVT,
8259 Op.getOperand(0)),
8260 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008261 }
Craig Topper69947b92012-04-23 06:57:04 +00008262
8263 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008264}
8265
Dan Gohmand858e902010-04-17 15:26:15 +00008266SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008267 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008268 SDValue Op0 = Op.getOperand(0);
8269 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008270 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008271 EVT VT = Op.getValueType();
8272 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008273
8274 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008275 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008276 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008277 SrcVT = VT;
8278 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008279 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008280 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008281 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008282 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008283 }
8284
8285 // At this point the operands and the result should have the same
8286 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008287
Evan Cheng68c47cb2007-01-05 07:55:56 +00008288 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008289 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008290 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008291 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8292 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008293 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008294 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8295 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8296 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8297 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008298 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008299 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008300 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008301 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008302 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008303 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008304 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008305
8306 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008307 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008308 // Op0 is MVT::f32, Op1 is MVT::f64.
8309 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8310 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8311 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008312 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008313 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008314 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008315 }
8316
Evan Cheng73d6cf12007-01-05 21:37:56 +00008317 // Clear first operand sign bit.
8318 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008319 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008320 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8321 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008322 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008323 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8324 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8325 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8326 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008327 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008328 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008329 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008330 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008331 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008332 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008333 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008334
8335 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008336 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008337}
8338
Craig Topper55b24052012-09-11 06:15:32 +00008339static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008340 SDValue N0 = Op.getOperand(0);
8341 DebugLoc dl = Op.getDebugLoc();
8342 EVT VT = Op.getValueType();
8343
8344 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8345 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8346 DAG.getConstant(1, VT));
8347 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8348}
8349
Dan Gohman076aee32009-03-04 19:44:21 +00008350/// Emit nodes that will be selected as "test Op0,Op0", or something
8351/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008352SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008353 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008354 DebugLoc dl = Op.getDebugLoc();
8355
Dan Gohman31125812009-03-07 01:58:32 +00008356 // CF and OF aren't always set the way we want. Determine which
8357 // of these we need.
8358 bool NeedCF = false;
8359 bool NeedOF = false;
8360 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008361 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008362 case X86::COND_A: case X86::COND_AE:
8363 case X86::COND_B: case X86::COND_BE:
8364 NeedCF = true;
8365 break;
8366 case X86::COND_G: case X86::COND_GE:
8367 case X86::COND_L: case X86::COND_LE:
8368 case X86::COND_O: case X86::COND_NO:
8369 NeedOF = true;
8370 break;
Dan Gohman31125812009-03-07 01:58:32 +00008371 }
8372
Dan Gohman076aee32009-03-04 19:44:21 +00008373 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008374 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8375 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008376 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8377 // Emit a CMP with 0, which is the TEST pattern.
8378 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8379 DAG.getConstant(0, Op.getValueType()));
8380
8381 unsigned Opcode = 0;
8382 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008383
8384 // Truncate operations may prevent the merge of the SETCC instruction
8385 // and the arithmetic intruction before it. Attempt to truncate the operands
8386 // of the arithmetic instruction and use a reduced bit-width instruction.
8387 bool NeedTruncation = false;
8388 SDValue ArithOp = Op;
8389 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8390 SDValue Arith = Op->getOperand(0);
8391 // Both the trunc and the arithmetic op need to have one user each.
8392 if (Arith->hasOneUse())
8393 switch (Arith.getOpcode()) {
8394 default: break;
8395 case ISD::ADD:
8396 case ISD::SUB:
8397 case ISD::AND:
8398 case ISD::OR:
8399 case ISD::XOR: {
8400 NeedTruncation = true;
8401 ArithOp = Arith;
8402 }
8403 }
8404 }
8405
8406 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8407 // which may be the result of a CAST. We use the variable 'Op', which is the
8408 // non-casted variable when we check for possible users.
8409 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008410 case ISD::ADD:
8411 // Due to an isel shortcoming, be conservative if this add is likely to be
8412 // selected as part of a load-modify-store instruction. When the root node
8413 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8414 // uses of other nodes in the match, such as the ADD in this case. This
8415 // leads to the ADD being left around and reselected, with the result being
8416 // two adds in the output. Alas, even if none our users are stores, that
8417 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8418 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8419 // climbing the DAG back to the root, and it doesn't seem to be worth the
8420 // effort.
8421 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008422 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8423 if (UI->getOpcode() != ISD::CopyToReg &&
8424 UI->getOpcode() != ISD::SETCC &&
8425 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008426 goto default_case;
8427
8428 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008429 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008430 // An add of one will be selected as an INC.
8431 if (C->getAPIntValue() == 1) {
8432 Opcode = X86ISD::INC;
8433 NumOperands = 1;
8434 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008435 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008436
8437 // An add of negative one (subtract of one) will be selected as a DEC.
8438 if (C->getAPIntValue().isAllOnesValue()) {
8439 Opcode = X86ISD::DEC;
8440 NumOperands = 1;
8441 break;
8442 }
Dan Gohman076aee32009-03-04 19:44:21 +00008443 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008444
8445 // Otherwise use a regular EFLAGS-setting add.
8446 Opcode = X86ISD::ADD;
8447 NumOperands = 2;
8448 break;
8449 case ISD::AND: {
8450 // If the primary and result isn't used, don't bother using X86ISD::AND,
8451 // because a TEST instruction will be better.
8452 bool NonFlagUse = false;
8453 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8454 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8455 SDNode *User = *UI;
8456 unsigned UOpNo = UI.getOperandNo();
8457 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8458 // Look pass truncate.
8459 UOpNo = User->use_begin().getOperandNo();
8460 User = *User->use_begin();
8461 }
8462
8463 if (User->getOpcode() != ISD::BRCOND &&
8464 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008465 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008466 NonFlagUse = true;
8467 break;
8468 }
Dan Gohman076aee32009-03-04 19:44:21 +00008469 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008470
8471 if (!NonFlagUse)
8472 break;
8473 }
8474 // FALL THROUGH
8475 case ISD::SUB:
8476 case ISD::OR:
8477 case ISD::XOR:
8478 // Due to the ISEL shortcoming noted above, be conservative if this op is
8479 // likely to be selected as part of a load-modify-store instruction.
8480 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8481 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8482 if (UI->getOpcode() == ISD::STORE)
8483 goto default_case;
8484
8485 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008486 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008487 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008488 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008489 case ISD::OR: Opcode = X86ISD::OR; break;
8490 case ISD::XOR: Opcode = X86ISD::XOR; break;
8491 case ISD::AND: Opcode = X86ISD::AND; break;
8492 }
8493
8494 NumOperands = 2;
8495 break;
8496 case X86ISD::ADD:
8497 case X86ISD::SUB:
8498 case X86ISD::INC:
8499 case X86ISD::DEC:
8500 case X86ISD::OR:
8501 case X86ISD::XOR:
8502 case X86ISD::AND:
8503 return SDValue(Op.getNode(), 1);
8504 default:
8505 default_case:
8506 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008507 }
8508
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008509 // If we found that truncation is beneficial, perform the truncation and
8510 // update 'Op'.
8511 if (NeedTruncation) {
8512 EVT VT = Op.getValueType();
8513 SDValue WideVal = Op->getOperand(0);
8514 EVT WideVT = WideVal.getValueType();
8515 unsigned ConvertedOp = 0;
8516 // Use a target machine opcode to prevent further DAGCombine
8517 // optimizations that may separate the arithmetic operations
8518 // from the setcc node.
8519 switch (WideVal.getOpcode()) {
8520 default: break;
8521 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8522 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8523 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8524 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8525 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8526 }
8527
8528 if (ConvertedOp) {
8529 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8530 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8531 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8532 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8533 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8534 }
8535 }
8536 }
8537
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008538 if (Opcode == 0)
8539 // Emit a CMP with 0, which is the TEST pattern.
8540 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8541 DAG.getConstant(0, Op.getValueType()));
8542
8543 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8544 SmallVector<SDValue, 4> Ops;
8545 for (unsigned i = 0; i != NumOperands; ++i)
8546 Ops.push_back(Op.getOperand(i));
8547
8548 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8549 DAG.ReplaceAllUsesWith(Op, New);
8550 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008551}
8552
8553/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8554/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008555SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008556 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8558 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008559 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008560
8561 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008562 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8563 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8564 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8565 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8566 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8567 Op0, Op1);
8568 return SDValue(Sub.getNode(), 1);
8569 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008570 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008571}
8572
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008573/// Convert a comparison if required by the subtarget.
8574SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8575 SelectionDAG &DAG) const {
8576 // If the subtarget does not support the FUCOMI instruction, floating-point
8577 // comparisons have to be converted.
8578 if (Subtarget->hasCMov() ||
8579 Cmp.getOpcode() != X86ISD::CMP ||
8580 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8581 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8582 return Cmp;
8583
8584 // The instruction selector will select an FUCOM instruction instead of
8585 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8586 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8587 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8588 DebugLoc dl = Cmp.getDebugLoc();
8589 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8590 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8591 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8592 DAG.getConstant(8, MVT::i8));
8593 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8594 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8595}
8596
Evan Chengd40d03e2010-01-06 19:38:29 +00008597/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8598/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008599SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8600 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008601 SDValue Op0 = And.getOperand(0);
8602 SDValue Op1 = And.getOperand(1);
8603 if (Op0.getOpcode() == ISD::TRUNCATE)
8604 Op0 = Op0.getOperand(0);
8605 if (Op1.getOpcode() == ISD::TRUNCATE)
8606 Op1 = Op1.getOperand(0);
8607
Evan Chengd40d03e2010-01-06 19:38:29 +00008608 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008609 if (Op1.getOpcode() == ISD::SHL)
8610 std::swap(Op0, Op1);
8611 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008612 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8613 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008614 // If we looked past a truncate, check that it's only truncating away
8615 // known zeros.
8616 unsigned BitWidth = Op0.getValueSizeInBits();
8617 unsigned AndBitWidth = And.getValueSizeInBits();
8618 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008619 APInt Zeros, Ones;
8620 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008621 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8622 return SDValue();
8623 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008624 LHS = Op1;
8625 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008626 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008627 } else if (Op1.getOpcode() == ISD::Constant) {
8628 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008629 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008630 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008631
8632 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008633 LHS = AndLHS.getOperand(0);
8634 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008635 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008636
8637 // Use BT if the immediate can't be encoded in a TEST instruction.
8638 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8639 LHS = AndLHS;
8640 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8641 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008642 }
Evan Cheng0488db92007-09-25 01:57:46 +00008643
Evan Chengd40d03e2010-01-06 19:38:29 +00008644 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008645 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008646 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008647 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008648 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008649 // Also promote i16 to i32 for performance / code size reason.
8650 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008651 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008652 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008653
Evan Chengd40d03e2010-01-06 19:38:29 +00008654 // If the operand types disagree, extend the shift amount to match. Since
8655 // BT ignores high bits (like shifts) we can use anyextend.
8656 if (LHS.getValueType() != RHS.getValueType())
8657 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008658
Evan Chengd40d03e2010-01-06 19:38:29 +00008659 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8660 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8661 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8662 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008663 }
8664
Evan Cheng54de3ea2010-01-05 06:52:31 +00008665 return SDValue();
8666}
8667
Dan Gohmand858e902010-04-17 15:26:15 +00008668SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008669
8670 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8671
Evan Cheng54de3ea2010-01-05 06:52:31 +00008672 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8673 SDValue Op0 = Op.getOperand(0);
8674 SDValue Op1 = Op.getOperand(1);
8675 DebugLoc dl = Op.getDebugLoc();
8676 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8677
8678 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008679 // Lower (X & (1 << N)) == 0 to BT(X, N).
8680 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8681 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008682 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008683 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008684 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008685 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8686 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8687 if (NewSetCC.getNode())
8688 return NewSetCC;
8689 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008690
Chris Lattner481eebc2010-12-19 21:23:48 +00008691 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8692 // these.
8693 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008694 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008695 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8696 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008697
Chris Lattner481eebc2010-12-19 21:23:48 +00008698 // If the input is a setcc, then reuse the input setcc or use a new one with
8699 // the inverted condition.
8700 if (Op0.getOpcode() == X86ISD::SETCC) {
8701 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8702 bool Invert = (CC == ISD::SETNE) ^
8703 cast<ConstantSDNode>(Op1)->isNullValue();
8704 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008705
Evan Cheng2c755ba2010-02-27 07:36:59 +00008706 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008707 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8708 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8709 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008710 }
8711
Evan Chenge5b51ac2010-04-17 06:13:15 +00008712 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008713 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008714 if (X86CC == X86::COND_INVALID)
8715 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008716
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008717 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008718 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008719 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008720 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008721}
8722
Craig Topper89af15e2011-09-18 08:03:58 +00008723// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008724// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008725static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008726 EVT VT = Op.getValueType();
8727
Craig Topper7a9a28b2012-08-12 02:23:29 +00008728 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008729 "Unsupported value type for operation");
8730
Craig Topper66ddd152012-04-27 22:54:43 +00008731 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008732 DebugLoc dl = Op.getDebugLoc();
8733 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008734
8735 // Extract the LHS vectors
8736 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008737 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8738 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008739
8740 // Extract the RHS vectors
8741 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008742 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8743 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008744
8745 // Issue the operation on the smaller types and concatenate the result back
8746 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8747 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8748 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8749 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8750 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8751}
8752
8753
Dan Gohmand858e902010-04-17 15:26:15 +00008754SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008755 SDValue Cond;
8756 SDValue Op0 = Op.getOperand(0);
8757 SDValue Op1 = Op.getOperand(1);
8758 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008759 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008760 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8761 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008762 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008763
8764 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008765#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008766 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008767 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8768#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008769
Craig Topper523908d2012-08-13 02:34:03 +00008770 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008771 bool Swap = false;
8772
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008773 // SSE Condition code mapping:
8774 // 0 - EQ
8775 // 1 - LT
8776 // 2 - LE
8777 // 3 - UNORD
8778 // 4 - NEQ
8779 // 5 - NLT
8780 // 6 - NLE
8781 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008782 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008783 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008784 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008785 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008786 case ISD::SETOGT:
8787 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008788 case ISD::SETLT:
8789 case ISD::SETOLT: SSECC = 1; break;
8790 case ISD::SETOGE:
8791 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008792 case ISD::SETLE:
8793 case ISD::SETOLE: SSECC = 2; break;
8794 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008795 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008796 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008797 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008798 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008799 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008800 case ISD::SETUGT: SSECC = 6; break;
8801 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008802 case ISD::SETUEQ:
8803 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008804 }
8805 if (Swap)
8806 std::swap(Op0, Op1);
8807
Nate Begemanfb8ead02008-07-25 19:05:58 +00008808 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008809 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008810 unsigned CC0, CC1;
8811 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008812 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008813 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8814 } else {
8815 assert(SetCCOpcode == ISD::SETONE);
8816 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008817 }
Craig Topper523908d2012-08-13 02:34:03 +00008818
8819 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8820 DAG.getConstant(CC0, MVT::i8));
8821 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8822 DAG.getConstant(CC1, MVT::i8));
8823 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008824 }
8825 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008826 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8827 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008828 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008829
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008830 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008831 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008832 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008833
Nate Begeman30a0de92008-07-17 16:51:19 +00008834 // We are handling one of the integer comparisons here. Since SSE only has
8835 // GT and EQ comparisons for integer, swapping operands and multiple
8836 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008837 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008838 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008839
Nate Begeman30a0de92008-07-17 16:51:19 +00008840 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008841 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008842 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008843 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008844 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008845 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008846 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008847 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008848 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008849 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008850 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008851 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008852 }
8853 if (Swap)
8854 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008855
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008856 // Check that the operation in question is available (most are plain SSE2,
8857 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008858 if (VT == MVT::v2i64) {
8859 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8860 return SDValue();
8861 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8862 return SDValue();
8863 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008864
Nate Begeman30a0de92008-07-17 16:51:19 +00008865 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8866 // bits of the inputs before performing those operations.
8867 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008868 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008869 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8870 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008871 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008872 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8873 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008874 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8875 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008877
Dale Johannesenace16102009-02-03 19:33:06 +00008878 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008879
8880 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008881 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008882 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008883
Nate Begeman30a0de92008-07-17 16:51:19 +00008884 return Result;
8885}
Evan Cheng0488db92007-09-25 01:57:46 +00008886
Evan Cheng370e5342008-12-03 08:38:43 +00008887// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008888static bool isX86LogicalCmp(SDValue Op) {
8889 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008890 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8891 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008892 return true;
8893 if (Op.getResNo() == 1 &&
8894 (Opc == X86ISD::ADD ||
8895 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008896 Opc == X86ISD::ADC ||
8897 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008898 Opc == X86ISD::SMUL ||
8899 Opc == X86ISD::UMUL ||
8900 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008901 Opc == X86ISD::DEC ||
8902 Opc == X86ISD::OR ||
8903 Opc == X86ISD::XOR ||
8904 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008905 return true;
8906
Chris Lattner9637d5b2010-12-05 07:49:54 +00008907 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8908 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008909
Dan Gohman076aee32009-03-04 19:44:21 +00008910 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008911}
8912
Chris Lattnera2b56002010-12-05 01:23:24 +00008913static bool isZero(SDValue V) {
8914 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8915 return C && C->isNullValue();
8916}
8917
Chris Lattner96908b12010-12-05 02:00:51 +00008918static bool isAllOnes(SDValue V) {
8919 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8920 return C && C->isAllOnesValue();
8921}
8922
Evan Chengb64dd5f2012-08-07 22:21:00 +00008923static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8924 if (V.getOpcode() != ISD::TRUNCATE)
8925 return false;
8926
8927 SDValue VOp0 = V.getOperand(0);
8928 unsigned InBits = VOp0.getValueSizeInBits();
8929 unsigned Bits = V.getValueSizeInBits();
8930 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8931}
8932
Dan Gohmand858e902010-04-17 15:26:15 +00008933SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008934 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008935 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008936 SDValue Op1 = Op.getOperand(1);
8937 SDValue Op2 = Op.getOperand(2);
8938 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008939 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008940
Dan Gohman1a492952009-10-20 16:22:37 +00008941 if (Cond.getOpcode() == ISD::SETCC) {
8942 SDValue NewCond = LowerSETCC(Cond, DAG);
8943 if (NewCond.getNode())
8944 Cond = NewCond;
8945 }
Evan Cheng734503b2006-09-11 02:19:56 +00008946
Chris Lattnera2b56002010-12-05 01:23:24 +00008947 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008948 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008949 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008950 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008951 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008952 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8953 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008954 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008955
Chris Lattnera2b56002010-12-05 01:23:24 +00008956 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008957
8958 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008959 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8960 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008961
8962 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008963 // Apply further optimizations for special cases
8964 // (select (x != 0), -1, 0) -> neg & sbb
8965 // (select (x == 0), 0, -1) -> neg & sbb
8966 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008967 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008968 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8969 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008970 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8971 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008972 CmpOp0);
8973 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8974 DAG.getConstant(X86::COND_B, MVT::i8),
8975 SDValue(Neg.getNode(), 1));
8976 return Res;
8977 }
8978
Chris Lattnera2b56002010-12-05 01:23:24 +00008979 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8980 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008981 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008982
Chris Lattner96908b12010-12-05 02:00:51 +00008983 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008984 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8985 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008986
Chris Lattner96908b12010-12-05 02:00:51 +00008987 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8988 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008989
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008990 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008991 if (N2C == 0 || !N2C->isNullValue())
8992 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8993 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008994 }
8995 }
8996
Chris Lattnera2b56002010-12-05 01:23:24 +00008997 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008998 if (Cond.getOpcode() == ISD::AND &&
8999 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9000 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009001 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009002 Cond = Cond.getOperand(0);
9003 }
9004
Evan Cheng3f41d662007-10-08 22:16:29 +00009005 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9006 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009007 unsigned CondOpcode = Cond.getOpcode();
9008 if (CondOpcode == X86ISD::SETCC ||
9009 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009010 CC = Cond.getOperand(0);
9011
Dan Gohman475871a2008-07-27 21:46:04 +00009012 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009013 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009014 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009015
Evan Cheng3f41d662007-10-08 22:16:29 +00009016 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009017 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009018 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009019 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009020
Chris Lattnerd1980a52009-03-12 06:52:53 +00009021 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9022 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009023 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009024 addTest = false;
9025 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009026 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9027 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9028 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9029 Cond.getOperand(0).getValueType() != MVT::i8)) {
9030 SDValue LHS = Cond.getOperand(0);
9031 SDValue RHS = Cond.getOperand(1);
9032 unsigned X86Opcode;
9033 unsigned X86Cond;
9034 SDVTList VTs;
9035 switch (CondOpcode) {
9036 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9037 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9038 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9039 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9040 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9041 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9042 default: llvm_unreachable("unexpected overflowing operator");
9043 }
9044 if (CondOpcode == ISD::UMULO)
9045 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9046 MVT::i32);
9047 else
9048 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9049
9050 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9051
9052 if (CondOpcode == ISD::UMULO)
9053 Cond = X86Op.getValue(2);
9054 else
9055 Cond = X86Op.getValue(1);
9056
9057 CC = DAG.getConstant(X86Cond, MVT::i8);
9058 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009059 }
9060
9061 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009062 // Look pass the truncate if the high bits are known zero.
9063 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9064 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009065
9066 // We know the result of AND is compared against zero. Try to match
9067 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009068 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009069 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009070 if (NewSetCC.getNode()) {
9071 CC = NewSetCC.getOperand(0);
9072 Cond = NewSetCC.getOperand(1);
9073 addTest = false;
9074 }
9075 }
9076 }
9077
9078 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009079 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009080 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009081 }
9082
Benjamin Kramere915ff32010-12-22 23:09:28 +00009083 // a < b ? -1 : 0 -> RES = ~setcc_carry
9084 // a < b ? 0 : -1 -> RES = setcc_carry
9085 // a >= b ? -1 : 0 -> RES = setcc_carry
9086 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009087 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009088 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009089 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9090
9091 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9092 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9093 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9094 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9095 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9096 return DAG.getNOT(DL, Res, Res.getValueType());
9097 return Res;
9098 }
9099 }
9100
Evan Cheng0488db92007-09-25 01:57:46 +00009101 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9102 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009103 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009104 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009105 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009106}
9107
Evan Cheng370e5342008-12-03 08:38:43 +00009108// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9109// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9110// from the AND / OR.
9111static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9112 Opc = Op.getOpcode();
9113 if (Opc != ISD::OR && Opc != ISD::AND)
9114 return false;
9115 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9116 Op.getOperand(0).hasOneUse() &&
9117 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9118 Op.getOperand(1).hasOneUse());
9119}
9120
Evan Cheng961d6d42009-02-02 08:19:07 +00009121// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9122// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009123static bool isXor1OfSetCC(SDValue Op) {
9124 if (Op.getOpcode() != ISD::XOR)
9125 return false;
9126 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9127 if (N1C && N1C->getAPIntValue() == 1) {
9128 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9129 Op.getOperand(0).hasOneUse();
9130 }
9131 return false;
9132}
9133
Dan Gohmand858e902010-04-17 15:26:15 +00009134SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009135 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009136 SDValue Chain = Op.getOperand(0);
9137 SDValue Cond = Op.getOperand(1);
9138 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009139 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009140 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009141 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009142
Dan Gohman1a492952009-10-20 16:22:37 +00009143 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009144 // Check for setcc([su]{add,sub,mul}o == 0).
9145 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9146 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9147 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9148 Cond.getOperand(0).getResNo() == 1 &&
9149 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9150 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9151 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9152 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9153 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9154 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9155 Inverted = true;
9156 Cond = Cond.getOperand(0);
9157 } else {
9158 SDValue NewCond = LowerSETCC(Cond, DAG);
9159 if (NewCond.getNode())
9160 Cond = NewCond;
9161 }
Dan Gohman1a492952009-10-20 16:22:37 +00009162 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009163#if 0
9164 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009165 else if (Cond.getOpcode() == X86ISD::ADD ||
9166 Cond.getOpcode() == X86ISD::SUB ||
9167 Cond.getOpcode() == X86ISD::SMUL ||
9168 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009169 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009170#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009171
Evan Chengad9c0a32009-12-15 00:53:42 +00009172 // Look pass (and (setcc_carry (cmp ...)), 1).
9173 if (Cond.getOpcode() == ISD::AND &&
9174 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9175 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009176 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009177 Cond = Cond.getOperand(0);
9178 }
9179
Evan Cheng3f41d662007-10-08 22:16:29 +00009180 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9181 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009182 unsigned CondOpcode = Cond.getOpcode();
9183 if (CondOpcode == X86ISD::SETCC ||
9184 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009185 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009186
Dan Gohman475871a2008-07-27 21:46:04 +00009187 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009188 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009189 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009190 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009191 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009192 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009193 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009194 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009195 default: break;
9196 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009197 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009198 // These can only come from an arithmetic instruction with overflow,
9199 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009200 Cond = Cond.getNode()->getOperand(1);
9201 addTest = false;
9202 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009203 }
Evan Cheng0488db92007-09-25 01:57:46 +00009204 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009205 }
9206 CondOpcode = Cond.getOpcode();
9207 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9208 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9209 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9210 Cond.getOperand(0).getValueType() != MVT::i8)) {
9211 SDValue LHS = Cond.getOperand(0);
9212 SDValue RHS = Cond.getOperand(1);
9213 unsigned X86Opcode;
9214 unsigned X86Cond;
9215 SDVTList VTs;
9216 switch (CondOpcode) {
9217 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9218 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9219 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9220 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9221 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9222 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9223 default: llvm_unreachable("unexpected overflowing operator");
9224 }
9225 if (Inverted)
9226 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9227 if (CondOpcode == ISD::UMULO)
9228 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9229 MVT::i32);
9230 else
9231 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9232
9233 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9234
9235 if (CondOpcode == ISD::UMULO)
9236 Cond = X86Op.getValue(2);
9237 else
9238 Cond = X86Op.getValue(1);
9239
9240 CC = DAG.getConstant(X86Cond, MVT::i8);
9241 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009242 } else {
9243 unsigned CondOpc;
9244 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9245 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009246 if (CondOpc == ISD::OR) {
9247 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9248 // two branches instead of an explicit OR instruction with a
9249 // separate test.
9250 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009251 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009252 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009253 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009254 Chain, Dest, CC, Cmp);
9255 CC = Cond.getOperand(1).getOperand(0);
9256 Cond = Cmp;
9257 addTest = false;
9258 }
9259 } else { // ISD::AND
9260 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9261 // two branches instead of an explicit AND instruction with a
9262 // separate test. However, we only do this if this block doesn't
9263 // have a fall-through edge, because this requires an explicit
9264 // jmp when the condition is false.
9265 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009266 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009267 Op.getNode()->hasOneUse()) {
9268 X86::CondCode CCode =
9269 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9270 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009271 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009272 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009273 // Look for an unconditional branch following this conditional branch.
9274 // We need this because we need to reverse the successors in order
9275 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009276 if (User->getOpcode() == ISD::BR) {
9277 SDValue FalseBB = User->getOperand(1);
9278 SDNode *NewBR =
9279 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009280 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009281 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009282 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009283
Dale Johannesene4d209d2009-02-03 20:21:25 +00009284 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009285 Chain, Dest, CC, Cmp);
9286 X86::CondCode CCode =
9287 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9288 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009289 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009290 Cond = Cmp;
9291 addTest = false;
9292 }
9293 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009294 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009295 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9296 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9297 // It should be transformed during dag combiner except when the condition
9298 // is set by a arithmetics with overflow node.
9299 X86::CondCode CCode =
9300 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9301 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009302 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009303 Cond = Cond.getOperand(0).getOperand(1);
9304 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009305 } else if (Cond.getOpcode() == ISD::SETCC &&
9306 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9307 // For FCMP_OEQ, we can emit
9308 // two branches instead of an explicit AND instruction with a
9309 // separate test. However, we only do this if this block doesn't
9310 // have a fall-through edge, because this requires an explicit
9311 // jmp when the condition is false.
9312 if (Op.getNode()->hasOneUse()) {
9313 SDNode *User = *Op.getNode()->use_begin();
9314 // Look for an unconditional branch following this conditional branch.
9315 // We need this because we need to reverse the successors in order
9316 // to implement FCMP_OEQ.
9317 if (User->getOpcode() == ISD::BR) {
9318 SDValue FalseBB = User->getOperand(1);
9319 SDNode *NewBR =
9320 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9321 assert(NewBR == User);
9322 (void)NewBR;
9323 Dest = FalseBB;
9324
9325 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9326 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009327 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009328 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9329 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9330 Chain, Dest, CC, Cmp);
9331 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9332 Cond = Cmp;
9333 addTest = false;
9334 }
9335 }
9336 } else if (Cond.getOpcode() == ISD::SETCC &&
9337 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9338 // For FCMP_UNE, we can emit
9339 // two branches instead of an explicit AND instruction with a
9340 // separate test. However, we only do this if this block doesn't
9341 // have a fall-through edge, because this requires an explicit
9342 // jmp when the condition is false.
9343 if (Op.getNode()->hasOneUse()) {
9344 SDNode *User = *Op.getNode()->use_begin();
9345 // Look for an unconditional branch following this conditional branch.
9346 // We need this because we need to reverse the successors in order
9347 // to implement FCMP_UNE.
9348 if (User->getOpcode() == ISD::BR) {
9349 SDValue FalseBB = User->getOperand(1);
9350 SDNode *NewBR =
9351 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9352 assert(NewBR == User);
9353 (void)NewBR;
9354
9355 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9356 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009357 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009358 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9359 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9360 Chain, Dest, CC, Cmp);
9361 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9362 Cond = Cmp;
9363 addTest = false;
9364 Dest = FalseBB;
9365 }
9366 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009367 }
Evan Cheng0488db92007-09-25 01:57:46 +00009368 }
9369
9370 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009371 // Look pass the truncate if the high bits are known zero.
9372 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9373 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009374
9375 // We know the result of AND is compared against zero. Try to match
9376 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009377 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009378 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9379 if (NewSetCC.getNode()) {
9380 CC = NewSetCC.getOperand(0);
9381 Cond = NewSetCC.getOperand(1);
9382 addTest = false;
9383 }
9384 }
9385 }
9386
9387 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009388 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009389 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009390 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009391 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009392 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009393 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009394}
9395
Anton Korobeynikove060b532007-04-17 19:34:00 +00009396
9397// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9398// Calls to _alloca is needed to probe the stack when allocating more than 4k
9399// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9400// that the guard pages used by the OS virtual memory manager are allocated in
9401// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009402SDValue
9403X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009404 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009405 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009406 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009407 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009408 "are being used");
9409 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009410 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009411
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009412 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009413 SDValue Chain = Op.getOperand(0);
9414 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009415 // FIXME: Ensure alignment here
9416
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009417 bool Is64Bit = Subtarget->is64Bit();
9418 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009419
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009420 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009421 MachineFunction &MF = DAG.getMachineFunction();
9422 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009423
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009424 if (Is64Bit) {
9425 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009426 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009427 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009428
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009429 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009430 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009431 if (I->hasNestAttr())
9432 report_fatal_error("Cannot use segmented stacks with functions that "
9433 "have nested arguments.");
9434 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009435
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009436 const TargetRegisterClass *AddrRegClass =
9437 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9438 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9439 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9440 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9441 DAG.getRegister(Vreg, SPTy));
9442 SDValue Ops1[2] = { Value, Chain };
9443 return DAG.getMergeValues(Ops1, 2, dl);
9444 } else {
9445 SDValue Flag;
9446 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009447
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009448 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9449 Flag = Chain.getValue(1);
9450 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009451
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009452 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9453 Flag = Chain.getValue(1);
9454
9455 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9456
9457 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9458 return DAG.getMergeValues(Ops1, 2, dl);
9459 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009460}
9461
Dan Gohmand858e902010-04-17 15:26:15 +00009462SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009463 MachineFunction &MF = DAG.getMachineFunction();
9464 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9465
Dan Gohman69de1932008-02-06 22:27:42 +00009466 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009467 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009468
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009469 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009470 // vastart just stores the address of the VarArgsFrameIndex slot into the
9471 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009472 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9473 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009474 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9475 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009476 }
9477
9478 // __va_list_tag:
9479 // gp_offset (0 - 6 * 8)
9480 // fp_offset (48 - 48 + 8 * 16)
9481 // overflow_arg_area (point to parameters coming in memory).
9482 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009483 SmallVector<SDValue, 8> MemOps;
9484 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009485 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009486 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009487 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9488 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009489 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009490 MemOps.push_back(Store);
9491
9492 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009493 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009494 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009495 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009496 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9497 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009498 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009499 MemOps.push_back(Store);
9500
9501 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009502 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009503 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009504 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9505 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009506 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9507 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009508 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009509 MemOps.push_back(Store);
9510
9511 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009512 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009513 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009514 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9515 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009516 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9517 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009518 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009519 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009520 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009521}
9522
Dan Gohmand858e902010-04-17 15:26:15 +00009523SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009524 assert(Subtarget->is64Bit() &&
9525 "LowerVAARG only handles 64-bit va_arg!");
9526 assert((Subtarget->isTargetLinux() ||
9527 Subtarget->isTargetDarwin()) &&
9528 "Unhandled target in LowerVAARG");
9529 assert(Op.getNode()->getNumOperands() == 4);
9530 SDValue Chain = Op.getOperand(0);
9531 SDValue SrcPtr = Op.getOperand(1);
9532 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9533 unsigned Align = Op.getConstantOperandVal(3);
9534 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009535
Dan Gohman320afb82010-10-12 18:00:49 +00009536 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009537 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009538 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9539 uint8_t ArgMode;
9540
9541 // Decide which area this value should be read from.
9542 // TODO: Implement the AMD64 ABI in its entirety. This simple
9543 // selection mechanism works only for the basic types.
9544 if (ArgVT == MVT::f80) {
9545 llvm_unreachable("va_arg for f80 not yet implemented");
9546 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9547 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9548 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9549 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9550 } else {
9551 llvm_unreachable("Unhandled argument type in LowerVAARG");
9552 }
9553
9554 if (ArgMode == 2) {
9555 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009556 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009557 !(DAG.getMachineFunction()
9558 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009559 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009560 }
9561
9562 // Insert VAARG_64 node into the DAG
9563 // VAARG_64 returns two values: Variable Argument Address, Chain
9564 SmallVector<SDValue, 11> InstOps;
9565 InstOps.push_back(Chain);
9566 InstOps.push_back(SrcPtr);
9567 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9568 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9569 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9570 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9571 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9572 VTs, &InstOps[0], InstOps.size(),
9573 MVT::i64,
9574 MachinePointerInfo(SV),
9575 /*Align=*/0,
9576 /*Volatile=*/false,
9577 /*ReadMem=*/true,
9578 /*WriteMem=*/true);
9579 Chain = VAARG.getValue(1);
9580
9581 // Load the next argument and return it
9582 return DAG.getLoad(ArgVT, dl,
9583 Chain,
9584 VAARG,
9585 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009586 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009587}
9588
Craig Topper55b24052012-09-11 06:15:32 +00009589static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9590 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009591 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009592 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009593 SDValue Chain = Op.getOperand(0);
9594 SDValue DstPtr = Op.getOperand(1);
9595 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009596 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9597 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009598 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009599
Chris Lattnere72f2022010-09-21 05:40:29 +00009600 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009601 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009602 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009603 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009604}
9605
Craig Topper80e46362012-01-23 06:16:53 +00009606// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9607// may or may not be a constant. Takes immediate version of shift as input.
9608static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9609 SDValue SrcOp, SDValue ShAmt,
9610 SelectionDAG &DAG) {
9611 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9612
9613 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009614 // Constant may be a TargetConstant. Use a regular constant.
9615 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009616 switch (Opc) {
9617 default: llvm_unreachable("Unknown target vector shift node");
9618 case X86ISD::VSHLI:
9619 case X86ISD::VSRLI:
9620 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009621 return DAG.getNode(Opc, dl, VT, SrcOp,
9622 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009623 }
9624 }
9625
9626 // Change opcode to non-immediate version
9627 switch (Opc) {
9628 default: llvm_unreachable("Unknown target vector shift node");
9629 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9630 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9631 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9632 }
9633
9634 // Need to build a vector containing shift amount
9635 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9636 SDValue ShOps[4];
9637 ShOps[0] = ShAmt;
9638 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009639 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009640 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009641
9642 // The return type has to be a 128-bit type with the same element
9643 // type as the input type.
9644 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9645 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9646
9647 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009648 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9649}
9650
Craig Topper55b24052012-09-11 06:15:32 +00009651static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009652 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009653 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009654 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009655 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009656 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009657 case Intrinsic::x86_sse_comieq_ss:
9658 case Intrinsic::x86_sse_comilt_ss:
9659 case Intrinsic::x86_sse_comile_ss:
9660 case Intrinsic::x86_sse_comigt_ss:
9661 case Intrinsic::x86_sse_comige_ss:
9662 case Intrinsic::x86_sse_comineq_ss:
9663 case Intrinsic::x86_sse_ucomieq_ss:
9664 case Intrinsic::x86_sse_ucomilt_ss:
9665 case Intrinsic::x86_sse_ucomile_ss:
9666 case Intrinsic::x86_sse_ucomigt_ss:
9667 case Intrinsic::x86_sse_ucomige_ss:
9668 case Intrinsic::x86_sse_ucomineq_ss:
9669 case Intrinsic::x86_sse2_comieq_sd:
9670 case Intrinsic::x86_sse2_comilt_sd:
9671 case Intrinsic::x86_sse2_comile_sd:
9672 case Intrinsic::x86_sse2_comigt_sd:
9673 case Intrinsic::x86_sse2_comige_sd:
9674 case Intrinsic::x86_sse2_comineq_sd:
9675 case Intrinsic::x86_sse2_ucomieq_sd:
9676 case Intrinsic::x86_sse2_ucomilt_sd:
9677 case Intrinsic::x86_sse2_ucomile_sd:
9678 case Intrinsic::x86_sse2_ucomigt_sd:
9679 case Intrinsic::x86_sse2_ucomige_sd:
9680 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009681 unsigned Opc;
9682 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009683 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009684 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009685 case Intrinsic::x86_sse_comieq_ss:
9686 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009687 Opc = X86ISD::COMI;
9688 CC = ISD::SETEQ;
9689 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009690 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009691 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009692 Opc = X86ISD::COMI;
9693 CC = ISD::SETLT;
9694 break;
9695 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009696 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009697 Opc = X86ISD::COMI;
9698 CC = ISD::SETLE;
9699 break;
9700 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009701 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009702 Opc = X86ISD::COMI;
9703 CC = ISD::SETGT;
9704 break;
9705 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009706 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009707 Opc = X86ISD::COMI;
9708 CC = ISD::SETGE;
9709 break;
9710 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009711 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009712 Opc = X86ISD::COMI;
9713 CC = ISD::SETNE;
9714 break;
9715 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009716 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009717 Opc = X86ISD::UCOMI;
9718 CC = ISD::SETEQ;
9719 break;
9720 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009721 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009722 Opc = X86ISD::UCOMI;
9723 CC = ISD::SETLT;
9724 break;
9725 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009726 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009727 Opc = X86ISD::UCOMI;
9728 CC = ISD::SETLE;
9729 break;
9730 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009731 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009732 Opc = X86ISD::UCOMI;
9733 CC = ISD::SETGT;
9734 break;
9735 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009736 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009737 Opc = X86ISD::UCOMI;
9738 CC = ISD::SETGE;
9739 break;
9740 case Intrinsic::x86_sse_ucomineq_ss:
9741 case Intrinsic::x86_sse2_ucomineq_sd:
9742 Opc = X86ISD::UCOMI;
9743 CC = ISD::SETNE;
9744 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009745 }
Evan Cheng734503b2006-09-11 02:19:56 +00009746
Dan Gohman475871a2008-07-27 21:46:04 +00009747 SDValue LHS = Op.getOperand(1);
9748 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009749 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009750 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009751 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9752 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9753 DAG.getConstant(X86CC, MVT::i8), Cond);
9754 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009755 }
Craig Topper6d688152012-08-14 07:43:25 +00009756
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009757 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009758 case Intrinsic::x86_sse2_pmulu_dq:
9759 case Intrinsic::x86_avx2_pmulu_dq:
9760 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9761 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009762
9763 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009764 case Intrinsic::x86_sse3_hadd_ps:
9765 case Intrinsic::x86_sse3_hadd_pd:
9766 case Intrinsic::x86_avx_hadd_ps_256:
9767 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009768 case Intrinsic::x86_sse3_hsub_ps:
9769 case Intrinsic::x86_sse3_hsub_pd:
9770 case Intrinsic::x86_avx_hsub_ps_256:
9771 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009772 case Intrinsic::x86_ssse3_phadd_w_128:
9773 case Intrinsic::x86_ssse3_phadd_d_128:
9774 case Intrinsic::x86_avx2_phadd_w:
9775 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009776 case Intrinsic::x86_ssse3_phsub_w_128:
9777 case Intrinsic::x86_ssse3_phsub_d_128:
9778 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009779 case Intrinsic::x86_avx2_phsub_d: {
9780 unsigned Opcode;
9781 switch (IntNo) {
9782 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9783 case Intrinsic::x86_sse3_hadd_ps:
9784 case Intrinsic::x86_sse3_hadd_pd:
9785 case Intrinsic::x86_avx_hadd_ps_256:
9786 case Intrinsic::x86_avx_hadd_pd_256:
9787 Opcode = X86ISD::FHADD;
9788 break;
9789 case Intrinsic::x86_sse3_hsub_ps:
9790 case Intrinsic::x86_sse3_hsub_pd:
9791 case Intrinsic::x86_avx_hsub_ps_256:
9792 case Intrinsic::x86_avx_hsub_pd_256:
9793 Opcode = X86ISD::FHSUB;
9794 break;
9795 case Intrinsic::x86_ssse3_phadd_w_128:
9796 case Intrinsic::x86_ssse3_phadd_d_128:
9797 case Intrinsic::x86_avx2_phadd_w:
9798 case Intrinsic::x86_avx2_phadd_d:
9799 Opcode = X86ISD::HADD;
9800 break;
9801 case Intrinsic::x86_ssse3_phsub_w_128:
9802 case Intrinsic::x86_ssse3_phsub_d_128:
9803 case Intrinsic::x86_avx2_phsub_w:
9804 case Intrinsic::x86_avx2_phsub_d:
9805 Opcode = X86ISD::HSUB;
9806 break;
9807 }
9808 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009809 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009810 }
9811
9812 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009813 case Intrinsic::x86_avx2_psllv_d:
9814 case Intrinsic::x86_avx2_psllv_q:
9815 case Intrinsic::x86_avx2_psllv_d_256:
9816 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009817 case Intrinsic::x86_avx2_psrlv_d:
9818 case Intrinsic::x86_avx2_psrlv_q:
9819 case Intrinsic::x86_avx2_psrlv_d_256:
9820 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009821 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009822 case Intrinsic::x86_avx2_psrav_d_256: {
9823 unsigned Opcode;
9824 switch (IntNo) {
9825 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9826 case Intrinsic::x86_avx2_psllv_d:
9827 case Intrinsic::x86_avx2_psllv_q:
9828 case Intrinsic::x86_avx2_psllv_d_256:
9829 case Intrinsic::x86_avx2_psllv_q_256:
9830 Opcode = ISD::SHL;
9831 break;
9832 case Intrinsic::x86_avx2_psrlv_d:
9833 case Intrinsic::x86_avx2_psrlv_q:
9834 case Intrinsic::x86_avx2_psrlv_d_256:
9835 case Intrinsic::x86_avx2_psrlv_q_256:
9836 Opcode = ISD::SRL;
9837 break;
9838 case Intrinsic::x86_avx2_psrav_d:
9839 case Intrinsic::x86_avx2_psrav_d_256:
9840 Opcode = ISD::SRA;
9841 break;
9842 }
9843 return DAG.getNode(Opcode, dl, Op.getValueType(),
9844 Op.getOperand(1), Op.getOperand(2));
9845 }
9846
Craig Topper969ba282012-01-25 06:43:11 +00009847 case Intrinsic::x86_ssse3_pshuf_b_128:
9848 case Intrinsic::x86_avx2_pshuf_b:
9849 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9850 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009851
Craig Topper969ba282012-01-25 06:43:11 +00009852 case Intrinsic::x86_ssse3_psign_b_128:
9853 case Intrinsic::x86_ssse3_psign_w_128:
9854 case Intrinsic::x86_ssse3_psign_d_128:
9855 case Intrinsic::x86_avx2_psign_b:
9856 case Intrinsic::x86_avx2_psign_w:
9857 case Intrinsic::x86_avx2_psign_d:
9858 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9859 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009860
Craig Toppere566cd02012-01-26 07:18:03 +00009861 case Intrinsic::x86_sse41_insertps:
9862 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9863 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009864
Craig Toppere566cd02012-01-26 07:18:03 +00009865 case Intrinsic::x86_avx_vperm2f128_ps_256:
9866 case Intrinsic::x86_avx_vperm2f128_pd_256:
9867 case Intrinsic::x86_avx_vperm2f128_si_256:
9868 case Intrinsic::x86_avx2_vperm2i128:
9869 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9870 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009871
Craig Topperffa6c402012-04-16 07:13:00 +00009872 case Intrinsic::x86_avx2_permd:
9873 case Intrinsic::x86_avx2_permps:
9874 // Operands intentionally swapped. Mask is last operand to intrinsic,
9875 // but second operand for node/intruction.
9876 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9877 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009878
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009879 // ptest and testp intrinsics. The intrinsic these come from are designed to
9880 // return an integer value, not just an instruction so lower it to the ptest
9881 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009882 case Intrinsic::x86_sse41_ptestz:
9883 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009884 case Intrinsic::x86_sse41_ptestnzc:
9885 case Intrinsic::x86_avx_ptestz_256:
9886 case Intrinsic::x86_avx_ptestc_256:
9887 case Intrinsic::x86_avx_ptestnzc_256:
9888 case Intrinsic::x86_avx_vtestz_ps:
9889 case Intrinsic::x86_avx_vtestc_ps:
9890 case Intrinsic::x86_avx_vtestnzc_ps:
9891 case Intrinsic::x86_avx_vtestz_pd:
9892 case Intrinsic::x86_avx_vtestc_pd:
9893 case Intrinsic::x86_avx_vtestnzc_pd:
9894 case Intrinsic::x86_avx_vtestz_ps_256:
9895 case Intrinsic::x86_avx_vtestc_ps_256:
9896 case Intrinsic::x86_avx_vtestnzc_ps_256:
9897 case Intrinsic::x86_avx_vtestz_pd_256:
9898 case Intrinsic::x86_avx_vtestc_pd_256:
9899 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9900 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009901 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009902 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009903 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009904 case Intrinsic::x86_avx_vtestz_ps:
9905 case Intrinsic::x86_avx_vtestz_pd:
9906 case Intrinsic::x86_avx_vtestz_ps_256:
9907 case Intrinsic::x86_avx_vtestz_pd_256:
9908 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009909 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009910 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009911 // ZF = 1
9912 X86CC = X86::COND_E;
9913 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009914 case Intrinsic::x86_avx_vtestc_ps:
9915 case Intrinsic::x86_avx_vtestc_pd:
9916 case Intrinsic::x86_avx_vtestc_ps_256:
9917 case Intrinsic::x86_avx_vtestc_pd_256:
9918 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009919 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009920 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009921 // CF = 1
9922 X86CC = X86::COND_B;
9923 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009924 case Intrinsic::x86_avx_vtestnzc_ps:
9925 case Intrinsic::x86_avx_vtestnzc_pd:
9926 case Intrinsic::x86_avx_vtestnzc_ps_256:
9927 case Intrinsic::x86_avx_vtestnzc_pd_256:
9928 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009929 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009930 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009931 // ZF and CF = 0
9932 X86CC = X86::COND_A;
9933 break;
9934 }
Eric Christopherfd179292009-08-27 18:07:15 +00009935
Eric Christopher71c67532009-07-29 00:28:05 +00009936 SDValue LHS = Op.getOperand(1);
9937 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009938 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9939 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009940 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9941 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9942 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009943 }
Evan Cheng5759f972008-05-04 09:15:50 +00009944
Craig Topper80e46362012-01-23 06:16:53 +00009945 // SSE/AVX shift intrinsics
9946 case Intrinsic::x86_sse2_psll_w:
9947 case Intrinsic::x86_sse2_psll_d:
9948 case Intrinsic::x86_sse2_psll_q:
9949 case Intrinsic::x86_avx2_psll_w:
9950 case Intrinsic::x86_avx2_psll_d:
9951 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +00009952 case Intrinsic::x86_sse2_psrl_w:
9953 case Intrinsic::x86_sse2_psrl_d:
9954 case Intrinsic::x86_sse2_psrl_q:
9955 case Intrinsic::x86_avx2_psrl_w:
9956 case Intrinsic::x86_avx2_psrl_d:
9957 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +00009958 case Intrinsic::x86_sse2_psra_w:
9959 case Intrinsic::x86_sse2_psra_d:
9960 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +00009961 case Intrinsic::x86_avx2_psra_d: {
9962 unsigned Opcode;
9963 switch (IntNo) {
9964 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9965 case Intrinsic::x86_sse2_psll_w:
9966 case Intrinsic::x86_sse2_psll_d:
9967 case Intrinsic::x86_sse2_psll_q:
9968 case Intrinsic::x86_avx2_psll_w:
9969 case Intrinsic::x86_avx2_psll_d:
9970 case Intrinsic::x86_avx2_psll_q:
9971 Opcode = X86ISD::VSHL;
9972 break;
9973 case Intrinsic::x86_sse2_psrl_w:
9974 case Intrinsic::x86_sse2_psrl_d:
9975 case Intrinsic::x86_sse2_psrl_q:
9976 case Intrinsic::x86_avx2_psrl_w:
9977 case Intrinsic::x86_avx2_psrl_d:
9978 case Intrinsic::x86_avx2_psrl_q:
9979 Opcode = X86ISD::VSRL;
9980 break;
9981 case Intrinsic::x86_sse2_psra_w:
9982 case Intrinsic::x86_sse2_psra_d:
9983 case Intrinsic::x86_avx2_psra_w:
9984 case Intrinsic::x86_avx2_psra_d:
9985 Opcode = X86ISD::VSRA;
9986 break;
9987 }
9988 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009989 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009990 }
9991
9992 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +00009993 case Intrinsic::x86_sse2_pslli_w:
9994 case Intrinsic::x86_sse2_pslli_d:
9995 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009996 case Intrinsic::x86_avx2_pslli_w:
9997 case Intrinsic::x86_avx2_pslli_d:
9998 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009999 case Intrinsic::x86_sse2_psrli_w:
10000 case Intrinsic::x86_sse2_psrli_d:
10001 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010002 case Intrinsic::x86_avx2_psrli_w:
10003 case Intrinsic::x86_avx2_psrli_d:
10004 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010005 case Intrinsic::x86_sse2_psrai_w:
10006 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010007 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010008 case Intrinsic::x86_avx2_psrai_d: {
10009 unsigned Opcode;
10010 switch (IntNo) {
10011 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10012 case Intrinsic::x86_sse2_pslli_w:
10013 case Intrinsic::x86_sse2_pslli_d:
10014 case Intrinsic::x86_sse2_pslli_q:
10015 case Intrinsic::x86_avx2_pslli_w:
10016 case Intrinsic::x86_avx2_pslli_d:
10017 case Intrinsic::x86_avx2_pslli_q:
10018 Opcode = X86ISD::VSHLI;
10019 break;
10020 case Intrinsic::x86_sse2_psrli_w:
10021 case Intrinsic::x86_sse2_psrli_d:
10022 case Intrinsic::x86_sse2_psrli_q:
10023 case Intrinsic::x86_avx2_psrli_w:
10024 case Intrinsic::x86_avx2_psrli_d:
10025 case Intrinsic::x86_avx2_psrli_q:
10026 Opcode = X86ISD::VSRLI;
10027 break;
10028 case Intrinsic::x86_sse2_psrai_w:
10029 case Intrinsic::x86_sse2_psrai_d:
10030 case Intrinsic::x86_avx2_psrai_w:
10031 case Intrinsic::x86_avx2_psrai_d:
10032 Opcode = X86ISD::VSRAI;
10033 break;
10034 }
10035 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010036 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010037 }
10038
Craig Topper4feb6472012-08-06 06:22:36 +000010039 case Intrinsic::x86_sse42_pcmpistria128:
10040 case Intrinsic::x86_sse42_pcmpestria128:
10041 case Intrinsic::x86_sse42_pcmpistric128:
10042 case Intrinsic::x86_sse42_pcmpestric128:
10043 case Intrinsic::x86_sse42_pcmpistrio128:
10044 case Intrinsic::x86_sse42_pcmpestrio128:
10045 case Intrinsic::x86_sse42_pcmpistris128:
10046 case Intrinsic::x86_sse42_pcmpestris128:
10047 case Intrinsic::x86_sse42_pcmpistriz128:
10048 case Intrinsic::x86_sse42_pcmpestriz128: {
10049 unsigned Opcode;
10050 unsigned X86CC;
10051 switch (IntNo) {
10052 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10053 case Intrinsic::x86_sse42_pcmpistria128:
10054 Opcode = X86ISD::PCMPISTRI;
10055 X86CC = X86::COND_A;
10056 break;
10057 case Intrinsic::x86_sse42_pcmpestria128:
10058 Opcode = X86ISD::PCMPESTRI;
10059 X86CC = X86::COND_A;
10060 break;
10061 case Intrinsic::x86_sse42_pcmpistric128:
10062 Opcode = X86ISD::PCMPISTRI;
10063 X86CC = X86::COND_B;
10064 break;
10065 case Intrinsic::x86_sse42_pcmpestric128:
10066 Opcode = X86ISD::PCMPESTRI;
10067 X86CC = X86::COND_B;
10068 break;
10069 case Intrinsic::x86_sse42_pcmpistrio128:
10070 Opcode = X86ISD::PCMPISTRI;
10071 X86CC = X86::COND_O;
10072 break;
10073 case Intrinsic::x86_sse42_pcmpestrio128:
10074 Opcode = X86ISD::PCMPESTRI;
10075 X86CC = X86::COND_O;
10076 break;
10077 case Intrinsic::x86_sse42_pcmpistris128:
10078 Opcode = X86ISD::PCMPISTRI;
10079 X86CC = X86::COND_S;
10080 break;
10081 case Intrinsic::x86_sse42_pcmpestris128:
10082 Opcode = X86ISD::PCMPESTRI;
10083 X86CC = X86::COND_S;
10084 break;
10085 case Intrinsic::x86_sse42_pcmpistriz128:
10086 Opcode = X86ISD::PCMPISTRI;
10087 X86CC = X86::COND_E;
10088 break;
10089 case Intrinsic::x86_sse42_pcmpestriz128:
10090 Opcode = X86ISD::PCMPESTRI;
10091 X86CC = X86::COND_E;
10092 break;
10093 }
10094 SmallVector<SDValue, 5> NewOps;
10095 NewOps.append(Op->op_begin()+1, Op->op_end());
10096 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10097 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10098 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10099 DAG.getConstant(X86CC, MVT::i8),
10100 SDValue(PCMP.getNode(), 1));
10101 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10102 }
Craig Topper6d688152012-08-14 07:43:25 +000010103
Craig Topper4feb6472012-08-06 06:22:36 +000010104 case Intrinsic::x86_sse42_pcmpistri128:
10105 case Intrinsic::x86_sse42_pcmpestri128: {
10106 unsigned Opcode;
10107 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10108 Opcode = X86ISD::PCMPISTRI;
10109 else
10110 Opcode = X86ISD::PCMPESTRI;
10111
10112 SmallVector<SDValue, 5> NewOps;
10113 NewOps.append(Op->op_begin()+1, Op->op_end());
10114 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10115 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10116 }
Craig Topper0e292372012-08-24 04:03:22 +000010117 case Intrinsic::x86_fma_vfmadd_ps:
10118 case Intrinsic::x86_fma_vfmadd_pd:
10119 case Intrinsic::x86_fma_vfmsub_ps:
10120 case Intrinsic::x86_fma_vfmsub_pd:
10121 case Intrinsic::x86_fma_vfnmadd_ps:
10122 case Intrinsic::x86_fma_vfnmadd_pd:
10123 case Intrinsic::x86_fma_vfnmsub_ps:
10124 case Intrinsic::x86_fma_vfnmsub_pd:
10125 case Intrinsic::x86_fma_vfmaddsub_ps:
10126 case Intrinsic::x86_fma_vfmaddsub_pd:
10127 case Intrinsic::x86_fma_vfmsubadd_ps:
10128 case Intrinsic::x86_fma_vfmsubadd_pd:
10129 case Intrinsic::x86_fma_vfmadd_ps_256:
10130 case Intrinsic::x86_fma_vfmadd_pd_256:
10131 case Intrinsic::x86_fma_vfmsub_ps_256:
10132 case Intrinsic::x86_fma_vfmsub_pd_256:
10133 case Intrinsic::x86_fma_vfnmadd_ps_256:
10134 case Intrinsic::x86_fma_vfnmadd_pd_256:
10135 case Intrinsic::x86_fma_vfnmsub_ps_256:
10136 case Intrinsic::x86_fma_vfnmsub_pd_256:
10137 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10138 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10139 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10140 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010141 unsigned Opc;
10142 switch (IntNo) {
10143 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10144 case Intrinsic::x86_fma_vfmadd_ps:
10145 case Intrinsic::x86_fma_vfmadd_pd:
10146 case Intrinsic::x86_fma_vfmadd_ps_256:
10147 case Intrinsic::x86_fma_vfmadd_pd_256:
10148 Opc = X86ISD::FMADD;
10149 break;
10150 case Intrinsic::x86_fma_vfmsub_ps:
10151 case Intrinsic::x86_fma_vfmsub_pd:
10152 case Intrinsic::x86_fma_vfmsub_ps_256:
10153 case Intrinsic::x86_fma_vfmsub_pd_256:
10154 Opc = X86ISD::FMSUB;
10155 break;
10156 case Intrinsic::x86_fma_vfnmadd_ps:
10157 case Intrinsic::x86_fma_vfnmadd_pd:
10158 case Intrinsic::x86_fma_vfnmadd_ps_256:
10159 case Intrinsic::x86_fma_vfnmadd_pd_256:
10160 Opc = X86ISD::FNMADD;
10161 break;
10162 case Intrinsic::x86_fma_vfnmsub_ps:
10163 case Intrinsic::x86_fma_vfnmsub_pd:
10164 case Intrinsic::x86_fma_vfnmsub_ps_256:
10165 case Intrinsic::x86_fma_vfnmsub_pd_256:
10166 Opc = X86ISD::FNMSUB;
10167 break;
10168 case Intrinsic::x86_fma_vfmaddsub_ps:
10169 case Intrinsic::x86_fma_vfmaddsub_pd:
10170 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10171 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10172 Opc = X86ISD::FMADDSUB;
10173 break;
10174 case Intrinsic::x86_fma_vfmsubadd_ps:
10175 case Intrinsic::x86_fma_vfmsubadd_pd:
10176 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10177 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10178 Opc = X86ISD::FMSUBADD;
10179 break;
10180 }
10181
10182 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10183 Op.getOperand(2), Op.getOperand(3));
10184 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010185 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010186}
Evan Cheng72261582005-12-20 06:22:03 +000010187
Craig Topper55b24052012-09-11 06:15:32 +000010188static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010189 DebugLoc dl = Op.getDebugLoc();
10190 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10191 switch (IntNo) {
10192 default: return SDValue(); // Don't custom lower most intrinsics.
10193
10194 // RDRAND intrinsics.
10195 case Intrinsic::x86_rdrand_16:
10196 case Intrinsic::x86_rdrand_32:
10197 case Intrinsic::x86_rdrand_64: {
10198 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010199 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10200 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010201
10202 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10203 // return the value from Rand, which is always 0, casted to i32.
10204 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10205 DAG.getConstant(1, Op->getValueType(1)),
10206 DAG.getConstant(X86::COND_B, MVT::i32),
10207 SDValue(Result.getNode(), 1) };
10208 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10209 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10210 Ops, 4);
10211
10212 // Return { result, isValid, chain }.
10213 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010214 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010215 }
10216 }
10217}
10218
Dan Gohmand858e902010-04-17 15:26:15 +000010219SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10220 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010221 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10222 MFI->setReturnAddressIsTaken(true);
10223
Bill Wendling64e87322009-01-16 19:25:27 +000010224 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010225 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010226
10227 if (Depth > 0) {
10228 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10229 SDValue Offset =
10230 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010231 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010232 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010233 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010234 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010235 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010236 }
10237
10238 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010239 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010240 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010241 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010242}
10243
Dan Gohmand858e902010-04-17 15:26:15 +000010244SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010245 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10246 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010247
Owen Andersone50ed302009-08-10 22:56:29 +000010248 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010249 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010250 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10251 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010252 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010253 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010254 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10255 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010256 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010257 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010258}
10259
Dan Gohman475871a2008-07-27 21:46:04 +000010260SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010261 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010262 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010263}
10264
Dan Gohmand858e902010-04-17 15:26:15 +000010265SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010266 SDValue Chain = Op.getOperand(0);
10267 SDValue Offset = Op.getOperand(1);
10268 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010269 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010270
Dan Gohmand8816272010-08-11 18:14:00 +000010271 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10272 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10273 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010274 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010275
Dan Gohmand8816272010-08-11 18:14:00 +000010276 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10277 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010278 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010279 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10280 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010281 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010282
Dale Johannesene4d209d2009-02-03 20:21:25 +000010283 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010284 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010285 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010286}
10287
Craig Topper55b24052012-09-11 06:15:32 +000010288static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010289 return Op.getOperand(0);
10290}
10291
10292SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10293 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010294 SDValue Root = Op.getOperand(0);
10295 SDValue Trmp = Op.getOperand(1); // trampoline
10296 SDValue FPtr = Op.getOperand(2); // nested function
10297 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010298 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010299
Dan Gohman69de1932008-02-06 22:27:42 +000010300 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010301
10302 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010303 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010304
10305 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010306 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10307 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010308
Evan Cheng0e6a0522011-07-18 20:57:22 +000010309 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10310 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010311
10312 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10313
10314 // Load the pointer to the nested function into R11.
10315 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010316 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010317 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010318 Addr, MachinePointerInfo(TrmpAddr),
10319 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010320
Owen Anderson825b72b2009-08-11 20:47:22 +000010321 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10322 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010323 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10324 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010325 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010326
10327 // Load the 'nest' parameter value into R10.
10328 // R10 is specified in X86CallingConv.td
10329 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010330 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10331 DAG.getConstant(10, MVT::i64));
10332 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010333 Addr, MachinePointerInfo(TrmpAddr, 10),
10334 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010335
Owen Anderson825b72b2009-08-11 20:47:22 +000010336 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10337 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010338 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10339 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010340 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010341
10342 // Jump to the nested function.
10343 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010344 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10345 DAG.getConstant(20, MVT::i64));
10346 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010347 Addr, MachinePointerInfo(TrmpAddr, 20),
10348 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010349
10350 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010351 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10352 DAG.getConstant(22, MVT::i64));
10353 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010354 MachinePointerInfo(TrmpAddr, 22),
10355 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010356
Duncan Sands4a544a72011-09-06 13:37:06 +000010357 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010358 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010359 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010360 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010361 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010362 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010363
10364 switch (CC) {
10365 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010366 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010367 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010368 case CallingConv::X86_StdCall: {
10369 // Pass 'nest' parameter in ECX.
10370 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010371 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010372
10373 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010374 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010375 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010376
Chris Lattner58d74912008-03-12 17:45:29 +000010377 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010378 unsigned InRegCount = 0;
10379 unsigned Idx = 1;
10380
10381 for (FunctionType::param_iterator I = FTy->param_begin(),
10382 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010383 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010384 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010385 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010386
10387 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010388 report_fatal_error("Nest register in use - reduce number of inreg"
10389 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010390 }
10391 }
10392 break;
10393 }
10394 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010395 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010396 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010397 // Pass 'nest' parameter in EAX.
10398 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010399 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010400 break;
10401 }
10402
Dan Gohman475871a2008-07-27 21:46:04 +000010403 SDValue OutChains[4];
10404 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010405
Owen Anderson825b72b2009-08-11 20:47:22 +000010406 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10407 DAG.getConstant(10, MVT::i32));
10408 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010409
Chris Lattnera62fe662010-02-05 19:20:30 +000010410 // This is storing the opcode for MOV32ri.
10411 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010412 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010413 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010414 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010415 Trmp, MachinePointerInfo(TrmpAddr),
10416 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010417
Owen Anderson825b72b2009-08-11 20:47:22 +000010418 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10419 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010420 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10421 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010422 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010423
Chris Lattnera62fe662010-02-05 19:20:30 +000010424 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010425 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10426 DAG.getConstant(5, MVT::i32));
10427 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010428 MachinePointerInfo(TrmpAddr, 5),
10429 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010430
Owen Anderson825b72b2009-08-11 20:47:22 +000010431 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10432 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010433 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10434 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010435 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010436
Duncan Sands4a544a72011-09-06 13:37:06 +000010437 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010438 }
10439}
10440
Dan Gohmand858e902010-04-17 15:26:15 +000010441SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10442 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010443 /*
10444 The rounding mode is in bits 11:10 of FPSR, and has the following
10445 settings:
10446 00 Round to nearest
10447 01 Round to -inf
10448 10 Round to +inf
10449 11 Round to 0
10450
10451 FLT_ROUNDS, on the other hand, expects the following:
10452 -1 Undefined
10453 0 Round to 0
10454 1 Round to nearest
10455 2 Round to +inf
10456 3 Round to -inf
10457
10458 To perform the conversion, we do:
10459 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10460 */
10461
10462 MachineFunction &MF = DAG.getMachineFunction();
10463 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010464 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010465 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010466 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010467 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010468
10469 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010470 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010471 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010472
Michael J. Spencerec38de22010-10-10 22:04:20 +000010473
Chris Lattner2156b792010-09-22 01:11:26 +000010474 MachineMemOperand *MMO =
10475 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10476 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010477
Chris Lattner2156b792010-09-22 01:11:26 +000010478 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10479 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10480 DAG.getVTList(MVT::Other),
10481 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010482
10483 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010484 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010485 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010486
10487 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010488 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010489 DAG.getNode(ISD::SRL, DL, MVT::i16,
10490 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010491 CWD, DAG.getConstant(0x800, MVT::i16)),
10492 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010493 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010494 DAG.getNode(ISD::SRL, DL, MVT::i16,
10495 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010496 CWD, DAG.getConstant(0x400, MVT::i16)),
10497 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010498
Dan Gohman475871a2008-07-27 21:46:04 +000010499 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010500 DAG.getNode(ISD::AND, DL, MVT::i16,
10501 DAG.getNode(ISD::ADD, DL, MVT::i16,
10502 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010503 DAG.getConstant(1, MVT::i16)),
10504 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010505
10506
Duncan Sands83ec4b62008-06-06 12:08:01 +000010507 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010508 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010509}
10510
Craig Topper55b24052012-09-11 06:15:32 +000010511static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010512 EVT VT = Op.getValueType();
10513 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010514 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010515 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010516
10517 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010518 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010519 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010520 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010521 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010522 }
Evan Cheng18efe262007-12-14 02:13:44 +000010523
Evan Cheng152804e2007-12-14 08:30:15 +000010524 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010525 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010526 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010527
10528 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010529 SDValue Ops[] = {
10530 Op,
10531 DAG.getConstant(NumBits+NumBits-1, OpVT),
10532 DAG.getConstant(X86::COND_E, MVT::i8),
10533 Op.getValue(1)
10534 };
10535 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010536
10537 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010538 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010539
Owen Anderson825b72b2009-08-11 20:47:22 +000010540 if (VT == MVT::i8)
10541 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010542 return Op;
10543}
10544
Craig Topper55b24052012-09-11 06:15:32 +000010545static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010546 EVT VT = Op.getValueType();
10547 EVT OpVT = VT;
10548 unsigned NumBits = VT.getSizeInBits();
10549 DebugLoc dl = Op.getDebugLoc();
10550
10551 Op = Op.getOperand(0);
10552 if (VT == MVT::i8) {
10553 // Zero extend to i32 since there is not an i8 bsr.
10554 OpVT = MVT::i32;
10555 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10556 }
10557
10558 // Issue a bsr (scan bits in reverse).
10559 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10560 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10561
10562 // And xor with NumBits-1.
10563 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10564
10565 if (VT == MVT::i8)
10566 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10567 return Op;
10568}
10569
Craig Topper55b24052012-09-11 06:15:32 +000010570static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010571 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010572 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010573 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010574 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010575
10576 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010577 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010578 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010579
10580 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010581 SDValue Ops[] = {
10582 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010583 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010584 DAG.getConstant(X86::COND_E, MVT::i8),
10585 Op.getValue(1)
10586 };
Chandler Carruth77821022011-12-24 12:12:34 +000010587 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010588}
10589
Craig Topper13894fa2011-08-24 06:14:18 +000010590// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10591// ones, and then concatenate the result back.
10592static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010593 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010594
Craig Topper7a9a28b2012-08-12 02:23:29 +000010595 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010596 "Unsupported value type for operation");
10597
Craig Topper66ddd152012-04-27 22:54:43 +000010598 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010599 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010600
10601 // Extract the LHS vectors
10602 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010603 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10604 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010605
10606 // Extract the RHS vectors
10607 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010608 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10609 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010610
10611 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10612 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10613
10614 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10615 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10616 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10617}
10618
Craig Topper55b24052012-09-11 06:15:32 +000010619static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010620 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010621 Op.getValueType().isInteger() &&
10622 "Only handle AVX 256-bit vector integer operation");
10623 return Lower256IntArith(Op, DAG);
10624}
10625
Craig Topper55b24052012-09-11 06:15:32 +000010626static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010627 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010628 Op.getValueType().isInteger() &&
10629 "Only handle AVX 256-bit vector integer operation");
10630 return Lower256IntArith(Op, DAG);
10631}
10632
Craig Topper55b24052012-09-11 06:15:32 +000010633static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10634 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010635 EVT VT = Op.getValueType();
10636
10637 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010638 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010639 return Lower256IntArith(Op, DAG);
10640
Craig Topper5b209e82012-02-05 03:14:49 +000010641 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10642 "Only know how to lower V2I64/V4I64 multiply");
10643
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010644 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010645
Craig Topper5b209e82012-02-05 03:14:49 +000010646 // Ahi = psrlqi(a, 32);
10647 // Bhi = psrlqi(b, 32);
10648 //
10649 // AloBlo = pmuludq(a, b);
10650 // AloBhi = pmuludq(a, Bhi);
10651 // AhiBlo = pmuludq(Ahi, b);
10652
10653 // AloBhi = psllqi(AloBhi, 32);
10654 // AhiBlo = psllqi(AhiBlo, 32);
10655 // return AloBlo + AloBhi + AhiBlo;
10656
Craig Topperaaa643c2011-11-09 07:28:55 +000010657 SDValue A = Op.getOperand(0);
10658 SDValue B = Op.getOperand(1);
10659
Craig Topper5b209e82012-02-05 03:14:49 +000010660 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010661
Craig Topper5b209e82012-02-05 03:14:49 +000010662 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10663 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010664
Craig Topper5b209e82012-02-05 03:14:49 +000010665 // Bit cast to 32-bit vectors for MULUDQ
10666 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10667 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10668 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10669 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10670 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010671
Craig Topper5b209e82012-02-05 03:14:49 +000010672 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10673 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10674 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010675
Craig Topper5b209e82012-02-05 03:14:49 +000010676 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10677 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010678
Dale Johannesene4d209d2009-02-03 20:21:25 +000010679 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010680 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010681}
10682
Nadav Rotem43012222011-05-11 08:12:09 +000010683SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10684
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010685 EVT VT = Op.getValueType();
10686 DebugLoc dl = Op.getDebugLoc();
10687 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010688 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010689 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010690
Craig Topper1accb7e2012-01-10 06:54:16 +000010691 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010692 return SDValue();
10693
Nadav Rotem43012222011-05-11 08:12:09 +000010694 // Optimize shl/srl/sra with constant shift amount.
10695 if (isSplatVector(Amt.getNode())) {
10696 SDValue SclrAmt = Amt->getOperand(0);
10697 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10698 uint64_t ShiftAmt = C->getZExtValue();
10699
Craig Toppered2e13d2012-01-22 19:15:14 +000010700 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10701 (Subtarget->hasAVX2() &&
10702 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10703 if (Op.getOpcode() == ISD::SHL)
10704 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10705 DAG.getConstant(ShiftAmt, MVT::i32));
10706 if (Op.getOpcode() == ISD::SRL)
10707 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10708 DAG.getConstant(ShiftAmt, MVT::i32));
10709 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10710 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10711 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010712 }
10713
Craig Toppered2e13d2012-01-22 19:15:14 +000010714 if (VT == MVT::v16i8) {
10715 if (Op.getOpcode() == ISD::SHL) {
10716 // Make a large shift.
10717 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10718 DAG.getConstant(ShiftAmt, MVT::i32));
10719 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10720 // Zero out the rightmost bits.
10721 SmallVector<SDValue, 16> V(16,
10722 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10723 MVT::i8));
10724 return DAG.getNode(ISD::AND, dl, VT, SHL,
10725 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010726 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010727 if (Op.getOpcode() == ISD::SRL) {
10728 // Make a large shift.
10729 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10730 DAG.getConstant(ShiftAmt, MVT::i32));
10731 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10732 // Zero out the leftmost bits.
10733 SmallVector<SDValue, 16> V(16,
10734 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10735 MVT::i8));
10736 return DAG.getNode(ISD::AND, dl, VT, SRL,
10737 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10738 }
10739 if (Op.getOpcode() == ISD::SRA) {
10740 if (ShiftAmt == 7) {
10741 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010742 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010743 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010744 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010745
Craig Toppered2e13d2012-01-22 19:15:14 +000010746 // R s>> a === ((R u>> a) ^ m) - m
10747 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10748 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10749 MVT::i8));
10750 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10751 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10752 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10753 return Res;
10754 }
Craig Topper731dfd02012-04-23 03:42:40 +000010755 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010756 }
Craig Topper46154eb2011-11-11 07:39:23 +000010757
Craig Topper0d86d462011-11-20 00:12:05 +000010758 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10759 if (Op.getOpcode() == ISD::SHL) {
10760 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010761 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10762 DAG.getConstant(ShiftAmt, MVT::i32));
10763 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010764 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010765 SmallVector<SDValue, 32> V(32,
10766 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10767 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010768 return DAG.getNode(ISD::AND, dl, VT, SHL,
10769 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010770 }
Craig Topper0d86d462011-11-20 00:12:05 +000010771 if (Op.getOpcode() == ISD::SRL) {
10772 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010773 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10774 DAG.getConstant(ShiftAmt, MVT::i32));
10775 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010776 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010777 SmallVector<SDValue, 32> V(32,
10778 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10779 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010780 return DAG.getNode(ISD::AND, dl, VT, SRL,
10781 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10782 }
10783 if (Op.getOpcode() == ISD::SRA) {
10784 if (ShiftAmt == 7) {
10785 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010786 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010787 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010788 }
10789
10790 // R s>> a === ((R u>> a) ^ m) - m
10791 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10792 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10793 MVT::i8));
10794 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10795 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10796 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10797 return Res;
10798 }
Craig Topper731dfd02012-04-23 03:42:40 +000010799 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010800 }
Nadav Rotem43012222011-05-11 08:12:09 +000010801 }
10802 }
10803
10804 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010805 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010806 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10807 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010808
Chris Lattner7302d802012-02-06 21:56:39 +000010809 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10810 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010811 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10812 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010813 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010814 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010815
10816 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010817 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010818 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10819 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10820 }
Nadav Rotem43012222011-05-11 08:12:09 +000010821 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010822 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010823
Nate Begeman51409212010-07-28 00:21:48 +000010824 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010825 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10826 DAG.getConstant(5, MVT::i32));
10827 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010828
Lang Hames8b99c1e2011-12-17 01:08:46 +000010829 // Turn 'a' into a mask suitable for VSELECT
10830 SDValue VSelM = DAG.getConstant(0x80, VT);
10831 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010832 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010833
Lang Hames8b99c1e2011-12-17 01:08:46 +000010834 SDValue CM1 = DAG.getConstant(0x0f, VT);
10835 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010836
Lang Hames8b99c1e2011-12-17 01:08:46 +000010837 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10838 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010839 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10840 DAG.getConstant(4, MVT::i32), DAG);
10841 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010842 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10843
Nate Begeman51409212010-07-28 00:21:48 +000010844 // a += a
10845 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010846 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010847 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010848
Lang Hames8b99c1e2011-12-17 01:08:46 +000010849 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10850 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010851 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10852 DAG.getConstant(2, MVT::i32), DAG);
10853 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010854 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10855
Nate Begeman51409212010-07-28 00:21:48 +000010856 // a += a
10857 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010858 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010859 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010860
Lang Hames8b99c1e2011-12-17 01:08:46 +000010861 // return VSELECT(r, r+r, a);
10862 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010863 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010864 return R;
10865 }
Craig Topper46154eb2011-11-11 07:39:23 +000010866
10867 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010868 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010869 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010870 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10871 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10872
10873 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010874 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10875 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010876
10877 // Recreate the shift amount vectors
10878 SDValue Amt1, Amt2;
10879 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10880 // Constant shift amount
10881 SmallVector<SDValue, 4> Amt1Csts;
10882 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010883 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010884 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010885 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010886 Amt2Csts.push_back(Amt->getOperand(i));
10887
10888 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10889 &Amt1Csts[0], NumElems/2);
10890 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10891 &Amt2Csts[0], NumElems/2);
10892 } else {
10893 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010894 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10895 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010896 }
10897
10898 // Issue new vector shifts for the smaller types
10899 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10900 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10901
10902 // Concatenate the result back
10903 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10904 }
10905
Nate Begeman51409212010-07-28 00:21:48 +000010906 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010907}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010908
Craig Topper55b24052012-09-11 06:15:32 +000010909static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000010910 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10911 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010912 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10913 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010914 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010915 SDValue LHS = N->getOperand(0);
10916 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010917 unsigned BaseOp = 0;
10918 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010919 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010920 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010921 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010922 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010923 // A subtract of one will be selected as a INC. Note that INC doesn't
10924 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010925 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10926 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010927 BaseOp = X86ISD::INC;
10928 Cond = X86::COND_O;
10929 break;
10930 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010931 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010932 Cond = X86::COND_O;
10933 break;
10934 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010935 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010936 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010937 break;
10938 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010939 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10940 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010941 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10942 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010943 BaseOp = X86ISD::DEC;
10944 Cond = X86::COND_O;
10945 break;
10946 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010947 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010948 Cond = X86::COND_O;
10949 break;
10950 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010951 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010952 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010953 break;
10954 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010955 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010956 Cond = X86::COND_O;
10957 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010958 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10959 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10960 MVT::i32);
10961 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010962
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010963 SDValue SetCC =
10964 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10965 DAG.getConstant(X86::COND_O, MVT::i32),
10966 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010967
Dan Gohman6e5fda22011-07-22 18:45:15 +000010968 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010969 }
Bill Wendling74c37652008-12-09 22:08:41 +000010970 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010971
Bill Wendling61edeb52008-12-02 01:06:39 +000010972 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010973 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010974 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010975
Bill Wendling61edeb52008-12-02 01:06:39 +000010976 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010977 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10978 DAG.getConstant(Cond, MVT::i32),
10979 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010980
Dan Gohman6e5fda22011-07-22 18:45:15 +000010981 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010982}
10983
Chad Rosier30450e82011-12-22 22:35:21 +000010984SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10985 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010986 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010987 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10988 EVT VT = Op.getValueType();
10989
Craig Toppered2e13d2012-01-22 19:15:14 +000010990 if (!Subtarget->hasSSE2() || !VT.isVector())
10991 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010992
Craig Toppered2e13d2012-01-22 19:15:14 +000010993 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10994 ExtraVT.getScalarType().getSizeInBits();
10995 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10996
10997 switch (VT.getSimpleVT().SimpleTy) {
10998 default: return SDValue();
10999 case MVT::v8i32:
11000 case MVT::v16i16:
11001 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011002 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011003 if (!Subtarget->hasAVX2()) {
11004 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011005 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011006
Craig Toppered2e13d2012-01-22 19:15:14 +000011007 // Extract the LHS vectors
11008 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011009 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11010 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011011
Craig Toppered2e13d2012-01-22 19:15:14 +000011012 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11013 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011014
Craig Toppered2e13d2012-01-22 19:15:14 +000011015 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011016 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011017 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11018 ExtraNumElems/2);
11019 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011020
Craig Toppered2e13d2012-01-22 19:15:14 +000011021 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11022 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011023
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011024 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011025 }
11026 // fall through
11027 case MVT::v4i32:
11028 case MVT::v8i16: {
11029 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11030 Op.getOperand(0), ShAmt, DAG);
11031 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011032 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011033 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011034}
11035
11036
Craig Topper55b24052012-09-11 06:15:32 +000011037static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11038 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011039 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011040
Eric Christopher77ed1352011-07-08 00:04:56 +000011041 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11042 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011043 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011044 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011045 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011046 SDValue Ops[] = {
11047 DAG.getRegister(X86::ESP, MVT::i32), // Base
11048 DAG.getTargetConstant(1, MVT::i8), // Scale
11049 DAG.getRegister(0, MVT::i32), // Index
11050 DAG.getTargetConstant(0, MVT::i32), // Disp
11051 DAG.getRegister(0, MVT::i32), // Segment.
11052 Zero,
11053 Chain
11054 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011055 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011056 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11057 array_lengthof(Ops));
11058 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011059 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011060
Eric Christopher9a9d2752010-07-22 02:48:34 +000011061 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011062 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011063 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011064
Chris Lattner132929a2010-08-14 17:26:09 +000011065 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11066 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11067 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11068 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011069
Chris Lattner132929a2010-08-14 17:26:09 +000011070 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11071 if (!Op1 && !Op2 && !Op3 && Op4)
11072 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011073
Chris Lattner132929a2010-08-14 17:26:09 +000011074 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11075 if (Op1 && !Op2 && !Op3 && !Op4)
11076 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011077
11078 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011079 // (MFENCE)>;
11080 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011081}
11082
Craig Topper55b24052012-09-11 06:15:32 +000011083static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11084 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011085 DebugLoc dl = Op.getDebugLoc();
11086 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11087 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11088 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11089 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11090
11091 // The only fence that needs an instruction is a sequentially-consistent
11092 // cross-thread fence.
11093 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11094 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11095 // no-sse2). There isn't any reason to disable it if the target processor
11096 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011097 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011098 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11099
11100 SDValue Chain = Op.getOperand(0);
11101 SDValue Zero = DAG.getConstant(0, MVT::i32);
11102 SDValue Ops[] = {
11103 DAG.getRegister(X86::ESP, MVT::i32), // Base
11104 DAG.getTargetConstant(1, MVT::i8), // Scale
11105 DAG.getRegister(0, MVT::i32), // Index
11106 DAG.getTargetConstant(0, MVT::i32), // Disp
11107 DAG.getRegister(0, MVT::i32), // Segment.
11108 Zero,
11109 Chain
11110 };
11111 SDNode *Res =
11112 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11113 array_lengthof(Ops));
11114 return SDValue(Res, 0);
11115 }
11116
11117 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11118 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11119}
11120
11121
Craig Topper55b24052012-09-11 06:15:32 +000011122static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11123 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011124 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011125 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011126 unsigned Reg = 0;
11127 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011128 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011129 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011130 case MVT::i8: Reg = X86::AL; size = 1; break;
11131 case MVT::i16: Reg = X86::AX; size = 2; break;
11132 case MVT::i32: Reg = X86::EAX; size = 4; break;
11133 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011134 assert(Subtarget->is64Bit() && "Node not type legal!");
11135 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011136 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011137 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011138 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011139 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011140 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011141 Op.getOperand(1),
11142 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011143 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011144 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011145 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011146 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11147 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11148 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011149 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011150 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011151 return cpOut;
11152}
11153
Craig Topper55b24052012-09-11 06:15:32 +000011154static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11155 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011156 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011157 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011158 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011159 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011160 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011161 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11162 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011163 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011164 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11165 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011166 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011167 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011168 rdx.getValue(1)
11169 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011170 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011171}
11172
Craig Topper55b24052012-09-11 06:15:32 +000011173SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011174 EVT SrcVT = Op.getOperand(0).getValueType();
11175 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011176 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011177 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011178 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011179 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011180 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011181 // i64 <=> MMX conversions are Legal.
11182 if (SrcVT==MVT::i64 && DstVT.isVector())
11183 return Op;
11184 if (DstVT==MVT::i64 && SrcVT.isVector())
11185 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011186 // MMX <=> MMX conversions are Legal.
11187 if (SrcVT.isVector() && DstVT.isVector())
11188 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011189 // All other conversions need to be expanded.
11190 return SDValue();
11191}
Chris Lattner5b856542010-12-20 00:59:46 +000011192
Craig Topper55b24052012-09-11 06:15:32 +000011193static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011194 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011195 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011196 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011197 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011198 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011199 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011200 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011201 Node->getOperand(0),
11202 Node->getOperand(1), negOp,
11203 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011204 cast<AtomicSDNode>(Node)->getAlignment(),
11205 cast<AtomicSDNode>(Node)->getOrdering(),
11206 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011207}
11208
Eli Friedman327236c2011-08-24 20:50:09 +000011209static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11210 SDNode *Node = Op.getNode();
11211 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011212 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011213
11214 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011215 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11216 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11217 // (The only way to get a 16-byte store is cmpxchg16b)
11218 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11219 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11220 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011221 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11222 cast<AtomicSDNode>(Node)->getMemoryVT(),
11223 Node->getOperand(0),
11224 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011225 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011226 cast<AtomicSDNode>(Node)->getOrdering(),
11227 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011228 return Swap.getValue(1);
11229 }
11230 // Other atomic stores have a simple pattern.
11231 return Op;
11232}
11233
Chris Lattner5b856542010-12-20 00:59:46 +000011234static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11235 EVT VT = Op.getNode()->getValueType(0);
11236
11237 // Let legalize expand this if it isn't a legal type yet.
11238 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11239 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011240
Chris Lattner5b856542010-12-20 00:59:46 +000011241 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011242
Chris Lattner5b856542010-12-20 00:59:46 +000011243 unsigned Opc;
11244 bool ExtraOp = false;
11245 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011246 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011247 case ISD::ADDC: Opc = X86ISD::ADD; break;
11248 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11249 case ISD::SUBC: Opc = X86ISD::SUB; break;
11250 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11251 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011252
Chris Lattner5b856542010-12-20 00:59:46 +000011253 if (!ExtraOp)
11254 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11255 Op.getOperand(1));
11256 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11257 Op.getOperand(1), Op.getOperand(2));
11258}
11259
Evan Cheng0db9fe62006-04-25 20:13:52 +000011260/// LowerOperation - Provide custom lowering hooks for some operations.
11261///
Dan Gohmand858e902010-04-17 15:26:15 +000011262SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011263 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011264 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011265 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011266 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11267 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11268 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011269 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011270 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011271 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011272 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011273 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11274 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11275 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011276 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11277 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011278 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11279 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11280 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011281 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011282 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011283 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011284 case ISD::SHL_PARTS:
11285 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011286 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011287 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011288 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011289 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011290 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011291 case ISD::FABS: return LowerFABS(Op, DAG);
11292 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011293 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011294 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011295 case ISD::SETCC: return LowerSETCC(Op, DAG);
11296 case ISD::SELECT: return LowerSELECT(Op, DAG);
11297 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011298 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011299 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011300 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011301 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011302 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011303 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011304 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11305 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011306 case ISD::FRAME_TO_ARGS_OFFSET:
11307 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011308 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011309 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011310 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11311 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011312 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011313 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011314 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011315 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011316 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011317 case ISD::SRA:
11318 case ISD::SRL:
11319 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011320 case ISD::SADDO:
11321 case ISD::UADDO:
11322 case ISD::SSUBO:
11323 case ISD::USUBO:
11324 case ISD::SMULO:
11325 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011326 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011327 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011328 case ISD::ADDC:
11329 case ISD::ADDE:
11330 case ISD::SUBC:
11331 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011332 case ISD::ADD: return LowerADD(Op, DAG);
11333 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011334 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011335}
11336
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011337static void ReplaceATOMIC_LOAD(SDNode *Node,
11338 SmallVectorImpl<SDValue> &Results,
11339 SelectionDAG &DAG) {
11340 DebugLoc dl = Node->getDebugLoc();
11341 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11342
11343 // Convert wide load -> cmpxchg8b/cmpxchg16b
11344 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11345 // (The only way to get a 16-byte load is cmpxchg16b)
11346 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011347 SDValue Zero = DAG.getConstant(0, VT);
11348 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011349 Node->getOperand(0),
11350 Node->getOperand(1), Zero, Zero,
11351 cast<AtomicSDNode>(Node)->getMemOperand(),
11352 cast<AtomicSDNode>(Node)->getOrdering(),
11353 cast<AtomicSDNode>(Node)->getSynchScope());
11354 Results.push_back(Swap.getValue(0));
11355 Results.push_back(Swap.getValue(1));
11356}
11357
Craig Topperc0878702012-08-17 06:55:11 +000011358static void
Duncan Sands1607f052008-12-01 11:39:25 +000011359ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011360 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011361 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011362 assert (Node->getValueType(0) == MVT::i64 &&
11363 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011364
11365 SDValue Chain = Node->getOperand(0);
11366 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011367 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011368 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011369 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011370 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011371 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011372 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011373 SDValue Result =
11374 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11375 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011376 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011377 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011378 Results.push_back(Result.getValue(2));
11379}
11380
Duncan Sands126d9072008-07-04 11:47:58 +000011381/// ReplaceNodeResults - Replace a node with an illegal result type
11382/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011383void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11384 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011385 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011386 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011387 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011388 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011389 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011390 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011391 case ISD::ADDC:
11392 case ISD::ADDE:
11393 case ISD::SUBC:
11394 case ISD::SUBE:
11395 // We don't want to expand or promote these.
11396 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011397 case ISD::FP_TO_SINT:
11398 case ISD::FP_TO_UINT: {
11399 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11400
11401 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11402 return;
11403
Eli Friedman948e95a2009-05-23 09:59:16 +000011404 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011405 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011406 SDValue FIST = Vals.first, StackSlot = Vals.second;
11407 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011408 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011409 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011410 if (StackSlot.getNode() != 0)
11411 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11412 MachinePointerInfo(),
11413 false, false, false, 0));
11414 else
11415 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011416 }
11417 return;
11418 }
11419 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011420 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011421 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011422 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011423 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011424 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011425 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011426 eax.getValue(2));
11427 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11428 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011429 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011430 Results.push_back(edx.getValue(1));
11431 return;
11432 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011433 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011434 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011435 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011436 bool Regs64bit = T == MVT::i128;
11437 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011438 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011439 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11440 DAG.getConstant(0, HalfT));
11441 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11442 DAG.getConstant(1, HalfT));
11443 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11444 Regs64bit ? X86::RAX : X86::EAX,
11445 cpInL, SDValue());
11446 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11447 Regs64bit ? X86::RDX : X86::EDX,
11448 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011449 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011450 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11451 DAG.getConstant(0, HalfT));
11452 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11453 DAG.getConstant(1, HalfT));
11454 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11455 Regs64bit ? X86::RBX : X86::EBX,
11456 swapInL, cpInH.getValue(1));
11457 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011458 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011459 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011460 SDValue Ops[] = { swapInH.getValue(0),
11461 N->getOperand(1),
11462 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011463 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011464 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011465 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11466 X86ISD::LCMPXCHG8_DAG;
11467 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011468 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011469 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11470 Regs64bit ? X86::RAX : X86::EAX,
11471 HalfT, Result.getValue(1));
11472 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11473 Regs64bit ? X86::RDX : X86::EDX,
11474 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011475 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011476 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011477 Results.push_back(cpOutH.getValue(1));
11478 return;
11479 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011480 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011481 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011482 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011483 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011484 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011485 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011486 case ISD::ATOMIC_SWAP: {
11487 unsigned Opc;
11488 switch (N->getOpcode()) {
11489 default: llvm_unreachable("Unexpected opcode");
11490 case ISD::ATOMIC_LOAD_ADD:
11491 Opc = X86ISD::ATOMADD64_DAG;
11492 break;
11493 case ISD::ATOMIC_LOAD_AND:
11494 Opc = X86ISD::ATOMAND64_DAG;
11495 break;
11496 case ISD::ATOMIC_LOAD_NAND:
11497 Opc = X86ISD::ATOMNAND64_DAG;
11498 break;
11499 case ISD::ATOMIC_LOAD_OR:
11500 Opc = X86ISD::ATOMOR64_DAG;
11501 break;
11502 case ISD::ATOMIC_LOAD_SUB:
11503 Opc = X86ISD::ATOMSUB64_DAG;
11504 break;
11505 case ISD::ATOMIC_LOAD_XOR:
11506 Opc = X86ISD::ATOMXOR64_DAG;
11507 break;
11508 case ISD::ATOMIC_SWAP:
11509 Opc = X86ISD::ATOMSWAP64_DAG;
11510 break;
11511 }
11512 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011513 return;
Craig Topperc0878702012-08-17 06:55:11 +000011514 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011515 case ISD::ATOMIC_LOAD:
11516 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011517 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011518}
11519
Evan Cheng72261582005-12-20 06:22:03 +000011520const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11521 switch (Opcode) {
11522 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011523 case X86ISD::BSF: return "X86ISD::BSF";
11524 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011525 case X86ISD::SHLD: return "X86ISD::SHLD";
11526 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011527 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011528 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011529 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011530 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011531 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011532 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011533 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11534 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11535 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011536 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011537 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011538 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011539 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011540 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011541 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011542 case X86ISD::COMI: return "X86ISD::COMI";
11543 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011544 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011545 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011546 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11547 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011548 case X86ISD::CMOV: return "X86ISD::CMOV";
11549 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011550 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011551 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11552 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011553 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011554 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011555 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011556 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011557 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011558 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11559 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011560 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011561 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011562 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011563 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011564 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011565 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11566 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11567 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011568 case X86ISD::HADD: return "X86ISD::HADD";
11569 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011570 case X86ISD::FHADD: return "X86ISD::FHADD";
11571 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011572 case X86ISD::FMAX: return "X86ISD::FMAX";
11573 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011574 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11575 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011576 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11577 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011578 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011579 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011580 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011581 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011582 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011583 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011584 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011585 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11586 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011587 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11588 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11589 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11590 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11591 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11592 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011593 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011594 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011595 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011596 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011597 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11598 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011599 case X86ISD::VSHL: return "X86ISD::VSHL";
11600 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011601 case X86ISD::VSRA: return "X86ISD::VSRA";
11602 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11603 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11604 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011605 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011606 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11607 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011608 case X86ISD::ADD: return "X86ISD::ADD";
11609 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011610 case X86ISD::ADC: return "X86ISD::ADC";
11611 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011612 case X86ISD::SMUL: return "X86ISD::SMUL";
11613 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011614 case X86ISD::INC: return "X86ISD::INC";
11615 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011616 case X86ISD::OR: return "X86ISD::OR";
11617 case X86ISD::XOR: return "X86ISD::XOR";
11618 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011619 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011620 case X86ISD::BLSI: return "X86ISD::BLSI";
11621 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11622 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011623 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011624 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011625 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011626 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11627 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11628 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011629 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011630 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011631 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011632 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011633 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011634 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11635 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011636 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11637 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11638 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011639 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11640 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011641 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11642 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011643 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011644 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011645 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011646 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11647 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011648 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011649 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011650 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011651 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011652 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011653 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011654 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011655 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011656 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011657 case X86ISD::FMADD: return "X86ISD::FMADD";
11658 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11659 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11660 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11661 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11662 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011663 }
11664}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011665
Chris Lattnerc9addb72007-03-30 23:15:24 +000011666// isLegalAddressingMode - Return true if the addressing mode represented
11667// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011668bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011669 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011670 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011671 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011672 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011673
Chris Lattnerc9addb72007-03-30 23:15:24 +000011674 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011675 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011676 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011677
Chris Lattnerc9addb72007-03-30 23:15:24 +000011678 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011679 unsigned GVFlags =
11680 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011681
Chris Lattnerdfed4132009-07-10 07:38:24 +000011682 // If a reference to this global requires an extra load, we can't fold it.
11683 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011684 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011685
Chris Lattnerdfed4132009-07-10 07:38:24 +000011686 // If BaseGV requires a register for the PIC base, we cannot also have a
11687 // BaseReg specified.
11688 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011689 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011690
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011691 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011692 if ((M != CodeModel::Small || R != Reloc::Static) &&
11693 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011694 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011695 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011696
Chris Lattnerc9addb72007-03-30 23:15:24 +000011697 switch (AM.Scale) {
11698 case 0:
11699 case 1:
11700 case 2:
11701 case 4:
11702 case 8:
11703 // These scales always work.
11704 break;
11705 case 3:
11706 case 5:
11707 case 9:
11708 // These scales are formed with basereg+scalereg. Only accept if there is
11709 // no basereg yet.
11710 if (AM.HasBaseReg)
11711 return false;
11712 break;
11713 default: // Other stuff never works.
11714 return false;
11715 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011716
Chris Lattnerc9addb72007-03-30 23:15:24 +000011717 return true;
11718}
11719
11720
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011721bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011722 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011723 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011724 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11725 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011726 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011727 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011728 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011729}
11730
Evan Cheng70e10d32012-07-17 06:53:39 +000011731bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11732 return Imm == (int32_t)Imm;
11733}
11734
11735bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011736 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011737 return Imm == (int32_t)Imm;
11738}
11739
Owen Andersone50ed302009-08-10 22:56:29 +000011740bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011741 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011742 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011743 unsigned NumBits1 = VT1.getSizeInBits();
11744 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011745 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011746 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011747 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011748}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011749
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011750bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011751 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011752 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011753}
11754
Owen Andersone50ed302009-08-10 22:56:29 +000011755bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011756 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011757 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011758}
11759
Owen Andersone50ed302009-08-10 22:56:29 +000011760bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011761 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011762 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011763}
11764
Evan Cheng60c07e12006-07-05 22:17:51 +000011765/// isShuffleMaskLegal - Targets can use this to indicate that they only
11766/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11767/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11768/// are assumed to be legal.
11769bool
Eric Christopherfd179292009-08-27 18:07:15 +000011770X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011771 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011772 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011773 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011774 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011775
Nate Begemana09008b2009-10-19 02:17:23 +000011776 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011777 return (VT.getVectorNumElements() == 2 ||
11778 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11779 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011780 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011781 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011782 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11783 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011784 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011785 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11786 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011787 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11788 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011789}
11790
Dan Gohman7d8143f2008-04-09 20:09:42 +000011791bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011792X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011793 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011794 unsigned NumElts = VT.getVectorNumElements();
11795 // FIXME: This collection of masks seems suspect.
11796 if (NumElts == 2)
11797 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011798 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011799 return (isMOVLMask(Mask, VT) ||
11800 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011801 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11802 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011803 }
11804 return false;
11805}
11806
11807//===----------------------------------------------------------------------===//
11808// X86 Scheduler Hooks
11809//===----------------------------------------------------------------------===//
11810
Mon P Wang63307c32008-05-05 19:05:59 +000011811// private utility function
11812MachineBasicBlock *
11813X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11814 MachineBasicBlock *MBB,
11815 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011816 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011817 unsigned LoadOpc,
11818 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011819 unsigned notOpc,
11820 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011821 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011822 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011823 // For the atomic bitwise operator, we generate
11824 // thisMBB:
11825 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011826 // ld t1 = [bitinstr.addr]
11827 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011828 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011829 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011830 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011831 // bz newMBB
11832 // fallthrough -->nextMBB
11833 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11834 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011835 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011836 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011837
Mon P Wang63307c32008-05-05 19:05:59 +000011838 /// First build the CFG
11839 MachineFunction *F = MBB->getParent();
11840 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011841 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11842 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11843 F->insert(MBBIter, newMBB);
11844 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011845
Dan Gohman14152b42010-07-06 20:24:04 +000011846 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11847 nextMBB->splice(nextMBB->begin(), thisMBB,
11848 llvm::next(MachineBasicBlock::iterator(bInstr)),
11849 thisMBB->end());
11850 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011851
Mon P Wang63307c32008-05-05 19:05:59 +000011852 // Update thisMBB to fall through to newMBB
11853 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011854
Mon P Wang63307c32008-05-05 19:05:59 +000011855 // newMBB jumps to itself and fall through to nextMBB
11856 newMBB->addSuccessor(nextMBB);
11857 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011858
Mon P Wang63307c32008-05-05 19:05:59 +000011859 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011860 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011861 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011862 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011863 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011864 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011865 int numArgs = bInstr->getNumOperands() - 1;
11866 for (int i=0; i < numArgs; ++i)
11867 argOpers[i] = &bInstr->getOperand(i+1);
11868
11869 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011870 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011871 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011872
Dale Johannesen140be2d2008-08-19 18:47:28 +000011873 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011874 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011875 for (int i=0; i <= lastAddrIndx; ++i)
11876 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011877
Dale Johannesen140be2d2008-08-19 18:47:28 +000011878 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011879 assert((argOpers[valArgIndx]->isReg() ||
11880 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011881 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011882 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011883 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011884 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011885 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011886 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011887 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011888
Richard Smith42fc29e2012-04-13 22:47:00 +000011889 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11890 if (Invert) {
11891 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11892 }
11893 else
11894 t3 = t2;
11895
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011896 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011897 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011898
Dale Johannesene4d209d2009-02-03 20:21:25 +000011899 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011900 for (int i=0; i <= lastAddrIndx; ++i)
11901 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011902 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011903 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011904 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11905 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011906
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011907 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011908 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011909
Mon P Wang63307c32008-05-05 19:05:59 +000011910 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011911 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011912
Dan Gohman14152b42010-07-06 20:24:04 +000011913 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011914 return nextMBB;
11915}
11916
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011917// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011918MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011919X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11920 MachineBasicBlock *MBB,
11921 unsigned regOpcL,
11922 unsigned regOpcH,
11923 unsigned immOpcL,
11924 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011925 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011926 // For the atomic bitwise operator, we generate
11927 // thisMBB (instructions are in pairs, except cmpxchg8b)
11928 // ld t1,t2 = [bitinstr.addr]
11929 // newMBB:
11930 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11931 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011932 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011933 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011934 // mov ECX, EBX <- t5, t6
11935 // mov EAX, EDX <- t1, t2
11936 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11937 // mov t3, t4 <- EAX, EDX
11938 // bz newMBB
11939 // result in out1, out2
11940 // fallthrough -->nextMBB
11941
Craig Topperc9099502012-04-20 06:31:50 +000011942 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011943 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011944 const unsigned NotOpc = X86::NOT32r;
11945 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11946 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11947 MachineFunction::iterator MBBIter = MBB;
11948 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011949
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011950 /// First build the CFG
11951 MachineFunction *F = MBB->getParent();
11952 MachineBasicBlock *thisMBB = MBB;
11953 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11954 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11955 F->insert(MBBIter, newMBB);
11956 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011957
Dan Gohman14152b42010-07-06 20:24:04 +000011958 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11959 nextMBB->splice(nextMBB->begin(), thisMBB,
11960 llvm::next(MachineBasicBlock::iterator(bInstr)),
11961 thisMBB->end());
11962 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011963
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011964 // Update thisMBB to fall through to newMBB
11965 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011966
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011967 // newMBB jumps to itself and fall through to nextMBB
11968 newMBB->addSuccessor(nextMBB);
11969 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011970
Dale Johannesene4d209d2009-02-03 20:21:25 +000011971 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011972 // Insert instructions into newMBB based on incoming instruction
11973 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011974 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011975 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011976 MachineOperand& dest1Oper = bInstr->getOperand(0);
11977 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011978 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11979 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011980 argOpers[i] = &bInstr->getOperand(i+2);
11981
Dan Gohman71ea4e52010-05-14 21:01:44 +000011982 // We use some of the operands multiple times, so conservatively just
11983 // clear any kill flags that might be present.
11984 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11985 argOpers[i]->setIsKill(false);
11986 }
11987
Evan Chengad5b52f2010-01-08 19:14:57 +000011988 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011989 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011990
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011991 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011992 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011993 for (int i=0; i <= lastAddrIndx; ++i)
11994 (*MIB).addOperand(*argOpers[i]);
11995 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011996 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011997 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011998 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011999 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000012000 MachineOperand newOp3 = *(argOpers[3]);
12001 if (newOp3.isImm())
12002 newOp3.setImm(newOp3.getImm()+4);
12003 else
12004 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012005 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000012006 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012007
12008 // t3/4 are defined later, at the bottom of the loop
12009 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
12010 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012011 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012012 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012013 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012014 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
12015
Evan Cheng306b4ca2010-01-08 23:41:50 +000012016 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000012017 // the PHI instructions.
12018 t1 = dest1Oper.getReg();
12019 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012020
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012021 int valArgIndx = lastAddrIndx + 1;
12022 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000012023 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012024 "invalid operand");
12025 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
12026 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012027 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012028 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012029 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012030 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000012031 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012032 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012033 (*MIB).addOperand(*argOpers[valArgIndx]);
12034 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012035 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012036 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012037 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012038 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012039 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012040 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012041 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000012042 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012043 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012044 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012045
Richard Smith42fc29e2012-04-13 22:47:00 +000012046 unsigned t7, t8;
12047 if (Invert) {
12048 t7 = F->getRegInfo().createVirtualRegister(RC);
12049 t8 = F->getRegInfo().createVirtualRegister(RC);
12050 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
12051 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
12052 } else {
12053 t7 = t5;
12054 t8 = t6;
12055 }
12056
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012057 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012058 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012059 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012060 MIB.addReg(t2);
12061
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012062 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012063 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012064 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012065 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000012066
Dale Johannesene4d209d2009-02-03 20:21:25 +000012067 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012068 for (int i=0; i <= lastAddrIndx; ++i)
12069 (*MIB).addOperand(*argOpers[i]);
12070
12071 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012072 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12073 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012074
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012075 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012076 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012077 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012078 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012079
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012080 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012081 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012082
Dan Gohman14152b42010-07-06 20:24:04 +000012083 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012084 return nextMBB;
12085}
12086
12087// private utility function
12088MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000012089X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
12090 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000012091 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000012092 // For the atomic min/max operator, we generate
12093 // thisMBB:
12094 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000012095 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000012096 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000012097 // cmp t1, t2
12098 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000012099 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000012100 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
12101 // bz newMBB
12102 // fallthrough -->nextMBB
12103 //
12104 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12105 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012106 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012107 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012108
Mon P Wang63307c32008-05-05 19:05:59 +000012109 /// First build the CFG
12110 MachineFunction *F = MBB->getParent();
12111 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012112 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12113 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12114 F->insert(MBBIter, newMBB);
12115 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012116
Dan Gohman14152b42010-07-06 20:24:04 +000012117 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12118 nextMBB->splice(nextMBB->begin(), thisMBB,
12119 llvm::next(MachineBasicBlock::iterator(mInstr)),
12120 thisMBB->end());
12121 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012122
Mon P Wang63307c32008-05-05 19:05:59 +000012123 // Update thisMBB to fall through to newMBB
12124 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012125
Mon P Wang63307c32008-05-05 19:05:59 +000012126 // newMBB jumps to newMBB and fall through to nextMBB
12127 newMBB->addSuccessor(nextMBB);
12128 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012129
Dale Johannesene4d209d2009-02-03 20:21:25 +000012130 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000012131 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012132 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012133 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000012134 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012135 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000012136 int numArgs = mInstr->getNumOperands() - 1;
12137 for (int i=0; i < numArgs; ++i)
12138 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012139
Mon P Wang63307c32008-05-05 19:05:59 +000012140 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012141 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012142 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000012143
Craig Topperc9099502012-04-20 06:31:50 +000012144 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012145 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000012146 for (int i=0; i <= lastAddrIndx; ++i)
12147 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000012148
Mon P Wang63307c32008-05-05 19:05:59 +000012149 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000012150 assert((argOpers[valArgIndx]->isReg() ||
12151 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000012152 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000012153
Craig Topperc9099502012-04-20 06:31:50 +000012154 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012155 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012156 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012157 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012158 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012159 (*MIB).addOperand(*argOpers[valArgIndx]);
12160
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012161 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012162 MIB.addReg(t1);
12163
Dale Johannesene4d209d2009-02-03 20:21:25 +000012164 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012165 MIB.addReg(t1);
12166 MIB.addReg(t2);
12167
12168 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012169 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012170 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012171 MIB.addReg(t2);
12172 MIB.addReg(t1);
12173
12174 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012175 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012176 for (int i=0; i <= lastAddrIndx; ++i)
12177 (*MIB).addOperand(*argOpers[i]);
12178 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012179 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012180 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12181 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012182
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012183 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012184 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012185
Mon P Wang63307c32008-05-05 19:05:59 +000012186 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012187 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012188
Dan Gohman14152b42010-07-06 20:24:04 +000012189 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012190 return nextMBB;
12191}
12192
Eric Christopherf83a5de2009-08-27 18:08:16 +000012193// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012194// or XMM0_V32I8 in AVX all of this code can be replaced with that
12195// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012196MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012197X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012198 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012199 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012200 "Target must have SSE4.2 or AVX features enabled");
12201
Eric Christopherb120ab42009-08-18 22:50:32 +000012202 DebugLoc dl = MI->getDebugLoc();
12203 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012204 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012205 if (!Subtarget->hasAVX()) {
12206 if (memArg)
12207 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12208 else
12209 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12210 } else {
12211 if (memArg)
12212 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12213 else
12214 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12215 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012216
Eric Christopher41c902f2010-11-30 08:20:21 +000012217 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012218 for (unsigned i = 0; i < numArgs; ++i) {
12219 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012220 if (!(Op.isReg() && Op.isImplicit()))
12221 MIB.addOperand(Op);
12222 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012223 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012224 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012225 .addReg(X86::XMM0);
12226
Dan Gohman14152b42010-07-06 20:24:04 +000012227 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012228 return BB;
12229}
12230
12231MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012232X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012233 DebugLoc dl = MI->getDebugLoc();
12234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012235
Eric Christopher228232b2010-11-30 07:20:12 +000012236 // Address into RAX/EAX, other two args into ECX, EDX.
12237 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12238 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12239 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12240 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012241 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012242
Eric Christopher228232b2010-11-30 07:20:12 +000012243 unsigned ValOps = X86::AddrNumOperands;
12244 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12245 .addReg(MI->getOperand(ValOps).getReg());
12246 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12247 .addReg(MI->getOperand(ValOps+1).getReg());
12248
12249 // The instruction doesn't actually take any operands though.
12250 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012251
Eric Christopher228232b2010-11-30 07:20:12 +000012252 MI->eraseFromParent(); // The pseudo is gone now.
12253 return BB;
12254}
12255
12256MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012257X86TargetLowering::EmitVAARG64WithCustomInserter(
12258 MachineInstr *MI,
12259 MachineBasicBlock *MBB) const {
12260 // Emit va_arg instruction on X86-64.
12261
12262 // Operands to this pseudo-instruction:
12263 // 0 ) Output : destination address (reg)
12264 // 1-5) Input : va_list address (addr, i64mem)
12265 // 6 ) ArgSize : Size (in bytes) of vararg type
12266 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12267 // 8 ) Align : Alignment of type
12268 // 9 ) EFLAGS (implicit-def)
12269
12270 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12271 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12272
12273 unsigned DestReg = MI->getOperand(0).getReg();
12274 MachineOperand &Base = MI->getOperand(1);
12275 MachineOperand &Scale = MI->getOperand(2);
12276 MachineOperand &Index = MI->getOperand(3);
12277 MachineOperand &Disp = MI->getOperand(4);
12278 MachineOperand &Segment = MI->getOperand(5);
12279 unsigned ArgSize = MI->getOperand(6).getImm();
12280 unsigned ArgMode = MI->getOperand(7).getImm();
12281 unsigned Align = MI->getOperand(8).getImm();
12282
12283 // Memory Reference
12284 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12285 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12286 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12287
12288 // Machine Information
12289 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12290 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12291 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12292 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12293 DebugLoc DL = MI->getDebugLoc();
12294
12295 // struct va_list {
12296 // i32 gp_offset
12297 // i32 fp_offset
12298 // i64 overflow_area (address)
12299 // i64 reg_save_area (address)
12300 // }
12301 // sizeof(va_list) = 24
12302 // alignment(va_list) = 8
12303
12304 unsigned TotalNumIntRegs = 6;
12305 unsigned TotalNumXMMRegs = 8;
12306 bool UseGPOffset = (ArgMode == 1);
12307 bool UseFPOffset = (ArgMode == 2);
12308 unsigned MaxOffset = TotalNumIntRegs * 8 +
12309 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12310
12311 /* Align ArgSize to a multiple of 8 */
12312 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12313 bool NeedsAlign = (Align > 8);
12314
12315 MachineBasicBlock *thisMBB = MBB;
12316 MachineBasicBlock *overflowMBB;
12317 MachineBasicBlock *offsetMBB;
12318 MachineBasicBlock *endMBB;
12319
12320 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12321 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12322 unsigned OffsetReg = 0;
12323
12324 if (!UseGPOffset && !UseFPOffset) {
12325 // If we only pull from the overflow region, we don't create a branch.
12326 // We don't need to alter control flow.
12327 OffsetDestReg = 0; // unused
12328 OverflowDestReg = DestReg;
12329
12330 offsetMBB = NULL;
12331 overflowMBB = thisMBB;
12332 endMBB = thisMBB;
12333 } else {
12334 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12335 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12336 // If not, pull from overflow_area. (branch to overflowMBB)
12337 //
12338 // thisMBB
12339 // | .
12340 // | .
12341 // offsetMBB overflowMBB
12342 // | .
12343 // | .
12344 // endMBB
12345
12346 // Registers for the PHI in endMBB
12347 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12348 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12349
12350 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12351 MachineFunction *MF = MBB->getParent();
12352 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12353 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12354 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12355
12356 MachineFunction::iterator MBBIter = MBB;
12357 ++MBBIter;
12358
12359 // Insert the new basic blocks
12360 MF->insert(MBBIter, offsetMBB);
12361 MF->insert(MBBIter, overflowMBB);
12362 MF->insert(MBBIter, endMBB);
12363
12364 // Transfer the remainder of MBB and its successor edges to endMBB.
12365 endMBB->splice(endMBB->begin(), thisMBB,
12366 llvm::next(MachineBasicBlock::iterator(MI)),
12367 thisMBB->end());
12368 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12369
12370 // Make offsetMBB and overflowMBB successors of thisMBB
12371 thisMBB->addSuccessor(offsetMBB);
12372 thisMBB->addSuccessor(overflowMBB);
12373
12374 // endMBB is a successor of both offsetMBB and overflowMBB
12375 offsetMBB->addSuccessor(endMBB);
12376 overflowMBB->addSuccessor(endMBB);
12377
12378 // Load the offset value into a register
12379 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12380 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12381 .addOperand(Base)
12382 .addOperand(Scale)
12383 .addOperand(Index)
12384 .addDisp(Disp, UseFPOffset ? 4 : 0)
12385 .addOperand(Segment)
12386 .setMemRefs(MMOBegin, MMOEnd);
12387
12388 // Check if there is enough room left to pull this argument.
12389 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12390 .addReg(OffsetReg)
12391 .addImm(MaxOffset + 8 - ArgSizeA8);
12392
12393 // Branch to "overflowMBB" if offset >= max
12394 // Fall through to "offsetMBB" otherwise
12395 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12396 .addMBB(overflowMBB);
12397 }
12398
12399 // In offsetMBB, emit code to use the reg_save_area.
12400 if (offsetMBB) {
12401 assert(OffsetReg != 0);
12402
12403 // Read the reg_save_area address.
12404 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12405 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12406 .addOperand(Base)
12407 .addOperand(Scale)
12408 .addOperand(Index)
12409 .addDisp(Disp, 16)
12410 .addOperand(Segment)
12411 .setMemRefs(MMOBegin, MMOEnd);
12412
12413 // Zero-extend the offset
12414 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12415 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12416 .addImm(0)
12417 .addReg(OffsetReg)
12418 .addImm(X86::sub_32bit);
12419
12420 // Add the offset to the reg_save_area to get the final address.
12421 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12422 .addReg(OffsetReg64)
12423 .addReg(RegSaveReg);
12424
12425 // Compute the offset for the next argument
12426 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12427 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12428 .addReg(OffsetReg)
12429 .addImm(UseFPOffset ? 16 : 8);
12430
12431 // Store it back into the va_list.
12432 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12433 .addOperand(Base)
12434 .addOperand(Scale)
12435 .addOperand(Index)
12436 .addDisp(Disp, UseFPOffset ? 4 : 0)
12437 .addOperand(Segment)
12438 .addReg(NextOffsetReg)
12439 .setMemRefs(MMOBegin, MMOEnd);
12440
12441 // Jump to endMBB
12442 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12443 .addMBB(endMBB);
12444 }
12445
12446 //
12447 // Emit code to use overflow area
12448 //
12449
12450 // Load the overflow_area address into a register.
12451 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12452 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12453 .addOperand(Base)
12454 .addOperand(Scale)
12455 .addOperand(Index)
12456 .addDisp(Disp, 8)
12457 .addOperand(Segment)
12458 .setMemRefs(MMOBegin, MMOEnd);
12459
12460 // If we need to align it, do so. Otherwise, just copy the address
12461 // to OverflowDestReg.
12462 if (NeedsAlign) {
12463 // Align the overflow address
12464 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12465 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12466
12467 // aligned_addr = (addr + (align-1)) & ~(align-1)
12468 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12469 .addReg(OverflowAddrReg)
12470 .addImm(Align-1);
12471
12472 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12473 .addReg(TmpReg)
12474 .addImm(~(uint64_t)(Align-1));
12475 } else {
12476 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12477 .addReg(OverflowAddrReg);
12478 }
12479
12480 // Compute the next overflow address after this argument.
12481 // (the overflow address should be kept 8-byte aligned)
12482 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12483 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12484 .addReg(OverflowDestReg)
12485 .addImm(ArgSizeA8);
12486
12487 // Store the new overflow address.
12488 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12489 .addOperand(Base)
12490 .addOperand(Scale)
12491 .addOperand(Index)
12492 .addDisp(Disp, 8)
12493 .addOperand(Segment)
12494 .addReg(NextAddrReg)
12495 .setMemRefs(MMOBegin, MMOEnd);
12496
12497 // If we branched, emit the PHI to the front of endMBB.
12498 if (offsetMBB) {
12499 BuildMI(*endMBB, endMBB->begin(), DL,
12500 TII->get(X86::PHI), DestReg)
12501 .addReg(OffsetDestReg).addMBB(offsetMBB)
12502 .addReg(OverflowDestReg).addMBB(overflowMBB);
12503 }
12504
12505 // Erase the pseudo instruction
12506 MI->eraseFromParent();
12507
12508 return endMBB;
12509}
12510
12511MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012512X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12513 MachineInstr *MI,
12514 MachineBasicBlock *MBB) const {
12515 // Emit code to save XMM registers to the stack. The ABI says that the
12516 // number of registers to save is given in %al, so it's theoretically
12517 // possible to do an indirect jump trick to avoid saving all of them,
12518 // however this code takes a simpler approach and just executes all
12519 // of the stores if %al is non-zero. It's less code, and it's probably
12520 // easier on the hardware branch predictor, and stores aren't all that
12521 // expensive anyway.
12522
12523 // Create the new basic blocks. One block contains all the XMM stores,
12524 // and one block is the final destination regardless of whether any
12525 // stores were performed.
12526 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12527 MachineFunction *F = MBB->getParent();
12528 MachineFunction::iterator MBBIter = MBB;
12529 ++MBBIter;
12530 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12531 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12532 F->insert(MBBIter, XMMSaveMBB);
12533 F->insert(MBBIter, EndMBB);
12534
Dan Gohman14152b42010-07-06 20:24:04 +000012535 // Transfer the remainder of MBB and its successor edges to EndMBB.
12536 EndMBB->splice(EndMBB->begin(), MBB,
12537 llvm::next(MachineBasicBlock::iterator(MI)),
12538 MBB->end());
12539 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12540
Dan Gohmand6708ea2009-08-15 01:38:56 +000012541 // The original block will now fall through to the XMM save block.
12542 MBB->addSuccessor(XMMSaveMBB);
12543 // The XMMSaveMBB will fall through to the end block.
12544 XMMSaveMBB->addSuccessor(EndMBB);
12545
12546 // Now add the instructions.
12547 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12548 DebugLoc DL = MI->getDebugLoc();
12549
12550 unsigned CountReg = MI->getOperand(0).getReg();
12551 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12552 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12553
12554 if (!Subtarget->isTargetWin64()) {
12555 // If %al is 0, branch around the XMM save block.
12556 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012557 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012558 MBB->addSuccessor(EndMBB);
12559 }
12560
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012561 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012562 // In the XMM save block, save all the XMM argument registers.
12563 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12564 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012565 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012566 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012567 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012568 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012569 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012570 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012571 .addFrameIndex(RegSaveFrameIndex)
12572 .addImm(/*Scale=*/1)
12573 .addReg(/*IndexReg=*/0)
12574 .addImm(/*Disp=*/Offset)
12575 .addReg(/*Segment=*/0)
12576 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012577 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012578 }
12579
Dan Gohman14152b42010-07-06 20:24:04 +000012580 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012581
12582 return EndMBB;
12583}
Mon P Wang63307c32008-05-05 19:05:59 +000012584
Lang Hames6e3f7e42012-02-03 01:13:49 +000012585// The EFLAGS operand of SelectItr might be missing a kill marker
12586// because there were multiple uses of EFLAGS, and ISel didn't know
12587// which to mark. Figure out whether SelectItr should have had a
12588// kill marker, and set it if it should. Returns the correct kill
12589// marker value.
12590static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12591 MachineBasicBlock* BB,
12592 const TargetRegisterInfo* TRI) {
12593 // Scan forward through BB for a use/def of EFLAGS.
12594 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12595 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012596 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012597 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012598 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012599 if (mi.definesRegister(X86::EFLAGS))
12600 break; // Should have kill-flag - update below.
12601 }
12602
12603 // If we hit the end of the block, check whether EFLAGS is live into a
12604 // successor.
12605 if (miI == BB->end()) {
12606 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12607 sEnd = BB->succ_end();
12608 sItr != sEnd; ++sItr) {
12609 MachineBasicBlock* succ = *sItr;
12610 if (succ->isLiveIn(X86::EFLAGS))
12611 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012612 }
12613 }
12614
Lang Hames6e3f7e42012-02-03 01:13:49 +000012615 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12616 // out. SelectMI should have a kill flag on EFLAGS.
12617 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012618 return true;
12619}
12620
Evan Cheng60c07e12006-07-05 22:17:51 +000012621MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012622X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012623 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012624 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12625 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012626
Chris Lattner52600972009-09-02 05:57:00 +000012627 // To "insert" a SELECT_CC instruction, we actually have to insert the
12628 // diamond control-flow pattern. The incoming instruction knows the
12629 // destination vreg to set, the condition code register to branch on, the
12630 // true/false values to select between, and a branch opcode to use.
12631 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12632 MachineFunction::iterator It = BB;
12633 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012634
Chris Lattner52600972009-09-02 05:57:00 +000012635 // thisMBB:
12636 // ...
12637 // TrueVal = ...
12638 // cmpTY ccX, r1, r2
12639 // bCC copy1MBB
12640 // fallthrough --> copy0MBB
12641 MachineBasicBlock *thisMBB = BB;
12642 MachineFunction *F = BB->getParent();
12643 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12644 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012645 F->insert(It, copy0MBB);
12646 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012647
Bill Wendling730c07e2010-06-25 20:48:10 +000012648 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12649 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012650 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12651 if (!MI->killsRegister(X86::EFLAGS) &&
12652 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12653 copy0MBB->addLiveIn(X86::EFLAGS);
12654 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012655 }
12656
Dan Gohman14152b42010-07-06 20:24:04 +000012657 // Transfer the remainder of BB and its successor edges to sinkMBB.
12658 sinkMBB->splice(sinkMBB->begin(), BB,
12659 llvm::next(MachineBasicBlock::iterator(MI)),
12660 BB->end());
12661 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12662
12663 // Add the true and fallthrough blocks as its successors.
12664 BB->addSuccessor(copy0MBB);
12665 BB->addSuccessor(sinkMBB);
12666
12667 // Create the conditional branch instruction.
12668 unsigned Opc =
12669 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12670 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12671
Chris Lattner52600972009-09-02 05:57:00 +000012672 // copy0MBB:
12673 // %FalseValue = ...
12674 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012675 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012676
Chris Lattner52600972009-09-02 05:57:00 +000012677 // sinkMBB:
12678 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12679 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012680 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12681 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012682 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12683 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12684
Dan Gohman14152b42010-07-06 20:24:04 +000012685 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012686 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012687}
12688
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012689MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012690X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12691 bool Is64Bit) const {
12692 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12693 DebugLoc DL = MI->getDebugLoc();
12694 MachineFunction *MF = BB->getParent();
12695 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12696
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012697 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012698
12699 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12700 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12701
12702 // BB:
12703 // ... [Till the alloca]
12704 // If stacklet is not large enough, jump to mallocMBB
12705 //
12706 // bumpMBB:
12707 // Allocate by subtracting from RSP
12708 // Jump to continueMBB
12709 //
12710 // mallocMBB:
12711 // Allocate by call to runtime
12712 //
12713 // continueMBB:
12714 // ...
12715 // [rest of original BB]
12716 //
12717
12718 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12719 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12720 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12721
12722 MachineRegisterInfo &MRI = MF->getRegInfo();
12723 const TargetRegisterClass *AddrRegClass =
12724 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12725
12726 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12727 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12728 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012729 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012730 sizeVReg = MI->getOperand(1).getReg(),
12731 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12732
12733 MachineFunction::iterator MBBIter = BB;
12734 ++MBBIter;
12735
12736 MF->insert(MBBIter, bumpMBB);
12737 MF->insert(MBBIter, mallocMBB);
12738 MF->insert(MBBIter, continueMBB);
12739
12740 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12741 (MachineBasicBlock::iterator(MI)), BB->end());
12742 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12743
12744 // Add code to the main basic block to check if the stack limit has been hit,
12745 // and if so, jump to mallocMBB otherwise to bumpMBB.
12746 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012747 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012748 .addReg(tmpSPVReg).addReg(sizeVReg);
12749 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012750 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012751 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012752 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12753
12754 // bumpMBB simply decreases the stack pointer, since we know the current
12755 // stacklet has enough space.
12756 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012757 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012758 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012759 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012760 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12761
12762 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012763 const uint32_t *RegMask =
12764 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012765 if (Is64Bit) {
12766 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12767 .addReg(sizeVReg);
12768 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012769 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012770 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012771 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012772 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012773 } else {
12774 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12775 .addImm(12);
12776 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12777 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012778 .addExternalSymbol("__morestack_allocate_stack_space")
12779 .addRegMask(RegMask)
12780 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012781 }
12782
12783 if (!Is64Bit)
12784 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12785 .addImm(16);
12786
12787 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12788 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12789 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12790
12791 // Set up the CFG correctly.
12792 BB->addSuccessor(bumpMBB);
12793 BB->addSuccessor(mallocMBB);
12794 mallocMBB->addSuccessor(continueMBB);
12795 bumpMBB->addSuccessor(continueMBB);
12796
12797 // Take care of the PHI nodes.
12798 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12799 MI->getOperand(0).getReg())
12800 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12801 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12802
12803 // Delete the original pseudo instruction.
12804 MI->eraseFromParent();
12805
12806 // And we're done.
12807 return continueMBB;
12808}
12809
12810MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012811X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012812 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012813 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12814 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012815
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012816 assert(!Subtarget->isTargetEnvMacho());
12817
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012818 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12819 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012820
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012821 if (Subtarget->isTargetWin64()) {
12822 if (Subtarget->isTargetCygMing()) {
12823 // ___chkstk(Mingw64):
12824 // Clobbers R10, R11, RAX and EFLAGS.
12825 // Updates RSP.
12826 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12827 .addExternalSymbol("___chkstk")
12828 .addReg(X86::RAX, RegState::Implicit)
12829 .addReg(X86::RSP, RegState::Implicit)
12830 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12831 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12832 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12833 } else {
12834 // __chkstk(MSVCRT): does not update stack pointer.
12835 // Clobbers R10, R11 and EFLAGS.
12836 // FIXME: RAX(allocated size) might be reused and not killed.
12837 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12838 .addExternalSymbol("__chkstk")
12839 .addReg(X86::RAX, RegState::Implicit)
12840 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12841 // RAX has the offset to subtracted from RSP.
12842 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12843 .addReg(X86::RSP)
12844 .addReg(X86::RAX);
12845 }
12846 } else {
12847 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012848 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12849
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012850 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12851 .addExternalSymbol(StackProbeSymbol)
12852 .addReg(X86::EAX, RegState::Implicit)
12853 .addReg(X86::ESP, RegState::Implicit)
12854 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12855 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12856 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12857 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012858
Dan Gohman14152b42010-07-06 20:24:04 +000012859 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012860 return BB;
12861}
Chris Lattner52600972009-09-02 05:57:00 +000012862
12863MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012864X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12865 MachineBasicBlock *BB) const {
12866 // This is pretty easy. We're taking the value that we received from
12867 // our load from the relocation, sticking it in either RDI (x86-64)
12868 // or EAX and doing an indirect call. The return value will then
12869 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012870 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012871 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012872 DebugLoc DL = MI->getDebugLoc();
12873 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012874
12875 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012876 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012877
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012878 // Get a register mask for the lowered call.
12879 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12880 // proper register mask.
12881 const uint32_t *RegMask =
12882 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012883 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012884 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12885 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012886 .addReg(X86::RIP)
12887 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012888 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012889 MI->getOperand(3).getTargetFlags())
12890 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012891 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012892 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012893 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012894 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012895 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12896 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012897 .addReg(0)
12898 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012899 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012900 MI->getOperand(3).getTargetFlags())
12901 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012902 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012903 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012904 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012905 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012906 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12907 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012908 .addReg(TII->getGlobalBaseReg(F))
12909 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012910 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012911 MI->getOperand(3).getTargetFlags())
12912 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012913 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012914 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012915 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012916 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012917
Dan Gohman14152b42010-07-06 20:24:04 +000012918 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012919 return BB;
12920}
12921
12922MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012923X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012924 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012925 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012926 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012927 case X86::TAILJMPd64:
12928 case X86::TAILJMPr64:
12929 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012930 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012931 case X86::TCRETURNdi64:
12932 case X86::TCRETURNri64:
12933 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012934 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012935 case X86::WIN_ALLOCA:
12936 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012937 case X86::SEG_ALLOCA_32:
12938 return EmitLoweredSegAlloca(MI, BB, false);
12939 case X86::SEG_ALLOCA_64:
12940 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012941 case X86::TLSCall_32:
12942 case X86::TLSCall_64:
12943 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012944 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012945 case X86::CMOV_FR32:
12946 case X86::CMOV_FR64:
12947 case X86::CMOV_V4F32:
12948 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012949 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012950 case X86::CMOV_V8F32:
12951 case X86::CMOV_V4F64:
12952 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012953 case X86::CMOV_GR16:
12954 case X86::CMOV_GR32:
12955 case X86::CMOV_RFP32:
12956 case X86::CMOV_RFP64:
12957 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012958 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012959
Dale Johannesen849f2142007-07-03 00:53:03 +000012960 case X86::FP32_TO_INT16_IN_MEM:
12961 case X86::FP32_TO_INT32_IN_MEM:
12962 case X86::FP32_TO_INT64_IN_MEM:
12963 case X86::FP64_TO_INT16_IN_MEM:
12964 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012965 case X86::FP64_TO_INT64_IN_MEM:
12966 case X86::FP80_TO_INT16_IN_MEM:
12967 case X86::FP80_TO_INT32_IN_MEM:
12968 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012969 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12970 DebugLoc DL = MI->getDebugLoc();
12971
Evan Cheng60c07e12006-07-05 22:17:51 +000012972 // Change the floating point control register to use "round towards zero"
12973 // mode when truncating to an integer value.
12974 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012975 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012976 addFrameReference(BuildMI(*BB, MI, DL,
12977 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012978
12979 // Load the old value of the high byte of the control word...
12980 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012981 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012982 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012983 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012984
12985 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012986 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012987 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012988
12989 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012990 addFrameReference(BuildMI(*BB, MI, DL,
12991 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012992
12993 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012994 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012995 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012996
12997 // Get the X86 opcode to use.
12998 unsigned Opc;
12999 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013000 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013001 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13002 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13003 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13004 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13005 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13006 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013007 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13008 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13009 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013010 }
13011
13012 X86AddressMode AM;
13013 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013014 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013015 AM.BaseType = X86AddressMode::RegBase;
13016 AM.Base.Reg = Op.getReg();
13017 } else {
13018 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013019 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013020 }
13021 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013022 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013023 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013024 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013025 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013026 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013027 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013028 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013029 AM.GV = Op.getGlobal();
13030 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013031 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013032 }
Dan Gohman14152b42010-07-06 20:24:04 +000013033 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013034 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013035
13036 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013037 addFrameReference(BuildMI(*BB, MI, DL,
13038 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013039
Dan Gohman14152b42010-07-06 20:24:04 +000013040 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013041 return BB;
13042 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013043 // String/text processing lowering.
13044 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013045 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013046 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013047 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013048 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013049 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013050 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013051 case X86::VPCMPESTRM128MEM: {
13052 unsigned NumArgs;
13053 bool MemArg;
13054 switch (MI->getOpcode()) {
13055 default: llvm_unreachable("illegal opcode!");
13056 case X86::PCMPISTRM128REG:
13057 case X86::VPCMPISTRM128REG:
13058 NumArgs = 3; MemArg = false; break;
13059 case X86::PCMPISTRM128MEM:
13060 case X86::VPCMPISTRM128MEM:
13061 NumArgs = 3; MemArg = true; break;
13062 case X86::PCMPESTRM128REG:
13063 case X86::VPCMPESTRM128REG:
13064 NumArgs = 5; MemArg = false; break;
13065 case X86::PCMPESTRM128MEM:
13066 case X86::VPCMPESTRM128MEM:
13067 NumArgs = 5; MemArg = true; break;
13068 }
13069 return EmitPCMP(MI, BB, NumArgs, MemArg);
13070 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013071
Eric Christopher228232b2010-11-30 07:20:12 +000013072 // Thread synchronization.
13073 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013074 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013075
Eric Christopherb120ab42009-08-18 22:50:32 +000013076 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000013077 case X86::ATOMMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013078 case X86::ATOMMAX32:
Mon P Wang63307c32008-05-05 19:05:59 +000013079 case X86::ATOMUMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013080 case X86::ATOMUMAX32:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013081 case X86::ATOMMIN16:
13082 case X86::ATOMMAX16:
13083 case X86::ATOMUMIN16:
13084 case X86::ATOMUMAX16:
13085 case X86::ATOMMIN64:
13086 case X86::ATOMMAX64:
13087 case X86::ATOMUMIN64:
13088 case X86::ATOMUMAX64: {
13089 unsigned Opc;
13090 switch (MI->getOpcode()) {
13091 default: llvm_unreachable("illegal opcode!");
13092 case X86::ATOMMIN32: Opc = X86::CMOVL32rr; break;
13093 case X86::ATOMMAX32: Opc = X86::CMOVG32rr; break;
13094 case X86::ATOMUMIN32: Opc = X86::CMOVB32rr; break;
13095 case X86::ATOMUMAX32: Opc = X86::CMOVA32rr; break;
13096 case X86::ATOMMIN16: Opc = X86::CMOVL16rr; break;
13097 case X86::ATOMMAX16: Opc = X86::CMOVG16rr; break;
13098 case X86::ATOMUMIN16: Opc = X86::CMOVB16rr; break;
13099 case X86::ATOMUMAX16: Opc = X86::CMOVA16rr; break;
13100 case X86::ATOMMIN64: Opc = X86::CMOVL64rr; break;
13101 case X86::ATOMMAX64: Opc = X86::CMOVG64rr; break;
13102 case X86::ATOMUMIN64: Opc = X86::CMOVB64rr; break;
13103 case X86::ATOMUMAX64: Opc = X86::CMOVA64rr; break;
13104 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
13105 }
13106 return EmitAtomicMinMaxWithCustomInserter(MI, BB, Opc);
13107 }
13108
13109 case X86::ATOMAND32:
13110 case X86::ATOMOR32:
13111 case X86::ATOMXOR32:
13112 case X86::ATOMNAND32: {
13113 bool Invert = false;
13114 unsigned RegOpc, ImmOpc;
13115 switch (MI->getOpcode()) {
13116 default: llvm_unreachable("illegal opcode!");
13117 case X86::ATOMAND32:
13118 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; break;
13119 case X86::ATOMOR32:
13120 RegOpc = X86::OR32rr; ImmOpc = X86::OR32ri; break;
13121 case X86::ATOMXOR32:
13122 RegOpc = X86::XOR32rr; ImmOpc = X86::XOR32ri; break;
13123 case X86::ATOMNAND32:
13124 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; Invert = true; break;
13125 }
13126 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13127 X86::MOV32rm, X86::LCMPXCHG32,
13128 X86::NOT32r, X86::EAX,
13129 &X86::GR32RegClass, Invert);
13130 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013131
13132 case X86::ATOMAND16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013133 case X86::ATOMOR16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013134 case X86::ATOMXOR16:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013135 case X86::ATOMNAND16: {
13136 bool Invert = false;
13137 unsigned RegOpc, ImmOpc;
13138 switch (MI->getOpcode()) {
13139 default: llvm_unreachable("illegal opcode!");
13140 case X86::ATOMAND16:
13141 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; break;
13142 case X86::ATOMOR16:
13143 RegOpc = X86::OR16rr; ImmOpc = X86::OR16ri; break;
13144 case X86::ATOMXOR16:
13145 RegOpc = X86::XOR16rr; ImmOpc = X86::XOR16ri; break;
13146 case X86::ATOMNAND16:
13147 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; Invert = true; break;
13148 }
13149 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13150 X86::MOV16rm, X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013151 X86::NOT16r, X86::AX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013152 &X86::GR16RegClass, Invert);
13153 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013154
13155 case X86::ATOMAND8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013156 case X86::ATOMOR8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013157 case X86::ATOMXOR8:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013158 case X86::ATOMNAND8: {
13159 bool Invert = false;
13160 unsigned RegOpc, ImmOpc;
13161 switch (MI->getOpcode()) {
13162 default: llvm_unreachable("illegal opcode!");
13163 case X86::ATOMAND8:
13164 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; break;
13165 case X86::ATOMOR8:
13166 RegOpc = X86::OR8rr; ImmOpc = X86::OR8ri; break;
13167 case X86::ATOMXOR8:
13168 RegOpc = X86::XOR8rr; ImmOpc = X86::XOR8ri; break;
13169 case X86::ATOMNAND8:
13170 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; Invert = true; break;
13171 }
13172 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13173 X86::MOV8rm, X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013174 X86::NOT8r, X86::AL,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013175 &X86::GR8RegClass, Invert);
13176 }
13177
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013178 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013179 case X86::ATOMAND64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013180 case X86::ATOMOR64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013181 case X86::ATOMXOR64:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013182 case X86::ATOMNAND64: {
13183 bool Invert = false;
13184 unsigned RegOpc, ImmOpc;
13185 switch (MI->getOpcode()) {
13186 default: llvm_unreachable("illegal opcode!");
13187 case X86::ATOMAND64:
13188 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; break;
13189 case X86::ATOMOR64:
13190 RegOpc = X86::OR64rr; ImmOpc = X86::OR64ri32; break;
13191 case X86::ATOMXOR64:
13192 RegOpc = X86::XOR64rr; ImmOpc = X86::XOR64ri32; break;
13193 case X86::ATOMNAND64:
13194 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; Invert = true; break;
13195 }
13196 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13197 X86::MOV64rm, X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013198 X86::NOT64r, X86::RAX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013199 &X86::GR64RegClass, Invert);
13200 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013201
13202 // This group does 64-bit operations on a 32-bit host.
13203 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013204 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013205 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013206 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013207 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013208 case X86::ATOMSUB6432:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013209 case X86::ATOMSWAP6432: {
13210 bool Invert = false;
13211 unsigned RegOpcL, RegOpcH, ImmOpcL, ImmOpcH;
13212 switch (MI->getOpcode()) {
13213 default: llvm_unreachable("illegal opcode!");
13214 case X86::ATOMAND6432:
13215 RegOpcL = RegOpcH = X86::AND32rr;
13216 ImmOpcL = ImmOpcH = X86::AND32ri;
13217 break;
13218 case X86::ATOMOR6432:
13219 RegOpcL = RegOpcH = X86::OR32rr;
13220 ImmOpcL = ImmOpcH = X86::OR32ri;
13221 break;
13222 case X86::ATOMXOR6432:
13223 RegOpcL = RegOpcH = X86::XOR32rr;
13224 ImmOpcL = ImmOpcH = X86::XOR32ri;
13225 break;
13226 case X86::ATOMNAND6432:
13227 RegOpcL = RegOpcH = X86::AND32rr;
13228 ImmOpcL = ImmOpcH = X86::AND32ri;
13229 Invert = true;
13230 break;
13231 case X86::ATOMADD6432:
13232 RegOpcL = X86::ADD32rr; RegOpcH = X86::ADC32rr;
13233 ImmOpcL = X86::ADD32ri; ImmOpcH = X86::ADC32ri;
13234 break;
13235 case X86::ATOMSUB6432:
13236 RegOpcL = X86::SUB32rr; RegOpcH = X86::SBB32rr;
13237 ImmOpcL = X86::SUB32ri; ImmOpcH = X86::SBB32ri;
13238 break;
13239 case X86::ATOMSWAP6432:
13240 RegOpcL = RegOpcH = X86::MOV32rr;
13241 ImmOpcL = ImmOpcH = X86::MOV32ri;
13242 break;
13243 }
13244 return EmitAtomicBit6432WithCustomInserter(MI, BB, RegOpcL, RegOpcH,
13245 ImmOpcL, ImmOpcH, Invert);
13246 }
13247
Dan Gohmand6708ea2009-08-15 01:38:56 +000013248 case X86::VASTART_SAVE_XMM_REGS:
13249 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013250
13251 case X86::VAARG_64:
13252 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013253 }
13254}
13255
13256//===----------------------------------------------------------------------===//
13257// X86 Optimization Hooks
13258//===----------------------------------------------------------------------===//
13259
Dan Gohman475871a2008-07-27 21:46:04 +000013260void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013261 APInt &KnownZero,
13262 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013263 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013264 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013265 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013266 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013267 assert((Opc >= ISD::BUILTIN_OP_END ||
13268 Opc == ISD::INTRINSIC_WO_CHAIN ||
13269 Opc == ISD::INTRINSIC_W_CHAIN ||
13270 Opc == ISD::INTRINSIC_VOID) &&
13271 "Should use MaskedValueIsZero if you don't know whether Op"
13272 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013273
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013274 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013275 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013276 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013277 case X86ISD::ADD:
13278 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013279 case X86ISD::ADC:
13280 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013281 case X86ISD::SMUL:
13282 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013283 case X86ISD::INC:
13284 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013285 case X86ISD::OR:
13286 case X86ISD::XOR:
13287 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013288 // These nodes' second result is a boolean.
13289 if (Op.getResNo() == 0)
13290 break;
13291 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013292 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013293 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013294 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013295 case ISD::INTRINSIC_WO_CHAIN: {
13296 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13297 unsigned NumLoBits = 0;
13298 switch (IntId) {
13299 default: break;
13300 case Intrinsic::x86_sse_movmsk_ps:
13301 case Intrinsic::x86_avx_movmsk_ps_256:
13302 case Intrinsic::x86_sse2_movmsk_pd:
13303 case Intrinsic::x86_avx_movmsk_pd_256:
13304 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013305 case Intrinsic::x86_sse2_pmovmskb_128:
13306 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013307 // High bits of movmskp{s|d}, pmovmskb are known zero.
13308 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013309 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013310 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13311 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13312 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13313 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13314 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13315 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013316 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013317 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013318 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013319 break;
13320 }
13321 }
13322 break;
13323 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013324 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013325}
Chris Lattner259e97c2006-01-31 19:43:35 +000013326
Owen Andersonbc146b02010-09-21 20:42:50 +000013327unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13328 unsigned Depth) const {
13329 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13330 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13331 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013332
Owen Andersonbc146b02010-09-21 20:42:50 +000013333 // Fallback case.
13334 return 1;
13335}
13336
Evan Cheng206ee9d2006-07-07 08:33:52 +000013337/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013338/// node is a GlobalAddress + offset.
13339bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013340 const GlobalValue* &GA,
13341 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013342 if (N->getOpcode() == X86ISD::Wrapper) {
13343 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013344 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013345 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013346 return true;
13347 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013348 }
Evan Chengad4196b2008-05-12 19:56:52 +000013349 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013350}
13351
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013352/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13353/// same as extracting the high 128-bit part of 256-bit vector and then
13354/// inserting the result into the low part of a new 256-bit vector
13355static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13356 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013357 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013358
13359 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013360 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013361 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13362 SVOp->getMaskElt(j) >= 0)
13363 return false;
13364
13365 return true;
13366}
13367
13368/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13369/// same as extracting the low 128-bit part of 256-bit vector and then
13370/// inserting the result into the high part of a new 256-bit vector
13371static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13372 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013373 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013374
13375 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013376 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013377 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13378 SVOp->getMaskElt(j) >= 0)
13379 return false;
13380
13381 return true;
13382}
13383
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013384/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13385static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013386 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013387 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013388 DebugLoc dl = N->getDebugLoc();
13389 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13390 SDValue V1 = SVOp->getOperand(0);
13391 SDValue V2 = SVOp->getOperand(1);
13392 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013393 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013394
13395 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13396 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13397 //
13398 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013399 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013400 // V UNDEF BUILD_VECTOR UNDEF
13401 // \ / \ /
13402 // CONCAT_VECTOR CONCAT_VECTOR
13403 // \ /
13404 // \ /
13405 // RESULT: V + zero extended
13406 //
13407 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13408 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13409 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13410 return SDValue();
13411
13412 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13413 return SDValue();
13414
13415 // To match the shuffle mask, the first half of the mask should
13416 // be exactly the first vector, and all the rest a splat with the
13417 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013418 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013419 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13420 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13421 return SDValue();
13422
Chad Rosier3d1161e2012-01-03 21:05:52 +000013423 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13424 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013425 if (Ld->hasNUsesOfValue(1, 0)) {
13426 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13427 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13428 SDValue ResNode =
13429 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13430 Ld->getMemoryVT(),
13431 Ld->getPointerInfo(),
13432 Ld->getAlignment(),
13433 false/*isVolatile*/, true/*ReadMem*/,
13434 false/*WriteMem*/);
13435 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13436 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013437 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013438
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013439 // Emit a zeroed vector and insert the desired subvector on its
13440 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013441 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013442 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013443 return DCI.CombineTo(N, InsV);
13444 }
13445
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013446 //===--------------------------------------------------------------------===//
13447 // Combine some shuffles into subvector extracts and inserts:
13448 //
13449
13450 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13451 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013452 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13453 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013454 return DCI.CombineTo(N, InsV);
13455 }
13456
13457 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13458 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013459 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13460 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013461 return DCI.CombineTo(N, InsV);
13462 }
13463
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013464 return SDValue();
13465}
13466
13467/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013468static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013469 TargetLowering::DAGCombinerInfo &DCI,
13470 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013471 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013472 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013473
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013474 // Don't create instructions with illegal types after legalize types has run.
13475 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13476 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13477 return SDValue();
13478
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013479 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013480 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013481 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013482 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013483
13484 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013485 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013486 return SDValue();
13487
13488 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13489 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13490 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013491 SmallVector<SDValue, 16> Elts;
13492 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013493 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013494
Nate Begemanfdea31a2010-03-24 20:49:50 +000013495 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013496}
Evan Chengd880b972008-05-09 21:53:03 +000013497
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013498
Craig Topper55b24052012-09-11 06:15:32 +000013499/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013500/// a sequence of vector shuffle operations.
13501/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000013502static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
13503 TargetLowering::DAGCombinerInfo &DCI,
13504 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013505 if (!DCI.isBeforeLegalizeOps())
13506 return SDValue();
13507
Craig Topper3ef43cf2012-04-24 06:36:35 +000013508 if (!Subtarget->hasAVX())
13509 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013510
13511 EVT VT = N->getValueType(0);
13512 SDValue Op = N->getOperand(0);
13513 EVT OpVT = Op.getValueType();
13514 DebugLoc dl = N->getDebugLoc();
13515
13516 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13517
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013518 if (Subtarget->hasAVX2()) {
13519 // AVX2: v4i64 -> v4i32
13520
13521 // VPERMD
13522 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13523
13524 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13525 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13526 ShufMask);
13527
Craig Topperd63fa652012-04-22 18:51:37 +000013528 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13529 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013530 }
13531
13532 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013533 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013534 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013535
13536 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013537 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013538
13539 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13540 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13541
13542 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013543 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013544
Craig Toppercacafd42012-08-14 08:18:43 +000013545 SDValue Undef = DAG.getUNDEF(VT);
13546 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13547 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013548
13549 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013550 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013551
Elena Demikhovsky73252572012-02-01 10:33:05 +000013552 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013553 }
Craig Topperd63fa652012-04-22 18:51:37 +000013554
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013555 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13556
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013557 if (Subtarget->hasAVX2()) {
13558 // AVX2: v8i32 -> v8i16
13559
13560 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013561
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013562 // PSHUFB
13563 SmallVector<SDValue,32> pshufbMask;
13564 for (unsigned i = 0; i < 2; ++i) {
13565 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13566 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13567 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13568 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13569 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13570 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13571 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13572 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13573 for (unsigned j = 0; j < 8; ++j)
13574 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13575 }
Craig Topperd63fa652012-04-22 18:51:37 +000013576 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13577 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013578 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13579
13580 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13581
13582 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013583 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013584 &ShufMask[0]);
13585
Craig Topperd63fa652012-04-22 18:51:37 +000013586 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13587 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013588
13589 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13590 }
13591
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013592 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013593 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013594
13595 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013596 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013597
13598 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13599 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13600
13601 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013602 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13603 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013604
Craig Toppercacafd42012-08-14 08:18:43 +000013605 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13606 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13607 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013608
13609 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13610 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13611
13612 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013613 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013614
Elena Demikhovsky73252572012-02-01 10:33:05 +000013615 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013616 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013617 }
13618
13619 return SDValue();
13620}
13621
Craig Topper89f4e662012-03-20 07:17:59 +000013622/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13623/// specific shuffle of a load can be folded into a single element load.
13624/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13625/// shuffles have been customed lowered so we need to handle those here.
13626static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13627 TargetLowering::DAGCombinerInfo &DCI) {
13628 if (DCI.isBeforeLegalizeOps())
13629 return SDValue();
13630
13631 SDValue InVec = N->getOperand(0);
13632 SDValue EltNo = N->getOperand(1);
13633
13634 if (!isa<ConstantSDNode>(EltNo))
13635 return SDValue();
13636
13637 EVT VT = InVec.getValueType();
13638
13639 bool HasShuffleIntoBitcast = false;
13640 if (InVec.getOpcode() == ISD::BITCAST) {
13641 // Don't duplicate a load with other uses.
13642 if (!InVec.hasOneUse())
13643 return SDValue();
13644 EVT BCVT = InVec.getOperand(0).getValueType();
13645 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13646 return SDValue();
13647 InVec = InVec.getOperand(0);
13648 HasShuffleIntoBitcast = true;
13649 }
13650
13651 if (!isTargetShuffle(InVec.getOpcode()))
13652 return SDValue();
13653
13654 // Don't duplicate a load with other uses.
13655 if (!InVec.hasOneUse())
13656 return SDValue();
13657
13658 SmallVector<int, 16> ShuffleMask;
13659 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013660 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13661 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013662 return SDValue();
13663
13664 // Select the input vector, guarding against out of range extract vector.
13665 unsigned NumElems = VT.getVectorNumElements();
13666 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13667 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13668 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13669 : InVec.getOperand(1);
13670
13671 // If inputs to shuffle are the same for both ops, then allow 2 uses
13672 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13673
13674 if (LdNode.getOpcode() == ISD::BITCAST) {
13675 // Don't duplicate a load with other uses.
13676 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13677 return SDValue();
13678
13679 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13680 LdNode = LdNode.getOperand(0);
13681 }
13682
13683 if (!ISD::isNormalLoad(LdNode.getNode()))
13684 return SDValue();
13685
13686 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13687
13688 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13689 return SDValue();
13690
13691 if (HasShuffleIntoBitcast) {
13692 // If there's a bitcast before the shuffle, check if the load type and
13693 // alignment is valid.
13694 unsigned Align = LN0->getAlignment();
13695 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13696 unsigned NewAlign = TLI.getTargetData()->
13697 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13698
13699 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13700 return SDValue();
13701 }
13702
13703 // All checks match so transform back to vector_shuffle so that DAG combiner
13704 // can finish the job
13705 DebugLoc dl = N->getDebugLoc();
13706
13707 // Create shuffle node taking into account the case that its a unary shuffle
13708 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13709 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13710 InVec.getOperand(0), Shuffle,
13711 &ShuffleMask[0]);
13712 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13713 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13714 EltNo);
13715}
13716
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013717/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13718/// generation and convert it from being a bunch of shuffles and extracts
13719/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013720static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013721 TargetLowering::DAGCombinerInfo &DCI) {
13722 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13723 if (NewOp.getNode())
13724 return NewOp;
13725
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013726 SDValue InputVector = N->getOperand(0);
13727
13728 // Only operate on vectors of 4 elements, where the alternative shuffling
13729 // gets to be more expensive.
13730 if (InputVector.getValueType() != MVT::v4i32)
13731 return SDValue();
13732
13733 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13734 // single use which is a sign-extend or zero-extend, and all elements are
13735 // used.
13736 SmallVector<SDNode *, 4> Uses;
13737 unsigned ExtractedElements = 0;
13738 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13739 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13740 if (UI.getUse().getResNo() != InputVector.getResNo())
13741 return SDValue();
13742
13743 SDNode *Extract = *UI;
13744 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13745 return SDValue();
13746
13747 if (Extract->getValueType(0) != MVT::i32)
13748 return SDValue();
13749 if (!Extract->hasOneUse())
13750 return SDValue();
13751 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13752 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13753 return SDValue();
13754 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13755 return SDValue();
13756
13757 // Record which element was extracted.
13758 ExtractedElements |=
13759 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13760
13761 Uses.push_back(Extract);
13762 }
13763
13764 // If not all the elements were used, this may not be worthwhile.
13765 if (ExtractedElements != 15)
13766 return SDValue();
13767
13768 // Ok, we've now decided to do the transformation.
13769 DebugLoc dl = InputVector.getDebugLoc();
13770
13771 // Store the value to a temporary stack slot.
13772 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013773 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13774 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013775
13776 // Replace each use (extract) with a load of the appropriate element.
13777 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13778 UE = Uses.end(); UI != UE; ++UI) {
13779 SDNode *Extract = *UI;
13780
Nadav Rotem86694292011-05-17 08:31:57 +000013781 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013782 SDValue Idx = Extract->getOperand(1);
13783 unsigned EltSize =
13784 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13785 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013786 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013787 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13788
Nadav Rotem86694292011-05-17 08:31:57 +000013789 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013790 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013791
13792 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013793 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013794 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013795 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013796
13797 // Replace the exact with the load.
13798 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13799 }
13800
13801 // The replacement was made in place; don't return anything.
13802 return SDValue();
13803}
13804
Duncan Sands6bcd2192011-09-17 16:49:39 +000013805/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13806/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013807static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013808 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013809 const X86Subtarget *Subtarget) {
13810 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013811 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013812 // Get the LHS/RHS of the select.
13813 SDValue LHS = N->getOperand(1);
13814 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013815 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013816
Dan Gohman670e5392009-09-21 18:03:22 +000013817 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013818 // instructions match the semantics of the common C idiom x<y?x:y but not
13819 // x<=y?x:y, because of how they handle negative zero (which can be
13820 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013821 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13822 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013823 (Subtarget->hasSSE2() ||
13824 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013825 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013826
Chris Lattner47b4ce82009-03-11 05:48:52 +000013827 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013828 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013829 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13830 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013831 switch (CC) {
13832 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013833 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013834 // Converting this to a min would handle NaNs incorrectly, and swapping
13835 // the operands would cause it to handle comparisons between positive
13836 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013837 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013838 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013839 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13840 break;
13841 std::swap(LHS, RHS);
13842 }
Dan Gohman670e5392009-09-21 18:03:22 +000013843 Opcode = X86ISD::FMIN;
13844 break;
13845 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013846 // Converting this to a min would handle comparisons between positive
13847 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013848 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013849 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13850 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013851 Opcode = X86ISD::FMIN;
13852 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013853 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013854 // Converting this to a min would handle both negative zeros and NaNs
13855 // incorrectly, but we can swap the operands to fix both.
13856 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013857 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013858 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013859 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013860 Opcode = X86ISD::FMIN;
13861 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013862
Dan Gohman670e5392009-09-21 18:03:22 +000013863 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013864 // Converting this to a max would handle comparisons between positive
13865 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013866 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013867 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013868 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013869 Opcode = X86ISD::FMAX;
13870 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013871 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013872 // Converting this to a max would handle NaNs incorrectly, and swapping
13873 // the operands would cause it to handle comparisons between positive
13874 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013875 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013876 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013877 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13878 break;
13879 std::swap(LHS, RHS);
13880 }
Dan Gohman670e5392009-09-21 18:03:22 +000013881 Opcode = X86ISD::FMAX;
13882 break;
13883 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013884 // Converting this to a max would handle both negative zeros and NaNs
13885 // incorrectly, but we can swap the operands to fix both.
13886 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013887 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013888 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013889 case ISD::SETGE:
13890 Opcode = X86ISD::FMAX;
13891 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013892 }
Dan Gohman670e5392009-09-21 18:03:22 +000013893 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013894 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13895 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013896 switch (CC) {
13897 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013898 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013899 // Converting this to a min would handle comparisons between positive
13900 // and negative zero incorrectly, and swapping the operands would
13901 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013902 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013903 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013904 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013905 break;
13906 std::swap(LHS, RHS);
13907 }
Dan Gohman670e5392009-09-21 18:03:22 +000013908 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013909 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013910 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013911 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013912 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013913 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13914 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013915 Opcode = X86ISD::FMIN;
13916 break;
13917 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013918 // Converting this to a min would handle both negative zeros and NaNs
13919 // incorrectly, but we can swap the operands to fix both.
13920 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013921 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013922 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013923 case ISD::SETGE:
13924 Opcode = X86ISD::FMIN;
13925 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013926
Dan Gohman670e5392009-09-21 18:03:22 +000013927 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013928 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013929 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013930 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013931 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013932 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013933 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013934 // Converting this to a max would handle comparisons between positive
13935 // and negative zero incorrectly, and swapping the operands would
13936 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013937 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013938 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013939 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013940 break;
13941 std::swap(LHS, RHS);
13942 }
Dan Gohman670e5392009-09-21 18:03:22 +000013943 Opcode = X86ISD::FMAX;
13944 break;
13945 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013946 // Converting this to a max would handle both negative zeros and NaNs
13947 // incorrectly, but we can swap the operands to fix both.
13948 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013949 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013950 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013951 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013952 Opcode = X86ISD::FMAX;
13953 break;
13954 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013955 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013956
Chris Lattner47b4ce82009-03-11 05:48:52 +000013957 if (Opcode)
13958 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013959 }
Eric Christopherfd179292009-08-27 18:07:15 +000013960
Chris Lattnerd1980a52009-03-12 06:52:53 +000013961 // If this is a select between two integer constants, try to do some
13962 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013963 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13964 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013965 // Don't do this for crazy integer types.
13966 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13967 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013968 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013969 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013970
Chris Lattnercee56e72009-03-13 05:53:31 +000013971 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013972 // Efficiently invertible.
13973 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13974 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13975 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13976 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013977 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013978 }
Eric Christopherfd179292009-08-27 18:07:15 +000013979
Chris Lattnerd1980a52009-03-12 06:52:53 +000013980 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013981 if (FalseC->getAPIntValue() == 0 &&
13982 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013983 if (NeedsCondInvert) // Invert the condition if needed.
13984 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13985 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013986
Chris Lattnerd1980a52009-03-12 06:52:53 +000013987 // Zero extend the condition if needed.
13988 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013989
Chris Lattnercee56e72009-03-13 05:53:31 +000013990 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013991 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013992 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013993 }
Eric Christopherfd179292009-08-27 18:07:15 +000013994
Chris Lattner97a29a52009-03-13 05:22:11 +000013995 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013996 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013997 if (NeedsCondInvert) // Invert the condition if needed.
13998 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13999 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014000
Chris Lattner97a29a52009-03-13 05:22:11 +000014001 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014002 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14003 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014004 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014005 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014006 }
Eric Christopherfd179292009-08-27 18:07:15 +000014007
Chris Lattnercee56e72009-03-13 05:53:31 +000014008 // Optimize cases that will turn into an LEA instruction. This requires
14009 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014010 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014011 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014012 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014013
Chris Lattnercee56e72009-03-13 05:53:31 +000014014 bool isFastMultiplier = false;
14015 if (Diff < 10) {
14016 switch ((unsigned char)Diff) {
14017 default: break;
14018 case 1: // result = add base, cond
14019 case 2: // result = lea base( , cond*2)
14020 case 3: // result = lea base(cond, cond*2)
14021 case 4: // result = lea base( , cond*4)
14022 case 5: // result = lea base(cond, cond*4)
14023 case 8: // result = lea base( , cond*8)
14024 case 9: // result = lea base(cond, cond*8)
14025 isFastMultiplier = true;
14026 break;
14027 }
14028 }
Eric Christopherfd179292009-08-27 18:07:15 +000014029
Chris Lattnercee56e72009-03-13 05:53:31 +000014030 if (isFastMultiplier) {
14031 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14032 if (NeedsCondInvert) // Invert the condition if needed.
14033 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14034 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014035
Chris Lattnercee56e72009-03-13 05:53:31 +000014036 // Zero extend the condition if needed.
14037 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14038 Cond);
14039 // Scale the condition by the difference.
14040 if (Diff != 1)
14041 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14042 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014043
Chris Lattnercee56e72009-03-13 05:53:31 +000014044 // Add the base if non-zero.
14045 if (FalseC->getAPIntValue() != 0)
14046 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14047 SDValue(FalseC, 0));
14048 return Cond;
14049 }
Eric Christopherfd179292009-08-27 18:07:15 +000014050 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014051 }
14052 }
Eric Christopherfd179292009-08-27 18:07:15 +000014053
Evan Cheng56f582d2012-01-04 01:41:39 +000014054 // Canonicalize max and min:
14055 // (x > y) ? x : y -> (x >= y) ? x : y
14056 // (x < y) ? x : y -> (x <= y) ? x : y
14057 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14058 // the need for an extra compare
14059 // against zero. e.g.
14060 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14061 // subl %esi, %edi
14062 // testl %edi, %edi
14063 // movl $0, %eax
14064 // cmovgl %edi, %eax
14065 // =>
14066 // xorl %eax, %eax
14067 // subl %esi, $edi
14068 // cmovsl %eax, %edi
14069 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14070 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14071 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14072 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14073 switch (CC) {
14074 default: break;
14075 case ISD::SETLT:
14076 case ISD::SETGT: {
14077 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14078 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14079 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14080 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14081 }
14082 }
14083 }
14084
Nadav Rotemcc616562012-01-15 19:27:55 +000014085 // If we know that this node is legal then we know that it is going to be
14086 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14087 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14088 // to simplify previous instructions.
14089 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14090 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014091 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014092 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014093
14094 // Don't optimize vector selects that map to mask-registers.
14095 if (BitWidth == 1)
14096 return SDValue();
14097
Nadav Rotemcc616562012-01-15 19:27:55 +000014098 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14099 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14100
14101 APInt KnownZero, KnownOne;
14102 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14103 DCI.isBeforeLegalizeOps());
14104 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14105 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14106 DCI.CommitTargetLoweringOpt(TLO);
14107 }
14108
Dan Gohman475871a2008-07-27 21:46:04 +000014109 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014110}
14111
Michael Liao2a33cec2012-08-10 19:58:13 +000014112// Check whether a boolean test is testing a boolean value generated by
14113// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14114// code.
14115//
14116// Simplify the following patterns:
14117// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14118// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14119// to (Op EFLAGS Cond)
14120//
14121// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14122// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14123// to (Op EFLAGS !Cond)
14124//
14125// where Op could be BRCOND or CMOV.
14126//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014127static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014128 // Quit if not CMP and SUB with its value result used.
14129 if (Cmp.getOpcode() != X86ISD::CMP &&
14130 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14131 return SDValue();
14132
14133 // Quit if not used as a boolean value.
14134 if (CC != X86::COND_E && CC != X86::COND_NE)
14135 return SDValue();
14136
14137 // Check CMP operands. One of them should be 0 or 1 and the other should be
14138 // an SetCC or extended from it.
14139 SDValue Op1 = Cmp.getOperand(0);
14140 SDValue Op2 = Cmp.getOperand(1);
14141
14142 SDValue SetCC;
14143 const ConstantSDNode* C = 0;
14144 bool needOppositeCond = (CC == X86::COND_E);
14145
14146 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14147 SetCC = Op2;
14148 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14149 SetCC = Op1;
14150 else // Quit if all operands are not constants.
14151 return SDValue();
14152
14153 if (C->getZExtValue() == 1)
14154 needOppositeCond = !needOppositeCond;
14155 else if (C->getZExtValue() != 0)
14156 // Quit if the constant is neither 0 or 1.
14157 return SDValue();
14158
14159 // Skip 'zext' node.
14160 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14161 SetCC = SetCC.getOperand(0);
14162
Michael Liao7fdc66b2012-09-10 16:36:16 +000014163 switch (SetCC.getOpcode()) {
14164 case X86ISD::SETCC:
14165 // Set the condition code or opposite one if necessary.
14166 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14167 if (needOppositeCond)
14168 CC = X86::GetOppositeBranchCondition(CC);
14169 return SetCC.getOperand(1);
14170 case X86ISD::CMOV: {
14171 // Check whether false/true value has canonical one, i.e. 0 or 1.
14172 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14173 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14174 // Quit if true value is not a constant.
14175 if (!TVal)
14176 return SDValue();
14177 // Quit if false value is not a constant.
14178 if (!FVal) {
14179 // A special case for rdrand, where 0 is set if false cond is found.
14180 SDValue Op = SetCC.getOperand(0);
14181 if (Op.getOpcode() != X86ISD::RDRAND)
14182 return SDValue();
14183 }
14184 // Quit if false value is not the constant 0 or 1.
14185 bool FValIsFalse = true;
14186 if (FVal && FVal->getZExtValue() != 0) {
14187 if (FVal->getZExtValue() != 1)
14188 return SDValue();
14189 // If FVal is 1, opposite cond is needed.
14190 needOppositeCond = !needOppositeCond;
14191 FValIsFalse = false;
14192 }
14193 // Quit if TVal is not the constant opposite of FVal.
14194 if (FValIsFalse && TVal->getZExtValue() != 1)
14195 return SDValue();
14196 if (!FValIsFalse && TVal->getZExtValue() != 0)
14197 return SDValue();
14198 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14199 if (needOppositeCond)
14200 CC = X86::GetOppositeBranchCondition(CC);
14201 return SetCC.getOperand(3);
14202 }
14203 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014204
Michael Liao7fdc66b2012-09-10 16:36:16 +000014205 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014206}
14207
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014208/// checkFlaggedOrCombine - DAG combination on X86ISD::OR, i.e. with EFLAGS
14209/// updated. If only flag result is used and the result is evaluated from a
14210/// series of element extraction, try to combine it into a PTEST.
14211static SDValue checkFlaggedOrCombine(SDValue Or, X86::CondCode &CC,
14212 SelectionDAG &DAG,
14213 const X86Subtarget *Subtarget) {
14214 SDNode *N = Or.getNode();
14215 DebugLoc DL = N->getDebugLoc();
14216
14217 // Only SSE4.1 and beyond supports PTEST or like.
14218 if (!Subtarget->hasSSE41())
14219 return SDValue();
14220
14221 if (N->getOpcode() != X86ISD::OR)
14222 return SDValue();
14223
14224 // Quit if the value result of OR is used.
14225 if (N->hasAnyUseOfValue(0))
14226 return SDValue();
14227
14228 // Quit if not used as a boolean value.
14229 if (CC != X86::COND_E && CC != X86::COND_NE)
14230 return SDValue();
14231
14232 SmallVector<SDValue, 8> Opnds;
14233 SDValue VecIn;
14234 EVT VT = MVT::Other;
14235 unsigned Mask = 0;
14236
14237 // Recognize a special case where a vector is casted into wide integer to
14238 // test all 0s.
14239 Opnds.push_back(N->getOperand(0));
14240 Opnds.push_back(N->getOperand(1));
14241
14242 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
14243 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
14244 // BFS traverse all OR'd operands.
14245 if (I->getOpcode() == ISD::OR) {
14246 Opnds.push_back(I->getOperand(0));
14247 Opnds.push_back(I->getOperand(1));
14248 // Re-evaluate the number of nodes to be traversed.
Michael Liao95c22a32012-08-28 23:42:17 +000014249 e += 2; // 2 more nodes (LHS and RHS) are pushed.
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014250 continue;
14251 }
14252
14253 // Quit if a non-EXTRACT_VECTOR_ELT
14254 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14255 return SDValue();
14256
14257 // Quit if without a constant index.
14258 SDValue Idx = I->getOperand(1);
14259 if (!isa<ConstantSDNode>(Idx))
14260 return SDValue();
14261
14262 // Check if all elements are extracted from the same vector.
14263 SDValue ExtractedFromVec = I->getOperand(0);
14264 if (VecIn.getNode() == 0) {
14265 VT = ExtractedFromVec.getValueType();
14266 // FIXME: only 128-bit vector is supported so far.
14267 if (!VT.is128BitVector())
14268 return SDValue();
14269 VecIn = ExtractedFromVec;
14270 } else if (VecIn != ExtractedFromVec)
14271 return SDValue();
14272
14273 // Record the constant index.
14274 Mask |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
14275 }
14276
14277 assert(VT.is128BitVector() && "Only 128-bit vector PTEST is supported so far.");
14278
14279 // Quit if not all elements are used.
14280 if (Mask != (1U << VT.getVectorNumElements()) - 1U)
14281 return SDValue();
14282
14283 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, VecIn, VecIn);
14284}
14285
Chris Lattnerd1980a52009-03-12 06:52:53 +000014286/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14287static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014288 TargetLowering::DAGCombinerInfo &DCI,
14289 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014290 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014291
Chris Lattnerd1980a52009-03-12 06:52:53 +000014292 // If the flag operand isn't dead, don't touch this CMOV.
14293 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14294 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014295
Evan Chengb5a55d92011-05-24 01:48:22 +000014296 SDValue FalseOp = N->getOperand(0);
14297 SDValue TrueOp = N->getOperand(1);
14298 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14299 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014300
Evan Chengb5a55d92011-05-24 01:48:22 +000014301 if (CC == X86::COND_E || CC == X86::COND_NE) {
14302 switch (Cond.getOpcode()) {
14303 default: break;
14304 case X86ISD::BSR:
14305 case X86ISD::BSF:
14306 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14307 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14308 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14309 }
14310 }
14311
Michael Liao2a33cec2012-08-10 19:58:13 +000014312 SDValue Flags;
14313
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014314 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014315 if (Flags.getNode() &&
14316 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014317 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014318 SDValue Ops[] = { FalseOp, TrueOp,
14319 DAG.getConstant(CC, MVT::i8), Flags };
14320 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14321 Ops, array_lengthof(Ops));
14322 }
14323
14324 Flags = checkFlaggedOrCombine(Cond, CC, DAG, Subtarget);
14325 if (Flags.getNode()) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014326 SDValue Ops[] = { FalseOp, TrueOp,
14327 DAG.getConstant(CC, MVT::i8), Flags };
14328 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14329 Ops, array_lengthof(Ops));
14330 }
14331
Chris Lattnerd1980a52009-03-12 06:52:53 +000014332 // If this is a select between two integer constants, try to do some
14333 // optimizations. Note that the operands are ordered the opposite of SELECT
14334 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014335 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14336 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014337 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14338 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014339 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14340 CC = X86::GetOppositeBranchCondition(CC);
14341 std::swap(TrueC, FalseC);
14342 }
Eric Christopherfd179292009-08-27 18:07:15 +000014343
Chris Lattnerd1980a52009-03-12 06:52:53 +000014344 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014345 // This is efficient for any integer data type (including i8/i16) and
14346 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014347 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014348 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14349 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014350
Chris Lattnerd1980a52009-03-12 06:52:53 +000014351 // Zero extend the condition if needed.
14352 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014353
Chris Lattnerd1980a52009-03-12 06:52:53 +000014354 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14355 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014356 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014357 if (N->getNumValues() == 2) // Dead flag value?
14358 return DCI.CombineTo(N, Cond, SDValue());
14359 return Cond;
14360 }
Eric Christopherfd179292009-08-27 18:07:15 +000014361
Chris Lattnercee56e72009-03-13 05:53:31 +000014362 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14363 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014364 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014365 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14366 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014367
Chris Lattner97a29a52009-03-13 05:22:11 +000014368 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014369 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14370 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014371 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14372 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014373
Chris Lattner97a29a52009-03-13 05:22:11 +000014374 if (N->getNumValues() == 2) // Dead flag value?
14375 return DCI.CombineTo(N, Cond, SDValue());
14376 return Cond;
14377 }
Eric Christopherfd179292009-08-27 18:07:15 +000014378
Chris Lattnercee56e72009-03-13 05:53:31 +000014379 // Optimize cases that will turn into an LEA instruction. This requires
14380 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014381 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014382 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014383 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014384
Chris Lattnercee56e72009-03-13 05:53:31 +000014385 bool isFastMultiplier = false;
14386 if (Diff < 10) {
14387 switch ((unsigned char)Diff) {
14388 default: break;
14389 case 1: // result = add base, cond
14390 case 2: // result = lea base( , cond*2)
14391 case 3: // result = lea base(cond, cond*2)
14392 case 4: // result = lea base( , cond*4)
14393 case 5: // result = lea base(cond, cond*4)
14394 case 8: // result = lea base( , cond*8)
14395 case 9: // result = lea base(cond, cond*8)
14396 isFastMultiplier = true;
14397 break;
14398 }
14399 }
Eric Christopherfd179292009-08-27 18:07:15 +000014400
Chris Lattnercee56e72009-03-13 05:53:31 +000014401 if (isFastMultiplier) {
14402 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014403 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14404 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014405 // Zero extend the condition if needed.
14406 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14407 Cond);
14408 // Scale the condition by the difference.
14409 if (Diff != 1)
14410 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14411 DAG.getConstant(Diff, Cond.getValueType()));
14412
14413 // Add the base if non-zero.
14414 if (FalseC->getAPIntValue() != 0)
14415 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14416 SDValue(FalseC, 0));
14417 if (N->getNumValues() == 2) // Dead flag value?
14418 return DCI.CombineTo(N, Cond, SDValue());
14419 return Cond;
14420 }
Eric Christopherfd179292009-08-27 18:07:15 +000014421 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014422 }
14423 }
14424 return SDValue();
14425}
14426
14427
Evan Cheng0b0cd912009-03-28 05:57:29 +000014428/// PerformMulCombine - Optimize a single multiply with constant into two
14429/// in order to implement it with two cheaper instructions, e.g.
14430/// LEA + SHL, LEA + LEA.
14431static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14432 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014433 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14434 return SDValue();
14435
Owen Andersone50ed302009-08-10 22:56:29 +000014436 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014437 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014438 return SDValue();
14439
14440 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14441 if (!C)
14442 return SDValue();
14443 uint64_t MulAmt = C->getZExtValue();
14444 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14445 return SDValue();
14446
14447 uint64_t MulAmt1 = 0;
14448 uint64_t MulAmt2 = 0;
14449 if ((MulAmt % 9) == 0) {
14450 MulAmt1 = 9;
14451 MulAmt2 = MulAmt / 9;
14452 } else if ((MulAmt % 5) == 0) {
14453 MulAmt1 = 5;
14454 MulAmt2 = MulAmt / 5;
14455 } else if ((MulAmt % 3) == 0) {
14456 MulAmt1 = 3;
14457 MulAmt2 = MulAmt / 3;
14458 }
14459 if (MulAmt2 &&
14460 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14461 DebugLoc DL = N->getDebugLoc();
14462
14463 if (isPowerOf2_64(MulAmt2) &&
14464 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14465 // If second multiplifer is pow2, issue it first. We want the multiply by
14466 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14467 // is an add.
14468 std::swap(MulAmt1, MulAmt2);
14469
14470 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014471 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014472 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014473 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014474 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014475 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014476 DAG.getConstant(MulAmt1, VT));
14477
Eric Christopherfd179292009-08-27 18:07:15 +000014478 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014479 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014480 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014481 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014482 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014483 DAG.getConstant(MulAmt2, VT));
14484
14485 // Do not add new nodes to DAG combiner worklist.
14486 DCI.CombineTo(N, NewMul, false);
14487 }
14488 return SDValue();
14489}
14490
Evan Chengad9c0a32009-12-15 00:53:42 +000014491static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14492 SDValue N0 = N->getOperand(0);
14493 SDValue N1 = N->getOperand(1);
14494 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14495 EVT VT = N0.getValueType();
14496
14497 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14498 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014499 if (VT.isInteger() && !VT.isVector() &&
14500 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014501 N0.getOperand(1).getOpcode() == ISD::Constant) {
14502 SDValue N00 = N0.getOperand(0);
14503 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14504 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14505 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14506 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14507 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14508 APInt ShAmt = N1C->getAPIntValue();
14509 Mask = Mask.shl(ShAmt);
14510 if (Mask != 0)
14511 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14512 N00, DAG.getConstant(Mask, VT));
14513 }
14514 }
14515
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014516
14517 // Hardware support for vector shifts is sparse which makes us scalarize the
14518 // vector operations in many cases. Also, on sandybridge ADD is faster than
14519 // shl.
14520 // (shl V, 1) -> add V,V
14521 if (isSplatVector(N1.getNode())) {
14522 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14523 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14524 // We shift all of the values by one. In many cases we do not have
14525 // hardware support for this operation. This is better expressed as an ADD
14526 // of two values.
14527 if (N1C && (1 == N1C->getZExtValue())) {
14528 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14529 }
14530 }
14531
Evan Chengad9c0a32009-12-15 00:53:42 +000014532 return SDValue();
14533}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014534
Nate Begeman740ab032009-01-26 00:52:55 +000014535/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14536/// when possible.
14537static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014538 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014539 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014540 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014541 if (N->getOpcode() == ISD::SHL) {
14542 SDValue V = PerformSHLCombine(N, DAG);
14543 if (V.getNode()) return V;
14544 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014545
Nate Begeman740ab032009-01-26 00:52:55 +000014546 // On X86 with SSE2 support, we can transform this to a vector shift if
14547 // all elements are shifted by the same amount. We can't do this in legalize
14548 // because the a constant vector is typically transformed to a constant pool
14549 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014550 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014551 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014552
Craig Topper7be5dfd2011-11-12 09:58:49 +000014553 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14554 (!Subtarget->hasAVX2() ||
14555 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014556 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014557
Mon P Wang3becd092009-01-28 08:12:05 +000014558 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014559 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014560 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014561 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014562 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14563 unsigned NumElts = VT.getVectorNumElements();
14564 unsigned i = 0;
14565 for (; i != NumElts; ++i) {
14566 SDValue Arg = ShAmtOp.getOperand(i);
14567 if (Arg.getOpcode() == ISD::UNDEF) continue;
14568 BaseShAmt = Arg;
14569 break;
14570 }
Craig Topper37c26772012-01-17 04:44:50 +000014571 // Handle the case where the build_vector is all undef
14572 // FIXME: Should DAG allow this?
14573 if (i == NumElts)
14574 return SDValue();
14575
Mon P Wang3becd092009-01-28 08:12:05 +000014576 for (; i != NumElts; ++i) {
14577 SDValue Arg = ShAmtOp.getOperand(i);
14578 if (Arg.getOpcode() == ISD::UNDEF) continue;
14579 if (Arg != BaseShAmt) {
14580 return SDValue();
14581 }
14582 }
14583 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014584 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014585 SDValue InVec = ShAmtOp.getOperand(0);
14586 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14587 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14588 unsigned i = 0;
14589 for (; i != NumElts; ++i) {
14590 SDValue Arg = InVec.getOperand(i);
14591 if (Arg.getOpcode() == ISD::UNDEF) continue;
14592 BaseShAmt = Arg;
14593 break;
14594 }
14595 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014597 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014598 if (C->getZExtValue() == SplatIdx)
14599 BaseShAmt = InVec.getOperand(1);
14600 }
14601 }
Mon P Wang845b1892012-02-01 22:15:20 +000014602 if (BaseShAmt.getNode() == 0) {
14603 // Don't create instructions with illegal types after legalize
14604 // types has run.
14605 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14606 !DCI.isBeforeLegalize())
14607 return SDValue();
14608
Mon P Wangefa42202009-09-03 19:56:25 +000014609 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14610 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014611 }
Mon P Wang3becd092009-01-28 08:12:05 +000014612 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014613 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014614
Mon P Wangefa42202009-09-03 19:56:25 +000014615 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014616 if (EltVT.bitsGT(MVT::i32))
14617 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14618 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014619 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014620
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014621 // The shift amount is identical so we can do a vector shift.
14622 SDValue ValOp = N->getOperand(0);
14623 switch (N->getOpcode()) {
14624 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014625 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014626 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014627 switch (VT.getSimpleVT().SimpleTy) {
14628 default: return SDValue();
14629 case MVT::v2i64:
14630 case MVT::v4i32:
14631 case MVT::v8i16:
14632 case MVT::v4i64:
14633 case MVT::v8i32:
14634 case MVT::v16i16:
14635 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14636 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014637 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014638 switch (VT.getSimpleVT().SimpleTy) {
14639 default: return SDValue();
14640 case MVT::v4i32:
14641 case MVT::v8i16:
14642 case MVT::v8i32:
14643 case MVT::v16i16:
14644 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14645 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014646 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014647 switch (VT.getSimpleVT().SimpleTy) {
14648 default: return SDValue();
14649 case MVT::v2i64:
14650 case MVT::v4i32:
14651 case MVT::v8i16:
14652 case MVT::v4i64:
14653 case MVT::v8i32:
14654 case MVT::v16i16:
14655 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14656 }
Nate Begeman740ab032009-01-26 00:52:55 +000014657 }
Nate Begeman740ab032009-01-26 00:52:55 +000014658}
14659
Nate Begemanb65c1752010-12-17 22:55:37 +000014660
Stuart Hastings865f0932011-06-03 23:53:54 +000014661// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14662// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14663// and friends. Likewise for OR -> CMPNEQSS.
14664static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14665 TargetLowering::DAGCombinerInfo &DCI,
14666 const X86Subtarget *Subtarget) {
14667 unsigned opcode;
14668
14669 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14670 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014671 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014672 SDValue N0 = N->getOperand(0);
14673 SDValue N1 = N->getOperand(1);
14674 SDValue CMP0 = N0->getOperand(1);
14675 SDValue CMP1 = N1->getOperand(1);
14676 DebugLoc DL = N->getDebugLoc();
14677
14678 // The SETCCs should both refer to the same CMP.
14679 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14680 return SDValue();
14681
14682 SDValue CMP00 = CMP0->getOperand(0);
14683 SDValue CMP01 = CMP0->getOperand(1);
14684 EVT VT = CMP00.getValueType();
14685
14686 if (VT == MVT::f32 || VT == MVT::f64) {
14687 bool ExpectingFlags = false;
14688 // Check for any users that want flags:
14689 for (SDNode::use_iterator UI = N->use_begin(),
14690 UE = N->use_end();
14691 !ExpectingFlags && UI != UE; ++UI)
14692 switch (UI->getOpcode()) {
14693 default:
14694 case ISD::BR_CC:
14695 case ISD::BRCOND:
14696 case ISD::SELECT:
14697 ExpectingFlags = true;
14698 break;
14699 case ISD::CopyToReg:
14700 case ISD::SIGN_EXTEND:
14701 case ISD::ZERO_EXTEND:
14702 case ISD::ANY_EXTEND:
14703 break;
14704 }
14705
14706 if (!ExpectingFlags) {
14707 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14708 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14709
14710 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14711 X86::CondCode tmp = cc0;
14712 cc0 = cc1;
14713 cc1 = tmp;
14714 }
14715
14716 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14717 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14718 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14719 X86ISD::NodeType NTOperator = is64BitFP ?
14720 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14721 // FIXME: need symbolic constants for these magic numbers.
14722 // See X86ATTInstPrinter.cpp:printSSECC().
14723 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14724 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14725 DAG.getConstant(x86cc, MVT::i8));
14726 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14727 OnesOrZeroesF);
14728 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14729 DAG.getConstant(1, MVT::i32));
14730 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14731 return OneBitOfTruth;
14732 }
14733 }
14734 }
14735 }
14736 return SDValue();
14737}
14738
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014739/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14740/// so it can be folded inside ANDNP.
14741static bool CanFoldXORWithAllOnes(const SDNode *N) {
14742 EVT VT = N->getValueType(0);
14743
14744 // Match direct AllOnes for 128 and 256-bit vectors
14745 if (ISD::isBuildVectorAllOnes(N))
14746 return true;
14747
14748 // Look through a bit convert.
14749 if (N->getOpcode() == ISD::BITCAST)
14750 N = N->getOperand(0).getNode();
14751
14752 // Sometimes the operand may come from a insert_subvector building a 256-bit
14753 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014754 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014755 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14756 SDValue V1 = N->getOperand(0);
14757 SDValue V2 = N->getOperand(1);
14758
14759 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14760 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14761 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14762 ISD::isBuildVectorAllOnes(V2.getNode()))
14763 return true;
14764 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014765
14766 return false;
14767}
14768
Nate Begemanb65c1752010-12-17 22:55:37 +000014769static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14770 TargetLowering::DAGCombinerInfo &DCI,
14771 const X86Subtarget *Subtarget) {
14772 if (DCI.isBeforeLegalizeOps())
14773 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014774
Stuart Hastings865f0932011-06-03 23:53:54 +000014775 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14776 if (R.getNode())
14777 return R;
14778
Craig Topper54a11172011-10-14 07:06:56 +000014779 EVT VT = N->getValueType(0);
14780
Craig Topperb4c94572011-10-21 06:55:01 +000014781 // Create ANDN, BLSI, and BLSR instructions
14782 // BLSI is X & (-X)
14783 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014784 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14785 SDValue N0 = N->getOperand(0);
14786 SDValue N1 = N->getOperand(1);
14787 DebugLoc DL = N->getDebugLoc();
14788
14789 // Check LHS for not
14790 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14791 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14792 // Check RHS for not
14793 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14794 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14795
Craig Topperb4c94572011-10-21 06:55:01 +000014796 // Check LHS for neg
14797 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14798 isZero(N0.getOperand(0)))
14799 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14800
14801 // Check RHS for neg
14802 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14803 isZero(N1.getOperand(0)))
14804 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14805
14806 // Check LHS for X-1
14807 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14808 isAllOnes(N0.getOperand(1)))
14809 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14810
14811 // Check RHS for X-1
14812 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14813 isAllOnes(N1.getOperand(1)))
14814 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14815
Craig Topper54a11172011-10-14 07:06:56 +000014816 return SDValue();
14817 }
14818
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014819 // Want to form ANDNP nodes:
14820 // 1) In the hopes of then easily combining them with OR and AND nodes
14821 // to form PBLEND/PSIGN.
14822 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014823 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014824 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014825
Nate Begemanb65c1752010-12-17 22:55:37 +000014826 SDValue N0 = N->getOperand(0);
14827 SDValue N1 = N->getOperand(1);
14828 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014829
Nate Begemanb65c1752010-12-17 22:55:37 +000014830 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014831 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014832 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14833 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014834 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014835
14836 // Check RHS for vnot
14837 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014838 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14839 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014840 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014841
Nate Begemanb65c1752010-12-17 22:55:37 +000014842 return SDValue();
14843}
14844
Evan Cheng760d1942010-01-04 21:22:48 +000014845static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014846 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014847 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014848 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014849 return SDValue();
14850
Stuart Hastings865f0932011-06-03 23:53:54 +000014851 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14852 if (R.getNode())
14853 return R;
14854
Evan Cheng760d1942010-01-04 21:22:48 +000014855 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014856
Evan Cheng760d1942010-01-04 21:22:48 +000014857 SDValue N0 = N->getOperand(0);
14858 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014859
Nate Begemanb65c1752010-12-17 22:55:37 +000014860 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014861 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014862 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014863 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14864 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014865
Craig Topper1666cb62011-11-19 07:07:26 +000014866 // Canonicalize pandn to RHS
14867 if (N0.getOpcode() == X86ISD::ANDNP)
14868 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014869 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014870 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14871 SDValue Mask = N1.getOperand(0);
14872 SDValue X = N1.getOperand(1);
14873 SDValue Y;
14874 if (N0.getOperand(0) == Mask)
14875 Y = N0.getOperand(1);
14876 if (N0.getOperand(1) == Mask)
14877 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014878
Craig Topper1666cb62011-11-19 07:07:26 +000014879 // Check to see if the mask appeared in both the AND and ANDNP and
14880 if (!Y.getNode())
14881 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014882
Craig Topper1666cb62011-11-19 07:07:26 +000014883 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014884 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014885 if (Mask.getOpcode() == ISD::BITCAST)
14886 Mask = Mask.getOperand(0);
14887 if (X.getOpcode() == ISD::BITCAST)
14888 X = X.getOperand(0);
14889 if (Y.getOpcode() == ISD::BITCAST)
14890 Y = Y.getOperand(0);
14891
Craig Topper1666cb62011-11-19 07:07:26 +000014892 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014893
Craig Toppered2e13d2012-01-22 19:15:14 +000014894 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014895 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14896 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014897 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014898 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014899
14900 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014901 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014902 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14903 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14904 if ((SraAmt + 1) != EltBits)
14905 return SDValue();
14906
14907 DebugLoc DL = N->getDebugLoc();
14908
14909 // Now we know we at least have a plendvb with the mask val. See if
14910 // we can form a psignb/w/d.
14911 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014912 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14913 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014914 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14915 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14916 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014917 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014918 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014919 }
14920 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014921 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014922 return SDValue();
14923
14924 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14925
14926 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14927 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14928 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014929 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014930 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014931 }
14932 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014933
Craig Topper1666cb62011-11-19 07:07:26 +000014934 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14935 return SDValue();
14936
Nate Begemanb65c1752010-12-17 22:55:37 +000014937 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014938 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14939 std::swap(N0, N1);
14940 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14941 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014942 if (!N0.hasOneUse() || !N1.hasOneUse())
14943 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014944
14945 SDValue ShAmt0 = N0.getOperand(1);
14946 if (ShAmt0.getValueType() != MVT::i8)
14947 return SDValue();
14948 SDValue ShAmt1 = N1.getOperand(1);
14949 if (ShAmt1.getValueType() != MVT::i8)
14950 return SDValue();
14951 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14952 ShAmt0 = ShAmt0.getOperand(0);
14953 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14954 ShAmt1 = ShAmt1.getOperand(0);
14955
14956 DebugLoc DL = N->getDebugLoc();
14957 unsigned Opc = X86ISD::SHLD;
14958 SDValue Op0 = N0.getOperand(0);
14959 SDValue Op1 = N1.getOperand(0);
14960 if (ShAmt0.getOpcode() == ISD::SUB) {
14961 Opc = X86ISD::SHRD;
14962 std::swap(Op0, Op1);
14963 std::swap(ShAmt0, ShAmt1);
14964 }
14965
Evan Cheng8b1190a2010-04-28 01:18:01 +000014966 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014967 if (ShAmt1.getOpcode() == ISD::SUB) {
14968 SDValue Sum = ShAmt1.getOperand(0);
14969 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014970 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14971 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14972 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14973 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014974 return DAG.getNode(Opc, DL, VT,
14975 Op0, Op1,
14976 DAG.getNode(ISD::TRUNCATE, DL,
14977 MVT::i8, ShAmt0));
14978 }
14979 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14980 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14981 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014982 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014983 return DAG.getNode(Opc, DL, VT,
14984 N0.getOperand(0), N1.getOperand(0),
14985 DAG.getNode(ISD::TRUNCATE, DL,
14986 MVT::i8, ShAmt0));
14987 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014988
Evan Cheng760d1942010-01-04 21:22:48 +000014989 return SDValue();
14990}
14991
Manman Ren92363622012-06-07 22:39:10 +000014992// Generate NEG and CMOV for integer abs.
14993static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14994 EVT VT = N->getValueType(0);
14995
14996 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14997 // 8-bit integer abs to NEG and CMOV.
14998 if (VT.isInteger() && VT.getSizeInBits() == 8)
14999 return SDValue();
15000
15001 SDValue N0 = N->getOperand(0);
15002 SDValue N1 = N->getOperand(1);
15003 DebugLoc DL = N->getDebugLoc();
15004
15005 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15006 // and change it to SUB and CMOV.
15007 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15008 N0.getOpcode() == ISD::ADD &&
15009 N0.getOperand(1) == N1 &&
15010 N1.getOpcode() == ISD::SRA &&
15011 N1.getOperand(0) == N0.getOperand(0))
15012 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15013 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15014 // Generate SUB & CMOV.
15015 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15016 DAG.getConstant(0, VT), N0.getOperand(0));
15017
15018 SDValue Ops[] = { N0.getOperand(0), Neg,
15019 DAG.getConstant(X86::COND_GE, MVT::i8),
15020 SDValue(Neg.getNode(), 1) };
15021 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15022 Ops, array_lengthof(Ops));
15023 }
15024 return SDValue();
15025}
15026
Craig Topper3738ccd2011-12-27 06:27:23 +000015027// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015028static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15029 TargetLowering::DAGCombinerInfo &DCI,
15030 const X86Subtarget *Subtarget) {
15031 if (DCI.isBeforeLegalizeOps())
15032 return SDValue();
15033
Manman Ren45d53b82012-06-08 18:58:26 +000015034 if (Subtarget->hasCMov()) {
15035 SDValue RV = performIntegerAbsCombine(N, DAG);
15036 if (RV.getNode())
15037 return RV;
15038 }
Manman Ren92363622012-06-07 22:39:10 +000015039
15040 // Try forming BMI if it is available.
15041 if (!Subtarget->hasBMI())
15042 return SDValue();
15043
Craig Topperb4c94572011-10-21 06:55:01 +000015044 EVT VT = N->getValueType(0);
15045
15046 if (VT != MVT::i32 && VT != MVT::i64)
15047 return SDValue();
15048
Craig Topper3738ccd2011-12-27 06:27:23 +000015049 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15050
Craig Topperb4c94572011-10-21 06:55:01 +000015051 // Create BLSMSK instructions by finding X ^ (X-1)
15052 SDValue N0 = N->getOperand(0);
15053 SDValue N1 = N->getOperand(1);
15054 DebugLoc DL = N->getDebugLoc();
15055
15056 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15057 isAllOnes(N0.getOperand(1)))
15058 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15059
15060 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15061 isAllOnes(N1.getOperand(1)))
15062 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15063
15064 return SDValue();
15065}
15066
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015067/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15068static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015069 TargetLowering::DAGCombinerInfo &DCI,
15070 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015071 LoadSDNode *Ld = cast<LoadSDNode>(N);
15072 EVT RegVT = Ld->getValueType(0);
15073 EVT MemVT = Ld->getMemoryVT();
15074 DebugLoc dl = Ld->getDebugLoc();
15075 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15076
15077 ISD::LoadExtType Ext = Ld->getExtensionType();
15078
Nadav Rotemca6f2962011-09-18 19:00:23 +000015079 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015080 // shuffle. We need SSE4 for the shuffles.
15081 // TODO: It is possible to support ZExt by zeroing the undef values
15082 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015083 if (RegVT.isVector() && RegVT.isInteger() &&
15084 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015085 assert(MemVT != RegVT && "Cannot extend to the same type");
15086 assert(MemVT.isVector() && "Must load a vector from memory");
15087
15088 unsigned NumElems = RegVT.getVectorNumElements();
15089 unsigned RegSz = RegVT.getSizeInBits();
15090 unsigned MemSz = MemVT.getSizeInBits();
15091 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015092
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015093 // All sizes must be a power of two.
15094 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15095 return SDValue();
15096
15097 // Attempt to load the original value using scalar loads.
15098 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015099 MVT SclrLoadTy = MVT::i8;
15100 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15101 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15102 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015103 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015104 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015105 }
15106 }
15107
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015108 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15109 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15110 (64 <= MemSz))
15111 SclrLoadTy = MVT::f64;
15112
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015113 // Calculate the number of scalar loads that we need to perform
15114 // in order to load our vector from memory.
15115 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015116
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015117 // Represent our vector as a sequence of elements which are the
15118 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015119 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15120 RegSz/SclrLoadTy.getSizeInBits());
15121
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015122 // Represent the data using the same element type that is stored in
15123 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015124 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15125 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015126
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015127 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15128 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015129
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015130 // We can't shuffle using an illegal type.
15131 if (!TLI.isTypeLegal(WideVecVT))
15132 return SDValue();
15133
15134 SmallVector<SDValue, 8> Chains;
15135 SDValue Ptr = Ld->getBasePtr();
15136 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15137 TLI.getPointerTy());
15138 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15139
15140 for (unsigned i = 0; i < NumLoads; ++i) {
15141 // Perform a single load.
15142 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15143 Ptr, Ld->getPointerInfo(),
15144 Ld->isVolatile(), Ld->isNonTemporal(),
15145 Ld->isInvariant(), Ld->getAlignment());
15146 Chains.push_back(ScalarLoad.getValue(1));
15147 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15148 // another round of DAGCombining.
15149 if (i == 0)
15150 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15151 else
15152 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15153 ScalarLoad, DAG.getIntPtrConstant(i));
15154
15155 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15156 }
15157
15158 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15159 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015160
15161 // Bitcast the loaded value to a vector of the original element type, in
15162 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015163 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015164 unsigned SizeRatio = RegSz/MemSz;
15165
15166 // Redistribute the loaded elements into the different locations.
15167 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015168 for (unsigned i = 0; i != NumElems; ++i)
15169 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015170
15171 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015172 DAG.getUNDEF(WideVecVT),
15173 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015174
15175 // Bitcast to the requested type.
15176 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15177 // Replace the original load with the new sequence
15178 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015179 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015180 }
15181
15182 return SDValue();
15183}
15184
Chris Lattner149a4e52008-02-22 02:09:43 +000015185/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015186static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015187 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015188 StoreSDNode *St = cast<StoreSDNode>(N);
15189 EVT VT = St->getValue().getValueType();
15190 EVT StVT = St->getMemoryVT();
15191 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015192 SDValue StoredVal = St->getOperand(1);
15193 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15194
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015195 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015196 // On Sandy Bridge, 256-bit memory operations are executed by two
15197 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15198 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015199 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015200 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15201 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015202 SDValue Value0 = StoredVal.getOperand(0);
15203 SDValue Value1 = StoredVal.getOperand(1);
15204
15205 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15206 SDValue Ptr0 = St->getBasePtr();
15207 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15208
15209 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15210 St->getPointerInfo(), St->isVolatile(),
15211 St->isNonTemporal(), St->getAlignment());
15212 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15213 St->getPointerInfo(), St->isVolatile(),
15214 St->isNonTemporal(), St->getAlignment());
15215 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15216 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015217
15218 // Optimize trunc store (of multiple scalars) to shuffle and store.
15219 // First, pack all of the elements in one place. Next, store to memory
15220 // in fewer chunks.
15221 if (St->isTruncatingStore() && VT.isVector()) {
15222 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15223 unsigned NumElems = VT.getVectorNumElements();
15224 assert(StVT != VT && "Cannot truncate to the same type");
15225 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15226 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15227
15228 // From, To sizes and ElemCount must be pow of two
15229 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015230 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015231 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015232 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015233
Nadav Rotem614061b2011-08-10 19:30:14 +000015234 unsigned SizeRatio = FromSz / ToSz;
15235
15236 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15237
15238 // Create a type on which we perform the shuffle
15239 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15240 StVT.getScalarType(), NumElems*SizeRatio);
15241
15242 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15243
15244 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15245 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015246 for (unsigned i = 0; i != NumElems; ++i)
15247 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015248
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015249 // Can't shuffle using an illegal type.
15250 if (!TLI.isTypeLegal(WideVecVT))
15251 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015252
15253 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015254 DAG.getUNDEF(WideVecVT),
15255 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015256 // At this point all of the data is stored at the bottom of the
15257 // register. We now need to save it to mem.
15258
15259 // Find the largest store unit
15260 MVT StoreType = MVT::i8;
15261 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15262 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15263 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015264 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015265 StoreType = Tp;
15266 }
15267
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015268 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15269 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15270 (64 <= NumElems * ToSz))
15271 StoreType = MVT::f64;
15272
Nadav Rotem614061b2011-08-10 19:30:14 +000015273 // Bitcast the original vector into a vector of store-size units
15274 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015275 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015276 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15277 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15278 SmallVector<SDValue, 8> Chains;
15279 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15280 TLI.getPointerTy());
15281 SDValue Ptr = St->getBasePtr();
15282
15283 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015284 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015285 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15286 StoreType, ShuffWide,
15287 DAG.getIntPtrConstant(i));
15288 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15289 St->getPointerInfo(), St->isVolatile(),
15290 St->isNonTemporal(), St->getAlignment());
15291 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15292 Chains.push_back(Ch);
15293 }
15294
15295 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15296 Chains.size());
15297 }
15298
15299
Chris Lattner149a4e52008-02-22 02:09:43 +000015300 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15301 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015302 // A preferable solution to the general problem is to figure out the right
15303 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015304
15305 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015306 if (VT.getSizeInBits() != 64)
15307 return SDValue();
15308
Devang Patel578efa92009-06-05 21:57:13 +000015309 const Function *F = DAG.getMachineFunction().getFunction();
15310 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015311 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015312 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015313 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015314 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015315 isa<LoadSDNode>(St->getValue()) &&
15316 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15317 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015318 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015319 LoadSDNode *Ld = 0;
15320 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015321 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015322 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015323 // Must be a store of a load. We currently handle two cases: the load
15324 // is a direct child, and it's under an intervening TokenFactor. It is
15325 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015326 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015327 Ld = cast<LoadSDNode>(St->getChain());
15328 else if (St->getValue().hasOneUse() &&
15329 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015330 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015331 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015332 TokenFactorIndex = i;
15333 Ld = cast<LoadSDNode>(St->getValue());
15334 } else
15335 Ops.push_back(ChainVal->getOperand(i));
15336 }
15337 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015338
Evan Cheng536e6672009-03-12 05:59:15 +000015339 if (!Ld || !ISD::isNormalLoad(Ld))
15340 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015341
Evan Cheng536e6672009-03-12 05:59:15 +000015342 // If this is not the MMX case, i.e. we are just turning i64 load/store
15343 // into f64 load/store, avoid the transformation if there are multiple
15344 // uses of the loaded value.
15345 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15346 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015347
Evan Cheng536e6672009-03-12 05:59:15 +000015348 DebugLoc LdDL = Ld->getDebugLoc();
15349 DebugLoc StDL = N->getDebugLoc();
15350 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15351 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15352 // pair instead.
15353 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015354 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015355 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15356 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015357 Ld->isNonTemporal(), Ld->isInvariant(),
15358 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015359 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015360 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015361 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015362 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015363 Ops.size());
15364 }
Evan Cheng536e6672009-03-12 05:59:15 +000015365 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015366 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015367 St->isVolatile(), St->isNonTemporal(),
15368 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015369 }
Evan Cheng536e6672009-03-12 05:59:15 +000015370
15371 // Otherwise, lower to two pairs of 32-bit loads / stores.
15372 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015373 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15374 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015375
Owen Anderson825b72b2009-08-11 20:47:22 +000015376 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015377 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015378 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015379 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015380 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015381 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015382 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015383 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015384 MinAlign(Ld->getAlignment(), 4));
15385
15386 SDValue NewChain = LoLd.getValue(1);
15387 if (TokenFactorIndex != -1) {
15388 Ops.push_back(LoLd);
15389 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015390 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015391 Ops.size());
15392 }
15393
15394 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015395 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15396 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015397
15398 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015399 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015400 St->isVolatile(), St->isNonTemporal(),
15401 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015402 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015403 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015404 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015405 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015406 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015407 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015408 }
Dan Gohman475871a2008-07-27 21:46:04 +000015409 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015410}
15411
Duncan Sands17470be2011-09-22 20:15:48 +000015412/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15413/// and return the operands for the horizontal operation in LHS and RHS. A
15414/// horizontal operation performs the binary operation on successive elements
15415/// of its first operand, then on successive elements of its second operand,
15416/// returning the resulting values in a vector. For example, if
15417/// A = < float a0, float a1, float a2, float a3 >
15418/// and
15419/// B = < float b0, float b1, float b2, float b3 >
15420/// then the result of doing a horizontal operation on A and B is
15421/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15422/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15423/// A horizontal-op B, for some already available A and B, and if so then LHS is
15424/// set to A, RHS to B, and the routine returns 'true'.
15425/// Note that the binary operation should have the property that if one of the
15426/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015427static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015428 // Look for the following pattern: if
15429 // A = < float a0, float a1, float a2, float a3 >
15430 // B = < float b0, float b1, float b2, float b3 >
15431 // and
15432 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15433 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15434 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15435 // which is A horizontal-op B.
15436
15437 // At least one of the operands should be a vector shuffle.
15438 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15439 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15440 return false;
15441
15442 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015443
15444 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15445 "Unsupported vector type for horizontal add/sub");
15446
15447 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15448 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015449 unsigned NumElts = VT.getVectorNumElements();
15450 unsigned NumLanes = VT.getSizeInBits()/128;
15451 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015452 assert((NumLaneElts % 2 == 0) &&
15453 "Vector type should have an even number of elements in each lane");
15454 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015455
15456 // View LHS in the form
15457 // LHS = VECTOR_SHUFFLE A, B, LMask
15458 // If LHS is not a shuffle then pretend it is the shuffle
15459 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15460 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15461 // type VT.
15462 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015463 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015464 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15465 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15466 A = LHS.getOperand(0);
15467 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15468 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015469 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15470 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015471 } else {
15472 if (LHS.getOpcode() != ISD::UNDEF)
15473 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015474 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015475 LMask[i] = i;
15476 }
15477
15478 // Likewise, view RHS in the form
15479 // RHS = VECTOR_SHUFFLE C, D, RMask
15480 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015481 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015482 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15483 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15484 C = RHS.getOperand(0);
15485 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15486 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015487 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15488 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015489 } else {
15490 if (RHS.getOpcode() != ISD::UNDEF)
15491 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015492 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015493 RMask[i] = i;
15494 }
15495
15496 // Check that the shuffles are both shuffling the same vectors.
15497 if (!(A == C && B == D) && !(A == D && B == C))
15498 return false;
15499
15500 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15501 if (!A.getNode() && !B.getNode())
15502 return false;
15503
15504 // If A and B occur in reverse order in RHS, then "swap" them (which means
15505 // rewriting the mask).
15506 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015507 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015508
15509 // At this point LHS and RHS are equivalent to
15510 // LHS = VECTOR_SHUFFLE A, B, LMask
15511 // RHS = VECTOR_SHUFFLE A, B, RMask
15512 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015513 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015514 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015515
Craig Topperf8363302011-12-02 08:18:41 +000015516 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015517 if (LIdx < 0 || RIdx < 0 ||
15518 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15519 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015520 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015521
Craig Topperf8363302011-12-02 08:18:41 +000015522 // Check that successive elements are being operated on. If not, this is
15523 // not a horizontal operation.
15524 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15525 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015526 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015527 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015528 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015529 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015530 }
15531
15532 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15533 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15534 return true;
15535}
15536
15537/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15538static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15539 const X86Subtarget *Subtarget) {
15540 EVT VT = N->getValueType(0);
15541 SDValue LHS = N->getOperand(0);
15542 SDValue RHS = N->getOperand(1);
15543
15544 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015545 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015546 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015547 isHorizontalBinOp(LHS, RHS, true))
15548 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15549 return SDValue();
15550}
15551
15552/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15553static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15554 const X86Subtarget *Subtarget) {
15555 EVT VT = N->getValueType(0);
15556 SDValue LHS = N->getOperand(0);
15557 SDValue RHS = N->getOperand(1);
15558
15559 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015560 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015561 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015562 isHorizontalBinOp(LHS, RHS, false))
15563 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15564 return SDValue();
15565}
15566
Chris Lattner6cf73262008-01-25 06:14:17 +000015567/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15568/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015569static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015570 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15571 // F[X]OR(0.0, x) -> x
15572 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015573 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15574 if (C->getValueAPF().isPosZero())
15575 return N->getOperand(1);
15576 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15577 if (C->getValueAPF().isPosZero())
15578 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015579 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015580}
15581
Nadav Rotemd60cb112012-08-19 13:06:16 +000015582/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15583/// X86ISD::FMAX nodes.
15584static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15585 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15586
15587 // Only perform optimizations if UnsafeMath is used.
15588 if (!DAG.getTarget().Options.UnsafeFPMath)
15589 return SDValue();
15590
15591 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015592 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015593 unsigned NewOp = 0;
15594 switch (N->getOpcode()) {
15595 default: llvm_unreachable("unknown opcode");
15596 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15597 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15598 }
15599
15600 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15601 N->getOperand(0), N->getOperand(1));
15602}
15603
15604
Chris Lattneraf723b92008-01-25 05:46:26 +000015605/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015606static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015607 // FAND(0.0, x) -> 0.0
15608 // FAND(x, 0.0) -> 0.0
15609 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15610 if (C->getValueAPF().isPosZero())
15611 return N->getOperand(0);
15612 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15613 if (C->getValueAPF().isPosZero())
15614 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015615 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015616}
15617
Dan Gohmane5af2d32009-01-29 01:59:02 +000015618static SDValue PerformBTCombine(SDNode *N,
15619 SelectionDAG &DAG,
15620 TargetLowering::DAGCombinerInfo &DCI) {
15621 // BT ignores high bits in the bit index operand.
15622 SDValue Op1 = N->getOperand(1);
15623 if (Op1.hasOneUse()) {
15624 unsigned BitWidth = Op1.getValueSizeInBits();
15625 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15626 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015627 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15628 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015629 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015630 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15631 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15632 DCI.CommitTargetLoweringOpt(TLO);
15633 }
15634 return SDValue();
15635}
Chris Lattner83e6c992006-10-04 06:57:07 +000015636
Eli Friedman7a5e5552009-06-07 06:52:44 +000015637static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15638 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015639 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015640 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015641 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015642 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015643 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015644 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015645 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015646 }
15647 return SDValue();
15648}
15649
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015650static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15651 TargetLowering::DAGCombinerInfo &DCI,
15652 const X86Subtarget *Subtarget) {
15653 if (!DCI.isBeforeLegalizeOps())
15654 return SDValue();
15655
Craig Topper3ef43cf2012-04-24 06:36:35 +000015656 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015657 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015658
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015659 EVT VT = N->getValueType(0);
15660 SDValue Op = N->getOperand(0);
15661 EVT OpVT = Op.getValueType();
15662 DebugLoc dl = N->getDebugLoc();
15663
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015664 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15665 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015666
Craig Topper3ef43cf2012-04-24 06:36:35 +000015667 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015668 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015669
15670 // Optimize vectors in AVX mode
15671 // Sign extend v8i16 to v8i32 and
15672 // v4i32 to v4i64
15673 //
15674 // Divide input vector into two parts
15675 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15676 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15677 // concat the vectors to original VT
15678
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015679 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015680 SDValue Undef = DAG.getUNDEF(OpVT);
15681
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015682 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015683 for (unsigned i = 0; i != NumElems/2; ++i)
15684 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015685
Craig Toppercacafd42012-08-14 08:18:43 +000015686 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015687
15688 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015689 for (unsigned i = 0; i != NumElems/2; ++i)
15690 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015691
Craig Toppercacafd42012-08-14 08:18:43 +000015692 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015693
Craig Topper3ef43cf2012-04-24 06:36:35 +000015694 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015695 VT.getVectorNumElements()/2);
15696
Craig Topper3ef43cf2012-04-24 06:36:35 +000015697 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015698 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15699
15700 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15701 }
15702 return SDValue();
15703}
15704
Michael Liaof6c24ee2012-08-10 14:39:24 +000015705static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015706 const X86Subtarget* Subtarget) {
15707 DebugLoc dl = N->getDebugLoc();
15708 EVT VT = N->getValueType(0);
15709
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015710 // Let legalize expand this if it isn't a legal type yet.
15711 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15712 return SDValue();
15713
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015714 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015715 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15716 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015717 return SDValue();
15718
15719 SDValue A = N->getOperand(0);
15720 SDValue B = N->getOperand(1);
15721 SDValue C = N->getOperand(2);
15722
15723 bool NegA = (A.getOpcode() == ISD::FNEG);
15724 bool NegB = (B.getOpcode() == ISD::FNEG);
15725 bool NegC = (C.getOpcode() == ISD::FNEG);
15726
Michael Liaof6c24ee2012-08-10 14:39:24 +000015727 // Negative multiplication when NegA xor NegB
15728 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015729 if (NegA)
15730 A = A.getOperand(0);
15731 if (NegB)
15732 B = B.getOperand(0);
15733 if (NegC)
15734 C = C.getOperand(0);
15735
15736 unsigned Opcode;
15737 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015738 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015739 else
Craig Topperbf404372012-08-31 15:40:30 +000015740 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15741
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015742 return DAG.getNode(Opcode, dl, VT, A, B, C);
15743}
15744
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015745static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015746 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015747 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015748 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15749 // (and (i32 x86isd::setcc_carry), 1)
15750 // This eliminates the zext. This transformation is necessary because
15751 // ISD::SETCC is always legalized to i8.
15752 DebugLoc dl = N->getDebugLoc();
15753 SDValue N0 = N->getOperand(0);
15754 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015755 EVT OpVT = N0.getValueType();
15756
Evan Cheng2e489c42009-12-16 00:53:11 +000015757 if (N0.getOpcode() == ISD::AND &&
15758 N0.hasOneUse() &&
15759 N0.getOperand(0).hasOneUse()) {
15760 SDValue N00 = N0.getOperand(0);
15761 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15762 return SDValue();
15763 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15764 if (!C || C->getZExtValue() != 1)
15765 return SDValue();
15766 return DAG.getNode(ISD::AND, dl, VT,
15767 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15768 N00.getOperand(0), N00.getOperand(1)),
15769 DAG.getConstant(1, VT));
15770 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015771
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015772 // Optimize vectors in AVX mode:
15773 //
15774 // v8i16 -> v8i32
15775 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15776 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15777 // Concat upper and lower parts.
15778 //
15779 // v4i32 -> v4i64
15780 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15781 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15782 // Concat upper and lower parts.
15783 //
Craig Topperc16f8512012-04-25 06:39:39 +000015784 if (!DCI.isBeforeLegalizeOps())
15785 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015786
Craig Topperc16f8512012-04-25 06:39:39 +000015787 if (!Subtarget->hasAVX())
15788 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015789
Craig Topperc16f8512012-04-25 06:39:39 +000015790 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15791 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015792
Craig Topperc16f8512012-04-25 06:39:39 +000015793 if (Subtarget->hasAVX2())
15794 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015795
Craig Topperc16f8512012-04-25 06:39:39 +000015796 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15797 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15798 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015799
Craig Topperc16f8512012-04-25 06:39:39 +000015800 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15801 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015802
Craig Topperc16f8512012-04-25 06:39:39 +000015803 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15804 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15805
15806 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015807 }
15808
Evan Cheng2e489c42009-12-16 00:53:11 +000015809 return SDValue();
15810}
15811
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015812// Optimize x == -y --> x+y == 0
15813// x != -y --> x+y != 0
15814static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15815 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15816 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015817 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015818
15819 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15820 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15821 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15822 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15823 LHS.getValueType(), RHS, LHS.getOperand(1));
15824 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15825 addV, DAG.getConstant(0, addV.getValueType()), CC);
15826 }
15827 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15828 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15829 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15830 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15831 RHS.getValueType(), LHS, RHS.getOperand(1));
15832 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15833 addV, DAG.getConstant(0, addV.getValueType()), CC);
15834 }
15835 return SDValue();
15836}
15837
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015838// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015839static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15840 TargetLowering::DAGCombinerInfo &DCI,
15841 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015842 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015843 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15844 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015845
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015846 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15847 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15848 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015849 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015850 return DAG.getNode(ISD::AND, DL, MVT::i8,
15851 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015852 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015853 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015854
Michael Liao2a33cec2012-08-10 19:58:13 +000015855 SDValue Flags;
15856
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015857 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15858 if (Flags.getNode()) {
15859 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15860 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15861 }
15862
15863 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015864 if (Flags.getNode()) {
15865 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15866 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15867 }
15868
15869 return SDValue();
15870}
15871
15872// Optimize branch condition evaluation.
15873//
15874static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15875 TargetLowering::DAGCombinerInfo &DCI,
15876 const X86Subtarget *Subtarget) {
15877 DebugLoc DL = N->getDebugLoc();
15878 SDValue Chain = N->getOperand(0);
15879 SDValue Dest = N->getOperand(1);
15880 SDValue EFLAGS = N->getOperand(3);
15881 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15882
15883 SDValue Flags;
15884
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015885 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15886 if (Flags.getNode()) {
15887 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15888 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15889 Flags);
15890 }
15891
15892 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015893 if (Flags.getNode()) {
15894 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15895 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15896 Flags);
15897 }
15898
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015899 return SDValue();
15900}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015901
Craig Topper7fd5e162012-04-24 06:02:29 +000015902static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015903 SDValue Op0 = N->getOperand(0);
15904 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015905
15906 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015907 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015908 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015909 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015910 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15911 // Notice that we use SINT_TO_FP because we know that the high bits
15912 // are zero and SINT_TO_FP is better supported by the hardware.
15913 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15914 }
15915
15916 return SDValue();
15917}
15918
Benjamin Kramer1396c402011-06-18 11:09:41 +000015919static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15920 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015921 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015922 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015923
15924 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015925 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015926 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015927 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015928 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15929 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15930 }
15931
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015932 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15933 // a 32-bit target where SSE doesn't support i64->FP operations.
15934 if (Op0.getOpcode() == ISD::LOAD) {
15935 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15936 EVT VT = Ld->getValueType(0);
15937 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15938 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15939 !XTLI->getSubtarget()->is64Bit() &&
15940 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015941 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15942 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015943 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15944 return FILDChain;
15945 }
15946 }
15947 return SDValue();
15948}
15949
Craig Topper7fd5e162012-04-24 06:02:29 +000015950static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15951 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015952
15953 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015954 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15955 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015956 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015957 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15958 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15959 }
15960
15961 return SDValue();
15962}
15963
Chris Lattner23a01992010-12-20 01:37:09 +000015964// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15965static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15966 X86TargetLowering::DAGCombinerInfo &DCI) {
15967 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15968 // the result is either zero or one (depending on the input carry bit).
15969 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15970 if (X86::isZeroNode(N->getOperand(0)) &&
15971 X86::isZeroNode(N->getOperand(1)) &&
15972 // We don't have a good way to replace an EFLAGS use, so only do this when
15973 // dead right now.
15974 SDValue(N, 1).use_empty()) {
15975 DebugLoc DL = N->getDebugLoc();
15976 EVT VT = N->getValueType(0);
15977 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15978 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15979 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15980 DAG.getConstant(X86::COND_B,MVT::i8),
15981 N->getOperand(2)),
15982 DAG.getConstant(1, VT));
15983 return DCI.CombineTo(N, Res1, CarryOut);
15984 }
15985
15986 return SDValue();
15987}
15988
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015989// fold (add Y, (sete X, 0)) -> adc 0, Y
15990// (add Y, (setne X, 0)) -> sbb -1, Y
15991// (sub (sete X, 0), Y) -> sbb 0, Y
15992// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015993static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015994 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015995
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015996 // Look through ZExts.
15997 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15998 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15999 return SDValue();
16000
16001 SDValue SetCC = Ext.getOperand(0);
16002 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16003 return SDValue();
16004
16005 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16006 if (CC != X86::COND_E && CC != X86::COND_NE)
16007 return SDValue();
16008
16009 SDValue Cmp = SetCC.getOperand(1);
16010 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016011 !X86::isZeroNode(Cmp.getOperand(1)) ||
16012 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016013 return SDValue();
16014
16015 SDValue CmpOp0 = Cmp.getOperand(0);
16016 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16017 DAG.getConstant(1, CmpOp0.getValueType()));
16018
16019 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16020 if (CC == X86::COND_NE)
16021 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16022 DL, OtherVal.getValueType(), OtherVal,
16023 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16024 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16025 DL, OtherVal.getValueType(), OtherVal,
16026 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16027}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016028
Craig Topper54f952a2011-11-19 09:02:40 +000016029/// PerformADDCombine - Do target-specific dag combines on integer adds.
16030static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16031 const X86Subtarget *Subtarget) {
16032 EVT VT = N->getValueType(0);
16033 SDValue Op0 = N->getOperand(0);
16034 SDValue Op1 = N->getOperand(1);
16035
16036 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016037 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016038 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016039 isHorizontalBinOp(Op0, Op1, true))
16040 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16041
16042 return OptimizeConditionalInDecrement(N, DAG);
16043}
16044
16045static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16046 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016047 SDValue Op0 = N->getOperand(0);
16048 SDValue Op1 = N->getOperand(1);
16049
16050 // X86 can't encode an immediate LHS of a sub. See if we can push the
16051 // negation into a preceding instruction.
16052 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016053 // If the RHS of the sub is a XOR with one use and a constant, invert the
16054 // immediate. Then add one to the LHS of the sub so we can turn
16055 // X-Y -> X+~Y+1, saving one register.
16056 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16057 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016058 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016059 EVT VT = Op0.getValueType();
16060 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16061 Op1.getOperand(0),
16062 DAG.getConstant(~XorC, VT));
16063 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016064 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016065 }
16066 }
16067
Craig Topper54f952a2011-11-19 09:02:40 +000016068 // Try to synthesize horizontal adds from adds of shuffles.
16069 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016070 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016071 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16072 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016073 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16074
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016075 return OptimizeConditionalInDecrement(N, DAG);
16076}
16077
Dan Gohman475871a2008-07-27 21:46:04 +000016078SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016079 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016080 SelectionDAG &DAG = DCI.DAG;
16081 switch (N->getOpcode()) {
16082 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016083 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016084 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016085 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016086 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016087 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016088 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16089 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016090 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016091 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016092 case ISD::SHL:
16093 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016094 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016095 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016096 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016097 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016098 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016099 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016100 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016101 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016102 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016103 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16104 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016105 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016106 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016107 case X86ISD::FMIN:
16108 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016109 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016110 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016111 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016112 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016113 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016114 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016115 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016116 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016117 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016118 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016119 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016120 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016121 case X86ISD::UNPCKH:
16122 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016123 case X86ISD::MOVHLPS:
16124 case X86ISD::MOVLHPS:
16125 case X86ISD::PSHUFD:
16126 case X86ISD::PSHUFHW:
16127 case X86ISD::PSHUFLW:
16128 case X86ISD::MOVSS:
16129 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016130 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016131 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016132 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016133 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016134 }
16135
Dan Gohman475871a2008-07-27 21:46:04 +000016136 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016137}
16138
Evan Chenge5b51ac2010-04-17 06:13:15 +000016139/// isTypeDesirableForOp - Return true if the target has native support for
16140/// the specified value type and it is 'desirable' to use the type for the
16141/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16142/// instruction encodings are longer and some i16 instructions are slow.
16143bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16144 if (!isTypeLegal(VT))
16145 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016146 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016147 return true;
16148
16149 switch (Opc) {
16150 default:
16151 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016152 case ISD::LOAD:
16153 case ISD::SIGN_EXTEND:
16154 case ISD::ZERO_EXTEND:
16155 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016156 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016157 case ISD::SRL:
16158 case ISD::SUB:
16159 case ISD::ADD:
16160 case ISD::MUL:
16161 case ISD::AND:
16162 case ISD::OR:
16163 case ISD::XOR:
16164 return false;
16165 }
16166}
16167
16168/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016169/// beneficial for dag combiner to promote the specified node. If true, it
16170/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016171bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016172 EVT VT = Op.getValueType();
16173 if (VT != MVT::i16)
16174 return false;
16175
Evan Cheng4c26e932010-04-19 19:29:22 +000016176 bool Promote = false;
16177 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016178 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016179 default: break;
16180 case ISD::LOAD: {
16181 LoadSDNode *LD = cast<LoadSDNode>(Op);
16182 // If the non-extending load has a single use and it's not live out, then it
16183 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016184 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16185 Op.hasOneUse()*/) {
16186 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16187 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16188 // The only case where we'd want to promote LOAD (rather then it being
16189 // promoted as an operand is when it's only use is liveout.
16190 if (UI->getOpcode() != ISD::CopyToReg)
16191 return false;
16192 }
16193 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016194 Promote = true;
16195 break;
16196 }
16197 case ISD::SIGN_EXTEND:
16198 case ISD::ZERO_EXTEND:
16199 case ISD::ANY_EXTEND:
16200 Promote = true;
16201 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016202 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016203 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016204 SDValue N0 = Op.getOperand(0);
16205 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016206 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016207 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016208 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016209 break;
16210 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016211 case ISD::ADD:
16212 case ISD::MUL:
16213 case ISD::AND:
16214 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016215 case ISD::XOR:
16216 Commute = true;
16217 // fallthrough
16218 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016219 SDValue N0 = Op.getOperand(0);
16220 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016221 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016222 return false;
16223 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016224 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016225 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016226 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016227 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016228 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016229 }
16230 }
16231
16232 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016233 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016234}
16235
Evan Cheng60c07e12006-07-05 22:17:51 +000016236//===----------------------------------------------------------------------===//
16237// X86 Inline Assembly Support
16238//===----------------------------------------------------------------------===//
16239
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016240namespace {
16241 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016242 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016243 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016244
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016245 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016246 StringRef piece(*args[i]);
16247 if (!s.startswith(piece)) // Check if the piece matches.
16248 return false;
16249
16250 s = s.substr(piece.size());
16251 StringRef::size_type pos = s.find_first_not_of(" \t");
16252 if (pos == 0) // We matched a prefix.
16253 return false;
16254
16255 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016256 }
16257
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016258 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016259 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016260 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016261}
16262
Chris Lattnerb8105652009-07-20 17:51:36 +000016263bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16264 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016265
16266 std::string AsmStr = IA->getAsmString();
16267
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016268 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16269 if (!Ty || Ty->getBitWidth() % 16 != 0)
16270 return false;
16271
Chris Lattnerb8105652009-07-20 17:51:36 +000016272 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016273 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016274 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016275
16276 switch (AsmPieces.size()) {
16277 default: return false;
16278 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016279 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016280 // we will turn this bswap into something that will be lowered to logical
16281 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16282 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016283 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016284 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16285 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16286 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16287 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16288 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16289 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016290 // No need to check constraints, nothing other than the equivalent of
16291 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016292 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016293 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016294
Chris Lattnerb8105652009-07-20 17:51:36 +000016295 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016296 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016297 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016298 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16299 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016300 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016301 const std::string &ConstraintsStr = IA->getConstraintString();
16302 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016303 std::sort(AsmPieces.begin(), AsmPieces.end());
16304 if (AsmPieces.size() == 4 &&
16305 AsmPieces[0] == "~{cc}" &&
16306 AsmPieces[1] == "~{dirflag}" &&
16307 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016308 AsmPieces[3] == "~{fpsr}")
16309 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016310 }
16311 break;
16312 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016313 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016314 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016315 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16316 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16317 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016318 AsmPieces.clear();
16319 const std::string &ConstraintsStr = IA->getConstraintString();
16320 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16321 std::sort(AsmPieces.begin(), AsmPieces.end());
16322 if (AsmPieces.size() == 4 &&
16323 AsmPieces[0] == "~{cc}" &&
16324 AsmPieces[1] == "~{dirflag}" &&
16325 AsmPieces[2] == "~{flags}" &&
16326 AsmPieces[3] == "~{fpsr}")
16327 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016328 }
Evan Cheng55d42002011-01-08 01:24:27 +000016329
16330 if (CI->getType()->isIntegerTy(64)) {
16331 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16332 if (Constraints.size() >= 2 &&
16333 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16334 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16335 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016336 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16337 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16338 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016339 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016340 }
16341 }
16342 break;
16343 }
16344 return false;
16345}
16346
16347
16348
Chris Lattnerf4dff842006-07-11 02:54:03 +000016349/// getConstraintType - Given a constraint letter, return the type of
16350/// constraint it is for this target.
16351X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016352X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16353 if (Constraint.size() == 1) {
16354 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016355 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016356 case 'q':
16357 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016358 case 'f':
16359 case 't':
16360 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016361 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016362 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016363 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016364 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016365 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016366 case 'a':
16367 case 'b':
16368 case 'c':
16369 case 'd':
16370 case 'S':
16371 case 'D':
16372 case 'A':
16373 return C_Register;
16374 case 'I':
16375 case 'J':
16376 case 'K':
16377 case 'L':
16378 case 'M':
16379 case 'N':
16380 case 'G':
16381 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016382 case 'e':
16383 case 'Z':
16384 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016385 default:
16386 break;
16387 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016388 }
Chris Lattner4234f572007-03-25 02:14:49 +000016389 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016390}
16391
John Thompson44ab89e2010-10-29 17:29:13 +000016392/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016393/// This object must already have been set up with the operand type
16394/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016395TargetLowering::ConstraintWeight
16396 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016397 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016398 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016399 Value *CallOperandVal = info.CallOperandVal;
16400 // If we don't have a value, we can't do a match,
16401 // but allow it at the lowest weight.
16402 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016403 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016404 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016405 // Look at the constraint type.
16406 switch (*constraint) {
16407 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016408 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16409 case 'R':
16410 case 'q':
16411 case 'Q':
16412 case 'a':
16413 case 'b':
16414 case 'c':
16415 case 'd':
16416 case 'S':
16417 case 'D':
16418 case 'A':
16419 if (CallOperandVal->getType()->isIntegerTy())
16420 weight = CW_SpecificReg;
16421 break;
16422 case 'f':
16423 case 't':
16424 case 'u':
16425 if (type->isFloatingPointTy())
16426 weight = CW_SpecificReg;
16427 break;
16428 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016429 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016430 weight = CW_SpecificReg;
16431 break;
16432 case 'x':
16433 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016434 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016435 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016436 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016437 break;
16438 case 'I':
16439 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16440 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016441 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016442 }
16443 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016444 case 'J':
16445 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16446 if (C->getZExtValue() <= 63)
16447 weight = CW_Constant;
16448 }
16449 break;
16450 case 'K':
16451 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16452 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16453 weight = CW_Constant;
16454 }
16455 break;
16456 case 'L':
16457 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16458 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16459 weight = CW_Constant;
16460 }
16461 break;
16462 case 'M':
16463 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16464 if (C->getZExtValue() <= 3)
16465 weight = CW_Constant;
16466 }
16467 break;
16468 case 'N':
16469 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16470 if (C->getZExtValue() <= 0xff)
16471 weight = CW_Constant;
16472 }
16473 break;
16474 case 'G':
16475 case 'C':
16476 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16477 weight = CW_Constant;
16478 }
16479 break;
16480 case 'e':
16481 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16482 if ((C->getSExtValue() >= -0x80000000LL) &&
16483 (C->getSExtValue() <= 0x7fffffffLL))
16484 weight = CW_Constant;
16485 }
16486 break;
16487 case 'Z':
16488 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16489 if (C->getZExtValue() <= 0xffffffff)
16490 weight = CW_Constant;
16491 }
16492 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016493 }
16494 return weight;
16495}
16496
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016497/// LowerXConstraint - try to replace an X constraint, which matches anything,
16498/// with another that has more specific requirements based on the type of the
16499/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016500const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016501LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016502 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16503 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016504 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016505 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016506 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016507 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016508 return "x";
16509 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016510
Chris Lattner5e764232008-04-26 23:02:14 +000016511 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016512}
16513
Chris Lattner48884cd2007-08-25 00:47:38 +000016514/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16515/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016516void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016517 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016518 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016519 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016520 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016521
Eric Christopher100c8332011-06-02 23:16:42 +000016522 // Only support length 1 constraints for now.
16523 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016524
Eric Christopher100c8332011-06-02 23:16:42 +000016525 char ConstraintLetter = Constraint[0];
16526 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016527 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016528 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016529 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016530 if (C->getZExtValue() <= 31) {
16531 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016532 break;
16533 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016534 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016535 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016536 case 'J':
16537 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016538 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016539 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16540 break;
16541 }
16542 }
16543 return;
16544 case 'K':
16545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016546 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016547 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16548 break;
16549 }
16550 }
16551 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016552 case 'N':
16553 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016554 if (C->getZExtValue() <= 255) {
16555 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016556 break;
16557 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016558 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016559 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016560 case 'e': {
16561 // 32-bit signed value
16562 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016563 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16564 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016565 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016566 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016567 break;
16568 }
16569 // FIXME gcc accepts some relocatable values here too, but only in certain
16570 // memory models; it's complicated.
16571 }
16572 return;
16573 }
16574 case 'Z': {
16575 // 32-bit unsigned value
16576 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016577 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16578 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016579 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16580 break;
16581 }
16582 }
16583 // FIXME gcc accepts some relocatable values here too, but only in certain
16584 // memory models; it's complicated.
16585 return;
16586 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016587 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016588 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016589 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016590 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016591 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016592 break;
16593 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016594
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016595 // In any sort of PIC mode addresses need to be computed at runtime by
16596 // adding in a register or some sort of table lookup. These can't
16597 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016598 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016599 return;
16600
Chris Lattnerdc43a882007-05-03 16:52:29 +000016601 // If we are in non-pic codegen mode, we allow the address of a global (with
16602 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016603 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016604 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016605
Chris Lattner49921962009-05-08 18:23:14 +000016606 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16607 while (1) {
16608 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16609 Offset += GA->getOffset();
16610 break;
16611 } else if (Op.getOpcode() == ISD::ADD) {
16612 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16613 Offset += C->getZExtValue();
16614 Op = Op.getOperand(0);
16615 continue;
16616 }
16617 } else if (Op.getOpcode() == ISD::SUB) {
16618 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16619 Offset += -C->getZExtValue();
16620 Op = Op.getOperand(0);
16621 continue;
16622 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016623 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016624
Chris Lattner49921962009-05-08 18:23:14 +000016625 // Otherwise, this isn't something we can handle, reject it.
16626 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016627 }
Eric Christopherfd179292009-08-27 18:07:15 +000016628
Dan Gohman46510a72010-04-15 01:51:59 +000016629 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016630 // If we require an extra load to get this address, as in PIC mode, we
16631 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016632 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16633 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016634 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016635
Devang Patel0d881da2010-07-06 22:08:15 +000016636 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16637 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016638 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016639 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016640 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016641
Gabor Greifba36cb52008-08-28 21:40:38 +000016642 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016643 Ops.push_back(Result);
16644 return;
16645 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016646 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016647}
16648
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016649std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016650X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016651 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016652 // First, see if this is a constraint that directly corresponds to an LLVM
16653 // register class.
16654 if (Constraint.size() == 1) {
16655 // GCC Constraint Letters
16656 switch (Constraint[0]) {
16657 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016658 // TODO: Slight differences here in allocation order and leaving
16659 // RIP in the class. Do they matter any more here than they do
16660 // in the normal allocation?
16661 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16662 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016663 if (VT == MVT::i32 || VT == MVT::f32)
16664 return std::make_pair(0U, &X86::GR32RegClass);
16665 if (VT == MVT::i16)
16666 return std::make_pair(0U, &X86::GR16RegClass);
16667 if (VT == MVT::i8 || VT == MVT::i1)
16668 return std::make_pair(0U, &X86::GR8RegClass);
16669 if (VT == MVT::i64 || VT == MVT::f64)
16670 return std::make_pair(0U, &X86::GR64RegClass);
16671 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016672 }
16673 // 32-bit fallthrough
16674 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016675 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016676 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16677 if (VT == MVT::i16)
16678 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16679 if (VT == MVT::i8 || VT == MVT::i1)
16680 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16681 if (VT == MVT::i64)
16682 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016683 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016684 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016685 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016686 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016687 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016688 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016689 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016690 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016691 return std::make_pair(0U, &X86::GR32RegClass);
16692 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016693 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016694 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016695 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016696 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016697 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016698 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016699 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16700 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016701 case 'f': // FP Stack registers.
16702 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16703 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016704 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016705 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016706 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016707 return std::make_pair(0U, &X86::RFP64RegClass);
16708 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016709 case 'y': // MMX_REGS if MMX allowed.
16710 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016711 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016712 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016713 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016714 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016715 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016716 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016717
Owen Anderson825b72b2009-08-11 20:47:22 +000016718 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016719 default: break;
16720 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016721 case MVT::f32:
16722 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016723 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016724 case MVT::f64:
16725 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016726 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016727 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016728 case MVT::v16i8:
16729 case MVT::v8i16:
16730 case MVT::v4i32:
16731 case MVT::v2i64:
16732 case MVT::v4f32:
16733 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016734 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016735 // AVX types.
16736 case MVT::v32i8:
16737 case MVT::v16i16:
16738 case MVT::v8i32:
16739 case MVT::v4i64:
16740 case MVT::v8f32:
16741 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016742 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016743 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016744 break;
16745 }
16746 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016747
Chris Lattnerf76d1802006-07-31 23:26:50 +000016748 // Use the default implementation in TargetLowering to convert the register
16749 // constraint into a member of a register class.
16750 std::pair<unsigned, const TargetRegisterClass*> Res;
16751 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016752
16753 // Not found as a standard register?
16754 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016755 // Map st(0) -> st(7) -> ST0
16756 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16757 tolower(Constraint[1]) == 's' &&
16758 tolower(Constraint[2]) == 't' &&
16759 Constraint[3] == '(' &&
16760 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16761 Constraint[5] == ')' &&
16762 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016763
Chris Lattner56d77c72009-09-13 22:41:48 +000016764 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016765 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016766 return Res;
16767 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016768
Chris Lattner56d77c72009-09-13 22:41:48 +000016769 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016770 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016771 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016772 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016773 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016774 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016775
16776 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016777 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016778 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016779 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016780 return Res;
16781 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016782
Dale Johannesen330169f2008-11-13 21:52:36 +000016783 // 'A' means EAX + EDX.
16784 if (Constraint == "A") {
16785 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016786 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016787 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016788 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016789 return Res;
16790 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016791
Chris Lattnerf76d1802006-07-31 23:26:50 +000016792 // Otherwise, check to see if this is a register class of the wrong value
16793 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16794 // turn into {ax},{dx}.
16795 if (Res.second->hasType(VT))
16796 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016797
Chris Lattnerf76d1802006-07-31 23:26:50 +000016798 // All of the single-register GCC register classes map their values onto
16799 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16800 // really want an 8-bit or 32-bit register, map to the appropriate register
16801 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016802 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016803 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016804 unsigned DestReg = 0;
16805 switch (Res.first) {
16806 default: break;
16807 case X86::AX: DestReg = X86::AL; break;
16808 case X86::DX: DestReg = X86::DL; break;
16809 case X86::CX: DestReg = X86::CL; break;
16810 case X86::BX: DestReg = X86::BL; break;
16811 }
16812 if (DestReg) {
16813 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016814 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016815 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016816 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016817 unsigned DestReg = 0;
16818 switch (Res.first) {
16819 default: break;
16820 case X86::AX: DestReg = X86::EAX; break;
16821 case X86::DX: DestReg = X86::EDX; break;
16822 case X86::CX: DestReg = X86::ECX; break;
16823 case X86::BX: DestReg = X86::EBX; break;
16824 case X86::SI: DestReg = X86::ESI; break;
16825 case X86::DI: DestReg = X86::EDI; break;
16826 case X86::BP: DestReg = X86::EBP; break;
16827 case X86::SP: DestReg = X86::ESP; break;
16828 }
16829 if (DestReg) {
16830 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016831 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016832 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016833 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016834 unsigned DestReg = 0;
16835 switch (Res.first) {
16836 default: break;
16837 case X86::AX: DestReg = X86::RAX; break;
16838 case X86::DX: DestReg = X86::RDX; break;
16839 case X86::CX: DestReg = X86::RCX; break;
16840 case X86::BX: DestReg = X86::RBX; break;
16841 case X86::SI: DestReg = X86::RSI; break;
16842 case X86::DI: DestReg = X86::RDI; break;
16843 case X86::BP: DestReg = X86::RBP; break;
16844 case X86::SP: DestReg = X86::RSP; break;
16845 }
16846 if (DestReg) {
16847 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016848 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016849 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016850 }
Craig Topperc9099502012-04-20 06:31:50 +000016851 } else if (Res.second == &X86::FR32RegClass ||
16852 Res.second == &X86::FR64RegClass ||
16853 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016854 // Handle references to XMM physical registers that got mapped into the
16855 // wrong class. This can happen with constraints like {xmm0} where the
16856 // target independent register mapper will just pick the first match it can
16857 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016858
16859 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016860 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016861 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016862 Res.second = &X86::FR64RegClass;
16863 else if (X86::VR128RegClass.hasType(VT))
16864 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016865 else if (X86::VR256RegClass.hasType(VT))
16866 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016867 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016868
Chris Lattnerf76d1802006-07-31 23:26:50 +000016869 return Res;
16870}