blob: 78bc7fa75a68b84eeb8e43d884634759f5804128 [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);
742 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000749 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000751 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000758 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
765 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000768 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000769 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000773 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000774 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
775 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000776 setTruncStoreAction((MVT::SimpleValueType)VT,
777 (MVT::SimpleValueType)InnerVT, Expand);
778 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
779 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
780 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000781 }
782
Evan Chengc7ce29b2009-02-13 22:36:38 +0000783 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
784 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000785 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000786 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000787 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000788 }
789
Dale Johannesen0488fb62010-09-30 23:57:10 +0000790 // MMX-sized vectors (other than x86mmx) are expected to be expanded
791 // into smaller operations.
792 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
793 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
794 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
795 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
796 setOperationAction(ISD::AND, MVT::v8i8, Expand);
797 setOperationAction(ISD::AND, MVT::v4i16, Expand);
798 setOperationAction(ISD::AND, MVT::v2i32, Expand);
799 setOperationAction(ISD::AND, MVT::v1i64, Expand);
800 setOperationAction(ISD::OR, MVT::v8i8, Expand);
801 setOperationAction(ISD::OR, MVT::v4i16, Expand);
802 setOperationAction(ISD::OR, MVT::v2i32, Expand);
803 setOperationAction(ISD::OR, MVT::v1i64, Expand);
804 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
805 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
806 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
807 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
808 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
809 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
810 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
811 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
812 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
813 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
814 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
815 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
816 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000817 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
818 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
819 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
820 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000821
Craig Topper1accb7e2012-01-10 06:54:16 +0000822 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000823 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000824
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
826 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
827 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
828 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
829 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
830 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
831 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
832 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
833 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
834 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
835 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000836 }
837
Craig Topper1accb7e2012-01-10 06:54:16 +0000838 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000839 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000840
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000841 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
842 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
844 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
845 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
846 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000847
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
849 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
850 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
851 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
852 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
853 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
854 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
855 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
856 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
857 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
858 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
859 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
860 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
861 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
862 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
863 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864
Nadav Rotem354efd82011-09-18 14:57:03 +0000865 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000866 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
867 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
868 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000869
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
871 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
872 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
873 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
874 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000875
Evan Cheng2c3ae372006-04-12 21:21:57 +0000876 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000877 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000878 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000879 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000880 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000881 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000882 // Do not attempt to custom lower non-128-bit vectors
883 if (!VT.is128BitVector())
884 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000885 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
886 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
887 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000888 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000889
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
891 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
892 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
893 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
894 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
895 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000896
Nate Begemancdd1eec2008-02-12 22:51:28 +0000897 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
899 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000900 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000901
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000902 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000903 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000904 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000905
906 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000907 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000908 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000909
Craig Topper0d1f1762012-08-12 00:34:56 +0000910 setOperationAction(ISD::AND, VT, Promote);
911 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
912 setOperationAction(ISD::OR, VT, Promote);
913 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
914 setOperationAction(ISD::XOR, VT, Promote);
915 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
916 setOperationAction(ISD::LOAD, VT, Promote);
917 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
918 setOperationAction(ISD::SELECT, VT, Promote);
919 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000920 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000921
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000923
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
926 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
927 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
928 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000929
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
931 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000932 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000933
Craig Topperd0a31172012-01-10 06:37:29 +0000934 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000935 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
936 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
937 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
938 setOperationAction(ISD::FRINT, MVT::f32, Legal);
939 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
940 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
941 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
942 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
943 setOperationAction(ISD::FRINT, MVT::f64, Legal);
944 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
945
Nate Begeman14d12ca2008-02-11 04:19:36 +0000946 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000949 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
950 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
951 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
952 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
953 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000954
Nate Begeman14d12ca2008-02-11 04:19:36 +0000955 // i8 and i16 vectors are custom , because the source register and source
956 // source memory operand types are not the same width. f32 vectors are
957 // custom since the immediate controlling the insert encodes additional
958 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
960 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
961 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
962 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000963
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
965 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
966 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
967 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000968
Pete Coopera77214a2011-11-14 19:38:42 +0000969 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000970 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000971 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000972 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
973 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000974 }
975 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000976
Craig Topper1accb7e2012-01-10 06:54:16 +0000977 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000978 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000979 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000980
Nadav Rotem43012222011-05-11 08:12:09 +0000981 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000982 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000983
Nadav Rotem43012222011-05-11 08:12:09 +0000984 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000985 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000986
987 if (Subtarget->hasAVX2()) {
988 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
989 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
990
991 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
992 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
993
994 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
995 } else {
996 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
997 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
998
999 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1000 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1001
1002 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1003 }
Nadav Rotem43012222011-05-11 08:12:09 +00001004 }
1005
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001006 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001007 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1008 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1009 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1010 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1011 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1012 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001013
Owen Anderson825b72b2009-08-11 20:47:22 +00001014 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001015 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1016 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001017
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1019 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1020 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1021 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1022 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1023 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001024
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1026 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1027 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1028 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1029 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1030 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001031
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001032 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1033 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001034 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001035
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001036 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1037 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1038
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001039 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1040 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1041
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001042 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001043 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001044
Duncan Sands28b77e92011-09-06 19:07:46 +00001045 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1046 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1047 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1048 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001049
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001050 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1051 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1052 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1053
Craig Topperaaa643c2011-11-09 07:28:55 +00001054 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1055 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1056 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1057 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001058
Craig Topperbf404372012-08-31 15:40:30 +00001059 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001060 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1061 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1062 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1063 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1064 setOperationAction(ISD::FMA, MVT::f32, Custom);
1065 setOperationAction(ISD::FMA, MVT::f64, Custom);
1066 }
Craig Topper880ef452012-08-11 22:34:26 +00001067
Craig Topperaaa643c2011-11-09 07:28:55 +00001068 if (Subtarget->hasAVX2()) {
1069 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1070 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1071 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1072 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001073
Craig Topperaaa643c2011-11-09 07:28:55 +00001074 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1075 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1076 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1077 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001078
Craig Topperaaa643c2011-11-09 07:28:55 +00001079 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1080 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1081 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001082 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001083
1084 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001085
1086 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1087 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1088
1089 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1090 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1091
1092 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001093 } else {
1094 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1095 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1096 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1097 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1098
1099 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1100 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1101 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1102 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1103
1104 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1105 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1106 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1107 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001108
1109 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1110 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1111
1112 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1113 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1114
1115 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001116 }
Craig Topper13894fa2011-08-24 06:14:18 +00001117
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001118 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001119 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1120 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001121 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001122
1123 // Extract subvector is special because the value type
1124 // (result) is 128-bit but the source is 256-bit wide.
1125 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001126 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001127
1128 // Do not attempt to custom lower other non-256-bit vectors
1129 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001130 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001131
Craig Topper0d1f1762012-08-12 00:34:56 +00001132 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1133 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1134 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1135 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1136 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1137 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1138 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001139 }
1140
David Greene54d8eba2011-01-27 22:38:56 +00001141 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001142 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001143 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001144
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001145 // Do not attempt to promote non-256-bit vectors
1146 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001147 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001148
Craig Topper0d1f1762012-08-12 00:34:56 +00001149 setOperationAction(ISD::AND, VT, Promote);
1150 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1151 setOperationAction(ISD::OR, VT, Promote);
1152 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1153 setOperationAction(ISD::XOR, VT, Promote);
1154 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1155 setOperationAction(ISD::LOAD, VT, Promote);
1156 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1157 setOperationAction(ISD::SELECT, VT, Promote);
1158 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001159 }
David Greene9b9838d2009-06-29 16:47:10 +00001160 }
1161
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001162 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1163 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001164 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1165 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001166 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1167 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001168 }
1169
Evan Cheng6be2c582006-04-05 23:38:46 +00001170 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001172 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001173
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001174
Eli Friedman962f5492010-06-02 19:35:46 +00001175 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1176 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001177 //
Eli Friedman962f5492010-06-02 19:35:46 +00001178 // FIXME: We really should do custom legalization for addition and
1179 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1180 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001181 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1182 // Add/Sub/Mul with overflow operations are custom lowered.
1183 MVT VT = IntVTs[i];
1184 setOperationAction(ISD::SADDO, VT, Custom);
1185 setOperationAction(ISD::UADDO, VT, Custom);
1186 setOperationAction(ISD::SSUBO, VT, Custom);
1187 setOperationAction(ISD::USUBO, VT, Custom);
1188 setOperationAction(ISD::SMULO, VT, Custom);
1189 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001190 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001191
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001192 // There are no 8-bit 3-address imul/mul instructions
1193 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1194 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001195
Evan Chengd54f2d52009-03-31 19:38:51 +00001196 if (!Subtarget->is64Bit()) {
1197 // These libcalls are not available in 32-bit.
1198 setLibcallName(RTLIB::SHL_I128, 0);
1199 setLibcallName(RTLIB::SRL_I128, 0);
1200 setLibcallName(RTLIB::SRA_I128, 0);
1201 }
1202
Evan Cheng206ee9d2006-07-07 08:33:52 +00001203 // We have target-specific dag combine patterns for the following nodes:
1204 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001205 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001206 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001207 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001208 setTargetDAGCombine(ISD::SHL);
1209 setTargetDAGCombine(ISD::SRA);
1210 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001211 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001212 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001213 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001214 setTargetDAGCombine(ISD::FADD);
1215 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001216 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001217 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001218 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001219 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001220 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001221 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001222 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001223 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001224 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001225 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001226 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001227 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001228 if (Subtarget->is64Bit())
1229 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001230 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001231
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001232 computeRegisterProperties();
1233
Evan Cheng05219282011-01-06 06:52:41 +00001234 // On Darwin, -Os means optimize for size without hurting performance,
1235 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001236 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001237 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001238 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001239 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1240 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1241 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001242 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001243 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001244
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001245 // Predictable cmov don't hurt on atom because it's in-order.
1246 predictableSelectIsExpensive = !Subtarget->isAtom();
1247
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001248 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001249}
1250
Scott Michel5b8f82e2008-03-10 15:42:14 +00001251
Duncan Sands28b77e92011-09-06 19:07:46 +00001252EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1253 if (!VT.isVector()) return MVT::i8;
1254 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001255}
1256
1257
Evan Cheng29286502008-01-23 23:17:41 +00001258/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1259/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001260static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001261 if (MaxAlign == 16)
1262 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001263 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001264 if (VTy->getBitWidth() == 128)
1265 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001266 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001267 unsigned EltAlign = 0;
1268 getMaxByValAlign(ATy->getElementType(), EltAlign);
1269 if (EltAlign > MaxAlign)
1270 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001271 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001272 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1273 unsigned EltAlign = 0;
1274 getMaxByValAlign(STy->getElementType(i), EltAlign);
1275 if (EltAlign > MaxAlign)
1276 MaxAlign = EltAlign;
1277 if (MaxAlign == 16)
1278 break;
1279 }
1280 }
Evan Cheng29286502008-01-23 23:17:41 +00001281}
1282
1283/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1284/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001285/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1286/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001287unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001288 if (Subtarget->is64Bit()) {
1289 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001290 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001291 if (TyAlign > 8)
1292 return TyAlign;
1293 return 8;
1294 }
1295
Evan Cheng29286502008-01-23 23:17:41 +00001296 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001297 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001298 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001299 return Align;
1300}
Chris Lattner2b02a442007-02-25 08:29:00 +00001301
Evan Chengf0df0312008-05-15 08:39:06 +00001302/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001303/// and store operations as a result of memset, memcpy, and memmove
1304/// lowering. If DstAlign is zero that means it's safe to destination
1305/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1306/// means there isn't a need to check it against alignment requirement,
1307/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001308/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001309/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1310/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1311/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001312/// It returns EVT::Other if the type should be determined using generic
1313/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001314EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001315X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1316 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001317 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001318 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001319 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001320 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1321 // linux. This is because the stack realignment code can't handle certain
1322 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001323 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001324 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001325 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001326 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001327 (Subtarget->isUnalignedMemAccessFast() ||
1328 ((DstAlign == 0 || DstAlign >= 16) &&
1329 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001330 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001331 if (Subtarget->getStackAlignment() >= 32) {
1332 if (Subtarget->hasAVX2())
1333 return MVT::v8i32;
1334 if (Subtarget->hasAVX())
1335 return MVT::v8f32;
1336 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001337 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001338 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001339 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001340 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001341 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001342 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001343 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001344 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001345 // Do not use f64 to lower memcpy if source is string constant. It's
1346 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001347 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001348 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001349 }
Evan Chengf0df0312008-05-15 08:39:06 +00001350 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001351 return MVT::i64;
1352 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001353}
1354
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001355/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1356/// current function. The returned value is a member of the
1357/// MachineJumpTableInfo::JTEntryKind enum.
1358unsigned X86TargetLowering::getJumpTableEncoding() const {
1359 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1360 // symbol.
1361 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1362 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001363 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001364
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001365 // Otherwise, use the normal jump table encoding heuristics.
1366 return TargetLowering::getJumpTableEncoding();
1367}
1368
Chris Lattnerc64daab2010-01-26 05:02:42 +00001369const MCExpr *
1370X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1371 const MachineBasicBlock *MBB,
1372 unsigned uid,MCContext &Ctx) const{
1373 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1374 Subtarget->isPICStyleGOT());
1375 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1376 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001377 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1378 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001379}
1380
Evan Chengcc415862007-11-09 01:32:10 +00001381/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1382/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001383SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001384 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001385 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001386 // This doesn't have DebugLoc associated with it, but is not really the
1387 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001388 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001389 return Table;
1390}
1391
Chris Lattner589c6f62010-01-26 06:28:43 +00001392/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1393/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1394/// MCExpr.
1395const MCExpr *X86TargetLowering::
1396getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1397 MCContext &Ctx) const {
1398 // X86-64 uses RIP relative addressing based on the jump table label.
1399 if (Subtarget->isPICStyleRIPRel())
1400 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1401
1402 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001403 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001404}
1405
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001406// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001407std::pair<const TargetRegisterClass*, uint8_t>
1408X86TargetLowering::findRepresentativeClass(EVT VT) const{
1409 const TargetRegisterClass *RRC = 0;
1410 uint8_t Cost = 1;
1411 switch (VT.getSimpleVT().SimpleTy) {
1412 default:
1413 return TargetLowering::findRepresentativeClass(VT);
1414 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001415 RRC = Subtarget->is64Bit() ?
1416 (const TargetRegisterClass*)&X86::GR64RegClass :
1417 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001418 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001419 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001420 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001421 break;
1422 case MVT::f32: case MVT::f64:
1423 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1424 case MVT::v4f32: case MVT::v2f64:
1425 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1426 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001427 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001428 break;
1429 }
1430 return std::make_pair(RRC, Cost);
1431}
1432
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001433bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1434 unsigned &Offset) const {
1435 if (!Subtarget->isTargetLinux())
1436 return false;
1437
1438 if (Subtarget->is64Bit()) {
1439 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1440 Offset = 0x28;
1441 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1442 AddressSpace = 256;
1443 else
1444 AddressSpace = 257;
1445 } else {
1446 // %gs:0x14 on i386
1447 Offset = 0x14;
1448 AddressSpace = 256;
1449 }
1450 return true;
1451}
1452
1453
Chris Lattner2b02a442007-02-25 08:29:00 +00001454//===----------------------------------------------------------------------===//
1455// Return Value Calling Convention Implementation
1456//===----------------------------------------------------------------------===//
1457
Chris Lattner59ed56b2007-02-28 04:55:35 +00001458#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001459
Michael J. Spencerec38de22010-10-10 22:04:20 +00001460bool
Eric Christopher471e4222011-06-08 23:55:35 +00001461X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001462 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001463 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001464 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001465 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001466 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001467 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001468 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001469}
1470
Dan Gohman98ca4f22009-08-05 01:29:28 +00001471SDValue
1472X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001473 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001474 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001475 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001476 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001477 MachineFunction &MF = DAG.getMachineFunction();
1478 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001479
Chris Lattner9774c912007-02-27 05:28:59 +00001480 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001481 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 RVLocs, *DAG.getContext());
1483 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Evan Chengdcea1632010-02-04 02:40:39 +00001485 // Add the regs to the liveout set for the function.
1486 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1487 for (unsigned i = 0; i != RVLocs.size(); ++i)
1488 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1489 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
Dan Gohman475871a2008-07-27 21:46:04 +00001491 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001492
Dan Gohman475871a2008-07-27 21:46:04 +00001493 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001494 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1495 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001496 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1497 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001498
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001499 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001500 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1501 CCValAssign &VA = RVLocs[i];
1502 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001503 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001504 EVT ValVT = ValToCopy.getValueType();
1505
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001506 // Promote values to the appropriate types
1507 if (VA.getLocInfo() == CCValAssign::SExt)
1508 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1509 else if (VA.getLocInfo() == CCValAssign::ZExt)
1510 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1511 else if (VA.getLocInfo() == CCValAssign::AExt)
1512 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1513 else if (VA.getLocInfo() == CCValAssign::BCvt)
1514 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1515
Dale Johannesenc4510512010-09-24 19:05:48 +00001516 // If this is x86-64, and we disabled SSE, we can't return FP values,
1517 // or SSE or MMX vectors.
1518 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1519 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001520 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001521 report_fatal_error("SSE register return with SSE disabled");
1522 }
1523 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1524 // llvm-gcc has never done it right and no one has noticed, so this
1525 // should be OK for now.
1526 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001527 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001528 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Chris Lattner447ff682008-03-11 03:23:40 +00001530 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1531 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001532 if (VA.getLocReg() == X86::ST0 ||
1533 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001534 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1535 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001536 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001537 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001538 RetOps.push_back(ValToCopy);
1539 // Don't emit a copytoreg.
1540 continue;
1541 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001542
Evan Cheng242b38b2009-02-23 09:03:22 +00001543 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1544 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001545 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001546 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001547 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001548 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001549 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1550 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001551 // If we don't have SSE2 available, convert to v4f32 so the generated
1552 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001553 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001554 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001555 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001556 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001557 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001558
Dale Johannesendd64c412009-02-04 00:33:20 +00001559 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001560 Flag = Chain.getValue(1);
1561 }
Dan Gohman61a92132008-04-21 23:59:07 +00001562
1563 // The x86-64 ABI for returning structs by value requires that we copy
1564 // the sret argument into %rax for the return. We saved the argument into
1565 // a virtual register in the entry block, so now we copy the value out
1566 // and into %rax.
1567 if (Subtarget->is64Bit() &&
1568 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1569 MachineFunction &MF = DAG.getMachineFunction();
1570 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1571 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001572 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001573 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001574 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001575
Dale Johannesendd64c412009-02-04 00:33:20 +00001576 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001577 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001578
1579 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001580 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001581 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001582
Chris Lattner447ff682008-03-11 03:23:40 +00001583 RetOps[0] = Chain; // Update chain.
1584
1585 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001586 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001587 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001588
1589 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001590 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001591}
1592
Evan Chengbf010eb2012-04-10 01:51:00 +00001593bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001594 if (N->getNumValues() != 1)
1595 return false;
1596 if (!N->hasNUsesOfValue(1, 0))
1597 return false;
1598
Evan Chengbf010eb2012-04-10 01:51:00 +00001599 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001600 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001601 if (Copy->getOpcode() == ISD::CopyToReg) {
1602 // If the copy has a glue operand, we conservatively assume it isn't safe to
1603 // perform a tail call.
1604 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1605 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001606 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001607 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001608 return false;
1609
Evan Cheng1bf891a2010-12-01 22:59:46 +00001610 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001611 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001612 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001613 if (UI->getOpcode() != X86ISD::RET_FLAG)
1614 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001615 HasRet = true;
1616 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001617
Evan Chengbf010eb2012-04-10 01:51:00 +00001618 if (!HasRet)
1619 return false;
1620
1621 Chain = TCChain;
1622 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001623}
1624
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001625EVT
1626X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001627 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001628 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001629 // TODO: Is this also valid on 32-bit?
1630 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001631 ReturnMVT = MVT::i8;
1632 else
1633 ReturnMVT = MVT::i32;
1634
1635 EVT MinVT = getRegisterType(Context, ReturnMVT);
1636 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001637}
1638
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639/// LowerCallResult - Lower the result values of a call into the
1640/// appropriate copies out of appropriate physical registers.
1641///
1642SDValue
1643X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001644 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001645 const SmallVectorImpl<ISD::InputArg> &Ins,
1646 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001647 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001648
Chris Lattnere32bbf62007-02-28 07:09:55 +00001649 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001650 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001651 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001652 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001653 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001655
Chris Lattner3085e152007-02-25 08:59:22 +00001656 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001657 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001658 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001659 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Torok Edwin3f142c32009-02-01 18:15:56 +00001661 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001662 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001663 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001664 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001665 }
1666
Evan Cheng79fb3b42009-02-20 20:43:02 +00001667 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001668
1669 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001670 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001671 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001672 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001673 // instead.
1674 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1675 // If we prefer to use the value in xmm registers, copy it out as f80 and
1676 // use a truncate to move it from fp stack reg to xmm reg.
1677 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001678 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001679 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1680 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001681 Val = Chain.getValue(0);
1682
1683 // Round the f80 to the right size, which also moves it to the appropriate
1684 // xmm register.
1685 if (CopyVT != VA.getValVT())
1686 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1687 // This truncation won't change the value.
1688 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001689 } else {
1690 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1691 CopyVT, InFlag).getValue(1);
1692 Val = Chain.getValue(0);
1693 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001694 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001695 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001696 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001697
Dan Gohman98ca4f22009-08-05 01:29:28 +00001698 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001699}
1700
1701
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001702//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001703// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001704//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001705// StdCall calling convention seems to be standard for many Windows' API
1706// routines and around. It differs from C calling convention just a little:
1707// callee should clean up the stack, not caller. Symbols should be also
1708// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001709// For info on fast calling convention see Fast Calling Convention (tail call)
1710// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001711
Dan Gohman98ca4f22009-08-05 01:29:28 +00001712/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001713/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001714enum StructReturnType {
1715 NotStructReturn,
1716 RegStructReturn,
1717 StackStructReturn
1718};
1719static StructReturnType
1720callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001722 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001723
Rafael Espindola1cee7102012-07-25 13:41:10 +00001724 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1725 if (!Flags.isSRet())
1726 return NotStructReturn;
1727 if (Flags.isInReg())
1728 return RegStructReturn;
1729 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001730}
1731
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001732/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001733/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001734static StructReturnType
1735argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001737 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001738
Rafael Espindola1cee7102012-07-25 13:41:10 +00001739 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1740 if (!Flags.isSRet())
1741 return NotStructReturn;
1742 if (Flags.isInReg())
1743 return RegStructReturn;
1744 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001745}
1746
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001747/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1748/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001749/// the specific parameter attribute. The copy will be passed as a byval
1750/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001751static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001752CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001753 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1754 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001755 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001756
Dale Johannesendd64c412009-02-04 00:33:20 +00001757 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001758 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001759 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001760}
1761
Chris Lattner29689432010-03-11 00:22:57 +00001762/// IsTailCallConvention - Return true if the calling convention is one that
1763/// supports tail call optimization.
1764static bool IsTailCallConvention(CallingConv::ID CC) {
1765 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1766}
1767
Evan Cheng485fafc2011-03-21 01:19:09 +00001768bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001769 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001770 return false;
1771
1772 CallSite CS(CI);
1773 CallingConv::ID CalleeCC = CS.getCallingConv();
1774 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1775 return false;
1776
1777 return true;
1778}
1779
Evan Cheng0c439eb2010-01-27 00:07:07 +00001780/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1781/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001782static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1783 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001784 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001785}
1786
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787SDValue
1788X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001789 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790 const SmallVectorImpl<ISD::InputArg> &Ins,
1791 DebugLoc dl, SelectionDAG &DAG,
1792 const CCValAssign &VA,
1793 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001794 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001795 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001796 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001797 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1798 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001799 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001800 EVT ValVT;
1801
1802 // If value is passed by pointer we have address passed instead of the value
1803 // itself.
1804 if (VA.getLocInfo() == CCValAssign::Indirect)
1805 ValVT = VA.getLocVT();
1806 else
1807 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001808
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001809 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001810 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001811 // In case of tail call optimization mark all arguments mutable. Since they
1812 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001813 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001814 unsigned Bytes = Flags.getByValSize();
1815 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1816 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001817 return DAG.getFrameIndex(FI, getPointerTy());
1818 } else {
1819 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001820 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001821 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1822 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001823 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001824 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001825 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001826}
1827
Dan Gohman475871a2008-07-27 21:46:04 +00001828SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001829X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001830 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001831 bool isVarArg,
1832 const SmallVectorImpl<ISD::InputArg> &Ins,
1833 DebugLoc dl,
1834 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001835 SmallVectorImpl<SDValue> &InVals)
1836 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001837 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001838 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001839
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 const Function* Fn = MF.getFunction();
1841 if (Fn->hasExternalLinkage() &&
1842 Subtarget->isTargetCygMing() &&
1843 Fn->getName() == "main")
1844 FuncInfo->setForceFramePointer(true);
1845
Evan Cheng1bc78042006-04-26 01:20:17 +00001846 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001848 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001849 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001850
Chris Lattner29689432010-03-11 00:22:57 +00001851 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1852 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001853
Chris Lattner638402b2007-02-28 07:00:42 +00001854 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001855 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001856 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001858
1859 // Allocate shadow area for Win64
1860 if (IsWin64) {
1861 CCInfo.AllocateStack(32, 8);
1862 }
1863
Duncan Sands45907662010-10-31 13:21:44 +00001864 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001865
Chris Lattnerf39f7712007-02-28 05:46:49 +00001866 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001867 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001868 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1869 CCValAssign &VA = ArgLocs[i];
1870 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1871 // places.
1872 assert(VA.getValNo() != LastVal &&
1873 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001874 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001875 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001876
Chris Lattnerf39f7712007-02-28 05:46:49 +00001877 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001878 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001879 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001881 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001883 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001885 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001886 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001887 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001888 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001889 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001890 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001891 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001892 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001893 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001894 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001895 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001896
Devang Patel68e6bee2011-02-21 23:21:26 +00001897 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Chris Lattnerf39f7712007-02-28 05:46:49 +00001900 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1901 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1902 // right size.
1903 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001904 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001905 DAG.getValueType(VA.getValVT()));
1906 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001907 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001908 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001909 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001910 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001912 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001913 // Handle MMX values passed in XMM regs.
1914 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001915 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1916 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001917 } else
1918 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001919 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001920 } else {
1921 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001922 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001923 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001924
1925 // If value is passed via pointer - do a load.
1926 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001927 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001928 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001929
Dan Gohman98ca4f22009-08-05 01:29:28 +00001930 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001931 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001932
Dan Gohman61a92132008-04-21 23:59:07 +00001933 // The x86-64 ABI for returning structs by value requires that we copy
1934 // the sret argument into %rax for the return. Save the argument into
1935 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001936 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001937 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1938 unsigned Reg = FuncInfo->getSRetReturnReg();
1939 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001941 FuncInfo->setSRetReturnReg(Reg);
1942 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001945 }
1946
Chris Lattnerf39f7712007-02-28 05:46:49 +00001947 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001948 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001949 if (FuncIsMadeTailCallSafe(CallConv,
1950 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001951 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001952
Evan Cheng1bc78042006-04-26 01:20:17 +00001953 // If the function takes variable number of arguments, make a frame index for
1954 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001955 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001956 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1957 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001958 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001959 }
1960 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001961 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1962
1963 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001964 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001965 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001967 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001968 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1969 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001970 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001971 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1972 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1973 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001974 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001975 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001976
1977 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001978 // The XMM registers which might contain var arg parameters are shadowed
1979 // in their paired GPR. So we only need to save the GPR to their home
1980 // slots.
1981 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001982 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001983 } else {
1984 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1985 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001986
Chad Rosier30450e82011-12-22 22:35:21 +00001987 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1988 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001989 }
1990 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1991 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001992
Devang Patel578efa92009-06-05 21:57:13 +00001993 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00001994 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001995 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001996 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1997 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001998 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001999 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002000 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002001 // Kernel mode asks for SSE to be disabled, so don't push them
2002 // on the stack.
2003 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002004
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002005 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002006 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002007 // Get to the caller-allocated home save location. Add 8 to account
2008 // for the return address.
2009 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002010 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002011 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002012 // Fixup to set vararg frame on shadow area (4 x i64).
2013 if (NumIntRegs < 4)
2014 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002015 } else {
2016 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002017 // registers, then we must store them to their spots on the stack so
2018 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002019 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2020 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2021 FuncInfo->setRegSaveFrameIndex(
2022 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002023 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002024 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002025
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002028 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2029 getPointerTy());
2030 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002031 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002032 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2033 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002034 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002035 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002038 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002039 MachinePointerInfo::getFixedStack(
2040 FuncInfo->getRegSaveFrameIndex(), Offset),
2041 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002042 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002043 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002044 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002045
Dan Gohmanface41a2009-08-16 21:24:25 +00002046 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2047 // Now store the XMM (fp + vector) parameter registers.
2048 SmallVector<SDValue, 11> SaveXMMOps;
2049 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002050
Craig Topperc9099502012-04-20 06:31:50 +00002051 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002052 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2053 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002054
Dan Gohman1e93df62010-04-17 14:41:14 +00002055 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2056 FuncInfo->getRegSaveFrameIndex()));
2057 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2058 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002059
Dan Gohmanface41a2009-08-16 21:24:25 +00002060 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002061 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002062 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002063 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2064 SaveXMMOps.push_back(Val);
2065 }
2066 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2067 MVT::Other,
2068 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002069 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002070
2071 if (!MemOps.empty())
2072 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2073 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002074 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002075 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002078 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2079 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002080 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002081 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002082 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002083 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002084 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002085 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002086 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002087 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002088
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002090 // RegSaveFrameIndex is X86-64 only.
2091 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002092 if (CallConv == CallingConv::X86_FastCall ||
2093 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002094 // fastcc functions can't have varargs.
2095 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002096 }
Evan Cheng25caf632006-05-23 21:06:34 +00002097
Rafael Espindola76927d752011-08-30 19:39:58 +00002098 FuncInfo->setArgumentStackSize(StackSize);
2099
Dan Gohman98ca4f22009-08-05 01:29:28 +00002100 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002101}
2102
Dan Gohman475871a2008-07-27 21:46:04 +00002103SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002104X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2105 SDValue StackPtr, SDValue Arg,
2106 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002107 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002108 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002109 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002111 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002112 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002113 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002114
2115 return DAG.getStore(Chain, dl, Arg, PtrOff,
2116 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002117 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002118}
2119
Bill Wendling64e87322009-01-16 19:25:27 +00002120/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002121/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002122SDValue
2123X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002124 SDValue &OutRetAddr, SDValue Chain,
2125 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002126 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002127 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002128 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002129 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002130
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002131 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002132 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002133 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002134 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002135}
2136
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002137/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002138/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002139static SDValue
2140EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002141 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002142 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143 // Store the return address to the appropriate stack slot.
2144 if (!FPDiff) return Chain;
2145 // Calculate the new stack slot for the return address.
2146 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002147 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002148 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002150 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002151 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002152 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002153 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002154 return Chain;
2155}
2156
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002158X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002159 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002160 SelectionDAG &DAG = CLI.DAG;
2161 DebugLoc &dl = CLI.DL;
2162 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2163 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2164 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2165 SDValue Chain = CLI.Chain;
2166 SDValue Callee = CLI.Callee;
2167 CallingConv::ID CallConv = CLI.CallConv;
2168 bool &isTailCall = CLI.IsTailCall;
2169 bool isVarArg = CLI.IsVarArg;
2170
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171 MachineFunction &MF = DAG.getMachineFunction();
2172 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002173 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002174 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002175 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002176 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177
Nick Lewycky22de16d2012-01-19 00:34:10 +00002178 if (MF.getTarget().Options.DisableTailCalls)
2179 isTailCall = false;
2180
Evan Cheng5f941932010-02-05 02:21:12 +00002181 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002182 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002183 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002184 isVarArg, SR != NotStructReturn,
2185 MF.getFunction()->hasStructRetAttr(),
2186 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002187
2188 // Sibcalls are automatically detected tailcalls which do not require
2189 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002190 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002191 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002192
2193 if (isTailCall)
2194 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002195 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002196
Chris Lattner29689432010-03-11 00:22:57 +00002197 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2198 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002199
Chris Lattner638402b2007-02-28 07:00:42 +00002200 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002201 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002202 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002204
2205 // Allocate shadow area for Win64
2206 if (IsWin64) {
2207 CCInfo.AllocateStack(32, 8);
2208 }
2209
Duncan Sands45907662010-10-31 13:21:44 +00002210 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002211
Chris Lattner423c5f42007-02-28 05:31:48 +00002212 // Get a count of how many bytes are to be pushed on the stack.
2213 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002214 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002215 // This is a sibcall. The memory operands are available in caller's
2216 // own caller's stack.
2217 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002218 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2219 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002220 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002221
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002223 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002224 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002225 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2227 FPDiff = NumBytesCallerPushed - NumBytes;
2228
2229 // Set the delta of movement of the returnaddr stackslot.
2230 // But only set if delta is greater than previous delta.
2231 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2232 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2233 }
2234
Evan Chengf22f9b32010-02-06 03:28:46 +00002235 if (!IsSibcall)
2236 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002237
Dan Gohman475871a2008-07-27 21:46:04 +00002238 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002239 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002240 if (isTailCall && FPDiff)
2241 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2242 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002243
Dan Gohman475871a2008-07-27 21:46:04 +00002244 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2245 SmallVector<SDValue, 8> MemOpChains;
2246 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002247
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002248 // Walk the register/memloc assignments, inserting copies/loads. In the case
2249 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002250 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2251 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002252 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002253 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002254 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002255 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002256
Chris Lattner423c5f42007-02-28 05:31:48 +00002257 // Promote the value if needed.
2258 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002259 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002260 case CCValAssign::Full: break;
2261 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002262 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002263 break;
2264 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002265 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002266 break;
2267 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002268 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002269 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002270 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2272 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002273 } else
2274 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2275 break;
2276 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002277 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002278 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002279 case CCValAssign::Indirect: {
2280 // Store the argument.
2281 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002282 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002283 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002284 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002285 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002286 Arg = SpillSlot;
2287 break;
2288 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Chris Lattner423c5f42007-02-28 05:31:48 +00002291 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002292 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2293 if (isVarArg && IsWin64) {
2294 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2295 // shadow reg if callee is a varargs function.
2296 unsigned ShadowReg = 0;
2297 switch (VA.getLocReg()) {
2298 case X86::XMM0: ShadowReg = X86::RCX; break;
2299 case X86::XMM1: ShadowReg = X86::RDX; break;
2300 case X86::XMM2: ShadowReg = X86::R8; break;
2301 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002302 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002303 if (ShadowReg)
2304 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002305 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002306 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002307 assert(VA.isMemLoc());
2308 if (StackPtr.getNode() == 0)
2309 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2310 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2311 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002312 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002314
Evan Cheng32fe1032006-05-25 00:59:30 +00002315 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002317 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002318
Chris Lattner88e1fd52009-07-09 04:24:46 +00002319 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002320 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2321 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002322 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002323 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2324 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002325 } else {
2326 // If we are tail calling and generating PIC/GOT style code load the
2327 // address of the callee into ECX. The value in ecx is used as target of
2328 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2329 // for tail calls on PIC/GOT architectures. Normally we would just put the
2330 // address of GOT into ebx and then call target@PLT. But for tail calls
2331 // ebx would be restored (since ebx is callee saved) before jumping to the
2332 // target@PLT.
2333
2334 // Note: The actual moving to ECX is done further down.
2335 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2336 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2337 !G->getGlobal()->hasProtectedVisibility())
2338 Callee = LowerGlobalAddress(Callee, DAG);
2339 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002340 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002341 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002342 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002343
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002344 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 // From AMD64 ABI document:
2346 // For calls that may call functions that use varargs or stdargs
2347 // (prototype-less calls or calls to functions containing ellipsis (...) in
2348 // the declaration) %al is used as hidden argument to specify the number
2349 // of SSE registers used. The contents of %al do not need to match exactly
2350 // the number of registers, but must be an ubound on the number of SSE
2351 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002352
Gordon Henriksen86737662008-01-05 16:56:59 +00002353 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002354 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2356 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2357 };
2358 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002359 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002360 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002361
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002362 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2363 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002364 }
2365
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002366 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002367 if (isTailCall) {
2368 // Force all the incoming stack arguments to be loaded from the stack
2369 // before any new outgoing arguments are stored to the stack, because the
2370 // outgoing stack slots may alias the incoming argument stack slots, and
2371 // the alias isn't otherwise explicit. This is slightly more conservative
2372 // than necessary, because it means that each store effectively depends
2373 // on every argument instead of just those arguments it would clobber.
2374 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2375
Dan Gohman475871a2008-07-27 21:46:04 +00002376 SmallVector<SDValue, 8> MemOpChains2;
2377 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002379 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002380 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2381 CCValAssign &VA = ArgLocs[i];
2382 if (VA.isRegLoc())
2383 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002384 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002385 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002386 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002387 // Create frame index.
2388 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002389 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002390 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002391 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002392
Duncan Sands276dcbd2008-03-21 09:14:45 +00002393 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002394 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002395 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002396 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002397 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002398 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002399 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002400
Dan Gohman98ca4f22009-08-05 01:29:28 +00002401 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2402 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002403 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002404 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002405 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002406 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002407 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002408 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002409 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002410 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002411 }
2412 }
2413
2414 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002415 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002416 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002417
2418 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002419 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002420 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002421 }
2422
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002423 // Build a sequence of copy-to-reg nodes chained together with token chain
2424 // and flag operands which copy the outgoing args into registers.
2425 SDValue InFlag;
2426 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2427 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2428 RegsToPass[i].second, InFlag);
2429 InFlag = Chain.getValue(1);
2430 }
2431
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002432 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2433 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2434 // In the 64-bit large code model, we have to make all calls
2435 // through a register, since the call instruction's 32-bit
2436 // pc-relative offset may not be large enough to hold the whole
2437 // address.
2438 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002439 // If the callee is a GlobalAddress node (quite common, every direct call
2440 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2441 // it.
2442
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002443 // We should use extra load for direct calls to dllimported functions in
2444 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002445 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002446 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002447 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002448 bool ExtraLoad = false;
2449 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002450
Chris Lattner48a7d022009-07-09 05:02:21 +00002451 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2452 // external symbols most go through the PLT in PIC mode. If the symbol
2453 // has hidden or protected visibility, or if it is static or local, then
2454 // we don't need to use the PLT - we can directly call it.
2455 if (Subtarget->isTargetELF() &&
2456 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002457 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002458 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002459 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002460 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002461 (!Subtarget->getTargetTriple().isMacOSX() ||
2462 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002463 // PC-relative references to external symbols should go through $stub,
2464 // unless we're building with the leopard linker or later, which
2465 // automatically synthesizes these stubs.
2466 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002467 } else if (Subtarget->isPICStyleRIPRel() &&
2468 isa<Function>(GV) &&
2469 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2470 // If the function is marked as non-lazy, generate an indirect call
2471 // which loads from the GOT directly. This avoids runtime overhead
2472 // at the cost of eager binding (and one extra byte of encoding).
2473 OpFlags = X86II::MO_GOTPCREL;
2474 WrapperKind = X86ISD::WrapperRIP;
2475 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002476 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002477
Devang Patel0d881da2010-07-06 22:08:15 +00002478 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002479 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002480
2481 // Add a wrapper if needed.
2482 if (WrapperKind != ISD::DELETED_NODE)
2483 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2484 // Add extra indirection if needed.
2485 if (ExtraLoad)
2486 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2487 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002488 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002489 }
Bill Wendling056292f2008-09-16 21:48:12 +00002490 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002491 unsigned char OpFlags = 0;
2492
Evan Cheng1bf891a2010-12-01 22:59:46 +00002493 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2494 // external symbols should go through the PLT.
2495 if (Subtarget->isTargetELF() &&
2496 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2497 OpFlags = X86II::MO_PLT;
2498 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002499 (!Subtarget->getTargetTriple().isMacOSX() ||
2500 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002501 // PC-relative references to external symbols should go through $stub,
2502 // unless we're building with the leopard linker or later, which
2503 // automatically synthesizes these stubs.
2504 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002505 }
Eric Christopherfd179292009-08-27 18:07:15 +00002506
Chris Lattner48a7d022009-07-09 05:02:21 +00002507 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2508 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002509 }
2510
Chris Lattnerd96d0722007-02-25 06:40:16 +00002511 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002512 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002513 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002514
Evan Chengf22f9b32010-02-06 03:28:46 +00002515 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002516 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2517 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002518 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002519 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002520
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002521 Ops.push_back(Chain);
2522 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002523
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002525 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002526
Gordon Henriksen86737662008-01-05 16:56:59 +00002527 // Add argument registers to the end of the list so that they are known live
2528 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002529 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2530 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2531 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002532
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002533 // Add a register mask operand representing the call-preserved registers.
2534 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2535 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2536 assert(Mask && "Missing call preserved mask for calling convention");
2537 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002538
Gabor Greifba36cb52008-08-28 21:40:38 +00002539 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002540 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002541
Dan Gohman98ca4f22009-08-05 01:29:28 +00002542 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002543 // We used to do:
2544 //// If this is the first return lowered for this function, add the regs
2545 //// to the liveout set for the function.
2546 // This isn't right, although it's probably harmless on x86; liveouts
2547 // should be computed from returns not tail calls. Consider a void
2548 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002549 return DAG.getNode(X86ISD::TC_RETURN, dl,
2550 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002551 }
2552
Dale Johannesenace16102009-02-03 19:33:06 +00002553 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002554 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002555
Chris Lattner2d297092006-05-23 18:50:38 +00002556 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002557 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002558 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2559 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002560 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002561 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002562 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002563 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002564 // pops the hidden struct pointer, so we have to push it back.
2565 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002566 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002567 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002568 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002569 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002570
Gordon Henriksenae636f82008-01-03 16:47:34 +00002571 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002572 if (!IsSibcall) {
2573 Chain = DAG.getCALLSEQ_END(Chain,
2574 DAG.getIntPtrConstant(NumBytes, true),
2575 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2576 true),
2577 InFlag);
2578 InFlag = Chain.getValue(1);
2579 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002580
Chris Lattner3085e152007-02-25 08:59:22 +00002581 // Handle result values, copying them out of physregs into vregs that we
2582 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002583 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2584 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002585}
2586
Evan Cheng25ab6902006-09-08 06:48:29 +00002587
2588//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002589// Fast Calling Convention (tail call) implementation
2590//===----------------------------------------------------------------------===//
2591
2592// Like std call, callee cleans arguments, convention except that ECX is
2593// reserved for storing the tail called function address. Only 2 registers are
2594// free for argument passing (inreg). Tail call optimization is performed
2595// provided:
2596// * tailcallopt is enabled
2597// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002598// On X86_64 architecture with GOT-style position independent code only local
2599// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002600// To keep the stack aligned according to platform abi the function
2601// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2602// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002603// If a tail called function callee has more arguments than the caller the
2604// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002605// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002606// original REtADDR, but before the saved framepointer or the spilled registers
2607// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2608// stack layout:
2609// arg1
2610// arg2
2611// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002612// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002613// move area ]
2614// (possible EBP)
2615// ESI
2616// EDI
2617// local1 ..
2618
2619/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2620/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002621unsigned
2622X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2623 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002624 MachineFunction &MF = DAG.getMachineFunction();
2625 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002626 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002627 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002628 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002629 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002630 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002631 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2632 // Number smaller than 12 so just add the difference.
2633 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2634 } else {
2635 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002636 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002637 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002638 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002639 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002640}
2641
Evan Cheng5f941932010-02-05 02:21:12 +00002642/// MatchingStackOffset - Return true if the given stack call argument is
2643/// already available in the same position (relatively) of the caller's
2644/// incoming argument stack.
2645static
2646bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2647 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2648 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002649 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2650 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002651 if (Arg.getOpcode() == ISD::CopyFromReg) {
2652 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002653 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002654 return false;
2655 MachineInstr *Def = MRI->getVRegDef(VR);
2656 if (!Def)
2657 return false;
2658 if (!Flags.isByVal()) {
2659 if (!TII->isLoadFromStackSlot(Def, FI))
2660 return false;
2661 } else {
2662 unsigned Opcode = Def->getOpcode();
2663 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2664 Def->getOperand(1).isFI()) {
2665 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002666 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002667 } else
2668 return false;
2669 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002670 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2671 if (Flags.isByVal())
2672 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002673 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002674 // define @foo(%struct.X* %A) {
2675 // tail call @bar(%struct.X* byval %A)
2676 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002677 return false;
2678 SDValue Ptr = Ld->getBasePtr();
2679 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2680 if (!FINode)
2681 return false;
2682 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002683 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002684 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002685 FI = FINode->getIndex();
2686 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002687 } else
2688 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002689
Evan Cheng4cae1332010-03-05 08:38:04 +00002690 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002691 if (!MFI->isFixedObjectIndex(FI))
2692 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002693 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002694}
2695
Dan Gohman98ca4f22009-08-05 01:29:28 +00002696/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2697/// for tail call optimization. Targets which want to do tail call
2698/// optimization should implement this function.
2699bool
2700X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002701 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002702 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002703 bool isCalleeStructRet,
2704 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002705 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002706 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002707 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002708 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002709 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002710 CalleeCC != CallingConv::C)
2711 return false;
2712
Evan Cheng7096ae42010-01-29 06:45:59 +00002713 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002714 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002715 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002716 CallingConv::ID CallerCC = CallerF->getCallingConv();
2717 bool CCMatch = CallerCC == CalleeCC;
2718
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002719 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002720 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002721 return true;
2722 return false;
2723 }
2724
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002725 // Look for obvious safe cases to perform tail call optimization that do not
2726 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002727
Evan Cheng2c12cb42010-03-26 16:26:03 +00002728 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2729 // emit a special epilogue.
2730 if (RegInfo->needsStackRealignment(MF))
2731 return false;
2732
Evan Chenga375d472010-03-15 18:54:48 +00002733 // Also avoid sibcall optimization if either caller or callee uses struct
2734 // return semantics.
2735 if (isCalleeStructRet || isCallerStructRet)
2736 return false;
2737
Chad Rosier2416da32011-06-24 21:15:36 +00002738 // An stdcall caller is expected to clean up its arguments; the callee
2739 // isn't going to do that.
2740 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2741 return false;
2742
Chad Rosier871f6642011-05-18 19:59:50 +00002743 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002744 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002745 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002746
2747 // Optimizing for varargs on Win64 is unlikely to be safe without
2748 // additional testing.
2749 if (Subtarget->isTargetWin64())
2750 return false;
2751
Chad Rosier871f6642011-05-18 19:59:50 +00002752 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002753 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002754 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002755
Chad Rosier871f6642011-05-18 19:59:50 +00002756 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2757 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2758 if (!ArgLocs[i].isRegLoc())
2759 return false;
2760 }
2761
Chad Rosier30450e82011-12-22 22:35:21 +00002762 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2763 // stack. Therefore, if it's not used by the call it is not safe to optimize
2764 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002765 bool Unused = false;
2766 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2767 if (!Ins[i].Used) {
2768 Unused = true;
2769 break;
2770 }
2771 }
2772 if (Unused) {
2773 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002774 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002775 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002776 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002777 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002778 CCValAssign &VA = RVLocs[i];
2779 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2780 return false;
2781 }
2782 }
2783
Evan Cheng13617962010-04-30 01:12:32 +00002784 // If the calling conventions do not match, then we'd better make sure the
2785 // results are returned in the same way as what the caller expects.
2786 if (!CCMatch) {
2787 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002788 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002789 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002790 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2791
2792 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002793 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002794 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002795 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2796
2797 if (RVLocs1.size() != RVLocs2.size())
2798 return false;
2799 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2800 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2801 return false;
2802 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2803 return false;
2804 if (RVLocs1[i].isRegLoc()) {
2805 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2806 return false;
2807 } else {
2808 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2809 return false;
2810 }
2811 }
2812 }
2813
Evan Chenga6bff982010-01-30 01:22:00 +00002814 // If the callee takes no arguments then go on to check the results of the
2815 // call.
2816 if (!Outs.empty()) {
2817 // Check if stack adjustment is needed. For now, do not do this if any
2818 // argument is passed on the stack.
2819 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002820 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002821 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002822
2823 // Allocate shadow area for Win64
2824 if (Subtarget->isTargetWin64()) {
2825 CCInfo.AllocateStack(32, 8);
2826 }
2827
Duncan Sands45907662010-10-31 13:21:44 +00002828 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002829 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002830 MachineFunction &MF = DAG.getMachineFunction();
2831 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2832 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002833
2834 // Check if the arguments are already laid out in the right way as
2835 // the caller's fixed stack objects.
2836 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002837 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2838 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002839 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002840 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2841 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002842 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002843 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002844 if (VA.getLocInfo() == CCValAssign::Indirect)
2845 return false;
2846 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002847 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2848 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002849 return false;
2850 }
2851 }
2852 }
Evan Cheng9c044672010-05-29 01:35:22 +00002853
2854 // If the tailcall address may be in a register, then make sure it's
2855 // possible to register allocate for it. In 32-bit, the call address can
2856 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002857 // callee-saved registers are restored. These happen to be the same
2858 // registers used to pass 'inreg' arguments so watch out for those.
2859 if (!Subtarget->is64Bit() &&
2860 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002861 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002862 unsigned NumInRegs = 0;
2863 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2864 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002865 if (!VA.isRegLoc())
2866 continue;
2867 unsigned Reg = VA.getLocReg();
2868 switch (Reg) {
2869 default: break;
2870 case X86::EAX: case X86::EDX: case X86::ECX:
2871 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002872 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002873 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002874 }
2875 }
2876 }
Evan Chenga6bff982010-01-30 01:22:00 +00002877 }
Evan Chengb1712452010-01-27 06:25:16 +00002878
Evan Cheng86809cc2010-02-03 03:28:02 +00002879 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002880}
2881
Dan Gohman3df24e62008-09-03 23:12:08 +00002882FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002883X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2884 const TargetLibraryInfo *libInfo) const {
2885 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002886}
2887
2888
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002889//===----------------------------------------------------------------------===//
2890// Other Lowering Hooks
2891//===----------------------------------------------------------------------===//
2892
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002893static bool MayFoldLoad(SDValue Op) {
2894 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2895}
2896
2897static bool MayFoldIntoStore(SDValue Op) {
2898 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2899}
2900
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002901static bool isTargetShuffle(unsigned Opcode) {
2902 switch(Opcode) {
2903 default: return false;
2904 case X86ISD::PSHUFD:
2905 case X86ISD::PSHUFHW:
2906 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002907 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002908 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002909 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002910 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002911 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002912 case X86ISD::MOVLPS:
2913 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002914 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002915 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002916 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002917 case X86ISD::MOVSS:
2918 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002919 case X86ISD::UNPCKL:
2920 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002921 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002922 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002923 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002924 return true;
2925 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002926}
2927
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002928static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002929 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002930 switch(Opc) {
2931 default: llvm_unreachable("Unknown x86 shuffle node");
2932 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002933 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002934 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002935 return DAG.getNode(Opc, dl, VT, V1);
2936 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002937}
2938
2939static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002940 SDValue V1, unsigned TargetMask,
2941 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002942 switch(Opc) {
2943 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002944 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002945 case X86ISD::PSHUFHW:
2946 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002947 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002948 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002949 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2950 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002951}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002952
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002953static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002954 SDValue V1, SDValue V2, unsigned TargetMask,
2955 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002956 switch(Opc) {
2957 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002958 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002959 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002960 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002961 return DAG.getNode(Opc, dl, VT, V1, V2,
2962 DAG.getConstant(TargetMask, MVT::i8));
2963 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002964}
2965
2966static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2967 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2968 switch(Opc) {
2969 default: llvm_unreachable("Unknown x86 shuffle node");
2970 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002971 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002972 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002973 case X86ISD::MOVLPS:
2974 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002975 case X86ISD::MOVSS:
2976 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002977 case X86ISD::UNPCKL:
2978 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002979 return DAG.getNode(Opc, dl, VT, V1, V2);
2980 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002981}
2982
Dan Gohmand858e902010-04-17 15:26:15 +00002983SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002984 MachineFunction &MF = DAG.getMachineFunction();
2985 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2986 int ReturnAddrIndex = FuncInfo->getRAIndex();
2987
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002988 if (ReturnAddrIndex == 0) {
2989 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002990 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002991 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002992 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002993 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002994 }
2995
Evan Cheng25ab6902006-09-08 06:48:29 +00002996 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002997}
2998
2999
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003000bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3001 bool hasSymbolicDisplacement) {
3002 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003003 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003004 return false;
3005
3006 // If we don't have a symbolic displacement - we don't have any extra
3007 // restrictions.
3008 if (!hasSymbolicDisplacement)
3009 return true;
3010
3011 // FIXME: Some tweaks might be needed for medium code model.
3012 if (M != CodeModel::Small && M != CodeModel::Kernel)
3013 return false;
3014
3015 // For small code model we assume that latest object is 16MB before end of 31
3016 // bits boundary. We may also accept pretty large negative constants knowing
3017 // that all objects are in the positive half of address space.
3018 if (M == CodeModel::Small && Offset < 16*1024*1024)
3019 return true;
3020
3021 // For kernel code model we know that all object resist in the negative half
3022 // of 32bits address space. We may not accept negative offsets, since they may
3023 // be just off and we may accept pretty large positive ones.
3024 if (M == CodeModel::Kernel && Offset > 0)
3025 return true;
3026
3027 return false;
3028}
3029
Evan Chengef41ff62011-06-23 17:54:54 +00003030/// isCalleePop - Determines whether the callee is required to pop its
3031/// own arguments. Callee pop is necessary to support tail calls.
3032bool X86::isCalleePop(CallingConv::ID CallingConv,
3033 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3034 if (IsVarArg)
3035 return false;
3036
3037 switch (CallingConv) {
3038 default:
3039 return false;
3040 case CallingConv::X86_StdCall:
3041 return !is64Bit;
3042 case CallingConv::X86_FastCall:
3043 return !is64Bit;
3044 case CallingConv::X86_ThisCall:
3045 return !is64Bit;
3046 case CallingConv::Fast:
3047 return TailCallOpt;
3048 case CallingConv::GHC:
3049 return TailCallOpt;
3050 }
3051}
3052
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003053/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3054/// specific condition code, returning the condition code and the LHS/RHS of the
3055/// comparison to make.
3056static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3057 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003058 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003059 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3060 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3061 // X > -1 -> X == 0, jump !sign.
3062 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003064 }
3065 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003066 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003067 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003068 }
3069 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003070 // X < 1 -> X <= 0
3071 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003072 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003073 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003074 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003075
Evan Chengd9558e02006-01-06 00:43:03 +00003076 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003077 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003078 case ISD::SETEQ: return X86::COND_E;
3079 case ISD::SETGT: return X86::COND_G;
3080 case ISD::SETGE: return X86::COND_GE;
3081 case ISD::SETLT: return X86::COND_L;
3082 case ISD::SETLE: return X86::COND_LE;
3083 case ISD::SETNE: return X86::COND_NE;
3084 case ISD::SETULT: return X86::COND_B;
3085 case ISD::SETUGT: return X86::COND_A;
3086 case ISD::SETULE: return X86::COND_BE;
3087 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003088 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003089 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003090
Chris Lattner4c78e022008-12-23 23:42:27 +00003091 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003092
Chris Lattner4c78e022008-12-23 23:42:27 +00003093 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003094 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3095 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003096 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3097 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003098 }
3099
Chris Lattner4c78e022008-12-23 23:42:27 +00003100 switch (SetCCOpcode) {
3101 default: break;
3102 case ISD::SETOLT:
3103 case ISD::SETOLE:
3104 case ISD::SETUGT:
3105 case ISD::SETUGE:
3106 std::swap(LHS, RHS);
3107 break;
3108 }
3109
3110 // On a floating point condition, the flags are set as follows:
3111 // ZF PF CF op
3112 // 0 | 0 | 0 | X > Y
3113 // 0 | 0 | 1 | X < Y
3114 // 1 | 0 | 0 | X == Y
3115 // 1 | 1 | 1 | unordered
3116 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003117 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003118 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003119 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003120 case ISD::SETOLT: // flipped
3121 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003122 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003123 case ISD::SETOLE: // flipped
3124 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003125 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003126 case ISD::SETUGT: // flipped
3127 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003128 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003129 case ISD::SETUGE: // flipped
3130 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003131 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003132 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003133 case ISD::SETNE: return X86::COND_NE;
3134 case ISD::SETUO: return X86::COND_P;
3135 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003136 case ISD::SETOEQ:
3137 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003138 }
Evan Chengd9558e02006-01-06 00:43:03 +00003139}
3140
Evan Cheng4a460802006-01-11 00:33:36 +00003141/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3142/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003143/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003144static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003145 switch (X86CC) {
3146 default:
3147 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003148 case X86::COND_B:
3149 case X86::COND_BE:
3150 case X86::COND_E:
3151 case X86::COND_P:
3152 case X86::COND_A:
3153 case X86::COND_AE:
3154 case X86::COND_NE:
3155 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003156 return true;
3157 }
3158}
3159
Evan Chengeb2f9692009-10-27 19:56:55 +00003160/// isFPImmLegal - Returns true if the target can instruction select the
3161/// specified FP immediate natively. If false, the legalizer will
3162/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003163bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003164 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3165 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3166 return true;
3167 }
3168 return false;
3169}
3170
Nate Begeman9008ca62009-04-27 18:41:29 +00003171/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3172/// the specified range (L, H].
3173static bool isUndefOrInRange(int Val, int Low, int Hi) {
3174 return (Val < 0) || (Val >= Low && Val < Hi);
3175}
3176
3177/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3178/// specified value.
3179static bool isUndefOrEqual(int Val, int CmpVal) {
3180 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003181 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003183}
3184
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003185/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003186/// from position Pos and ending in Pos+Size, falls within the specified
3187/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003188static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003189 unsigned Pos, unsigned Size, int Low) {
3190 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003191 if (!isUndefOrEqual(Mask[i], Low))
3192 return false;
3193 return true;
3194}
3195
Nate Begeman9008ca62009-04-27 18:41:29 +00003196/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3197/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3198/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003199static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003200 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003202 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 return (Mask[0] < 2 && Mask[1] < 2);
3204 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003205}
3206
Nate Begeman9008ca62009-04-27 18:41:29 +00003207/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3208/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003209static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3210 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003211 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003212
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003214 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3215 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003216
Evan Cheng506d3df2006-03-29 23:07:14 +00003217 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003218 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003219 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003220 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003221
Craig Toppera9a568a2012-05-02 08:03:44 +00003222 if (VT == MVT::v16i16) {
3223 // Lower quadword copied in order or undef.
3224 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3225 return false;
3226
3227 // Upper quadword shuffled.
3228 for (unsigned i = 12; i != 16; ++i)
3229 if (!isUndefOrInRange(Mask[i], 12, 16))
3230 return false;
3231 }
3232
Evan Cheng506d3df2006-03-29 23:07:14 +00003233 return true;
3234}
3235
Nate Begeman9008ca62009-04-27 18:41:29 +00003236/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3237/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003238static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3239 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003240 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003241
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003243 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3244 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003245
Rafael Espindola15684b22009-04-24 12:40:33 +00003246 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003247 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003248 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003249 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003250
Craig Toppera9a568a2012-05-02 08:03:44 +00003251 if (VT == MVT::v16i16) {
3252 // Upper quadword copied in order.
3253 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3254 return false;
3255
3256 // Lower quadword shuffled.
3257 for (unsigned i = 8; i != 12; ++i)
3258 if (!isUndefOrInRange(Mask[i], 8, 12))
3259 return false;
3260 }
3261
Rafael Espindola15684b22009-04-24 12:40:33 +00003262 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003263}
3264
Nate Begemana09008b2009-10-19 02:17:23 +00003265/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3266/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003267static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3268 const X86Subtarget *Subtarget) {
3269 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3270 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003271 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003272
Craig Topper0e2037b2012-01-20 05:53:00 +00003273 unsigned NumElts = VT.getVectorNumElements();
3274 unsigned NumLanes = VT.getSizeInBits()/128;
3275 unsigned NumLaneElts = NumElts/NumLanes;
3276
3277 // Do not handle 64-bit element shuffles with palignr.
3278 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003279 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003280
Craig Topper0e2037b2012-01-20 05:53:00 +00003281 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3282 unsigned i;
3283 for (i = 0; i != NumLaneElts; ++i) {
3284 if (Mask[i+l] >= 0)
3285 break;
3286 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003287
Craig Topper0e2037b2012-01-20 05:53:00 +00003288 // Lane is all undef, go to next lane
3289 if (i == NumLaneElts)
3290 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003291
Craig Topper0e2037b2012-01-20 05:53:00 +00003292 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003293
Craig Topper0e2037b2012-01-20 05:53:00 +00003294 // Make sure its in this lane in one of the sources
3295 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3296 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003297 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003298
3299 // If not lane 0, then we must match lane 0
3300 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3301 return false;
3302
3303 // Correct second source to be contiguous with first source
3304 if (Start >= (int)NumElts)
3305 Start -= NumElts - NumLaneElts;
3306
3307 // Make sure we're shifting in the right direction.
3308 if (Start <= (int)(i+l))
3309 return false;
3310
3311 Start -= i;
3312
3313 // Check the rest of the elements to see if they are consecutive.
3314 for (++i; i != NumLaneElts; ++i) {
3315 int Idx = Mask[i+l];
3316
3317 // Make sure its in this lane
3318 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3319 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3320 return false;
3321
3322 // If not lane 0, then we must match lane 0
3323 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3324 return false;
3325
3326 if (Idx >= (int)NumElts)
3327 Idx -= NumElts - NumLaneElts;
3328
3329 if (!isUndefOrEqual(Idx, Start+i))
3330 return false;
3331
3332 }
Nate Begemana09008b2009-10-19 02:17:23 +00003333 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003334
Nate Begemana09008b2009-10-19 02:17:23 +00003335 return true;
3336}
3337
Craig Topper1a7700a2012-01-19 08:19:12 +00003338/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3339/// the two vector operands have swapped position.
3340static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3341 unsigned NumElems) {
3342 for (unsigned i = 0; i != NumElems; ++i) {
3343 int idx = Mask[i];
3344 if (idx < 0)
3345 continue;
3346 else if (idx < (int)NumElems)
3347 Mask[i] = idx + NumElems;
3348 else
3349 Mask[i] = idx - NumElems;
3350 }
3351}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003352
Craig Topper1a7700a2012-01-19 08:19:12 +00003353/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3354/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3355/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3356/// reverse of what x86 shuffles want.
3357static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3358 bool Commuted = false) {
3359 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003360 return false;
3361
Craig Topper1a7700a2012-01-19 08:19:12 +00003362 unsigned NumElems = VT.getVectorNumElements();
3363 unsigned NumLanes = VT.getSizeInBits()/128;
3364 unsigned NumLaneElems = NumElems/NumLanes;
3365
3366 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003367 return false;
3368
3369 // VSHUFPSY divides the resulting vector into 4 chunks.
3370 // The sources are also splitted into 4 chunks, and each destination
3371 // chunk must come from a different source chunk.
3372 //
3373 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3374 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3375 //
3376 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3377 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3378 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003379 // VSHUFPDY divides the resulting vector into 4 chunks.
3380 // The sources are also splitted into 4 chunks, and each destination
3381 // chunk must come from a different source chunk.
3382 //
3383 // SRC1 => X3 X2 X1 X0
3384 // SRC2 => Y3 Y2 Y1 Y0
3385 //
3386 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3387 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003388 unsigned HalfLaneElems = NumLaneElems/2;
3389 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3390 for (unsigned i = 0; i != NumLaneElems; ++i) {
3391 int Idx = Mask[i+l];
3392 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3393 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3394 return false;
3395 // For VSHUFPSY, the mask of the second half must be the same as the
3396 // first but with the appropriate offsets. This works in the same way as
3397 // VPERMILPS works with masks.
3398 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3399 continue;
3400 if (!isUndefOrEqual(Idx, Mask[i]+l))
3401 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003402 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003403 }
3404
3405 return true;
3406}
3407
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003408/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3409/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003410static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003411 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003412 return false;
3413
Craig Topper7a9a28b2012-08-12 02:23:29 +00003414 unsigned NumElems = VT.getVectorNumElements();
3415
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003416 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003417 return false;
3418
Evan Cheng2064a2b2006-03-28 06:50:32 +00003419 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003420 return isUndefOrEqual(Mask[0], 6) &&
3421 isUndefOrEqual(Mask[1], 7) &&
3422 isUndefOrEqual(Mask[2], 2) &&
3423 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003424}
3425
Nate Begeman0b10b912009-11-07 23:17:15 +00003426/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3427/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3428/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003429static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003430 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003431 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003432
Craig Topper7a9a28b2012-08-12 02:23:29 +00003433 unsigned NumElems = VT.getVectorNumElements();
3434
Nate Begeman0b10b912009-11-07 23:17:15 +00003435 if (NumElems != 4)
3436 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003437
Craig Topperdd637ae2012-02-19 05:41:45 +00003438 return isUndefOrEqual(Mask[0], 2) &&
3439 isUndefOrEqual(Mask[1], 3) &&
3440 isUndefOrEqual(Mask[2], 2) &&
3441 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003442}
3443
Evan Cheng5ced1d82006-04-06 23:23:56 +00003444/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3445/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003446static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003447 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003448 return false;
3449
Craig Topperdd637ae2012-02-19 05:41:45 +00003450 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003451
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452 if (NumElems != 2 && NumElems != 4)
3453 return false;
3454
Chad Rosier238ae312012-04-30 17:47:15 +00003455 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003456 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003457 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
Chad Rosier238ae312012-04-30 17:47:15 +00003459 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003460 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003461 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003462
3463 return true;
3464}
3465
Nate Begeman0b10b912009-11-07 23:17:15 +00003466/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3467/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003468static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003469 if (!VT.is128BitVector())
3470 return false;
3471
Craig Topperdd637ae2012-02-19 05:41:45 +00003472 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003473
Craig Topper7a9a28b2012-08-12 02:23:29 +00003474 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003475 return false;
3476
Chad Rosier238ae312012-04-30 17:47:15 +00003477 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003478 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003479 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480
Chad Rosier238ae312012-04-30 17:47:15 +00003481 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3482 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003483 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003484
3485 return true;
3486}
3487
Elena Demikhovsky15963732012-06-26 08:04:10 +00003488//
3489// Some special combinations that can be optimized.
3490//
3491static
3492SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3493 SelectionDAG &DAG) {
3494 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003495 DebugLoc dl = SVOp->getDebugLoc();
3496
3497 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3498 return SDValue();
3499
3500 ArrayRef<int> Mask = SVOp->getMask();
3501
3502 // These are the special masks that may be optimized.
3503 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3504 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3505 bool MatchEvenMask = true;
3506 bool MatchOddMask = true;
3507 for (int i=0; i<8; ++i) {
3508 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3509 MatchEvenMask = false;
3510 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3511 MatchOddMask = false;
3512 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003513
Elena Demikhovsky32510202012-09-04 12:49:02 +00003514 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003515 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003516
Elena Demikhovsky15963732012-06-26 08:04:10 +00003517 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3518
Elena Demikhovsky32510202012-09-04 12:49:02 +00003519 SDValue Op0 = SVOp->getOperand(0);
3520 SDValue Op1 = SVOp->getOperand(1);
3521
3522 if (MatchEvenMask) {
3523 // Shift the second operand right to 32 bits.
3524 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3525 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3526 } else {
3527 // Shift the first operand left to 32 bits.
3528 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3529 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3530 }
3531 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3532 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003533}
3534
Evan Cheng0038e592006-03-28 00:39:58 +00003535/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3536/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003537static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003538 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003539 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003540
3541 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3542 "Unsupported vector type for unpckh");
3543
Craig Topper6347e862011-11-21 06:57:39 +00003544 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003545 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003546 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003547
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003548 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3549 // independently on 128-bit lanes.
3550 unsigned NumLanes = VT.getSizeInBits()/128;
3551 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003552
Craig Topper94438ba2011-12-16 08:06:31 +00003553 for (unsigned l = 0; l != NumLanes; ++l) {
3554 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3555 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003556 i += 2, ++j) {
3557 int BitI = Mask[i];
3558 int BitI1 = Mask[i+1];
3559 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003560 return false;
David Greenea20244d2011-03-02 17:23:43 +00003561 if (V2IsSplat) {
3562 if (!isUndefOrEqual(BitI1, NumElts))
3563 return false;
3564 } else {
3565 if (!isUndefOrEqual(BitI1, j + NumElts))
3566 return false;
3567 }
Evan Cheng39623da2006-04-20 08:58:49 +00003568 }
Evan Cheng0038e592006-03-28 00:39:58 +00003569 }
David Greenea20244d2011-03-02 17:23:43 +00003570
Evan Cheng0038e592006-03-28 00:39:58 +00003571 return true;
3572}
3573
Evan Cheng4fcb9222006-03-28 02:43:26 +00003574/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3575/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003576static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003577 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003578 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003579
3580 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3581 "Unsupported vector type for unpckh");
3582
Craig Topper6347e862011-11-21 06:57:39 +00003583 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003584 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003585 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003586
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003587 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3588 // independently on 128-bit lanes.
3589 unsigned NumLanes = VT.getSizeInBits()/128;
3590 unsigned NumLaneElts = NumElts/NumLanes;
3591
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003592 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003593 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3594 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003595 int BitI = Mask[i];
3596 int BitI1 = Mask[i+1];
3597 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003598 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003599 if (V2IsSplat) {
3600 if (isUndefOrEqual(BitI1, NumElts))
3601 return false;
3602 } else {
3603 if (!isUndefOrEqual(BitI1, j+NumElts))
3604 return false;
3605 }
Evan Cheng39623da2006-04-20 08:58:49 +00003606 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003607 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003608 return true;
3609}
3610
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003611/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3612/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3613/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003614static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003615 bool HasAVX2) {
3616 unsigned NumElts = VT.getVectorNumElements();
3617
3618 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3619 "Unsupported vector type for unpckh");
3620
3621 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3622 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003623 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003624
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003625 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3626 // FIXME: Need a better way to get rid of this, there's no latency difference
3627 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3628 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003629 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003630 return false;
3631
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003632 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3633 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003634 unsigned NumLanes = VT.getSizeInBits()/128;
3635 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003636
Craig Topper94438ba2011-12-16 08:06:31 +00003637 for (unsigned l = 0; l != NumLanes; ++l) {
3638 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3639 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003640 i += 2, ++j) {
3641 int BitI = Mask[i];
3642 int BitI1 = Mask[i+1];
3643
3644 if (!isUndefOrEqual(BitI, j))
3645 return false;
3646 if (!isUndefOrEqual(BitI1, j))
3647 return false;
3648 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003649 }
David Greenea20244d2011-03-02 17:23:43 +00003650
Rafael Espindola15684b22009-04-24 12:40:33 +00003651 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003652}
3653
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003654/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3655/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3656/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003657static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003658 unsigned NumElts = VT.getVectorNumElements();
3659
3660 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3661 "Unsupported vector type for unpckh");
3662
3663 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3664 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003665 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003666
Craig Topper94438ba2011-12-16 08:06:31 +00003667 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3668 // independently on 128-bit lanes.
3669 unsigned NumLanes = VT.getSizeInBits()/128;
3670 unsigned NumLaneElts = NumElts/NumLanes;
3671
3672 for (unsigned l = 0; l != NumLanes; ++l) {
3673 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3674 i != (l+1)*NumLaneElts; i += 2, ++j) {
3675 int BitI = Mask[i];
3676 int BitI1 = Mask[i+1];
3677 if (!isUndefOrEqual(BitI, j))
3678 return false;
3679 if (!isUndefOrEqual(BitI1, j))
3680 return false;
3681 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003682 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003683 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003684}
3685
Evan Cheng017dcc62006-04-21 01:05:10 +00003686/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3687/// specifies a shuffle of elements that is suitable for input to MOVSS,
3688/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003689static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003690 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003691 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003692 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003693 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003694
Craig Topperc612d792012-01-02 09:17:37 +00003695 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003696
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003698 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003699
Craig Topperc612d792012-01-02 09:17:37 +00003700 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003702 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003703
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003704 return true;
3705}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003706
Craig Topper70b883b2011-11-28 10:14:51 +00003707/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003708/// as permutations between 128-bit chunks or halves. As an example: this
3709/// shuffle bellow:
3710/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3711/// The first half comes from the second half of V1 and the second half from the
3712/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003713static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003714 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003715 return false;
3716
3717 // The shuffle result is divided into half A and half B. In total the two
3718 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3719 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003720 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003721 bool MatchA = false, MatchB = false;
3722
3723 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003724 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003725 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3726 MatchA = true;
3727 break;
3728 }
3729 }
3730
3731 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003732 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003733 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3734 MatchB = true;
3735 break;
3736 }
3737 }
3738
3739 return MatchA && MatchB;
3740}
3741
Craig Topper70b883b2011-11-28 10:14:51 +00003742/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3743/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003744static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003745 EVT VT = SVOp->getValueType(0);
3746
Craig Topperc612d792012-01-02 09:17:37 +00003747 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003748
Craig Topperc612d792012-01-02 09:17:37 +00003749 unsigned FstHalf = 0, SndHalf = 0;
3750 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003751 if (SVOp->getMaskElt(i) > 0) {
3752 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3753 break;
3754 }
3755 }
Craig Topperc612d792012-01-02 09:17:37 +00003756 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003757 if (SVOp->getMaskElt(i) > 0) {
3758 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3759 break;
3760 }
3761 }
3762
3763 return (FstHalf | (SndHalf << 4));
3764}
3765
Craig Topper70b883b2011-11-28 10:14:51 +00003766/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003767/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3768/// Note that VPERMIL mask matching is different depending whether theunderlying
3769/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3770/// to the same elements of the low, but to the higher half of the source.
3771/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003772/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003773static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003774 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003775 return false;
3776
Craig Topperc612d792012-01-02 09:17:37 +00003777 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003778 // Only match 256-bit with 32/64-bit types
3779 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003780 return false;
3781
Craig Topperc612d792012-01-02 09:17:37 +00003782 unsigned NumLanes = VT.getSizeInBits()/128;
3783 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003784 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003785 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003786 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003787 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003788 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003789 continue;
3790 // VPERMILPS handling
3791 if (Mask[i] < 0)
3792 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003793 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003794 return false;
3795 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003796 }
3797
3798 return true;
3799}
3800
Craig Topper5aaffa82012-02-19 02:53:47 +00003801/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003802/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003803/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003804static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003805 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003806 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003807 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003808
3809 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003810 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003811 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003812
Nate Begeman9008ca62009-04-27 18:41:29 +00003813 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003814 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003815
Craig Topperc612d792012-01-02 09:17:37 +00003816 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3818 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3819 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003820 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003821
Evan Cheng39623da2006-04-20 08:58:49 +00003822 return true;
3823}
3824
Evan Chengd9539472006-04-14 21:59:03 +00003825/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3826/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003827/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003828static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003829 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003830 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003831 return false;
3832
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003833 unsigned NumElems = VT.getVectorNumElements();
3834
3835 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3836 (VT.getSizeInBits() == 256 && NumElems != 8))
3837 return false;
3838
3839 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003840 for (unsigned i = 0; i != NumElems; i += 2)
3841 if (!isUndefOrEqual(Mask[i], i+1) ||
3842 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003844
3845 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003846}
3847
3848/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3849/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003850/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003851static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003852 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003853 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003854 return false;
3855
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003856 unsigned NumElems = VT.getVectorNumElements();
3857
3858 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3859 (VT.getSizeInBits() == 256 && NumElems != 8))
3860 return false;
3861
3862 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003863 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003864 if (!isUndefOrEqual(Mask[i], i) ||
3865 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003867
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003868 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003869}
3870
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003871/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3872/// specifies a shuffle of elements that is suitable for input to 256-bit
3873/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003874static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003875 if (!HasAVX || !VT.is256BitVector())
3876 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003877
Craig Topper7a9a28b2012-08-12 02:23:29 +00003878 unsigned NumElts = VT.getVectorNumElements();
3879 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003880 return false;
3881
Craig Topperc612d792012-01-02 09:17:37 +00003882 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003883 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003884 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003885 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003886 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003887 return false;
3888 return true;
3889}
3890
Evan Cheng0b457f02008-09-25 20:50:48 +00003891/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003892/// specifies a shuffle of elements that is suitable for input to 128-bit
3893/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003894static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003895 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003896 return false;
3897
Craig Topperc612d792012-01-02 09:17:37 +00003898 unsigned e = VT.getVectorNumElements() / 2;
3899 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003900 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003901 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003902 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003903 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003904 return false;
3905 return true;
3906}
3907
David Greenec38a03e2011-02-03 15:50:00 +00003908/// isVEXTRACTF128Index - Return true if the specified
3909/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3910/// suitable for input to VEXTRACTF128.
3911bool X86::isVEXTRACTF128Index(SDNode *N) {
3912 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3913 return false;
3914
3915 // The index should be aligned on a 128-bit boundary.
3916 uint64_t Index =
3917 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3918
3919 unsigned VL = N->getValueType(0).getVectorNumElements();
3920 unsigned VBits = N->getValueType(0).getSizeInBits();
3921 unsigned ElSize = VBits / VL;
3922 bool Result = (Index * ElSize) % 128 == 0;
3923
3924 return Result;
3925}
3926
David Greeneccacdc12011-02-04 16:08:29 +00003927/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3928/// operand specifies a subvector insert that is suitable for input to
3929/// VINSERTF128.
3930bool X86::isVINSERTF128Index(SDNode *N) {
3931 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3932 return false;
3933
3934 // The index should be aligned on a 128-bit boundary.
3935 uint64_t Index =
3936 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3937
3938 unsigned VL = N->getValueType(0).getVectorNumElements();
3939 unsigned VBits = N->getValueType(0).getSizeInBits();
3940 unsigned ElSize = VBits / VL;
3941 bool Result = (Index * ElSize) % 128 == 0;
3942
3943 return Result;
3944}
3945
Evan Cheng63d33002006-03-22 08:01:21 +00003946/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003947/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003948/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003949static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003950 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003951
Craig Topper1a7700a2012-01-19 08:19:12 +00003952 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3953 "Unsupported vector type for PSHUF/SHUFP");
3954
3955 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3956 // independently on 128-bit lanes.
3957 unsigned NumElts = VT.getVectorNumElements();
3958 unsigned NumLanes = VT.getSizeInBits()/128;
3959 unsigned NumLaneElts = NumElts/NumLanes;
3960
3961 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3962 "Only supports 2 or 4 elements per lane");
3963
3964 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003965 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003966 for (unsigned i = 0; i != NumElts; ++i) {
3967 int Elt = N->getMaskElt(i);
3968 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003969 Elt &= NumLaneElts - 1;
3970 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003971 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003972 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003973
Evan Cheng63d33002006-03-22 08:01:21 +00003974 return Mask;
3975}
3976
Evan Cheng506d3df2006-03-29 23:07:14 +00003977/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003978/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003979static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003980 EVT VT = N->getValueType(0);
3981
3982 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3983 "Unsupported vector type for PSHUFHW");
3984
3985 unsigned NumElts = VT.getVectorNumElements();
3986
Evan Cheng506d3df2006-03-29 23:07:14 +00003987 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00003988 for (unsigned l = 0; l != NumElts; l += 8) {
3989 // 8 nodes per lane, but we only care about the last 4.
3990 for (unsigned i = 0; i < 4; ++i) {
3991 int Elt = N->getMaskElt(l+i+4);
3992 if (Elt < 0) continue;
3993 Elt &= 0x3; // only 2-bits.
3994 Mask |= Elt << (i * 2);
3995 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003996 }
Craig Topper6b28d352012-05-03 07:12:59 +00003997
Evan Cheng506d3df2006-03-29 23:07:14 +00003998 return Mask;
3999}
4000
4001/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004002/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004003static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004004 EVT VT = N->getValueType(0);
4005
4006 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4007 "Unsupported vector type for PSHUFHW");
4008
4009 unsigned NumElts = VT.getVectorNumElements();
4010
Evan Cheng506d3df2006-03-29 23:07:14 +00004011 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004012 for (unsigned l = 0; l != NumElts; l += 8) {
4013 // 8 nodes per lane, but we only care about the first 4.
4014 for (unsigned i = 0; i < 4; ++i) {
4015 int Elt = N->getMaskElt(l+i);
4016 if (Elt < 0) continue;
4017 Elt &= 0x3; // only 2-bits
4018 Mask |= Elt << (i * 2);
4019 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004020 }
Craig Topper6b28d352012-05-03 07:12:59 +00004021
Evan Cheng506d3df2006-03-29 23:07:14 +00004022 return Mask;
4023}
4024
Nate Begemana09008b2009-10-19 02:17:23 +00004025/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4026/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004027static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4028 EVT VT = SVOp->getValueType(0);
4029 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004030
Craig Topper0e2037b2012-01-20 05:53:00 +00004031 unsigned NumElts = VT.getVectorNumElements();
4032 unsigned NumLanes = VT.getSizeInBits()/128;
4033 unsigned NumLaneElts = NumElts/NumLanes;
4034
4035 int Val = 0;
4036 unsigned i;
4037 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004038 Val = SVOp->getMaskElt(i);
4039 if (Val >= 0)
4040 break;
4041 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004042 if (Val >= (int)NumElts)
4043 Val -= NumElts - NumLaneElts;
4044
Eli Friedman63f8dde2011-07-25 21:36:45 +00004045 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004046 return (Val - i) * EltSize;
4047}
4048
David Greenec38a03e2011-02-03 15:50:00 +00004049/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4050/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4051/// instructions.
4052unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4053 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4054 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4055
4056 uint64_t Index =
4057 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4058
4059 EVT VecVT = N->getOperand(0).getValueType();
4060 EVT ElVT = VecVT.getVectorElementType();
4061
4062 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004063 return Index / NumElemsPerChunk;
4064}
4065
David Greeneccacdc12011-02-04 16:08:29 +00004066/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4067/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4068/// instructions.
4069unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4070 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4071 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4072
4073 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004074 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004075
4076 EVT VecVT = N->getValueType(0);
4077 EVT ElVT = VecVT.getVectorElementType();
4078
4079 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004080 return Index / NumElemsPerChunk;
4081}
4082
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004083/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4084/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4085/// Handles 256-bit.
4086static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4087 EVT VT = N->getValueType(0);
4088
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004089 unsigned NumElts = VT.getVectorNumElements();
4090
Craig Topper095c5282012-04-15 23:48:57 +00004091 assert((VT.is256BitVector() && NumElts == 4) &&
4092 "Unsupported vector type for VPERMQ/VPERMPD");
4093
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004094 unsigned Mask = 0;
4095 for (unsigned i = 0; i != NumElts; ++i) {
4096 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004097 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004098 continue;
4099 Mask |= Elt << (i*2);
4100 }
4101
4102 return Mask;
4103}
Evan Cheng37b73872009-07-30 08:33:02 +00004104/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4105/// constant +0.0.
4106bool X86::isZeroNode(SDValue Elt) {
4107 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004108 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004109 (isa<ConstantFPSDNode>(Elt) &&
4110 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4111}
4112
Nate Begeman9008ca62009-04-27 18:41:29 +00004113/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4114/// their permute mask.
4115static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4116 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004117 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004118 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004119 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004120
Nate Begeman5a5ca152009-04-29 05:20:52 +00004121 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004122 int Idx = SVOp->getMaskElt(i);
4123 if (Idx >= 0) {
4124 if (Idx < (int)NumElems)
4125 Idx += NumElems;
4126 else
4127 Idx -= NumElems;
4128 }
4129 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004130 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004131 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4132 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004133}
4134
Evan Cheng533a0aa2006-04-19 20:35:22 +00004135/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4136/// match movhlps. The lower half elements should come from upper half of
4137/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004138/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004139static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004140 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004141 return false;
4142 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004143 return false;
4144 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004145 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004146 return false;
4147 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004148 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004149 return false;
4150 return true;
4151}
4152
Evan Cheng5ced1d82006-04-06 23:23:56 +00004153/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004154/// is promoted to a vector. It also returns the LoadSDNode by reference if
4155/// required.
4156static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004157 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4158 return false;
4159 N = N->getOperand(0).getNode();
4160 if (!ISD::isNON_EXTLoad(N))
4161 return false;
4162 if (LD)
4163 *LD = cast<LoadSDNode>(N);
4164 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004165}
4166
Dan Gohman65fd6562011-11-03 21:49:52 +00004167// Test whether the given value is a vector value which will be legalized
4168// into a load.
4169static bool WillBeConstantPoolLoad(SDNode *N) {
4170 if (N->getOpcode() != ISD::BUILD_VECTOR)
4171 return false;
4172
4173 // Check for any non-constant elements.
4174 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4175 switch (N->getOperand(i).getNode()->getOpcode()) {
4176 case ISD::UNDEF:
4177 case ISD::ConstantFP:
4178 case ISD::Constant:
4179 break;
4180 default:
4181 return false;
4182 }
4183
4184 // Vectors of all-zeros and all-ones are materialized with special
4185 // instructions rather than being loaded.
4186 return !ISD::isBuildVectorAllZeros(N) &&
4187 !ISD::isBuildVectorAllOnes(N);
4188}
4189
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4191/// match movlp{s|d}. The lower half elements should come from lower half of
4192/// V1 (and in order), and the upper half elements should come from the upper
4193/// half of V2 (and in order). And since V1 will become the source of the
4194/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004195static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004196 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004197 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004198 return false;
4199
Evan Cheng466685d2006-10-09 20:57:25 +00004200 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004201 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004202 // Is V2 is a vector load, don't do this transformation. We will try to use
4203 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004204 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004205 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004206
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004207 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004208
Evan Cheng533a0aa2006-04-19 20:35:22 +00004209 if (NumElems != 2 && NumElems != 4)
4210 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004211 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004212 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004213 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004214 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004215 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004216 return false;
4217 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004218}
4219
Evan Cheng39623da2006-04-20 08:58:49 +00004220/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4221/// all the same.
4222static bool isSplatVector(SDNode *N) {
4223 if (N->getOpcode() != ISD::BUILD_VECTOR)
4224 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004225
Dan Gohman475871a2008-07-27 21:46:04 +00004226 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004227 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4228 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004229 return false;
4230 return true;
4231}
4232
Evan Cheng213d2cf2007-05-17 18:45:50 +00004233/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004234/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004235/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004236static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004237 SDValue V1 = N->getOperand(0);
4238 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004239 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4240 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004242 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004244 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4245 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004246 if (Opc != ISD::BUILD_VECTOR ||
4247 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 return false;
4249 } else if (Idx >= 0) {
4250 unsigned Opc = V1.getOpcode();
4251 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4252 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004253 if (Opc != ISD::BUILD_VECTOR ||
4254 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004255 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004256 }
4257 }
4258 return true;
4259}
4260
4261/// getZeroVector - Returns a vector of specified type with all zero elements.
4262///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004263static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004264 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004265 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004266 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004267
Dale Johannesen0488fb62010-09-30 23:57:10 +00004268 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004269 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004270 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004271 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004272 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004273 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4274 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4275 } else { // SSE1
4276 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4277 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4278 }
Craig Topper9d352402012-04-23 07:24:41 +00004279 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004280 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004281 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4282 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4283 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4284 } else {
4285 // 256-bit logic and arithmetic instructions in AVX are all
4286 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4287 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4288 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4289 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4290 }
Craig Topper9d352402012-04-23 07:24:41 +00004291 } else
4292 llvm_unreachable("Unexpected vector type");
4293
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004294 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004295}
4296
Chris Lattner8a594482007-11-25 00:24:49 +00004297/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004298/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4299/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4300/// Then bitcast to their original type, ensuring they get CSE'd.
4301static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4302 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004303 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004304 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004305
Owen Anderson825b72b2009-08-11 20:47:22 +00004306 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004307 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004308 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004309 if (HasAVX2) { // AVX2
4310 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4311 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4312 } else { // AVX
4313 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004314 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004315 }
Craig Topper9d352402012-04-23 07:24:41 +00004316 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004317 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004318 } else
4319 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004320
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004321 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004322}
4323
Evan Cheng39623da2006-04-20 08:58:49 +00004324/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4325/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004326static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004327 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004328 if (Mask[i] > (int)NumElems) {
4329 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004330 }
Evan Cheng39623da2006-04-20 08:58:49 +00004331 }
Evan Cheng39623da2006-04-20 08:58:49 +00004332}
4333
Evan Cheng017dcc62006-04-21 01:05:10 +00004334/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4335/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004336static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 SDValue V2) {
4338 unsigned NumElems = VT.getVectorNumElements();
4339 SmallVector<int, 8> Mask;
4340 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004341 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 Mask.push_back(i);
4343 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004344}
4345
Nate Begeman9008ca62009-04-27 18:41:29 +00004346/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004347static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 SDValue V2) {
4349 unsigned NumElems = VT.getVectorNumElements();
4350 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004351 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004352 Mask.push_back(i);
4353 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004354 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004356}
4357
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004358/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004359static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 SDValue V2) {
4361 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004363 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 Mask.push_back(i + Half);
4365 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004366 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004368}
4369
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004370// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004371// a generic shuffle instruction because the target has no such instructions.
4372// Generate shuffles which repeat i16 and i8 several times until they can be
4373// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004374static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004375 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004377 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004378
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 while (NumElems > 4) {
4380 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004381 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004383 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 EltNo -= NumElems/2;
4385 }
4386 NumElems >>= 1;
4387 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004388 return V;
4389}
Eric Christopherfd179292009-08-27 18:07:15 +00004390
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4392static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4393 EVT VT = V.getValueType();
4394 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004395 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004396
Craig Topper9d352402012-04-23 07:24:41 +00004397 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004398 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004400 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4401 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004402 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004403 // To use VPERMILPS to splat scalars, the second half of indicies must
4404 // refer to the higher part, which is a duplication of the lower one,
4405 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4407 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004408
4409 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4410 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4411 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004412 } else
4413 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414
4415 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4416}
4417
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004418/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004419static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4420 EVT SrcVT = SV->getValueType(0);
4421 SDValue V1 = SV->getOperand(0);
4422 DebugLoc dl = SV->getDebugLoc();
4423
4424 int EltNo = SV->getSplatIndex();
4425 int NumElems = SrcVT.getVectorNumElements();
4426 unsigned Size = SrcVT.getSizeInBits();
4427
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004428 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4429 "Unknown how to promote splat for type");
4430
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004431 // Extract the 128-bit part containing the splat element and update
4432 // the splat element index when it refers to the higher register.
4433 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004434 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4435 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 EltNo -= NumElems/2;
4437 }
4438
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004439 // All i16 and i8 vector types can't be used directly by a generic shuffle
4440 // instruction because the target has no such instruction. Generate shuffles
4441 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004442 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004443 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004444 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004445 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004446
4447 // Recreate the 256-bit vector and place the same 128-bit vector
4448 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004449 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004450 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004451 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452 }
4453
4454 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004455}
4456
Evan Chengba05f722006-04-21 23:03:30 +00004457/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004458/// vector of zero or undef vector. This produces a shuffle where the low
4459/// element of V2 is swizzled into the zero/undef vector, landing at element
4460/// 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 +00004461static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004462 bool IsZero,
4463 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004464 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004465 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004466 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004467 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 unsigned NumElems = VT.getVectorNumElements();
4469 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004470 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 // If this is the insertion idx, put the low elt of V2 here.
4472 MaskVec.push_back(i == Idx ? NumElems : i);
4473 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004474}
4475
Craig Toppera1ffc682012-03-20 06:42:26 +00004476/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4477/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004478/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004479static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004480 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004481 unsigned NumElems = VT.getVectorNumElements();
4482 SDValue ImmN;
4483
Craig Topper89f4e662012-03-20 07:17:59 +00004484 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004485 switch(N->getOpcode()) {
4486 case X86ISD::SHUFP:
4487 ImmN = N->getOperand(N->getNumOperands()-1);
4488 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4489 break;
4490 case X86ISD::UNPCKH:
4491 DecodeUNPCKHMask(VT, Mask);
4492 break;
4493 case X86ISD::UNPCKL:
4494 DecodeUNPCKLMask(VT, Mask);
4495 break;
4496 case X86ISD::MOVHLPS:
4497 DecodeMOVHLPSMask(NumElems, Mask);
4498 break;
4499 case X86ISD::MOVLHPS:
4500 DecodeMOVLHPSMask(NumElems, Mask);
4501 break;
4502 case X86ISD::PSHUFD:
4503 case X86ISD::VPERMILP:
4504 ImmN = N->getOperand(N->getNumOperands()-1);
4505 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004506 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004507 break;
4508 case X86ISD::PSHUFHW:
4509 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004510 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004511 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004512 break;
4513 case X86ISD::PSHUFLW:
4514 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004515 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004516 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004517 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004518 case X86ISD::VPERMI:
4519 ImmN = N->getOperand(N->getNumOperands()-1);
4520 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4521 IsUnary = true;
4522 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004523 case X86ISD::MOVSS:
4524 case X86ISD::MOVSD: {
4525 // The index 0 always comes from the first element of the second source,
4526 // this is why MOVSS and MOVSD are used in the first place. The other
4527 // elements come from the other positions of the first source vector
4528 Mask.push_back(NumElems);
4529 for (unsigned i = 1; i != NumElems; ++i) {
4530 Mask.push_back(i);
4531 }
4532 break;
4533 }
4534 case X86ISD::VPERM2X128:
4535 ImmN = N->getOperand(N->getNumOperands()-1);
4536 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004537 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004538 break;
4539 case X86ISD::MOVDDUP:
4540 case X86ISD::MOVLHPD:
4541 case X86ISD::MOVLPD:
4542 case X86ISD::MOVLPS:
4543 case X86ISD::MOVSHDUP:
4544 case X86ISD::MOVSLDUP:
4545 case X86ISD::PALIGN:
4546 // Not yet implemented
4547 return false;
4548 default: llvm_unreachable("unknown target shuffle node");
4549 }
4550
4551 return true;
4552}
4553
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004554/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4555/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004556static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004557 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004558 if (Depth == 6)
4559 return SDValue(); // Limit search depth.
4560
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004561 SDValue V = SDValue(N, 0);
4562 EVT VT = V.getValueType();
4563 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004564
4565 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4566 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004567 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004568
Craig Topper3d092db2012-03-21 02:14:01 +00004569 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004570 return DAG.getUNDEF(VT.getVectorElementType());
4571
Craig Topperd156dc12012-02-06 07:17:51 +00004572 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004573 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4574 : SV->getOperand(1);
4575 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004576 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004577
4578 // Recurse into target specific vector shuffles to find scalars.
4579 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004580 MVT ShufVT = V.getValueType().getSimpleVT();
4581 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004582 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004583 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004584 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004585
Craig Topperd978c542012-05-06 19:46:21 +00004586 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004587 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004588
Craig Topper3d092db2012-03-21 02:14:01 +00004589 int Elt = ShuffleMask[Index];
4590 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004591 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004592
Craig Topper3d092db2012-03-21 02:14:01 +00004593 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004594 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004595 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004596 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004597 }
4598
4599 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004600 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004601 V = V.getOperand(0);
4602 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004603 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004604
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004605 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004606 return SDValue();
4607 }
4608
4609 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4610 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004611 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004612
4613 if (V.getOpcode() == ISD::BUILD_VECTOR)
4614 return V.getOperand(Index);
4615
4616 return SDValue();
4617}
4618
4619/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4620/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004621/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004622static
Craig Topper3d092db2012-03-21 02:14:01 +00004623unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004624 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004625 unsigned i;
4626 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004628 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 if (!(Elt.getNode() &&
4630 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4631 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004632 }
4633
4634 return i;
4635}
4636
Craig Topper3d092db2012-03-21 02:14:01 +00004637/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4638/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4640static
Craig Topper3d092db2012-03-21 02:14:01 +00004641bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4642 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4643 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004644 bool SeenV1 = false;
4645 bool SeenV2 = false;
4646
Craig Topper3d092db2012-03-21 02:14:01 +00004647 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 int Idx = SVOp->getMaskElt(i);
4649 // Ignore undef indicies
4650 if (Idx < 0)
4651 continue;
4652
Craig Topper3d092db2012-03-21 02:14:01 +00004653 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654 SeenV1 = true;
4655 else
4656 SeenV2 = true;
4657
4658 // Only accept consecutive elements from the same vector
4659 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4660 return false;
4661 }
4662
4663 OpNum = SeenV1 ? 0 : 1;
4664 return true;
4665}
4666
4667/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4668/// logical left shift of a vector.
4669static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4670 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4671 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4672 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4673 false /* check zeros from right */, DAG);
4674 unsigned OpSrc;
4675
4676 if (!NumZeros)
4677 return false;
4678
4679 // Considering the elements in the mask that are not consecutive zeros,
4680 // check if they consecutively come from only one of the source vectors.
4681 //
4682 // V1 = {X, A, B, C} 0
4683 // \ \ \ /
4684 // vector_shuffle V1, V2 <1, 2, 3, X>
4685 //
4686 if (!isShuffleMaskConsecutive(SVOp,
4687 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004688 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004689 NumZeros, // Where to start looking in the src vector
4690 NumElems, // Number of elements in vector
4691 OpSrc)) // Which source operand ?
4692 return false;
4693
4694 isLeft = false;
4695 ShAmt = NumZeros;
4696 ShVal = SVOp->getOperand(OpSrc);
4697 return true;
4698}
4699
4700/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4701/// logical left shift of a vector.
4702static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4703 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4704 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4705 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4706 true /* check zeros from left */, DAG);
4707 unsigned OpSrc;
4708
4709 if (!NumZeros)
4710 return false;
4711
4712 // Considering the elements in the mask that are not consecutive zeros,
4713 // check if they consecutively come from only one of the source vectors.
4714 //
4715 // 0 { A, B, X, X } = V2
4716 // / \ / /
4717 // vector_shuffle V1, V2 <X, X, 4, 5>
4718 //
4719 if (!isShuffleMaskConsecutive(SVOp,
4720 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004721 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004722 0, // Where to start looking in the src vector
4723 NumElems, // Number of elements in vector
4724 OpSrc)) // Which source operand ?
4725 return false;
4726
4727 isLeft = true;
4728 ShAmt = NumZeros;
4729 ShVal = SVOp->getOperand(OpSrc);
4730 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004731}
4732
4733/// isVectorShift - Returns true if the shuffle can be implemented as a
4734/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004735static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004736 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004737 // Although the logic below support any bitwidth size, there are no
4738 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004739 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004740 return false;
4741
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004742 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4743 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4744 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004745
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004746 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004747}
4748
Evan Chengc78d3b42006-04-24 18:01:45 +00004749/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4750///
Dan Gohman475871a2008-07-27 21:46:04 +00004751static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004752 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004753 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004754 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004755 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004756 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004757 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004758
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004759 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004760 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004761 bool First = true;
4762 for (unsigned i = 0; i < 16; ++i) {
4763 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4764 if (ThisIsNonZero && First) {
4765 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004766 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004767 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004768 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004769 First = false;
4770 }
4771
4772 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004773 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004774 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4775 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004776 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004777 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004778 }
4779 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004780 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4781 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4782 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004783 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004785 } else
4786 ThisElt = LastElt;
4787
Gabor Greifba36cb52008-08-28 21:40:38 +00004788 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004790 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004791 }
4792 }
4793
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004794 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004795}
4796
Bill Wendlinga348c562007-03-22 18:42:45 +00004797/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004798///
Dan Gohman475871a2008-07-27 21:46:04 +00004799static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004800 unsigned NumNonZero, unsigned NumZero,
4801 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004802 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004803 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004804 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004805 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004806
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004807 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004808 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 bool First = true;
4810 for (unsigned i = 0; i < 8; ++i) {
4811 bool isNonZero = (NonZeros & (1 << i)) != 0;
4812 if (isNonZero) {
4813 if (First) {
4814 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004815 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004816 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004818 First = false;
4819 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004820 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004822 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 }
4824 }
4825
4826 return V;
4827}
4828
Evan Chengf26ffe92008-05-29 08:22:04 +00004829/// getVShift - Return a vector logical shift node.
4830///
Owen Andersone50ed302009-08-10 22:56:29 +00004831static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004832 unsigned NumBits, SelectionDAG &DAG,
4833 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004834 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004835 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004836 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004837 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4838 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004839 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004840 DAG.getConstant(NumBits,
4841 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004842}
4843
Dan Gohman475871a2008-07-27 21:46:04 +00004844SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004845X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004846 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004847
Evan Chengc3630942009-12-09 21:00:30 +00004848 // Check if the scalar load can be widened into a vector load. And if
4849 // the address is "base + cst" see if the cst can be "absorbed" into
4850 // the shuffle mask.
4851 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4852 SDValue Ptr = LD->getBasePtr();
4853 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4854 return SDValue();
4855 EVT PVT = LD->getValueType(0);
4856 if (PVT != MVT::i32 && PVT != MVT::f32)
4857 return SDValue();
4858
4859 int FI = -1;
4860 int64_t Offset = 0;
4861 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4862 FI = FINode->getIndex();
4863 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004864 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004865 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4866 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4867 Offset = Ptr.getConstantOperandVal(1);
4868 Ptr = Ptr.getOperand(0);
4869 } else {
4870 return SDValue();
4871 }
4872
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004873 // FIXME: 256-bit vector instructions don't require a strict alignment,
4874 // improve this code to support it better.
4875 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004876 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004877 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004878 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004879 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004880 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004881 // Can't change the alignment. FIXME: It's possible to compute
4882 // the exact stack offset and reference FI + adjust offset instead.
4883 // If someone *really* cares about this. That's the way to implement it.
4884 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004885 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004886 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004887 }
4888 }
4889
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004890 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004891 // Ptr + (Offset & ~15).
4892 if (Offset < 0)
4893 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004894 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004895 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004896 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004897 if (StartOffset)
4898 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4899 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4900
4901 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004902 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004903
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004904 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4905 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004906 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004907 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004908
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004909 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004910 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004911 Mask.push_back(EltNo);
4912
Craig Toppercc3000632012-01-30 07:50:31 +00004913 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004914 }
4915
4916 return SDValue();
4917}
4918
Michael J. Spencerec38de22010-10-10 22:04:20 +00004919/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4920/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004921/// load which has the same value as a build_vector whose operands are 'elts'.
4922///
4923/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004924///
Nate Begeman1449f292010-03-24 22:19:06 +00004925/// FIXME: we'd also like to handle the case where the last elements are zero
4926/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4927/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004928static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004929 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004930 EVT EltVT = VT.getVectorElementType();
4931 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004932
Nate Begemanfdea31a2010-03-24 20:49:50 +00004933 LoadSDNode *LDBase = NULL;
4934 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004935
Nate Begeman1449f292010-03-24 22:19:06 +00004936 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004937 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004938 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004939 for (unsigned i = 0; i < NumElems; ++i) {
4940 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004941
Nate Begemanfdea31a2010-03-24 20:49:50 +00004942 if (!Elt.getNode() ||
4943 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4944 return SDValue();
4945 if (!LDBase) {
4946 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4947 return SDValue();
4948 LDBase = cast<LoadSDNode>(Elt.getNode());
4949 LastLoadedElt = i;
4950 continue;
4951 }
4952 if (Elt.getOpcode() == ISD::UNDEF)
4953 continue;
4954
4955 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4956 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4957 return SDValue();
4958 LastLoadedElt = i;
4959 }
Nate Begeman1449f292010-03-24 22:19:06 +00004960
4961 // If we have found an entire vector of loads and undefs, then return a large
4962 // load of the entire vector width starting at the base pointer. If we found
4963 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004964 if (LastLoadedElt == NumElems - 1) {
4965 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004966 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004967 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004968 LDBase->isVolatile(), LDBase->isNonTemporal(),
4969 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004970 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004971 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004972 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004973 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004974 }
4975 if (NumElems == 4 && LastLoadedElt == 1 &&
4976 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004977 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4978 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004979 SDValue ResNode =
4980 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4981 LDBase->getPointerInfo(),
4982 LDBase->getAlignment(),
4983 false/*isVolatile*/, true/*ReadMem*/,
4984 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00004985
4986 // Make sure the newly-created LOAD is in the same position as LDBase in
4987 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
4988 // update uses of LDBase's output chain to use the TokenFactor.
4989 if (LDBase->hasAnyUseOfValue(1)) {
4990 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
4991 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
4992 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
4993 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
4994 SDValue(ResNode.getNode(), 1));
4995 }
4996
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004997 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004998 }
4999 return SDValue();
5000}
5001
Nadav Rotem9d68b062012-04-08 12:54:54 +00005002/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5003/// to generate a splat value for the following cases:
5004/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005005/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005006/// a scalar load, or a constant.
5007/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005008/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005009SDValue
5010X86TargetLowering::LowerVectorBroadcast(SDValue &Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005011 if (!Subtarget->hasAVX())
5012 return SDValue();
5013
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005014 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005015 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005016
Craig Topper5da8a802012-05-04 05:49:51 +00005017 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5018 "Unsupported vector type for broadcast.");
5019
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005020 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005021 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005022
Nadav Rotem9d68b062012-04-08 12:54:54 +00005023 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005024 default:
5025 // Unknown pattern found.
5026 return SDValue();
5027
5028 case ISD::BUILD_VECTOR: {
5029 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005030 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005031 return SDValue();
5032
Nadav Rotem9d68b062012-04-08 12:54:54 +00005033 Ld = Op.getOperand(0);
5034 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5035 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005036
5037 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005038 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005039 // Constants may have multiple users.
5040 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005041 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005042 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005043 }
5044
5045 case ISD::VECTOR_SHUFFLE: {
5046 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5047
5048 // Shuffles must have a splat mask where the first element is
5049 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005050 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005051 return SDValue();
5052
5053 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005054 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005055 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5056
5057 if (!Subtarget->hasAVX2())
5058 return SDValue();
5059
5060 // Use the register form of the broadcast instruction available on AVX2.
5061 if (VT.is256BitVector())
5062 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5063 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5064 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005065
5066 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005067 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005068 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005069
5070 // The scalar_to_vector node and the suspected
5071 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005072 // Constants may have multiple users.
5073 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005074 return SDValue();
5075 break;
5076 }
5077 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005078
Craig Topper7a9a28b2012-08-12 02:23:29 +00005079 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005080
5081 // Handle the broadcasting a single constant scalar from the constant pool
5082 // into a vector. On Sandybridge it is still better to load a constant vector
5083 // from the constant pool and not to broadcast it from a scalar.
5084 if (ConstSplatVal && Subtarget->hasAVX2()) {
5085 EVT CVT = Ld.getValueType();
5086 assert(!CVT.isVector() && "Must not broadcast a vector type");
5087 unsigned ScalarSize = CVT.getSizeInBits();
5088
Craig Topper5da8a802012-05-04 05:49:51 +00005089 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005090 const Constant *C = 0;
5091 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5092 C = CI->getConstantIntValue();
5093 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5094 C = CF->getConstantFPValue();
5095
5096 assert(C && "Invalid constant type");
5097
Nadav Rotem154819d2012-04-09 07:45:58 +00005098 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005099 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005100 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005101 MachinePointerInfo::getConstantPool(),
5102 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005103
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5105 }
5106 }
5107
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005108 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005109 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5110
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005111 // Handle AVX2 in-register broadcasts.
5112 if (!IsLoad && Subtarget->hasAVX2() &&
5113 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5114 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5115
5116 // The scalar source must be a normal load.
5117 if (!IsLoad)
5118 return SDValue();
5119
Craig Topper5da8a802012-05-04 05:49:51 +00005120 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005121 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005122
Craig Toppera9376332012-01-10 08:23:59 +00005123 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005124 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005125 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005126 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005127 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005128 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005129
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005130 // Unsupported broadcast.
5131 return SDValue();
5132}
5133
Michael Liao7091b242012-08-14 21:24:47 +00005134// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5135// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5136// constraint of matching input/output vector elements.
5137SDValue
5138X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5139 DebugLoc DL = Op.getDebugLoc();
5140 SDNode *N = Op.getNode();
5141 EVT VT = Op.getValueType();
5142 unsigned NumElts = Op.getNumOperands();
5143
5144 // Check supported types and sub-targets.
5145 //
5146 // Only v2f32 -> v2f64 needs special handling.
5147 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5148 return SDValue();
5149
5150 SDValue VecIn;
5151 EVT VecInVT;
5152 SmallVector<int, 8> Mask;
5153 EVT SrcVT = MVT::Other;
5154
5155 // Check the patterns could be translated into X86vfpext.
5156 for (unsigned i = 0; i < NumElts; ++i) {
5157 SDValue In = N->getOperand(i);
5158 unsigned Opcode = In.getOpcode();
5159
5160 // Skip if the element is undefined.
5161 if (Opcode == ISD::UNDEF) {
5162 Mask.push_back(-1);
5163 continue;
5164 }
5165
5166 // Quit if one of the elements is not defined from 'fpext'.
5167 if (Opcode != ISD::FP_EXTEND)
5168 return SDValue();
5169
5170 // Check how the source of 'fpext' is defined.
5171 SDValue L2In = In.getOperand(0);
5172 EVT L2InVT = L2In.getValueType();
5173
5174 // Check the original type
5175 if (SrcVT == MVT::Other)
5176 SrcVT = L2InVT;
5177 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5178 return SDValue();
5179
5180 // Check whether the value being 'fpext'ed is extracted from the same
5181 // source.
5182 Opcode = L2In.getOpcode();
5183
5184 // Quit if it's not extracted with a constant index.
5185 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5186 !isa<ConstantSDNode>(L2In.getOperand(1)))
5187 return SDValue();
5188
5189 SDValue ExtractedFromVec = L2In.getOperand(0);
5190
5191 if (VecIn.getNode() == 0) {
5192 VecIn = ExtractedFromVec;
5193 VecInVT = ExtractedFromVec.getValueType();
5194 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5195 return SDValue();
5196
5197 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5198 }
5199
Michael Liao24438b82012-08-20 17:59:18 +00005200 // Quit if all operands of BUILD_VECTOR are undefined.
5201 if (!VecIn.getNode())
5202 return SDValue();
5203
Michael Liao7091b242012-08-14 21:24:47 +00005204 // Fill the remaining mask as undef.
5205 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5206 Mask.push_back(-1);
5207
5208 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5209 DAG.getVectorShuffle(VecInVT, DL,
5210 VecIn, DAG.getUNDEF(VecInVT),
5211 &Mask[0]));
5212}
5213
Evan Chengc3630942009-12-09 21:00:30 +00005214SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005215X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005216 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005217
David Greenef125a292011-02-08 19:04:41 +00005218 EVT VT = Op.getValueType();
5219 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005220 unsigned NumElems = Op.getNumOperands();
5221
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005222 // Vectors containing all zeros can be matched by pxor and xorps later
5223 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5224 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5225 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005226 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005227 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005229 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005230 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005232 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005233 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5234 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005235 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005236 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005237 return Op;
5238
Craig Topper07a27622012-01-22 03:07:48 +00005239 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005240 }
5241
Nadav Rotem154819d2012-04-09 07:45:58 +00005242 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005243 if (Broadcast.getNode())
5244 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005245
Michael Liao7091b242012-08-14 21:24:47 +00005246 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5247 if (FpExt.getNode())
5248 return FpExt;
5249
Owen Andersone50ed302009-08-10 22:56:29 +00005250 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005251
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252 unsigned NumZero = 0;
5253 unsigned NumNonZero = 0;
5254 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005255 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005256 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005258 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005259 if (Elt.getOpcode() == ISD::UNDEF)
5260 continue;
5261 Values.insert(Elt);
5262 if (Elt.getOpcode() != ISD::Constant &&
5263 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005264 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005265 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005266 NumZero++;
5267 else {
5268 NonZeros |= (1 << i);
5269 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 }
5271 }
5272
Chris Lattner97a2a562010-08-26 05:24:29 +00005273 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5274 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005275 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276
Chris Lattner67f453a2008-03-09 05:42:06 +00005277 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005278 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005279 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005280 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005281
Chris Lattner62098042008-03-09 01:05:04 +00005282 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5283 // the value are obviously zero, truncate the value to i32 and do the
5284 // insertion that way. Only do this if the value is non-constant or if the
5285 // value is a constant being inserted into element 0. It is cheaper to do
5286 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005288 (!IsAllConstants || Idx == 0)) {
5289 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005290 // Handle SSE only.
5291 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5292 EVT VecVT = MVT::v4i32;
5293 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005294
Chris Lattner62098042008-03-09 01:05:04 +00005295 // Truncate the value (which may itself be a constant) to i32, and
5296 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005297 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005298 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005299 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005300
Chris Lattner62098042008-03-09 01:05:04 +00005301 // Now we have our 32-bit value zero extended in the low element of
5302 // a vector. If Idx != 0, swizzle it into place.
5303 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005304 SmallVector<int, 4> Mask;
5305 Mask.push_back(Idx);
5306 for (unsigned i = 1; i != VecElts; ++i)
5307 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005308 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005310 }
Craig Topper07a27622012-01-22 03:07:48 +00005311 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005312 }
5313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005314
Chris Lattner19f79692008-03-08 22:59:52 +00005315 // If we have a constant or non-constant insertion into the low element of
5316 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5317 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005318 // depending on what the source datatype is.
5319 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005320 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005321 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005322
5323 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005324 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005325 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005326 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005327 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5328 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005329 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005330 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005331 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5332 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005333 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005334 }
5335
5336 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005337 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005338 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005339 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005340 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005341 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005342 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005343 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005344 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005345 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005346 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005347 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005348 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005349
5350 // Is it a vector logical left shift?
5351 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005352 X86::isZeroNode(Op.getOperand(0)) &&
5353 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005354 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005355 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005356 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005357 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005358 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005359 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005360
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005361 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005362 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363
Chris Lattner19f79692008-03-08 22:59:52 +00005364 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5365 // is a non-constant being inserted into an element other than the low one,
5366 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5367 // movd/movss) to move this into the low element, then shuffle it into
5368 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005369 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005370 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005371
Evan Cheng0db9fe62006-04-25 20:13:52 +00005372 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005373 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005374 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005375 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005376 MaskVec.push_back(i == Idx ? 0 : 1);
5377 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005378 }
5379 }
5380
Chris Lattner67f453a2008-03-09 05:42:06 +00005381 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005382 if (Values.size() == 1) {
5383 if (EVTBits == 32) {
5384 // Instead of a shuffle like this:
5385 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5386 // Check if it's possible to issue this instead.
5387 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5388 unsigned Idx = CountTrailingZeros_32(NonZeros);
5389 SDValue Item = Op.getOperand(Idx);
5390 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5391 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5392 }
Dan Gohman475871a2008-07-27 21:46:04 +00005393 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005394 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005395
Dan Gohmana3941172007-07-24 22:55:08 +00005396 // A vector full of immediates; various special cases are already
5397 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005398 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005399 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005400
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005401 // For AVX-length vectors, build the individual 128-bit pieces and use
5402 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005403 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005404 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005405 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005406 V.push_back(Op.getOperand(i));
5407
5408 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5409
5410 // Build both the lower and upper subvector.
5411 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5412 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5413 NumElems/2);
5414
5415 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005416 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005417 }
5418
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005419 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005420 if (EVTBits == 64) {
5421 if (NumNonZero == 1) {
5422 // One half is zero or undef.
5423 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005424 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005425 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005426 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005427 }
Dan Gohman475871a2008-07-27 21:46:04 +00005428 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005429 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005430
5431 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005432 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005433 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005434 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005435 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005436 }
5437
Bill Wendling826f36f2007-03-28 00:57:11 +00005438 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005439 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005440 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005441 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442 }
5443
5444 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005445 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005446 if (NumElems == 4 && NumZero > 0) {
5447 for (unsigned i = 0; i < 4; ++i) {
5448 bool isZero = !(NonZeros & (1 << i));
5449 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005450 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005451 else
Dale Johannesenace16102009-02-03 19:33:06 +00005452 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005453 }
5454
5455 for (unsigned i = 0; i < 2; ++i) {
5456 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5457 default: break;
5458 case 0:
5459 V[i] = V[i*2]; // Must be a zero vector.
5460 break;
5461 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005462 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005463 break;
5464 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005465 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005466 break;
5467 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469 break;
5470 }
5471 }
5472
Benjamin Kramer9c683542012-01-30 15:16:21 +00005473 bool Reverse1 = (NonZeros & 0x3) == 2;
5474 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5475 int MaskVec[] = {
5476 Reverse1 ? 1 : 0,
5477 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005478 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5479 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005480 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005481 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005482 }
5483
Craig Topper7a9a28b2012-08-12 02:23:29 +00005484 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005485 // Check for a build vector of consecutive loads.
5486 for (unsigned i = 0; i < NumElems; ++i)
5487 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005488
Nate Begemanfdea31a2010-03-24 20:49:50 +00005489 // Check for elements which are consecutive loads.
5490 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5491 if (LD.getNode())
5492 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005493
5494 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005495 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005496 SDValue Result;
5497 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5498 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5499 else
5500 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005501
Chris Lattner24faf612010-08-28 17:59:08 +00005502 for (unsigned i = 1; i < NumElems; ++i) {
5503 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5504 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005505 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005506 }
5507 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005508 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005509
Chris Lattner6e80e442010-08-28 17:15:43 +00005510 // Otherwise, expand into a number of unpckl*, start by extending each of
5511 // our (non-undef) elements to the full vector width with the element in the
5512 // bottom slot of the vector (which generates no code for SSE).
5513 for (unsigned i = 0; i < NumElems; ++i) {
5514 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5515 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5516 else
5517 V[i] = DAG.getUNDEF(VT);
5518 }
5519
5520 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005521 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5522 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5523 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005524 unsigned EltStride = NumElems >> 1;
5525 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005526 for (unsigned i = 0; i < EltStride; ++i) {
5527 // If V[i+EltStride] is undef and this is the first round of mixing,
5528 // then it is safe to just drop this shuffle: V[i] is already in the
5529 // right place, the one element (since it's the first round) being
5530 // inserted as undef can be dropped. This isn't safe for successive
5531 // rounds because they will permute elements within both vectors.
5532 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5533 EltStride == NumElems/2)
5534 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005535
Chris Lattner6e80e442010-08-28 17:15:43 +00005536 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005537 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005538 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005539 }
5540 return V[0];
5541 }
Dan Gohman475871a2008-07-27 21:46:04 +00005542 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543}
5544
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005545// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5546// to create 256-bit vectors from two other 128-bit ones.
5547static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5548 DebugLoc dl = Op.getDebugLoc();
5549 EVT ResVT = Op.getValueType();
5550
Craig Topper7a9a28b2012-08-12 02:23:29 +00005551 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005552
5553 SDValue V1 = Op.getOperand(0);
5554 SDValue V2 = Op.getOperand(1);
5555 unsigned NumElems = ResVT.getVectorNumElements();
5556
Craig Topper4c7972d2012-04-22 18:15:59 +00005557 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005558}
5559
5560SDValue
5561X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005562 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005563
5564 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5565 // from two other 128-bit ones.
5566 return LowerAVXCONCAT_VECTORS(Op, DAG);
5567}
5568
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005569// Try to lower a shuffle node into a simple blend instruction.
Craig Topper1842ba02012-04-23 06:38:28 +00005570static SDValue LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005571 const X86Subtarget *Subtarget,
Nadav Rotem91794872012-04-11 11:05:21 +00005572 SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005573 SDValue V1 = SVOp->getOperand(0);
5574 SDValue V2 = SVOp->getOperand(1);
5575 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005576 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005577 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005578
Nadav Roteme6113782012-04-11 06:40:27 +00005579 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005580 return SDValue();
5581
Craig Topper1842ba02012-04-23 06:38:28 +00005582 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005583 MVT OpTy;
5584
Craig Topper708e44f2012-04-23 07:36:33 +00005585 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005586 default: return SDValue();
5587 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005588 ISDNo = X86ISD::BLENDPW;
5589 OpTy = MVT::v8i16;
5590 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005591 case MVT::v4i32:
5592 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005593 ISDNo = X86ISD::BLENDPS;
5594 OpTy = MVT::v4f32;
5595 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005596 case MVT::v2i64:
5597 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005598 ISDNo = X86ISD::BLENDPD;
5599 OpTy = MVT::v2f64;
5600 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005601 case MVT::v8i32:
5602 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005603 if (!Subtarget->hasAVX())
5604 return SDValue();
5605 ISDNo = X86ISD::BLENDPS;
5606 OpTy = MVT::v8f32;
5607 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005608 case MVT::v4i64:
5609 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005610 if (!Subtarget->hasAVX())
5611 return SDValue();
5612 ISDNo = X86ISD::BLENDPD;
5613 OpTy = MVT::v4f64;
5614 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005615 }
5616 assert(ISDNo && "Invalid Op Number");
5617
5618 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005619
Craig Topper1842ba02012-04-23 06:38:28 +00005620 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005621 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005622 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005623 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005624 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005625 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005626 else
5627 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005628 }
5629
Nadav Roteme6113782012-04-11 06:40:27 +00005630 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5631 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5632 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5633 DAG.getConstant(MaskVals, MVT::i32));
5634 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005635}
5636
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637// v8i16 shuffles - Prefer shuffles in the following order:
5638// 1. [all] pshuflw, pshufhw, optional move
5639// 2. [ssse3] 1 x pshufb
5640// 3. [ssse3] 2 x pshufb + 1 x por
5641// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005642SDValue
5643X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5644 SelectionDAG &DAG) const {
5645 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 SDValue V1 = SVOp->getOperand(0);
5647 SDValue V2 = SVOp->getOperand(1);
5648 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005649 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005650
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 // Determine if more than 1 of the words in each of the low and high quadwords
5652 // of the result come from the same quadword of one of the two inputs. Undef
5653 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005654 unsigned LoQuad[] = { 0, 0, 0, 0 };
5655 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005656 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005657 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005658 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005659 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 MaskVals.push_back(EltIdx);
5661 if (EltIdx < 0) {
5662 ++Quad[0];
5663 ++Quad[1];
5664 ++Quad[2];
5665 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005666 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005667 }
5668 ++Quad[EltIdx / 4];
5669 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005670 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005671
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005673 unsigned MaxQuad = 1;
5674 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 if (LoQuad[i] > MaxQuad) {
5676 BestLoQuad = i;
5677 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005678 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005679 }
5680
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005682 MaxQuad = 1;
5683 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 if (HiQuad[i] > MaxQuad) {
5685 BestHiQuad = i;
5686 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005687 }
5688 }
5689
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005691 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005692 // single pshufb instruction is necessary. If There are more than 2 input
5693 // quads, disable the next transformation since it does not help SSSE3.
5694 bool V1Used = InputQuads[0] || InputQuads[1];
5695 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005696 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005698 BestLoQuad = InputQuads[0] ? 0 : 1;
5699 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 }
5701 if (InputQuads.count() > 2) {
5702 BestLoQuad = -1;
5703 BestHiQuad = -1;
5704 }
5705 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005706
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5708 // the shuffle mask. If a quad is scored as -1, that means that it contains
5709 // words from all 4 input quadwords.
5710 SDValue NewV;
5711 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005712 int MaskV[] = {
5713 BestLoQuad < 0 ? 0 : BestLoQuad,
5714 BestHiQuad < 0 ? 1 : BestHiQuad
5715 };
Eric Christopherfd179292009-08-27 18:07:15 +00005716 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005717 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5718 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5719 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005720
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5722 // source words for the shuffle, to aid later transformations.
5723 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005724 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005725 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005727 if (idx != (int)i)
5728 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005730 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 AllWordsInNewV = false;
5732 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005733 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005734
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5736 if (AllWordsInNewV) {
5737 for (int i = 0; i != 8; ++i) {
5738 int idx = MaskVals[i];
5739 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005740 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005741 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 if ((idx != i) && idx < 4)
5743 pshufhw = false;
5744 if ((idx != i) && idx > 3)
5745 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005746 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 V1 = NewV;
5748 V2Used = false;
5749 BestLoQuad = 0;
5750 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005751 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5754 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005755 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005756 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5757 unsigned TargetMask = 0;
5758 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005759 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005760 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5761 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5762 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005763 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005764 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005765 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005766 }
Eric Christopherfd179292009-08-27 18:07:15 +00005767
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 // If we have SSSE3, and all words of the result are from 1 input vector,
5769 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5770 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005771 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005773
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005775 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 // mask, and elements that come from V1 in the V2 mask, so that the two
5777 // results can be OR'd together.
5778 bool TwoInputs = V1Used && V2Used;
5779 for (unsigned i = 0; i != 8; ++i) {
5780 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005781 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5782 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5783 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5784 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005786 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005787 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005788 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005791 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005792
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 // Calculate the shuffle mask for the second input, shuffle it, and
5794 // OR it with the first shuffled input.
5795 pshufbMask.clear();
5796 for (unsigned i = 0; i != 8; ++i) {
5797 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005798 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5799 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5800 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5801 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005803 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005804 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005805 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 MVT::v16i8, &pshufbMask[0], 16));
5807 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005808 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005809 }
5810
5811 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5812 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005813 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005815 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 for (int i = 0; i != 4; ++i) {
5817 int idx = MaskVals[i];
5818 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 InOrder.set(i);
5820 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005821 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005823 }
5824 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005825 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005826 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005827
Craig Topperdd637ae2012-02-19 05:41:45 +00005828 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5829 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005830 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005831 NewV.getOperand(0),
5832 getShufflePSHUFLWImmediate(SVOp), DAG);
5833 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 }
Eric Christopherfd179292009-08-27 18:07:15 +00005835
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5837 // and update MaskVals with the new element order.
5838 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005839 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 for (unsigned i = 4; i != 8; ++i) {
5841 int idx = MaskVals[i];
5842 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 InOrder.set(i);
5844 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005845 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 }
5848 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005849 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005850 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005851
Craig Topperdd637ae2012-02-19 05:41:45 +00005852 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5853 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005854 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005855 NewV.getOperand(0),
5856 getShufflePSHUFHWImmediate(SVOp), DAG);
5857 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 }
Eric Christopherfd179292009-08-27 18:07:15 +00005859
Nate Begemanb9a47b82009-02-23 08:49:38 +00005860 // In case BestHi & BestLo were both -1, which means each quadword has a word
5861 // from each of the four input quadwords, calculate the InOrder bitvector now
5862 // before falling through to the insert/extract cleanup.
5863 if (BestLoQuad == -1 && BestHiQuad == -1) {
5864 NewV = V1;
5865 for (int i = 0; i != 8; ++i)
5866 if (MaskVals[i] < 0 || MaskVals[i] == i)
5867 InOrder.set(i);
5868 }
Eric Christopherfd179292009-08-27 18:07:15 +00005869
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 // The other elements are put in the right place using pextrw and pinsrw.
5871 for (unsigned i = 0; i != 8; ++i) {
5872 if (InOrder[i])
5873 continue;
5874 int EltIdx = MaskVals[i];
5875 if (EltIdx < 0)
5876 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005877 SDValue ExtOp = (EltIdx < 8) ?
5878 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5879 DAG.getIntPtrConstant(EltIdx)) :
5880 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 DAG.getIntPtrConstant(i));
5884 }
5885 return NewV;
5886}
5887
5888// v16i8 shuffles - Prefer shuffles in the following order:
5889// 1. [ssse3] 1 x pshufb
5890// 2. [ssse3] 2 x pshufb + 1 x por
5891// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5892static
Nate Begeman9008ca62009-04-27 18:41:29 +00005893SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005894 SelectionDAG &DAG,
5895 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005896 SDValue V1 = SVOp->getOperand(0);
5897 SDValue V2 = SVOp->getOperand(1);
5898 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005899 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005900
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005902 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005904
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005906 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005907 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005908
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005910 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 //
5912 // Otherwise, we have elements from both input vectors, and must zero out
5913 // elements that come from V2 in the first mask, and V1 in the second mask
5914 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 for (unsigned i = 0; i != 16; ++i) {
5916 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005917 if (EltIdx < 0 || EltIdx >= 16)
5918 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005919 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005922 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005923 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005924
5925 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5926 // the 2nd operand if it's undefined or zero.
5927 if (V2.getOpcode() == ISD::UNDEF ||
5928 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005930
Nate Begemanb9a47b82009-02-23 08:49:38 +00005931 // Calculate the shuffle mask for the second input, shuffle it, and
5932 // OR it with the first shuffled input.
5933 pshufbMask.clear();
5934 for (unsigned i = 0; i != 16; ++i) {
5935 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005936 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005937 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005940 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005941 MVT::v16i8, &pshufbMask[0], 16));
5942 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005943 }
Eric Christopherfd179292009-08-27 18:07:15 +00005944
Nate Begemanb9a47b82009-02-23 08:49:38 +00005945 // No SSSE3 - Calculate in place words and then fix all out of place words
5946 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5947 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005948 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5949 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005950 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 for (int i = 0; i != 8; ++i) {
5952 int Elt0 = MaskVals[i*2];
5953 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005954
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 // This word of the result is all undef, skip it.
5956 if (Elt0 < 0 && Elt1 < 0)
5957 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005958
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005960 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005961 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005962
Nate Begemanb9a47b82009-02-23 08:49:38 +00005963 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5964 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5965 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005966
5967 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5968 // using a single extract together, load it and store it.
5969 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005970 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005971 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005972 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005973 DAG.getIntPtrConstant(i));
5974 continue;
5975 }
5976
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005978 // source byte is not also odd, shift the extracted word left 8 bits
5979 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005980 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005981 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 DAG.getIntPtrConstant(Elt1 / 2));
5983 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005984 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005985 DAG.getConstant(8,
5986 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005987 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5989 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005990 }
5991 // If Elt0 is defined, extract it from the appropriate source. If the
5992 // source byte is not also even, shift the extracted word right 8 bits. If
5993 // Elt1 was also defined, OR the extracted values together before
5994 // inserting them in the result.
5995 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005996 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005997 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5998 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005999 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006000 DAG.getConstant(8,
6001 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006002 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6004 DAG.getConstant(0x00FF, MVT::i16));
6005 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006006 : InsElt0;
6007 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006009 DAG.getIntPtrConstant(i));
6010 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006011 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006012}
6013
Evan Cheng7a831ce2007-12-15 03:00:47 +00006014/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006015/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006016/// done when every pair / quad of shuffle mask elements point to elements in
6017/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006018/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006019static
Nate Begeman9008ca62009-04-27 18:41:29 +00006020SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006021 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006022 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006023 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006024 MVT NewVT;
6025 unsigned Scale;
6026 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006027 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006028 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6029 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6030 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6031 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6032 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6033 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006034 }
6035
Nate Begeman9008ca62009-04-27 18:41:29 +00006036 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006037 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006038 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006039 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006040 int EltIdx = SVOp->getMaskElt(i+j);
6041 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006042 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006043 if (StartIdx < 0)
6044 StartIdx = (EltIdx / Scale);
6045 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006046 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006047 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006048 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006049 }
6050
Craig Topper11ac1f82012-05-04 04:08:44 +00006051 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6052 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006053 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006054}
6055
Evan Chengd880b972008-05-09 21:53:03 +00006056/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006057///
Owen Andersone50ed302009-08-10 22:56:29 +00006058static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006059 SDValue SrcOp, SelectionDAG &DAG,
6060 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006061 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006062 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006063 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006064 LD = dyn_cast<LoadSDNode>(SrcOp);
6065 if (!LD) {
6066 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6067 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006068 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006069 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006070 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006071 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006072 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006073 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006074 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006075 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006076 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6077 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6078 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006079 SrcOp.getOperand(0)
6080 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006081 }
6082 }
6083 }
6084
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006085 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006086 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006087 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006088 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006089}
6090
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006091/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6092/// which could not be matched by any known target speficic shuffle
6093static SDValue
6094LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006095
6096 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6097 if (NewOp.getNode())
6098 return NewOp;
6099
Craig Topper8f35c132012-01-20 09:29:03 +00006100 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006101
Craig Topper8f35c132012-01-20 09:29:03 +00006102 unsigned NumElems = VT.getVectorNumElements();
6103 unsigned NumLaneElems = NumElems / 2;
6104
Craig Topper8f35c132012-01-20 09:29:03 +00006105 DebugLoc dl = SVOp->getDebugLoc();
6106 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006107 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006108 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006109
Craig Topper9a2b6e12012-04-06 07:45:23 +00006110 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006111 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006112 // Build a shuffle mask for the output, discovering on the fly which
6113 // input vectors to use as shuffle operands (recorded in InputUsed).
6114 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006115 // out with UseBuildVector set.
6116 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006117 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006118 unsigned LaneStart = l * NumLaneElems;
6119 for (unsigned i = 0; i != NumLaneElems; ++i) {
6120 // The mask element. This indexes into the input.
6121 int Idx = SVOp->getMaskElt(i+LaneStart);
6122 if (Idx < 0) {
6123 // the mask element does not index into any input vector.
6124 Mask.push_back(-1);
6125 continue;
6126 }
Craig Topper8f35c132012-01-20 09:29:03 +00006127
Craig Topper9a2b6e12012-04-06 07:45:23 +00006128 // The input vector this mask element indexes into.
6129 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006130
Craig Topper9a2b6e12012-04-06 07:45:23 +00006131 // Turn the index into an offset from the start of the input vector.
6132 Idx -= Input * NumLaneElems;
6133
6134 // Find or create a shuffle vector operand to hold this input.
6135 unsigned OpNo;
6136 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6137 if (InputUsed[OpNo] == Input)
6138 // This input vector is already an operand.
6139 break;
6140 if (InputUsed[OpNo] < 0) {
6141 // Create a new operand for this input vector.
6142 InputUsed[OpNo] = Input;
6143 break;
6144 }
6145 }
6146
6147 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006148 // More than two input vectors used! Give up on trying to create a
6149 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6150 UseBuildVector = true;
6151 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006152 }
6153
6154 // Add the mask index for the new shuffle vector.
6155 Mask.push_back(Idx + OpNo * NumLaneElems);
6156 }
6157
Craig Topper8ae97ba2012-05-21 06:40:16 +00006158 if (UseBuildVector) {
6159 SmallVector<SDValue, 16> SVOps;
6160 for (unsigned i = 0; i != NumLaneElems; ++i) {
6161 // The mask element. This indexes into the input.
6162 int Idx = SVOp->getMaskElt(i+LaneStart);
6163 if (Idx < 0) {
6164 SVOps.push_back(DAG.getUNDEF(EltVT));
6165 continue;
6166 }
6167
6168 // The input vector this mask element indexes into.
6169 int Input = Idx / NumElems;
6170
6171 // Turn the index into an offset from the start of the input vector.
6172 Idx -= Input * NumElems;
6173
6174 // Extract the vector element by hand.
6175 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6176 SVOp->getOperand(Input),
6177 DAG.getIntPtrConstant(Idx)));
6178 }
6179
6180 // Construct the output using a BUILD_VECTOR.
6181 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6182 SVOps.size());
6183 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006184 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006185 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006186 } else {
6187 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006188 (InputUsed[0] % 2) * NumLaneElems,
6189 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006190 // If only one input was used, use an undefined vector for the other.
6191 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6192 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006193 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006194 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006195 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006196 }
6197
6198 Mask.clear();
6199 }
Craig Topper8f35c132012-01-20 09:29:03 +00006200
6201 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006202 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006203}
6204
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006205/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6206/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006207static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006208LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006209 SDValue V1 = SVOp->getOperand(0);
6210 SDValue V2 = SVOp->getOperand(1);
6211 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006212 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006213
Craig Topper7a9a28b2012-08-12 02:23:29 +00006214 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006215
Benjamin Kramer9c683542012-01-30 15:16:21 +00006216 std::pair<int, int> Locs[4];
6217 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006218 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006219
Evan Chengace3c172008-07-22 21:13:36 +00006220 unsigned NumHi = 0;
6221 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006222 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006223 int Idx = PermMask[i];
6224 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006225 Locs[i] = std::make_pair(-1, -1);
6226 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006227 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6228 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006229 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006230 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006231 NumLo++;
6232 } else {
6233 Locs[i] = std::make_pair(1, NumHi);
6234 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006235 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006236 NumHi++;
6237 }
6238 }
6239 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006240
Evan Chengace3c172008-07-22 21:13:36 +00006241 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006242 // If no more than two elements come from either vector. This can be
6243 // implemented with two shuffles. First shuffle gather the elements.
6244 // The second shuffle, which takes the first shuffle as both of its
6245 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006246 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006247
Benjamin Kramer9c683542012-01-30 15:16:21 +00006248 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006249
Benjamin Kramer9c683542012-01-30 15:16:21 +00006250 for (unsigned i = 0; i != 4; ++i)
6251 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006252 unsigned Idx = (i < 2) ? 0 : 4;
6253 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006254 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006255 }
Evan Chengace3c172008-07-22 21:13:36 +00006256
Nate Begeman9008ca62009-04-27 18:41:29 +00006257 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006258 }
6259
6260 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006261 // Otherwise, we must have three elements from one vector, call it X, and
6262 // one element from the other, call it Y. First, use a shufps to build an
6263 // intermediate vector with the one element from Y and the element from X
6264 // that will be in the same half in the final destination (the indexes don't
6265 // matter). Then, use a shufps to build the final vector, taking the half
6266 // containing the element from Y from the intermediate, and the other half
6267 // from X.
6268 if (NumHi == 3) {
6269 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006270 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006271 std::swap(V1, V2);
6272 }
6273
6274 // Find the element from V2.
6275 unsigned HiIndex;
6276 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006277 int Val = PermMask[HiIndex];
6278 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006279 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006280 if (Val >= 4)
6281 break;
6282 }
6283
Nate Begeman9008ca62009-04-27 18:41:29 +00006284 Mask1[0] = PermMask[HiIndex];
6285 Mask1[1] = -1;
6286 Mask1[2] = PermMask[HiIndex^1];
6287 Mask1[3] = -1;
6288 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006289
6290 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006291 Mask1[0] = PermMask[0];
6292 Mask1[1] = PermMask[1];
6293 Mask1[2] = HiIndex & 1 ? 6 : 4;
6294 Mask1[3] = HiIndex & 1 ? 4 : 6;
6295 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006296 }
Craig Topper69947b92012-04-23 06:57:04 +00006297
6298 Mask1[0] = HiIndex & 1 ? 2 : 0;
6299 Mask1[1] = HiIndex & 1 ? 0 : 2;
6300 Mask1[2] = PermMask[2];
6301 Mask1[3] = PermMask[3];
6302 if (Mask1[2] >= 0)
6303 Mask1[2] += 4;
6304 if (Mask1[3] >= 0)
6305 Mask1[3] += 4;
6306 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006307 }
6308
6309 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006310 int LoMask[] = { -1, -1, -1, -1 };
6311 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006312
Benjamin Kramer9c683542012-01-30 15:16:21 +00006313 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006314 unsigned MaskIdx = 0;
6315 unsigned LoIdx = 0;
6316 unsigned HiIdx = 2;
6317 for (unsigned i = 0; i != 4; ++i) {
6318 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006319 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006320 MaskIdx = 1;
6321 LoIdx = 0;
6322 HiIdx = 2;
6323 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006324 int Idx = PermMask[i];
6325 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006326 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006327 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006328 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006329 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006330 LoIdx++;
6331 } else {
6332 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006333 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006334 HiIdx++;
6335 }
6336 }
6337
Nate Begeman9008ca62009-04-27 18:41:29 +00006338 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6339 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006340 int MaskOps[] = { -1, -1, -1, -1 };
6341 for (unsigned i = 0; i != 4; ++i)
6342 if (Locs[i].first != -1)
6343 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006344 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006345}
6346
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006347static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006348 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006349 V = V.getOperand(0);
6350 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6351 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006352 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6353 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6354 // BUILD_VECTOR (load), undef
6355 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006356 if (MayFoldLoad(V))
6357 return true;
6358 return false;
6359}
6360
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006361// FIXME: the version above should always be used. Since there's
6362// a bug where several vector shuffles can't be folded because the
6363// DAG is not updated during lowering and a node claims to have two
6364// uses while it only has one, use this version, and let isel match
6365// another instruction if the load really happens to have more than
6366// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006367// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006368static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006369 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006370 V = V.getOperand(0);
6371 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6372 V = V.getOperand(0);
6373 if (ISD::isNormalLoad(V.getNode()))
6374 return true;
6375 return false;
6376}
6377
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006378static
Evan Cheng835580f2010-10-07 20:50:20 +00006379SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6380 EVT VT = Op.getValueType();
6381
6382 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006383 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6384 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006385 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6386 V1, DAG));
6387}
6388
6389static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006390SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006391 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006392 SDValue V1 = Op.getOperand(0);
6393 SDValue V2 = Op.getOperand(1);
6394 EVT VT = Op.getValueType();
6395
6396 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6397
Craig Topper1accb7e2012-01-10 06:54:16 +00006398 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006399 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6400
Evan Cheng0899f5c2011-08-31 02:05:24 +00006401 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6402 return DAG.getNode(ISD::BITCAST, dl, VT,
6403 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6404 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6405 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006406}
6407
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006408static
6409SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6410 SDValue V1 = Op.getOperand(0);
6411 SDValue V2 = Op.getOperand(1);
6412 EVT VT = Op.getValueType();
6413
6414 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6415 "unsupported shuffle type");
6416
6417 if (V2.getOpcode() == ISD::UNDEF)
6418 V2 = V1;
6419
6420 // v4i32 or v4f32
6421 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6422}
6423
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006424static
Craig Topper1accb7e2012-01-10 06:54:16 +00006425SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006426 SDValue V1 = Op.getOperand(0);
6427 SDValue V2 = Op.getOperand(1);
6428 EVT VT = Op.getValueType();
6429 unsigned NumElems = VT.getVectorNumElements();
6430
6431 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6432 // operand of these instructions is only memory, so check if there's a
6433 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6434 // same masks.
6435 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006436
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006437 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006438 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006439 CanFoldLoad = true;
6440
6441 // When V1 is a load, it can be folded later into a store in isel, example:
6442 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6443 // turns into:
6444 // (MOVLPSmr addr:$src1, VR128:$src2)
6445 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006446 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006447 CanFoldLoad = true;
6448
Dan Gohman65fd6562011-11-03 21:49:52 +00006449 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006450 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006451 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006452 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6453
6454 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006455 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006456 if (SVOp->getMaskElt(1) != -1)
6457 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006458 }
6459
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006460 // movl and movlp will both match v2i64, but v2i64 is never matched by
6461 // movl earlier because we make it strict to avoid messing with the movlp load
6462 // folding logic (see the code above getMOVLP call). Match it here then,
6463 // this is horrible, but will stay like this until we move all shuffle
6464 // matching to x86 specific nodes. Note that for the 1st condition all
6465 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006466 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006467 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6468 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006469 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006470 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006471 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006472 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006473
6474 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6475
6476 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006477 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006478 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006479}
6480
Nadav Rotem154819d2012-04-09 07:45:58 +00006481SDValue
6482X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006483 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6484 EVT VT = Op.getValueType();
6485 DebugLoc dl = Op.getDebugLoc();
6486 SDValue V1 = Op.getOperand(0);
6487 SDValue V2 = Op.getOperand(1);
6488
6489 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006490 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006491
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006492 // Handle splat operations
6493 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006494 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006495 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006496
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006497 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006498 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006499 if (Broadcast.getNode())
6500 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006501
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006502 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006503 if ((Size == 128 && NumElem <= 4) ||
6504 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006505 return SDValue();
6506
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006507 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006508 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006509 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006510
6511 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6512 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006513 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6514 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006515 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6516 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006517 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006518 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006519 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006520 // FIXME: Figure out a cleaner way to do this.
6521 // Try to make use of movq to zero out the top part.
6522 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6523 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6524 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006525 EVT NewVT = NewOp.getValueType();
6526 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6527 NewVT, true, false))
6528 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006529 DAG, Subtarget, dl);
6530 }
6531 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6532 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006533 if (NewOp.getNode()) {
6534 EVT NewVT = NewOp.getValueType();
6535 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6536 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6537 DAG, Subtarget, dl);
6538 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006539 }
6540 }
6541 return SDValue();
6542}
6543
Dan Gohman475871a2008-07-27 21:46:04 +00006544SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006545X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006546 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006547 SDValue V1 = Op.getOperand(0);
6548 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006549 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006550 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006551 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006552 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006553 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006554 bool V1IsSplat = false;
6555 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006556 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006557 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006558 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006559 MachineFunction &MF = DAG.getMachineFunction();
6560 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006561
Craig Topper3426a3e2011-11-14 06:46:21 +00006562 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006563
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006564 if (V1IsUndef && V2IsUndef)
6565 return DAG.getUNDEF(VT);
6566
6567 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006568
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006569 // Vector shuffle lowering takes 3 steps:
6570 //
6571 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6572 // narrowing and commutation of operands should be handled.
6573 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6574 // shuffle nodes.
6575 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6576 // so the shuffle can be broken into other shuffles and the legalizer can
6577 // try the lowering again.
6578 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006579 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006580 // be matched during isel, all of them must be converted to a target specific
6581 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006582
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006583 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6584 // narrowing and commutation of operands should be handled. The actual code
6585 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006586 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006587 if (NewOp.getNode())
6588 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006589
Craig Topper5aaffa82012-02-19 02:53:47 +00006590 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6591
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006592 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6593 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006594 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006595 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006596 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006597 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006598
Craig Topperdd637ae2012-02-19 05:41:45 +00006599 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006600 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006601 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006602
Craig Topperdd637ae2012-02-19 05:41:45 +00006603 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006604 return getMOVHighToLow(Op, dl, DAG);
6605
6606 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006607 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006608 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006609 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006610
Craig Topper5aaffa82012-02-19 02:53:47 +00006611 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006612 // The actual implementation will match the mask in the if above and then
6613 // during isel it can match several different instructions, not only pshufd
6614 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006615 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6616 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006617
Craig Topper5aaffa82012-02-19 02:53:47 +00006618 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006619
Craig Topperdbd98a42012-02-07 06:28:42 +00006620 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6621 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6622
Craig Topper1accb7e2012-01-10 06:54:16 +00006623 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006624 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6625
Craig Topperb3982da2011-12-31 23:50:21 +00006626 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006627 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006628 }
Eric Christopherfd179292009-08-27 18:07:15 +00006629
Evan Chengf26ffe92008-05-29 08:22:04 +00006630 // Check if this can be converted into a logical shift.
6631 bool isLeft = false;
6632 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006633 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006634 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006635 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006636 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006637 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006638 EVT EltVT = VT.getVectorElementType();
6639 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006640 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006641 }
Eric Christopherfd179292009-08-27 18:07:15 +00006642
Craig Topper5aaffa82012-02-19 02:53:47 +00006643 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006644 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006645 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006646 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006647 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006648 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6649
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006650 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006651 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6652 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006653 }
Eric Christopherfd179292009-08-27 18:07:15 +00006654
Nate Begeman9008ca62009-04-27 18:41:29 +00006655 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006656 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006657 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006658
Craig Topperdd637ae2012-02-19 05:41:45 +00006659 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006660 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006661
Craig Topperdd637ae2012-02-19 05:41:45 +00006662 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006663 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006664
Craig Topperdd637ae2012-02-19 05:41:45 +00006665 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006666 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006667
Craig Topperdd637ae2012-02-19 05:41:45 +00006668 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006669 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006670
Craig Topperdd637ae2012-02-19 05:41:45 +00006671 if (ShouldXformToMOVHLPS(M, VT) ||
6672 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006673 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006674
Evan Chengf26ffe92008-05-29 08:22:04 +00006675 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006676 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006677 EVT EltVT = VT.getVectorElementType();
6678 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006679 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006680 }
Eric Christopherfd179292009-08-27 18:07:15 +00006681
Evan Cheng9eca5e82006-10-25 21:49:50 +00006682 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006683 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6684 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006685 V1IsSplat = isSplatVector(V1.getNode());
6686 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006687
Chris Lattner8a594482007-11-25 00:24:49 +00006688 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006689 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6690 CommuteVectorShuffleMask(M, NumElems);
6691 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006692 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006693 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006694 }
6695
Craig Topperbeabc6c2011-12-05 06:56:46 +00006696 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006697 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006698 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006699 return V1;
6700 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6701 // the instruction selector will not match, so get a canonical MOVL with
6702 // swapped operands to undo the commute.
6703 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006704 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705
Craig Topperbeabc6c2011-12-05 06:56:46 +00006706 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006707 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006708
Craig Topperbeabc6c2011-12-05 06:56:46 +00006709 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006710 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006711
Evan Cheng9bbbb982006-10-25 20:48:19 +00006712 if (V2IsSplat) {
6713 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006714 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006715 // new vector_shuffle with the corrected mask.p
6716 SmallVector<int, 8> NewMask(M.begin(), M.end());
6717 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006718 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006719 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006720 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006721 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006722 }
6723
Evan Cheng9eca5e82006-10-25 21:49:50 +00006724 if (Commuted) {
6725 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006726 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006727 CommuteVectorShuffleMask(M, NumElems);
6728 std::swap(V1, V2);
6729 std::swap(V1IsSplat, V2IsSplat);
6730 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006731
Craig Topper39a9e482012-02-11 06:24:48 +00006732 if (isUNPCKLMask(M, VT, HasAVX2))
6733 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006734
Craig Topper39a9e482012-02-11 06:24:48 +00006735 if (isUNPCKHMask(M, VT, HasAVX2))
6736 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006737 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006738
Nate Begeman9008ca62009-04-27 18:41:29 +00006739 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006740 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006741 return CommuteVectorShuffle(SVOp, DAG);
6742
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006743 // The checks below are all present in isShuffleMaskLegal, but they are
6744 // inlined here right now to enable us to directly emit target specific
6745 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006746
Craig Topper0e2037b2012-01-20 05:53:00 +00006747 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006748 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006749 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006750 DAG);
6751
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006752 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6753 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006754 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006755 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006756 }
6757
Craig Toppera9a568a2012-05-02 08:03:44 +00006758 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006759 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006760 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006761 DAG);
6762
Craig Toppera9a568a2012-05-02 08:03:44 +00006763 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006764 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006765 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006766 DAG);
6767
Craig Topper1a7700a2012-01-19 08:19:12 +00006768 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006769 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006770 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006771
Craig Topper94438ba2011-12-16 08:06:31 +00006772 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006773 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006774 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006775 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006776
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006777 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006778 // Generate target specific nodes for 128 or 256-bit shuffles only
6779 // supported in the AVX instruction set.
6780 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006781
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006782 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006783 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006784 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6785
Craig Topper70b883b2011-11-28 10:14:51 +00006786 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006787 if (isVPERMILPMask(M, VT, HasAVX)) {
6788 if (HasAVX2 && VT == MVT::v8i32)
6789 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006790 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006791 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006792 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006793 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006794
Craig Topper70b883b2011-11-28 10:14:51 +00006795 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006796 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006797 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006798 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006799
Craig Topper1842ba02012-04-23 06:38:28 +00006800 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006801 if (BlendOp.getNode())
6802 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006803
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006804 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006805 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006806 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006807 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006808 }
Craig Topper92040742012-04-16 06:43:40 +00006809 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6810 &permclMask[0], 8);
6811 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006812 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006813 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006814 }
Craig Topper095c5282012-04-15 23:48:57 +00006815
Craig Topper8325c112012-04-16 00:41:45 +00006816 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6817 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006818 getShuffleCLImmediate(SVOp), DAG);
6819
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006820
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006821 //===--------------------------------------------------------------------===//
6822 // Since no target specific shuffle was selected for this generic one,
6823 // lower it into other known shuffles. FIXME: this isn't true yet, but
6824 // this is the plan.
6825 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006826
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006827 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6828 if (VT == MVT::v8i16) {
6829 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6830 if (NewOp.getNode())
6831 return NewOp;
6832 }
6833
6834 if (VT == MVT::v16i8) {
6835 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6836 if (NewOp.getNode())
6837 return NewOp;
6838 }
6839
6840 // Handle all 128-bit wide vectors with 4 elements, and match them with
6841 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006842 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006843 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6844
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006845 // Handle general 256-bit shuffles
6846 if (VT.is256BitVector())
6847 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6848
Dan Gohman475871a2008-07-27 21:46:04 +00006849 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006850}
6851
Dan Gohman475871a2008-07-27 21:46:04 +00006852SDValue
6853X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006854 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006855 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006856 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006857
Craig Topper7a9a28b2012-08-12 02:23:29 +00006858 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006859 return SDValue();
6860
Duncan Sands83ec4b62008-06-06 12:08:01 +00006861 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006863 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006864 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006865 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006866 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006867 }
6868
6869 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006870 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6871 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6872 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006873 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6874 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006875 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006877 Op.getOperand(0)),
6878 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006879 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006880 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006881 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006882 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006883 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006884 }
6885
6886 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006887 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6888 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006889 // result has a single use which is a store or a bitcast to i32. And in
6890 // the case of a store, it's not worth it if the index is a constant 0,
6891 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006892 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006893 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006894 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006895 if ((User->getOpcode() != ISD::STORE ||
6896 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6897 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006898 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006899 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006900 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006902 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006903 Op.getOperand(0)),
6904 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006905 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006906 }
6907
6908 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006909 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006910 if (isa<ConstantSDNode>(Op.getOperand(1)))
6911 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006912 }
Dan Gohman475871a2008-07-27 21:46:04 +00006913 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006914}
6915
6916
Dan Gohman475871a2008-07-27 21:46:04 +00006917SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006918X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6919 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006921 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006922
David Greene74a579d2011-02-10 16:57:36 +00006923 SDValue Vec = Op.getOperand(0);
6924 EVT VecVT = Vec.getValueType();
6925
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006926 // If this is a 256-bit vector result, first extract the 128-bit vector and
6927 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006928 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006929 DebugLoc dl = Op.getNode()->getDebugLoc();
6930 unsigned NumElems = VecVT.getVectorNumElements();
6931 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006932 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6933
6934 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006935 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006936
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006937 if (IdxVal >= NumElems/2)
6938 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006939 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006940 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006941 }
6942
Craig Topper7a9a28b2012-08-12 02:23:29 +00006943 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00006944
Craig Topperd0a31172012-01-10 06:37:29 +00006945 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006946 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006947 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006948 return Res;
6949 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006950
Owen Andersone50ed302009-08-10 22:56:29 +00006951 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006952 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006953 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006954 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006955 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006956 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006957 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006958 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6959 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006960 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006961 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006962 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006963 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006964 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006965 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006966 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006967 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006969 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006970 }
6971
6972 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006973 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 if (Idx == 0)
6975 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006976
Evan Cheng0db9fe62006-04-25 20:13:52 +00006977 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006978 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006979 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006980 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006981 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006982 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006983 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00006984 }
6985
6986 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006987 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6988 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6989 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006990 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991 if (Idx == 0)
6992 return Op;
6993
6994 // UNPCKHPD the element to the lowest double word, then movsd.
6995 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6996 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006997 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006998 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006999 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007000 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007001 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007002 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007003 }
7004
Dan Gohman475871a2008-07-27 21:46:04 +00007005 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007006}
7007
Dan Gohman475871a2008-07-27 21:46:04 +00007008SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007009X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7010 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007011 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007012 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007013 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007014
Dan Gohman475871a2008-07-27 21:46:04 +00007015 SDValue N0 = Op.getOperand(0);
7016 SDValue N1 = Op.getOperand(1);
7017 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007018
Craig Topper7a9a28b2012-08-12 02:23:29 +00007019 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007020 return SDValue();
7021
Dan Gohman8a55ce42009-09-23 21:02:20 +00007022 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007023 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007024 unsigned Opc;
7025 if (VT == MVT::v8i16)
7026 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007027 else if (VT == MVT::v16i8)
7028 Opc = X86ISD::PINSRB;
7029 else
7030 Opc = X86ISD::PINSRB;
7031
Nate Begeman14d12ca2008-02-11 04:19:36 +00007032 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7033 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007034 if (N1.getValueType() != MVT::i32)
7035 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7036 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007037 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007038 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007039 }
7040
7041 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007042 // Bits [7:6] of the constant are the source select. This will always be
7043 // zero here. The DAG Combiner may combine an extract_elt index into these
7044 // bits. For example (insert (extract, 3), 2) could be matched by putting
7045 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007046 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007047 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007048 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007049 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007050 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007051 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007053 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007054 }
7055
7056 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007057 // PINSR* works with constant index.
7058 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007059 }
Dan Gohman475871a2008-07-27 21:46:04 +00007060 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007061}
7062
Dan Gohman475871a2008-07-27 21:46:04 +00007063SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007064X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007065 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007066 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007067
David Greene6b381262011-02-09 15:32:06 +00007068 DebugLoc dl = Op.getDebugLoc();
7069 SDValue N0 = Op.getOperand(0);
7070 SDValue N1 = Op.getOperand(1);
7071 SDValue N2 = Op.getOperand(2);
7072
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007073 // If this is a 256-bit vector result, first extract the 128-bit vector,
7074 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007075 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007076 if (!isa<ConstantSDNode>(N2))
7077 return SDValue();
7078
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007079 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007080 unsigned NumElems = VT.getVectorNumElements();
7081 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007082 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007083
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007084 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007085 bool Upper = IdxVal >= NumElems/2;
7086 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7087 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007088
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007089 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007090 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007091 }
7092
Craig Topperd0a31172012-01-10 06:37:29 +00007093 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007094 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7095
Dan Gohman8a55ce42009-09-23 21:02:20 +00007096 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007097 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007098
Dan Gohman8a55ce42009-09-23 21:02:20 +00007099 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007100 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7101 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007102 if (N1.getValueType() != MVT::i32)
7103 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7104 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007105 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007106 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007107 }
Dan Gohman475871a2008-07-27 21:46:04 +00007108 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007109}
7110
Dan Gohman475871a2008-07-27 21:46:04 +00007111SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007112X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007113 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007114 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007115 EVT OpVT = Op.getValueType();
7116
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007117 // If this is a 256-bit vector result, first insert into a 128-bit
7118 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007119 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007120 // Insert into a 128-bit vector.
7121 EVT VT128 = EVT::getVectorVT(*Context,
7122 OpVT.getVectorElementType(),
7123 OpVT.getVectorNumElements() / 2);
7124
7125 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7126
7127 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007128 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007129 }
7130
Craig Topperd77d2fe2012-04-29 20:22:05 +00007131 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007132 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007133 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007134
Owen Anderson825b72b2009-08-11 20:47:22 +00007135 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007136 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007137 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007138 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007139}
7140
David Greene91585092011-01-26 15:38:49 +00007141// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7142// a simple subregister reference or explicit instructions to grab
7143// upper bits of a vector.
7144SDValue
7145X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7146 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007147 DebugLoc dl = Op.getNode()->getDebugLoc();
7148 SDValue Vec = Op.getNode()->getOperand(0);
7149 SDValue Idx = Op.getNode()->getOperand(1);
7150
Craig Topper7a9a28b2012-08-12 02:23:29 +00007151 if (Op.getNode()->getValueType(0).is128BitVector() &&
7152 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007153 isa<ConstantSDNode>(Idx)) {
7154 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7155 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007156 }
David Greene91585092011-01-26 15:38:49 +00007157 }
7158 return SDValue();
7159}
7160
David Greenecfe33c42011-01-26 19:13:22 +00007161// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7162// simple superregister reference or explicit instructions to insert
7163// the upper bits of a vector.
7164SDValue
7165X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7166 if (Subtarget->hasAVX()) {
7167 DebugLoc dl = Op.getNode()->getDebugLoc();
7168 SDValue Vec = Op.getNode()->getOperand(0);
7169 SDValue SubVec = Op.getNode()->getOperand(1);
7170 SDValue Idx = Op.getNode()->getOperand(2);
7171
Craig Topper7a9a28b2012-08-12 02:23:29 +00007172 if (Op.getNode()->getValueType(0).is256BitVector() &&
7173 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007174 isa<ConstantSDNode>(Idx)) {
7175 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7176 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007177 }
7178 }
7179 return SDValue();
7180}
7181
Bill Wendling056292f2008-09-16 21:48:12 +00007182// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7183// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7184// one of the above mentioned nodes. It has to be wrapped because otherwise
7185// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7186// be used to form addressing mode. These wrapped nodes will be selected
7187// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007188SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007189X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007190 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007191
Chris Lattner41621a22009-06-26 19:22:52 +00007192 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7193 // global base reg.
7194 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007195 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007196 CodeModel::Model M = getTargetMachine().getCodeModel();
7197
Chris Lattner4f066492009-07-11 20:29:19 +00007198 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007199 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007200 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007201 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007202 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007203 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007204 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007205
Evan Cheng1606e8e2009-03-13 07:51:59 +00007206 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007207 CP->getAlignment(),
7208 CP->getOffset(), OpFlag);
7209 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007210 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007211 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007212 if (OpFlag) {
7213 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007214 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007215 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007216 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007217 }
7218
7219 return Result;
7220}
7221
Dan Gohmand858e902010-04-17 15:26:15 +00007222SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007223 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007224
Chris Lattner18c59872009-06-27 04:16:01 +00007225 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7226 // global base reg.
7227 unsigned char OpFlag = 0;
7228 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007229 CodeModel::Model M = getTargetMachine().getCodeModel();
7230
Chris Lattner4f066492009-07-11 20:29:19 +00007231 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007232 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007233 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007234 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007235 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007236 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007237 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007238
Chris Lattner18c59872009-06-27 04:16:01 +00007239 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7240 OpFlag);
7241 DebugLoc DL = JT->getDebugLoc();
7242 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007243
Chris Lattner18c59872009-06-27 04:16:01 +00007244 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007245 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007246 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7247 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007248 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007249 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007250
Chris Lattner18c59872009-06-27 04:16:01 +00007251 return Result;
7252}
7253
7254SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007255X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007256 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007257
Chris Lattner18c59872009-06-27 04:16:01 +00007258 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7259 // global base reg.
7260 unsigned char OpFlag = 0;
7261 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007262 CodeModel::Model M = getTargetMachine().getCodeModel();
7263
Chris Lattner4f066492009-07-11 20:29:19 +00007264 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007265 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7266 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7267 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007268 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007269 } else if (Subtarget->isPICStyleGOT()) {
7270 OpFlag = X86II::MO_GOT;
7271 } else if (Subtarget->isPICStyleStubPIC()) {
7272 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7273 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7274 OpFlag = X86II::MO_DARWIN_NONLAZY;
7275 }
Eric Christopherfd179292009-08-27 18:07:15 +00007276
Chris Lattner18c59872009-06-27 04:16:01 +00007277 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007278
Chris Lattner18c59872009-06-27 04:16:01 +00007279 DebugLoc DL = Op.getDebugLoc();
7280 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007281
7282
Chris Lattner18c59872009-06-27 04:16:01 +00007283 // With PIC, the address is actually $g + Offset.
7284 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007285 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007286 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7287 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007288 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007289 Result);
7290 }
Eric Christopherfd179292009-08-27 18:07:15 +00007291
Eli Friedman586272d2011-08-11 01:48:05 +00007292 // For symbols that require a load from a stub to get the address, emit the
7293 // load.
7294 if (isGlobalStubReference(OpFlag))
7295 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007296 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007297
Chris Lattner18c59872009-06-27 04:16:01 +00007298 return Result;
7299}
7300
Dan Gohman475871a2008-07-27 21:46:04 +00007301SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007302X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007303 // Create the TargetBlockAddressAddress node.
7304 unsigned char OpFlags =
7305 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007306 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007307 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007308 DebugLoc dl = Op.getDebugLoc();
7309 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7310 /*isTarget=*/true, OpFlags);
7311
Dan Gohmanf705adb2009-10-30 01:28:02 +00007312 if (Subtarget->isPICStyleRIPRel() &&
7313 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007314 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7315 else
7316 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007317
Dan Gohman29cbade2009-11-20 23:18:13 +00007318 // With PIC, the address is actually $g + Offset.
7319 if (isGlobalRelativeToPICBase(OpFlags)) {
7320 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7321 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7322 Result);
7323 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007324
7325 return Result;
7326}
7327
7328SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007329X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007330 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007331 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007332 // Create the TargetGlobalAddress node, folding in the constant
7333 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007334 unsigned char OpFlags =
7335 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007336 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007337 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007338 if (OpFlags == X86II::MO_NO_FLAG &&
7339 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007340 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007341 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007342 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007343 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007344 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007345 }
Eric Christopherfd179292009-08-27 18:07:15 +00007346
Chris Lattner4f066492009-07-11 20:29:19 +00007347 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007348 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007349 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7350 else
7351 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007352
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007353 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007354 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007355 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7356 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007357 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007358 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007359
Chris Lattner36c25012009-07-10 07:34:39 +00007360 // For globals that require a load from a stub to get the address, emit the
7361 // load.
7362 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007363 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007364 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007365
Dan Gohman6520e202008-10-18 02:06:02 +00007366 // If there was a non-zero offset that we didn't fold, create an explicit
7367 // addition for it.
7368 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007369 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007370 DAG.getConstant(Offset, getPointerTy()));
7371
Evan Cheng0db9fe62006-04-25 20:13:52 +00007372 return Result;
7373}
7374
Evan Chengda43bcf2008-09-24 00:05:32 +00007375SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007376X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007377 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007378 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007379 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007380}
7381
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007382static SDValue
7383GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007384 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007385 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007386 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007387 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007388 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007389 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007390 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007391 GA->getOffset(),
7392 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007393
7394 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7395 : X86ISD::TLSADDR;
7396
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007397 if (InFlag) {
7398 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007399 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007400 } else {
7401 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007402 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007403 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007404
7405 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007406 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007407
Rafael Espindola15f1b662009-04-24 12:59:40 +00007408 SDValue Flag = Chain.getValue(1);
7409 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007410}
7411
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007412// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007413static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007414LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007415 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007416 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007417 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7418 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007419 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007420 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007421 InFlag = Chain.getValue(1);
7422
Chris Lattnerb903bed2009-06-26 21:20:29 +00007423 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007424}
7425
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007426// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007427static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007428LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007429 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007430 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7431 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007432}
7433
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007434static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7435 SelectionDAG &DAG,
7436 const EVT PtrVT,
7437 bool is64Bit) {
7438 DebugLoc dl = GA->getDebugLoc();
7439
7440 // Get the start address of the TLS block for this module.
7441 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7442 .getInfo<X86MachineFunctionInfo>();
7443 MFI->incNumLocalDynamicTLSAccesses();
7444
7445 SDValue Base;
7446 if (is64Bit) {
7447 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7448 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7449 } else {
7450 SDValue InFlag;
7451 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7452 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7453 InFlag = Chain.getValue(1);
7454 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7455 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7456 }
7457
7458 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7459 // of Base.
7460
7461 // Build x@dtpoff.
7462 unsigned char OperandFlags = X86II::MO_DTPOFF;
7463 unsigned WrapperKind = X86ISD::Wrapper;
7464 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7465 GA->getValueType(0),
7466 GA->getOffset(), OperandFlags);
7467 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7468
7469 // Add x@dtpoff with the base.
7470 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7471}
7472
Hans Wennborg228756c2012-05-11 10:11:01 +00007473// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007474static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007475 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007476 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007477 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007478
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007479 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7480 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7481 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007482
Michael J. Spencerec38de22010-10-10 22:04:20 +00007483 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007484 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007485 MachinePointerInfo(Ptr),
7486 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007487
Chris Lattnerb903bed2009-06-26 21:20:29 +00007488 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007489 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7490 // initialexec.
7491 unsigned WrapperKind = X86ISD::Wrapper;
7492 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007493 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007494 } else if (model == TLSModel::InitialExec) {
7495 if (is64Bit) {
7496 OperandFlags = X86II::MO_GOTTPOFF;
7497 WrapperKind = X86ISD::WrapperRIP;
7498 } else {
7499 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7500 }
Chris Lattner18c59872009-06-27 04:16:01 +00007501 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007502 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007503 }
Eric Christopherfd179292009-08-27 18:07:15 +00007504
Hans Wennborg228756c2012-05-11 10:11:01 +00007505 // emit "addl x@ntpoff,%eax" (local exec)
7506 // or "addl x@indntpoff,%eax" (initial exec)
7507 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007508 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007509 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007510 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007511 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007512
Hans Wennborg228756c2012-05-11 10:11:01 +00007513 if (model == TLSModel::InitialExec) {
7514 if (isPIC && !is64Bit) {
7515 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7516 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7517 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007518 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007519
7520 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7521 MachinePointerInfo::getGOT(), false, false, false,
7522 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007523 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007524
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007525 // The address of the thread local variable is the add of the thread
7526 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007527 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007528}
7529
Dan Gohman475871a2008-07-27 21:46:04 +00007530SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007531X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007532
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007533 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007534 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007535
Eric Christopher30ef0e52010-06-03 04:07:48 +00007536 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007537 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007538
Eric Christopher30ef0e52010-06-03 04:07:48 +00007539 switch (model) {
7540 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007541 if (Subtarget->is64Bit())
7542 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7543 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007544 case TLSModel::LocalDynamic:
7545 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7546 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007547 case TLSModel::InitialExec:
7548 case TLSModel::LocalExec:
7549 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007550 Subtarget->is64Bit(),
7551 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007552 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007553 llvm_unreachable("Unknown TLS model.");
7554 }
7555
7556 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007557 // Darwin only has one model of TLS. Lower to that.
7558 unsigned char OpFlag = 0;
7559 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7560 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007561
Eric Christopher30ef0e52010-06-03 04:07:48 +00007562 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7563 // global base reg.
7564 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7565 !Subtarget->is64Bit();
7566 if (PIC32)
7567 OpFlag = X86II::MO_TLVP_PIC_BASE;
7568 else
7569 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007570 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007571 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007572 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007573 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007574 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007575
Eric Christopher30ef0e52010-06-03 04:07:48 +00007576 // With PIC32, the address is actually $g + Offset.
7577 if (PIC32)
7578 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7579 DAG.getNode(X86ISD::GlobalBaseReg,
7580 DebugLoc(), getPointerTy()),
7581 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007582
Eric Christopher30ef0e52010-06-03 04:07:48 +00007583 // Lowering the machine isd will make sure everything is in the right
7584 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007585 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007586 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007587 SDValue Args[] = { Chain, Offset };
7588 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007589
Eric Christopher30ef0e52010-06-03 04:07:48 +00007590 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7591 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7592 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007593
Eric Christopher30ef0e52010-06-03 04:07:48 +00007594 // And our return value (tls address) is in the standard call return value
7595 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007596 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007597 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7598 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007599 }
7600
7601 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007602 // Just use the implicit TLS architecture
7603 // Need to generate someting similar to:
7604 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7605 // ; from TEB
7606 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7607 // mov rcx, qword [rdx+rcx*8]
7608 // mov eax, .tls$:tlsvar
7609 // [rax+rcx] contains the address
7610 // Windows 64bit: gs:0x58
7611 // Windows 32bit: fs:__tls_array
7612
7613 // If GV is an alias then use the aliasee for determining
7614 // thread-localness.
7615 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7616 GV = GA->resolveAliasedGlobal(false);
7617 DebugLoc dl = GA->getDebugLoc();
7618 SDValue Chain = DAG.getEntryNode();
7619
7620 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7621 // %gs:0x58 (64-bit).
7622 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7623 ? Type::getInt8PtrTy(*DAG.getContext(),
7624 256)
7625 : Type::getInt32PtrTy(*DAG.getContext(),
7626 257));
7627
7628 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7629 Subtarget->is64Bit()
7630 ? DAG.getIntPtrConstant(0x58)
7631 : DAG.getExternalSymbol("_tls_array",
7632 getPointerTy()),
7633 MachinePointerInfo(Ptr),
7634 false, false, false, 0);
7635
7636 // Load the _tls_index variable
7637 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7638 if (Subtarget->is64Bit())
7639 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7640 IDX, MachinePointerInfo(), MVT::i32,
7641 false, false, 0);
7642 else
7643 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7644 false, false, false, 0);
7645
7646 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007647 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007648 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7649
7650 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7651 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7652 false, false, false, 0);
7653
7654 // Get the offset of start of .tls section
7655 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7656 GA->getValueType(0),
7657 GA->getOffset(), X86II::MO_SECREL);
7658 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7659
7660 // The address of the thread local variable is the add of the thread
7661 // pointer with the offset of the variable.
7662 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007663 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007664
David Blaikie4d6ccb52012-01-20 21:51:11 +00007665 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007666}
7667
Evan Cheng0db9fe62006-04-25 20:13:52 +00007668
Chad Rosierb90d2a92012-01-03 23:19:12 +00007669/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7670/// and take a 2 x i32 value to shift plus a shift amount.
7671SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007672 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007673 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007674 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007675 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007676 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007677 SDValue ShOpLo = Op.getOperand(0);
7678 SDValue ShOpHi = Op.getOperand(1);
7679 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007680 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007681 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007682 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007683
Dan Gohman475871a2008-07-27 21:46:04 +00007684 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007685 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007686 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7687 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007688 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007689 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7690 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007691 }
Evan Chenge3413162006-01-09 18:33:28 +00007692
Owen Anderson825b72b2009-08-11 20:47:22 +00007693 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7694 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007695 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007696 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007697
Dan Gohman475871a2008-07-27 21:46:04 +00007698 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007699 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007700 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7701 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007702
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007703 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007704 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7705 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007706 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007707 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7708 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007709 }
7710
Dan Gohman475871a2008-07-27 21:46:04 +00007711 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007712 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007713}
Evan Chenga3195e82006-01-12 22:54:21 +00007714
Dan Gohmand858e902010-04-17 15:26:15 +00007715SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7716 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007717 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007718
Dale Johannesen0488fb62010-09-30 23:57:10 +00007719 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007720 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007721
Owen Anderson825b72b2009-08-11 20:47:22 +00007722 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007723 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007724
Eli Friedman36df4992009-05-27 00:47:34 +00007725 // These are really Legal; return the operand so the caller accepts it as
7726 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007727 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007728 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007729 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007730 Subtarget->is64Bit()) {
7731 return Op;
7732 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007733
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007734 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007735 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007736 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007737 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007738 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007739 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007740 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007741 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007742 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007743 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7744}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007745
Owen Andersone50ed302009-08-10 22:56:29 +00007746SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007747 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007748 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007749 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007750 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007751 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007752 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007753 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007754 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007755 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007756 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007757
Chris Lattner492a43e2010-09-22 01:28:21 +00007758 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007759
Stuart Hastings84be9582011-06-02 15:57:11 +00007760 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7761 MachineMemOperand *MMO;
7762 if (FI) {
7763 int SSFI = FI->getIndex();
7764 MMO =
7765 DAG.getMachineFunction()
7766 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7767 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7768 } else {
7769 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7770 StackSlot = StackSlot.getOperand(1);
7771 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007772 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007773 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7774 X86ISD::FILD, DL,
7775 Tys, Ops, array_lengthof(Ops),
7776 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007778 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007779 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007780 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007781
7782 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7783 // shouldn't be necessary except that RFP cannot be live across
7784 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007785 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007786 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7787 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007788 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007789 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007790 SDValue Ops[] = {
7791 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7792 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007793 MachineMemOperand *MMO =
7794 DAG.getMachineFunction()
7795 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007796 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007797
Chris Lattner492a43e2010-09-22 01:28:21 +00007798 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7799 Ops, array_lengthof(Ops),
7800 Op.getValueType(), MMO);
7801 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007802 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007803 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007804 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007805
Evan Cheng0db9fe62006-04-25 20:13:52 +00007806 return Result;
7807}
7808
Bill Wendling8b8a6362009-01-17 03:56:04 +00007809// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007810SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7811 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007812 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007813 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007814 movq %rax, %xmm0
7815 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7816 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7817 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007818 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007819 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007820 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007821 addpd %xmm1, %xmm0
7822 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007823 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007824
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007825 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007826 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007827
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007828 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007829 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7830 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007831 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007832
Chris Lattner97484792012-01-25 09:56:22 +00007833 SmallVector<Constant*,2> CV1;
7834 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007835 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007836 CV1.push_back(
7837 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7838 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007839 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007840
Bill Wendling397ae212012-01-05 02:13:20 +00007841 // Load the 64-bit value into an XMM register.
7842 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7843 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007844 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007845 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007846 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007847 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7848 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7849 CLod0);
7850
Owen Anderson825b72b2009-08-11 20:47:22 +00007851 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007852 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007853 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007854 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007856 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007857
Craig Topperd0a31172012-01-10 06:37:29 +00007858 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007859 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7860 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7861 } else {
7862 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7863 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7864 S2F, 0x4E, DAG);
7865 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7866 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7867 Sub);
7868 }
7869
7870 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007871 DAG.getIntPtrConstant(0));
7872}
7873
Bill Wendling8b8a6362009-01-17 03:56:04 +00007874// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007875SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7876 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007877 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007878 // FP constant to bias correct the final result.
7879 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007881
7882 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007883 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007884 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007885
Eli Friedmanf3704762011-08-29 21:15:46 +00007886 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007887 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007888
Owen Anderson825b72b2009-08-11 20:47:22 +00007889 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007890 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007891 DAG.getIntPtrConstant(0));
7892
7893 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007894 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007895 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007896 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007897 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007898 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007899 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 MVT::v2f64, Bias)));
7901 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007902 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007903 DAG.getIntPtrConstant(0));
7904
7905 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007906 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007907
7908 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007909 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007910
Craig Topper69947b92012-04-23 06:57:04 +00007911 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007912 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007913 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007914 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007915 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007916
7917 // Handle final rounding.
7918 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007919}
7920
Dan Gohmand858e902010-04-17 15:26:15 +00007921SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7922 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007923 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007924 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007925
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007926 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007927 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7928 // the optimization here.
7929 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007930 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007931
Owen Andersone50ed302009-08-10 22:56:29 +00007932 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007933 EVT DstVT = Op.getValueType();
7934 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007935 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007936 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007937 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007938 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007939 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007940
7941 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007942 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007943 if (SrcVT == MVT::i32) {
7944 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7945 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7946 getPointerTy(), StackSlot, WordOff);
7947 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007948 StackSlot, MachinePointerInfo(),
7949 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007950 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007951 OffsetSlot, MachinePointerInfo(),
7952 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007953 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7954 return Fild;
7955 }
7956
7957 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7958 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007959 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007960 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007961 // For i64 source, we need to add the appropriate power of 2 if the input
7962 // was negative. This is the same as the optimization in
7963 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7964 // we must be careful to do the computation in x87 extended precision, not
7965 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007966 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7967 MachineMemOperand *MMO =
7968 DAG.getMachineFunction()
7969 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7970 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007971
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007972 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7973 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007974 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7975 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007976
7977 APInt FF(32, 0x5F800000ULL);
7978
7979 // Check whether the sign bit is set.
7980 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7981 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7982 ISD::SETLT);
7983
7984 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7985 SDValue FudgePtr = DAG.getConstantPool(
7986 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7987 getPointerTy());
7988
7989 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7990 SDValue Zero = DAG.getIntPtrConstant(0);
7991 SDValue Four = DAG.getIntPtrConstant(4);
7992 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7993 Zero, Four);
7994 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7995
7996 // Load the value out, extending it from f32 to f80.
7997 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007998 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007999 FudgePtr, MachinePointerInfo::getConstantPool(),
8000 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008001 // Extend everything to 80 bits to force it to be done on x87.
8002 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8003 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008004}
8005
Dan Gohman475871a2008-07-27 21:46:04 +00008006std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008007FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008008 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008009
Owen Andersone50ed302009-08-10 22:56:29 +00008010 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008011
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008012 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008013 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8014 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008015 }
8016
Owen Anderson825b72b2009-08-11 20:47:22 +00008017 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8018 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008019 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008020
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008021 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008022 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008023 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008024 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008025 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008026 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008027 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008028 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008029
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008030 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8031 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008032 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008033 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008034 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008035 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008036
Evan Cheng0db9fe62006-04-25 20:13:52 +00008037 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008038 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8039 Opc = X86ISD::WIN_FTOL;
8040 else
8041 switch (DstTy.getSimpleVT().SimpleTy) {
8042 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8043 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8044 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8045 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8046 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008047
Dan Gohman475871a2008-07-27 21:46:04 +00008048 SDValue Chain = DAG.getEntryNode();
8049 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008050 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008051 // FIXME This causes a redundant load/store if the SSE-class value is already
8052 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008053 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008054 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008055 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008056 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008057 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008059 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008060 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008061 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008062
Chris Lattner492a43e2010-09-22 01:28:21 +00008063 MachineMemOperand *MMO =
8064 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8065 MachineMemOperand::MOLoad, MemSize, MemSize);
8066 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8067 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008068 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008069 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008070 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8071 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008072
Chris Lattner07290932010-09-22 01:05:16 +00008073 MachineMemOperand *MMO =
8074 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8075 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008076
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008077 if (Opc != X86ISD::WIN_FTOL) {
8078 // Build the FP_TO_INT*_IN_MEM
8079 SDValue Ops[] = { Chain, Value, StackSlot };
8080 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8081 Ops, 3, DstTy, MMO);
8082 return std::make_pair(FIST, StackSlot);
8083 } else {
8084 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8085 DAG.getVTList(MVT::Other, MVT::Glue),
8086 Chain, Value);
8087 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8088 MVT::i32, ftol.getValue(1));
8089 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8090 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008091 SDValue Ops[] = { eax, edx };
8092 SDValue pair = IsReplace
8093 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8094 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008095 return std::make_pair(pair, SDValue());
8096 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008097}
8098
Dan Gohmand858e902010-04-17 15:26:15 +00008099SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8100 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008101 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008102 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008103
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008104 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8105 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008106 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008107 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8108 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008109
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008110 if (StackSlot.getNode())
8111 // Load the result.
8112 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8113 FIST, StackSlot, MachinePointerInfo(),
8114 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008115
8116 // The node is the result.
8117 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008118}
8119
Dan Gohmand858e902010-04-17 15:26:15 +00008120SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8121 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008122 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8123 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008124 SDValue FIST = Vals.first, StackSlot = Vals.second;
8125 assert(FIST.getNode() && "Unexpected failure");
8126
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008127 if (StackSlot.getNode())
8128 // Load the result.
8129 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8130 FIST, StackSlot, MachinePointerInfo(),
8131 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008132
8133 // The node is the result.
8134 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008135}
8136
Dan Gohmand858e902010-04-17 15:26:15 +00008137SDValue X86TargetLowering::LowerFABS(SDValue Op,
8138 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008139 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008140 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008141 EVT VT = Op.getValueType();
8142 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008143 if (VT.isVector())
8144 EltVT = VT.getVectorElementType();
Chris Lattner4ca829e2012-01-25 06:02:56 +00008145 Constant *C;
Owen Anderson825b72b2009-08-11 20:47:22 +00008146 if (EltVT == MVT::f64) {
Chad Rosiera20e1e72012-08-01 18:39:17 +00008147 C = ConstantVector::getSplat(2,
Chris Lattner4ca829e2012-01-25 06:02:56 +00008148 ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008149 } else {
Chris Lattner4ca829e2012-01-25 06:02:56 +00008150 C = ConstantVector::getSplat(4,
8151 ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008152 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00008153 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008154 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008155 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008156 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008157 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008158}
8159
Dan Gohmand858e902010-04-17 15:26:15 +00008160SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008161 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008162 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008163 EVT VT = Op.getValueType();
8164 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008165 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8166 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008167 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008168 NumElts = VT.getVectorNumElements();
8169 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008170 Constant *C;
8171 if (EltVT == MVT::f64)
8172 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8173 else
8174 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8175 C = ConstantVector::getSplat(NumElts, C);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008176 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008177 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008178 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008179 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008180 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008181 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008182 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008183 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008184 DAG.getNode(ISD::BITCAST, dl, XORVT,
8185 Op.getOperand(0)),
8186 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008187 }
Craig Topper69947b92012-04-23 06:57:04 +00008188
8189 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008190}
8191
Dan Gohmand858e902010-04-17 15:26:15 +00008192SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008193 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008194 SDValue Op0 = Op.getOperand(0);
8195 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008196 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008197 EVT VT = Op.getValueType();
8198 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008199
8200 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008201 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008202 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008203 SrcVT = VT;
8204 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008205 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008206 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008207 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008208 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008209 }
8210
8211 // At this point the operands and the result should have the same
8212 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008213
Evan Cheng68c47cb2007-01-05 07:55:56 +00008214 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008215 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008216 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008217 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8218 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008219 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008220 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8221 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8222 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8223 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008224 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008225 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008226 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008227 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008228 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008229 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008230 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008231
8232 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008233 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008234 // Op0 is MVT::f32, Op1 is MVT::f64.
8235 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8236 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8237 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008238 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008239 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008240 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008241 }
8242
Evan Cheng73d6cf12007-01-05 21:37:56 +00008243 // Clear first operand sign bit.
8244 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008245 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008246 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8247 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008248 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008249 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8250 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8251 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8252 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008253 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008254 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008255 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008256 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008257 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008258 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008259 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008260
8261 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008262 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008263}
8264
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008265SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8266 SDValue N0 = Op.getOperand(0);
8267 DebugLoc dl = Op.getDebugLoc();
8268 EVT VT = Op.getValueType();
8269
8270 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8271 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8272 DAG.getConstant(1, VT));
8273 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8274}
8275
Dan Gohman076aee32009-03-04 19:44:21 +00008276/// Emit nodes that will be selected as "test Op0,Op0", or something
8277/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008278SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008279 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008280 DebugLoc dl = Op.getDebugLoc();
8281
Dan Gohman31125812009-03-07 01:58:32 +00008282 // CF and OF aren't always set the way we want. Determine which
8283 // of these we need.
8284 bool NeedCF = false;
8285 bool NeedOF = false;
8286 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008287 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008288 case X86::COND_A: case X86::COND_AE:
8289 case X86::COND_B: case X86::COND_BE:
8290 NeedCF = true;
8291 break;
8292 case X86::COND_G: case X86::COND_GE:
8293 case X86::COND_L: case X86::COND_LE:
8294 case X86::COND_O: case X86::COND_NO:
8295 NeedOF = true;
8296 break;
Dan Gohman31125812009-03-07 01:58:32 +00008297 }
8298
Dan Gohman076aee32009-03-04 19:44:21 +00008299 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008300 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8301 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008302 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8303 // Emit a CMP with 0, which is the TEST pattern.
8304 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8305 DAG.getConstant(0, Op.getValueType()));
8306
8307 unsigned Opcode = 0;
8308 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008309
8310 // Truncate operations may prevent the merge of the SETCC instruction
8311 // and the arithmetic intruction before it. Attempt to truncate the operands
8312 // of the arithmetic instruction and use a reduced bit-width instruction.
8313 bool NeedTruncation = false;
8314 SDValue ArithOp = Op;
8315 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8316 SDValue Arith = Op->getOperand(0);
8317 // Both the trunc and the arithmetic op need to have one user each.
8318 if (Arith->hasOneUse())
8319 switch (Arith.getOpcode()) {
8320 default: break;
8321 case ISD::ADD:
8322 case ISD::SUB:
8323 case ISD::AND:
8324 case ISD::OR:
8325 case ISD::XOR: {
8326 NeedTruncation = true;
8327 ArithOp = Arith;
8328 }
8329 }
8330 }
8331
8332 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8333 // which may be the result of a CAST. We use the variable 'Op', which is the
8334 // non-casted variable when we check for possible users.
8335 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008336 case ISD::ADD:
8337 // Due to an isel shortcoming, be conservative if this add is likely to be
8338 // selected as part of a load-modify-store instruction. When the root node
8339 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8340 // uses of other nodes in the match, such as the ADD in this case. This
8341 // leads to the ADD being left around and reselected, with the result being
8342 // two adds in the output. Alas, even if none our users are stores, that
8343 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8344 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8345 // climbing the DAG back to the root, and it doesn't seem to be worth the
8346 // effort.
8347 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008348 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8349 if (UI->getOpcode() != ISD::CopyToReg &&
8350 UI->getOpcode() != ISD::SETCC &&
8351 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008352 goto default_case;
8353
8354 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008355 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008356 // An add of one will be selected as an INC.
8357 if (C->getAPIntValue() == 1) {
8358 Opcode = X86ISD::INC;
8359 NumOperands = 1;
8360 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008361 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008362
8363 // An add of negative one (subtract of one) will be selected as a DEC.
8364 if (C->getAPIntValue().isAllOnesValue()) {
8365 Opcode = X86ISD::DEC;
8366 NumOperands = 1;
8367 break;
8368 }
Dan Gohman076aee32009-03-04 19:44:21 +00008369 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008370
8371 // Otherwise use a regular EFLAGS-setting add.
8372 Opcode = X86ISD::ADD;
8373 NumOperands = 2;
8374 break;
8375 case ISD::AND: {
8376 // If the primary and result isn't used, don't bother using X86ISD::AND,
8377 // because a TEST instruction will be better.
8378 bool NonFlagUse = false;
8379 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8380 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8381 SDNode *User = *UI;
8382 unsigned UOpNo = UI.getOperandNo();
8383 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8384 // Look pass truncate.
8385 UOpNo = User->use_begin().getOperandNo();
8386 User = *User->use_begin();
8387 }
8388
8389 if (User->getOpcode() != ISD::BRCOND &&
8390 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008391 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008392 NonFlagUse = true;
8393 break;
8394 }
Dan Gohman076aee32009-03-04 19:44:21 +00008395 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008396
8397 if (!NonFlagUse)
8398 break;
8399 }
8400 // FALL THROUGH
8401 case ISD::SUB:
8402 case ISD::OR:
8403 case ISD::XOR:
8404 // Due to the ISEL shortcoming noted above, be conservative if this op is
8405 // likely to be selected as part of a load-modify-store instruction.
8406 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8407 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8408 if (UI->getOpcode() == ISD::STORE)
8409 goto default_case;
8410
8411 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008412 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008413 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008414 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008415 case ISD::OR: Opcode = X86ISD::OR; break;
8416 case ISD::XOR: Opcode = X86ISD::XOR; break;
8417 case ISD::AND: Opcode = X86ISD::AND; break;
8418 }
8419
8420 NumOperands = 2;
8421 break;
8422 case X86ISD::ADD:
8423 case X86ISD::SUB:
8424 case X86ISD::INC:
8425 case X86ISD::DEC:
8426 case X86ISD::OR:
8427 case X86ISD::XOR:
8428 case X86ISD::AND:
8429 return SDValue(Op.getNode(), 1);
8430 default:
8431 default_case:
8432 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008433 }
8434
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008435 // If we found that truncation is beneficial, perform the truncation and
8436 // update 'Op'.
8437 if (NeedTruncation) {
8438 EVT VT = Op.getValueType();
8439 SDValue WideVal = Op->getOperand(0);
8440 EVT WideVT = WideVal.getValueType();
8441 unsigned ConvertedOp = 0;
8442 // Use a target machine opcode to prevent further DAGCombine
8443 // optimizations that may separate the arithmetic operations
8444 // from the setcc node.
8445 switch (WideVal.getOpcode()) {
8446 default: break;
8447 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8448 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8449 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8450 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8451 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8452 }
8453
8454 if (ConvertedOp) {
8455 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8456 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8457 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8458 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8459 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8460 }
8461 }
8462 }
8463
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008464 if (Opcode == 0)
8465 // Emit a CMP with 0, which is the TEST pattern.
8466 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8467 DAG.getConstant(0, Op.getValueType()));
8468
8469 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8470 SmallVector<SDValue, 4> Ops;
8471 for (unsigned i = 0; i != NumOperands; ++i)
8472 Ops.push_back(Op.getOperand(i));
8473
8474 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8475 DAG.ReplaceAllUsesWith(Op, New);
8476 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008477}
8478
8479/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8480/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008481SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008482 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008483 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8484 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008485 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008486
8487 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008488 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8489 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8490 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8491 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8492 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8493 Op0, Op1);
8494 return SDValue(Sub.getNode(), 1);
8495 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008496 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008497}
8498
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008499/// Convert a comparison if required by the subtarget.
8500SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8501 SelectionDAG &DAG) const {
8502 // If the subtarget does not support the FUCOMI instruction, floating-point
8503 // comparisons have to be converted.
8504 if (Subtarget->hasCMov() ||
8505 Cmp.getOpcode() != X86ISD::CMP ||
8506 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8507 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8508 return Cmp;
8509
8510 // The instruction selector will select an FUCOM instruction instead of
8511 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8512 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8513 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8514 DebugLoc dl = Cmp.getDebugLoc();
8515 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8516 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8517 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8518 DAG.getConstant(8, MVT::i8));
8519 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8520 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8521}
8522
Evan Chengd40d03e2010-01-06 19:38:29 +00008523/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8524/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008525SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8526 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008527 SDValue Op0 = And.getOperand(0);
8528 SDValue Op1 = And.getOperand(1);
8529 if (Op0.getOpcode() == ISD::TRUNCATE)
8530 Op0 = Op0.getOperand(0);
8531 if (Op1.getOpcode() == ISD::TRUNCATE)
8532 Op1 = Op1.getOperand(0);
8533
Evan Chengd40d03e2010-01-06 19:38:29 +00008534 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008535 if (Op1.getOpcode() == ISD::SHL)
8536 std::swap(Op0, Op1);
8537 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008538 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8539 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008540 // If we looked past a truncate, check that it's only truncating away
8541 // known zeros.
8542 unsigned BitWidth = Op0.getValueSizeInBits();
8543 unsigned AndBitWidth = And.getValueSizeInBits();
8544 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008545 APInt Zeros, Ones;
8546 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008547 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8548 return SDValue();
8549 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008550 LHS = Op1;
8551 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008552 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008553 } else if (Op1.getOpcode() == ISD::Constant) {
8554 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008555 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008556 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008557
8558 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008559 LHS = AndLHS.getOperand(0);
8560 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008561 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008562
8563 // Use BT if the immediate can't be encoded in a TEST instruction.
8564 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8565 LHS = AndLHS;
8566 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8567 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008568 }
Evan Cheng0488db92007-09-25 01:57:46 +00008569
Evan Chengd40d03e2010-01-06 19:38:29 +00008570 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008571 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008572 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008573 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008574 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008575 // Also promote i16 to i32 for performance / code size reason.
8576 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008577 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008578 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008579
Evan Chengd40d03e2010-01-06 19:38:29 +00008580 // If the operand types disagree, extend the shift amount to match. Since
8581 // BT ignores high bits (like shifts) we can use anyextend.
8582 if (LHS.getValueType() != RHS.getValueType())
8583 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008584
Evan Chengd40d03e2010-01-06 19:38:29 +00008585 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8586 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8587 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8588 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008589 }
8590
Evan Cheng54de3ea2010-01-05 06:52:31 +00008591 return SDValue();
8592}
8593
Dan Gohmand858e902010-04-17 15:26:15 +00008594SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008595
8596 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8597
Evan Cheng54de3ea2010-01-05 06:52:31 +00008598 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8599 SDValue Op0 = Op.getOperand(0);
8600 SDValue Op1 = Op.getOperand(1);
8601 DebugLoc dl = Op.getDebugLoc();
8602 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8603
8604 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008605 // Lower (X & (1 << N)) == 0 to BT(X, N).
8606 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8607 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008608 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008609 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008610 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008611 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8612 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8613 if (NewSetCC.getNode())
8614 return NewSetCC;
8615 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008616
Chris Lattner481eebc2010-12-19 21:23:48 +00008617 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8618 // these.
8619 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008620 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008621 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8622 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008623
Chris Lattner481eebc2010-12-19 21:23:48 +00008624 // If the input is a setcc, then reuse the input setcc or use a new one with
8625 // the inverted condition.
8626 if (Op0.getOpcode() == X86ISD::SETCC) {
8627 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8628 bool Invert = (CC == ISD::SETNE) ^
8629 cast<ConstantSDNode>(Op1)->isNullValue();
8630 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008631
Evan Cheng2c755ba2010-02-27 07:36:59 +00008632 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008633 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8634 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8635 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008636 }
8637
Evan Chenge5b51ac2010-04-17 06:13:15 +00008638 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008639 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008640 if (X86CC == X86::COND_INVALID)
8641 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008642
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008643 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008644 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008645 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008646 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008647}
8648
Craig Topper89af15e2011-09-18 08:03:58 +00008649// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008650// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008651static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008652 EVT VT = Op.getValueType();
8653
Craig Topper7a9a28b2012-08-12 02:23:29 +00008654 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008655 "Unsupported value type for operation");
8656
Craig Topper66ddd152012-04-27 22:54:43 +00008657 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008658 DebugLoc dl = Op.getDebugLoc();
8659 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008660
8661 // Extract the LHS vectors
8662 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008663 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8664 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008665
8666 // Extract the RHS vectors
8667 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008668 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8669 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008670
8671 // Issue the operation on the smaller types and concatenate the result back
8672 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8673 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8674 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8675 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8676 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8677}
8678
8679
Dan Gohmand858e902010-04-17 15:26:15 +00008680SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008681 SDValue Cond;
8682 SDValue Op0 = Op.getOperand(0);
8683 SDValue Op1 = Op.getOperand(1);
8684 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008685 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008686 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8687 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008688 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008689
8690 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008691#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008692 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008693 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8694#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008695
Craig Topper523908d2012-08-13 02:34:03 +00008696 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008697 bool Swap = false;
8698
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008699 // SSE Condition code mapping:
8700 // 0 - EQ
8701 // 1 - LT
8702 // 2 - LE
8703 // 3 - UNORD
8704 // 4 - NEQ
8705 // 5 - NLT
8706 // 6 - NLE
8707 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008708 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008709 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008710 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008711 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008712 case ISD::SETOGT:
8713 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008714 case ISD::SETLT:
8715 case ISD::SETOLT: SSECC = 1; break;
8716 case ISD::SETOGE:
8717 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008718 case ISD::SETLE:
8719 case ISD::SETOLE: SSECC = 2; break;
8720 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008721 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008722 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008723 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008724 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008725 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008726 case ISD::SETUGT: SSECC = 6; break;
8727 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008728 case ISD::SETUEQ:
8729 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008730 }
8731 if (Swap)
8732 std::swap(Op0, Op1);
8733
Nate Begemanfb8ead02008-07-25 19:05:58 +00008734 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008735 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008736 unsigned CC0, CC1;
8737 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008738 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008739 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8740 } else {
8741 assert(SetCCOpcode == ISD::SETONE);
8742 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008743 }
Craig Topper523908d2012-08-13 02:34:03 +00008744
8745 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8746 DAG.getConstant(CC0, MVT::i8));
8747 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8748 DAG.getConstant(CC1, MVT::i8));
8749 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008750 }
8751 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008752 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8753 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008755
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008756 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008757 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008758 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008759
Nate Begeman30a0de92008-07-17 16:51:19 +00008760 // We are handling one of the integer comparisons here. Since SSE only has
8761 // GT and EQ comparisons for integer, swapping operands and multiple
8762 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008763 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008764 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008765
Nate Begeman30a0de92008-07-17 16:51:19 +00008766 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008767 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008768 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008769 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008770 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008771 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008772 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008773 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008774 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008775 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008776 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008777 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008778 }
8779 if (Swap)
8780 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008781
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008782 // Check that the operation in question is available (most are plain SSE2,
8783 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008784 if (VT == MVT::v2i64) {
8785 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8786 return SDValue();
8787 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8788 return SDValue();
8789 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008790
Nate Begeman30a0de92008-07-17 16:51:19 +00008791 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8792 // bits of the inputs before performing those operations.
8793 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008794 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008795 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8796 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008797 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008798 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8799 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008800 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8801 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008802 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008803
Dale Johannesenace16102009-02-03 19:33:06 +00008804 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008805
8806 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008807 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008808 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008809
Nate Begeman30a0de92008-07-17 16:51:19 +00008810 return Result;
8811}
Evan Cheng0488db92007-09-25 01:57:46 +00008812
Evan Cheng370e5342008-12-03 08:38:43 +00008813// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008814static bool isX86LogicalCmp(SDValue Op) {
8815 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008816 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8817 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008818 return true;
8819 if (Op.getResNo() == 1 &&
8820 (Opc == X86ISD::ADD ||
8821 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008822 Opc == X86ISD::ADC ||
8823 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008824 Opc == X86ISD::SMUL ||
8825 Opc == X86ISD::UMUL ||
8826 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008827 Opc == X86ISD::DEC ||
8828 Opc == X86ISD::OR ||
8829 Opc == X86ISD::XOR ||
8830 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008831 return true;
8832
Chris Lattner9637d5b2010-12-05 07:49:54 +00008833 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8834 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008835
Dan Gohman076aee32009-03-04 19:44:21 +00008836 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008837}
8838
Chris Lattnera2b56002010-12-05 01:23:24 +00008839static bool isZero(SDValue V) {
8840 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8841 return C && C->isNullValue();
8842}
8843
Chris Lattner96908b12010-12-05 02:00:51 +00008844static bool isAllOnes(SDValue V) {
8845 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8846 return C && C->isAllOnesValue();
8847}
8848
Evan Chengb64dd5f2012-08-07 22:21:00 +00008849static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8850 if (V.getOpcode() != ISD::TRUNCATE)
8851 return false;
8852
8853 SDValue VOp0 = V.getOperand(0);
8854 unsigned InBits = VOp0.getValueSizeInBits();
8855 unsigned Bits = V.getValueSizeInBits();
8856 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8857}
8858
Dan Gohmand858e902010-04-17 15:26:15 +00008859SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008860 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008861 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008862 SDValue Op1 = Op.getOperand(1);
8863 SDValue Op2 = Op.getOperand(2);
8864 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008865 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008866
Dan Gohman1a492952009-10-20 16:22:37 +00008867 if (Cond.getOpcode() == ISD::SETCC) {
8868 SDValue NewCond = LowerSETCC(Cond, DAG);
8869 if (NewCond.getNode())
8870 Cond = NewCond;
8871 }
Evan Cheng734503b2006-09-11 02:19:56 +00008872
Chris Lattnera2b56002010-12-05 01:23:24 +00008873 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008874 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008875 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008876 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008877 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008878 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8879 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008880 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008881
Chris Lattnera2b56002010-12-05 01:23:24 +00008882 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008883
8884 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008885 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8886 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008887
8888 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008889 // Apply further optimizations for special cases
8890 // (select (x != 0), -1, 0) -> neg & sbb
8891 // (select (x == 0), 0, -1) -> neg & sbb
8892 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008893 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008894 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8895 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008896 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8897 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008898 CmpOp0);
8899 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8900 DAG.getConstant(X86::COND_B, MVT::i8),
8901 SDValue(Neg.getNode(), 1));
8902 return Res;
8903 }
8904
Chris Lattnera2b56002010-12-05 01:23:24 +00008905 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8906 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008907 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008908
Chris Lattner96908b12010-12-05 02:00:51 +00008909 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008910 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8911 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008912
Chris Lattner96908b12010-12-05 02:00:51 +00008913 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8914 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008915
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008916 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008917 if (N2C == 0 || !N2C->isNullValue())
8918 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8919 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008920 }
8921 }
8922
Chris Lattnera2b56002010-12-05 01:23:24 +00008923 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008924 if (Cond.getOpcode() == ISD::AND &&
8925 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8926 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008927 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008928 Cond = Cond.getOperand(0);
8929 }
8930
Evan Cheng3f41d662007-10-08 22:16:29 +00008931 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8932 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008933 unsigned CondOpcode = Cond.getOpcode();
8934 if (CondOpcode == X86ISD::SETCC ||
8935 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008936 CC = Cond.getOperand(0);
8937
Dan Gohman475871a2008-07-27 21:46:04 +00008938 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008939 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008940 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008941
Evan Cheng3f41d662007-10-08 22:16:29 +00008942 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008943 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008944 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008945 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008946
Chris Lattnerd1980a52009-03-12 06:52:53 +00008947 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8948 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008949 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008950 addTest = false;
8951 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008952 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8953 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8954 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8955 Cond.getOperand(0).getValueType() != MVT::i8)) {
8956 SDValue LHS = Cond.getOperand(0);
8957 SDValue RHS = Cond.getOperand(1);
8958 unsigned X86Opcode;
8959 unsigned X86Cond;
8960 SDVTList VTs;
8961 switch (CondOpcode) {
8962 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8963 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8964 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8965 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8966 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8967 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8968 default: llvm_unreachable("unexpected overflowing operator");
8969 }
8970 if (CondOpcode == ISD::UMULO)
8971 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8972 MVT::i32);
8973 else
8974 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8975
8976 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8977
8978 if (CondOpcode == ISD::UMULO)
8979 Cond = X86Op.getValue(2);
8980 else
8981 Cond = X86Op.getValue(1);
8982
8983 CC = DAG.getConstant(X86Cond, MVT::i8);
8984 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008985 }
8986
8987 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00008988 // Look pass the truncate if the high bits are known zero.
8989 if (isTruncWithZeroHighBitsInput(Cond, DAG))
8990 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00008991
8992 // We know the result of AND is compared against zero. Try to match
8993 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008994 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008995 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008996 if (NewSetCC.getNode()) {
8997 CC = NewSetCC.getOperand(0);
8998 Cond = NewSetCC.getOperand(1);
8999 addTest = false;
9000 }
9001 }
9002 }
9003
9004 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009005 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009006 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009007 }
9008
Benjamin Kramere915ff32010-12-22 23:09:28 +00009009 // a < b ? -1 : 0 -> RES = ~setcc_carry
9010 // a < b ? 0 : -1 -> RES = setcc_carry
9011 // a >= b ? -1 : 0 -> RES = setcc_carry
9012 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009013 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009014 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009015 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9016
9017 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9018 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9019 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9020 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9021 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9022 return DAG.getNOT(DL, Res, Res.getValueType());
9023 return Res;
9024 }
9025 }
9026
Evan Cheng0488db92007-09-25 01:57:46 +00009027 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9028 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009029 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009030 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009031 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009032}
9033
Evan Cheng370e5342008-12-03 08:38:43 +00009034// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9035// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9036// from the AND / OR.
9037static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9038 Opc = Op.getOpcode();
9039 if (Opc != ISD::OR && Opc != ISD::AND)
9040 return false;
9041 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9042 Op.getOperand(0).hasOneUse() &&
9043 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9044 Op.getOperand(1).hasOneUse());
9045}
9046
Evan Cheng961d6d42009-02-02 08:19:07 +00009047// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9048// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009049static bool isXor1OfSetCC(SDValue Op) {
9050 if (Op.getOpcode() != ISD::XOR)
9051 return false;
9052 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9053 if (N1C && N1C->getAPIntValue() == 1) {
9054 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9055 Op.getOperand(0).hasOneUse();
9056 }
9057 return false;
9058}
9059
Dan Gohmand858e902010-04-17 15:26:15 +00009060SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009061 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009062 SDValue Chain = Op.getOperand(0);
9063 SDValue Cond = Op.getOperand(1);
9064 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009065 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009066 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009067 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009068
Dan Gohman1a492952009-10-20 16:22:37 +00009069 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009070 // Check for setcc([su]{add,sub,mul}o == 0).
9071 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9072 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9073 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9074 Cond.getOperand(0).getResNo() == 1 &&
9075 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9076 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9077 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9078 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9079 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9080 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9081 Inverted = true;
9082 Cond = Cond.getOperand(0);
9083 } else {
9084 SDValue NewCond = LowerSETCC(Cond, DAG);
9085 if (NewCond.getNode())
9086 Cond = NewCond;
9087 }
Dan Gohman1a492952009-10-20 16:22:37 +00009088 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009089#if 0
9090 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009091 else if (Cond.getOpcode() == X86ISD::ADD ||
9092 Cond.getOpcode() == X86ISD::SUB ||
9093 Cond.getOpcode() == X86ISD::SMUL ||
9094 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009095 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009096#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009097
Evan Chengad9c0a32009-12-15 00:53:42 +00009098 // Look pass (and (setcc_carry (cmp ...)), 1).
9099 if (Cond.getOpcode() == ISD::AND &&
9100 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9101 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009102 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009103 Cond = Cond.getOperand(0);
9104 }
9105
Evan Cheng3f41d662007-10-08 22:16:29 +00009106 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9107 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009108 unsigned CondOpcode = Cond.getOpcode();
9109 if (CondOpcode == X86ISD::SETCC ||
9110 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009111 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009112
Dan Gohman475871a2008-07-27 21:46:04 +00009113 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009114 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009115 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009116 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009117 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009118 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009119 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009120 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009121 default: break;
9122 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009123 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009124 // These can only come from an arithmetic instruction with overflow,
9125 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009126 Cond = Cond.getNode()->getOperand(1);
9127 addTest = false;
9128 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009129 }
Evan Cheng0488db92007-09-25 01:57:46 +00009130 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009131 }
9132 CondOpcode = Cond.getOpcode();
9133 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9134 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9135 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9136 Cond.getOperand(0).getValueType() != MVT::i8)) {
9137 SDValue LHS = Cond.getOperand(0);
9138 SDValue RHS = Cond.getOperand(1);
9139 unsigned X86Opcode;
9140 unsigned X86Cond;
9141 SDVTList VTs;
9142 switch (CondOpcode) {
9143 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9144 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9145 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9146 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9147 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9148 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9149 default: llvm_unreachable("unexpected overflowing operator");
9150 }
9151 if (Inverted)
9152 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9153 if (CondOpcode == ISD::UMULO)
9154 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9155 MVT::i32);
9156 else
9157 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9158
9159 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9160
9161 if (CondOpcode == ISD::UMULO)
9162 Cond = X86Op.getValue(2);
9163 else
9164 Cond = X86Op.getValue(1);
9165
9166 CC = DAG.getConstant(X86Cond, MVT::i8);
9167 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009168 } else {
9169 unsigned CondOpc;
9170 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9171 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009172 if (CondOpc == ISD::OR) {
9173 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9174 // two branches instead of an explicit OR instruction with a
9175 // separate test.
9176 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009177 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009178 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009179 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009180 Chain, Dest, CC, Cmp);
9181 CC = Cond.getOperand(1).getOperand(0);
9182 Cond = Cmp;
9183 addTest = false;
9184 }
9185 } else { // ISD::AND
9186 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9187 // two branches instead of an explicit AND instruction with a
9188 // separate test. However, we only do this if this block doesn't
9189 // have a fall-through edge, because this requires an explicit
9190 // jmp when the condition is false.
9191 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009192 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009193 Op.getNode()->hasOneUse()) {
9194 X86::CondCode CCode =
9195 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9196 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009197 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009198 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009199 // Look for an unconditional branch following this conditional branch.
9200 // We need this because we need to reverse the successors in order
9201 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009202 if (User->getOpcode() == ISD::BR) {
9203 SDValue FalseBB = User->getOperand(1);
9204 SDNode *NewBR =
9205 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009206 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009207 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009208 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009209
Dale Johannesene4d209d2009-02-03 20:21:25 +00009210 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009211 Chain, Dest, CC, Cmp);
9212 X86::CondCode CCode =
9213 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9214 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009215 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009216 Cond = Cmp;
9217 addTest = false;
9218 }
9219 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009220 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009221 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9222 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9223 // It should be transformed during dag combiner except when the condition
9224 // is set by a arithmetics with overflow node.
9225 X86::CondCode CCode =
9226 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9227 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009228 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009229 Cond = Cond.getOperand(0).getOperand(1);
9230 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009231 } else if (Cond.getOpcode() == ISD::SETCC &&
9232 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9233 // For FCMP_OEQ, we can emit
9234 // two branches instead of an explicit AND instruction with a
9235 // separate test. However, we only do this if this block doesn't
9236 // have a fall-through edge, because this requires an explicit
9237 // jmp when the condition is false.
9238 if (Op.getNode()->hasOneUse()) {
9239 SDNode *User = *Op.getNode()->use_begin();
9240 // Look for an unconditional branch following this conditional branch.
9241 // We need this because we need to reverse the successors in order
9242 // to implement FCMP_OEQ.
9243 if (User->getOpcode() == ISD::BR) {
9244 SDValue FalseBB = User->getOperand(1);
9245 SDNode *NewBR =
9246 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9247 assert(NewBR == User);
9248 (void)NewBR;
9249 Dest = FalseBB;
9250
9251 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9252 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009253 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009254 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9255 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9256 Chain, Dest, CC, Cmp);
9257 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9258 Cond = Cmp;
9259 addTest = false;
9260 }
9261 }
9262 } else if (Cond.getOpcode() == ISD::SETCC &&
9263 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9264 // For FCMP_UNE, we can emit
9265 // two branches instead of an explicit AND instruction with a
9266 // separate test. However, we only do this if this block doesn't
9267 // have a fall-through edge, because this requires an explicit
9268 // jmp when the condition is false.
9269 if (Op.getNode()->hasOneUse()) {
9270 SDNode *User = *Op.getNode()->use_begin();
9271 // Look for an unconditional branch following this conditional branch.
9272 // We need this because we need to reverse the successors in order
9273 // to implement FCMP_UNE.
9274 if (User->getOpcode() == ISD::BR) {
9275 SDValue FalseBB = User->getOperand(1);
9276 SDNode *NewBR =
9277 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9278 assert(NewBR == User);
9279 (void)NewBR;
9280
9281 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9282 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009283 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009284 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9285 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9286 Chain, Dest, CC, Cmp);
9287 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9288 Cond = Cmp;
9289 addTest = false;
9290 Dest = FalseBB;
9291 }
9292 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009293 }
Evan Cheng0488db92007-09-25 01:57:46 +00009294 }
9295
9296 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009297 // Look pass the truncate if the high bits are known zero.
9298 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9299 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009300
9301 // We know the result of AND is compared against zero. Try to match
9302 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009303 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009304 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9305 if (NewSetCC.getNode()) {
9306 CC = NewSetCC.getOperand(0);
9307 Cond = NewSetCC.getOperand(1);
9308 addTest = false;
9309 }
9310 }
9311 }
9312
9313 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009314 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009315 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009316 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009317 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009318 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009319 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009320}
9321
Anton Korobeynikove060b532007-04-17 19:34:00 +00009322
9323// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9324// Calls to _alloca is needed to probe the stack when allocating more than 4k
9325// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9326// that the guard pages used by the OS virtual memory manager are allocated in
9327// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009328SDValue
9329X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009330 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009331 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009332 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009333 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009334 "are being used");
9335 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009336 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009337
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009338 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009339 SDValue Chain = Op.getOperand(0);
9340 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009341 // FIXME: Ensure alignment here
9342
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009343 bool Is64Bit = Subtarget->is64Bit();
9344 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009345
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009346 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009347 MachineFunction &MF = DAG.getMachineFunction();
9348 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009349
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009350 if (Is64Bit) {
9351 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009352 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009353 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009354
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009355 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009356 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009357 if (I->hasNestAttr())
9358 report_fatal_error("Cannot use segmented stacks with functions that "
9359 "have nested arguments.");
9360 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009361
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009362 const TargetRegisterClass *AddrRegClass =
9363 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9364 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9365 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9366 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9367 DAG.getRegister(Vreg, SPTy));
9368 SDValue Ops1[2] = { Value, Chain };
9369 return DAG.getMergeValues(Ops1, 2, dl);
9370 } else {
9371 SDValue Flag;
9372 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009373
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009374 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9375 Flag = Chain.getValue(1);
9376 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009377
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009378 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9379 Flag = Chain.getValue(1);
9380
9381 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9382
9383 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9384 return DAG.getMergeValues(Ops1, 2, dl);
9385 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009386}
9387
Dan Gohmand858e902010-04-17 15:26:15 +00009388SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009389 MachineFunction &MF = DAG.getMachineFunction();
9390 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9391
Dan Gohman69de1932008-02-06 22:27:42 +00009392 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009393 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009394
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009395 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009396 // vastart just stores the address of the VarArgsFrameIndex slot into the
9397 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009398 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9399 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009400 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9401 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009402 }
9403
9404 // __va_list_tag:
9405 // gp_offset (0 - 6 * 8)
9406 // fp_offset (48 - 48 + 8 * 16)
9407 // overflow_arg_area (point to parameters coming in memory).
9408 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009409 SmallVector<SDValue, 8> MemOps;
9410 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009411 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009412 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009413 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9414 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009415 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009416 MemOps.push_back(Store);
9417
9418 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009419 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009420 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009421 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009422 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9423 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009424 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009425 MemOps.push_back(Store);
9426
9427 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009428 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009429 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009430 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9431 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009432 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9433 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009434 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009435 MemOps.push_back(Store);
9436
9437 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009438 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009439 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009440 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9441 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009442 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9443 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009444 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009445 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009446 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009447}
9448
Dan Gohmand858e902010-04-17 15:26:15 +00009449SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009450 assert(Subtarget->is64Bit() &&
9451 "LowerVAARG only handles 64-bit va_arg!");
9452 assert((Subtarget->isTargetLinux() ||
9453 Subtarget->isTargetDarwin()) &&
9454 "Unhandled target in LowerVAARG");
9455 assert(Op.getNode()->getNumOperands() == 4);
9456 SDValue Chain = Op.getOperand(0);
9457 SDValue SrcPtr = Op.getOperand(1);
9458 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9459 unsigned Align = Op.getConstantOperandVal(3);
9460 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009461
Dan Gohman320afb82010-10-12 18:00:49 +00009462 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009463 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009464 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9465 uint8_t ArgMode;
9466
9467 // Decide which area this value should be read from.
9468 // TODO: Implement the AMD64 ABI in its entirety. This simple
9469 // selection mechanism works only for the basic types.
9470 if (ArgVT == MVT::f80) {
9471 llvm_unreachable("va_arg for f80 not yet implemented");
9472 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9473 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9474 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9475 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9476 } else {
9477 llvm_unreachable("Unhandled argument type in LowerVAARG");
9478 }
9479
9480 if (ArgMode == 2) {
9481 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009482 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009483 !(DAG.getMachineFunction()
9484 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009485 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009486 }
9487
9488 // Insert VAARG_64 node into the DAG
9489 // VAARG_64 returns two values: Variable Argument Address, Chain
9490 SmallVector<SDValue, 11> InstOps;
9491 InstOps.push_back(Chain);
9492 InstOps.push_back(SrcPtr);
9493 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9494 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9495 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9496 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9497 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9498 VTs, &InstOps[0], InstOps.size(),
9499 MVT::i64,
9500 MachinePointerInfo(SV),
9501 /*Align=*/0,
9502 /*Volatile=*/false,
9503 /*ReadMem=*/true,
9504 /*WriteMem=*/true);
9505 Chain = VAARG.getValue(1);
9506
9507 // Load the next argument and return it
9508 return DAG.getLoad(ArgVT, dl,
9509 Chain,
9510 VAARG,
9511 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009512 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009513}
9514
Dan Gohmand858e902010-04-17 15:26:15 +00009515SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009516 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009517 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009518 SDValue Chain = Op.getOperand(0);
9519 SDValue DstPtr = Op.getOperand(1);
9520 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009521 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9522 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009523 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009524
Chris Lattnere72f2022010-09-21 05:40:29 +00009525 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009526 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009527 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009528 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009529}
9530
Craig Topper80e46362012-01-23 06:16:53 +00009531// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9532// may or may not be a constant. Takes immediate version of shift as input.
9533static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9534 SDValue SrcOp, SDValue ShAmt,
9535 SelectionDAG &DAG) {
9536 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9537
9538 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009539 // Constant may be a TargetConstant. Use a regular constant.
9540 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009541 switch (Opc) {
9542 default: llvm_unreachable("Unknown target vector shift node");
9543 case X86ISD::VSHLI:
9544 case X86ISD::VSRLI:
9545 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009546 return DAG.getNode(Opc, dl, VT, SrcOp,
9547 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009548 }
9549 }
9550
9551 // Change opcode to non-immediate version
9552 switch (Opc) {
9553 default: llvm_unreachable("Unknown target vector shift node");
9554 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9555 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9556 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9557 }
9558
9559 // Need to build a vector containing shift amount
9560 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9561 SDValue ShOps[4];
9562 ShOps[0] = ShAmt;
9563 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009564 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009565 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009566
9567 // The return type has to be a 128-bit type with the same element
9568 // type as the input type.
9569 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9570 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9571
9572 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009573 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9574}
9575
Dan Gohman475871a2008-07-27 21:46:04 +00009576SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009577X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009578 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009579 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009580 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009581 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009582 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009583 case Intrinsic::x86_sse_comieq_ss:
9584 case Intrinsic::x86_sse_comilt_ss:
9585 case Intrinsic::x86_sse_comile_ss:
9586 case Intrinsic::x86_sse_comigt_ss:
9587 case Intrinsic::x86_sse_comige_ss:
9588 case Intrinsic::x86_sse_comineq_ss:
9589 case Intrinsic::x86_sse_ucomieq_ss:
9590 case Intrinsic::x86_sse_ucomilt_ss:
9591 case Intrinsic::x86_sse_ucomile_ss:
9592 case Intrinsic::x86_sse_ucomigt_ss:
9593 case Intrinsic::x86_sse_ucomige_ss:
9594 case Intrinsic::x86_sse_ucomineq_ss:
9595 case Intrinsic::x86_sse2_comieq_sd:
9596 case Intrinsic::x86_sse2_comilt_sd:
9597 case Intrinsic::x86_sse2_comile_sd:
9598 case Intrinsic::x86_sse2_comigt_sd:
9599 case Intrinsic::x86_sse2_comige_sd:
9600 case Intrinsic::x86_sse2_comineq_sd:
9601 case Intrinsic::x86_sse2_ucomieq_sd:
9602 case Intrinsic::x86_sse2_ucomilt_sd:
9603 case Intrinsic::x86_sse2_ucomile_sd:
9604 case Intrinsic::x86_sse2_ucomigt_sd:
9605 case Intrinsic::x86_sse2_ucomige_sd:
9606 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009607 unsigned Opc;
9608 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009609 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009610 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009611 case Intrinsic::x86_sse_comieq_ss:
9612 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009613 Opc = X86ISD::COMI;
9614 CC = ISD::SETEQ;
9615 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009616 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009617 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009618 Opc = X86ISD::COMI;
9619 CC = ISD::SETLT;
9620 break;
9621 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009622 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009623 Opc = X86ISD::COMI;
9624 CC = ISD::SETLE;
9625 break;
9626 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009627 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009628 Opc = X86ISD::COMI;
9629 CC = ISD::SETGT;
9630 break;
9631 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009632 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009633 Opc = X86ISD::COMI;
9634 CC = ISD::SETGE;
9635 break;
9636 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009637 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009638 Opc = X86ISD::COMI;
9639 CC = ISD::SETNE;
9640 break;
9641 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009642 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009643 Opc = X86ISD::UCOMI;
9644 CC = ISD::SETEQ;
9645 break;
9646 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009647 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009648 Opc = X86ISD::UCOMI;
9649 CC = ISD::SETLT;
9650 break;
9651 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009652 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009653 Opc = X86ISD::UCOMI;
9654 CC = ISD::SETLE;
9655 break;
9656 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009657 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009658 Opc = X86ISD::UCOMI;
9659 CC = ISD::SETGT;
9660 break;
9661 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009662 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009663 Opc = X86ISD::UCOMI;
9664 CC = ISD::SETGE;
9665 break;
9666 case Intrinsic::x86_sse_ucomineq_ss:
9667 case Intrinsic::x86_sse2_ucomineq_sd:
9668 Opc = X86ISD::UCOMI;
9669 CC = ISD::SETNE;
9670 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009671 }
Evan Cheng734503b2006-09-11 02:19:56 +00009672
Dan Gohman475871a2008-07-27 21:46:04 +00009673 SDValue LHS = Op.getOperand(1);
9674 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009675 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009676 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009677 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9678 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9679 DAG.getConstant(X86CC, MVT::i8), Cond);
9680 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009681 }
Craig Topper6d688152012-08-14 07:43:25 +00009682
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009683 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009684 case Intrinsic::x86_sse2_pmulu_dq:
9685 case Intrinsic::x86_avx2_pmulu_dq:
9686 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9687 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009688
9689 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009690 case Intrinsic::x86_sse3_hadd_ps:
9691 case Intrinsic::x86_sse3_hadd_pd:
9692 case Intrinsic::x86_avx_hadd_ps_256:
9693 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009694 case Intrinsic::x86_sse3_hsub_ps:
9695 case Intrinsic::x86_sse3_hsub_pd:
9696 case Intrinsic::x86_avx_hsub_ps_256:
9697 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009698 case Intrinsic::x86_ssse3_phadd_w_128:
9699 case Intrinsic::x86_ssse3_phadd_d_128:
9700 case Intrinsic::x86_avx2_phadd_w:
9701 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009702 case Intrinsic::x86_ssse3_phsub_w_128:
9703 case Intrinsic::x86_ssse3_phsub_d_128:
9704 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009705 case Intrinsic::x86_avx2_phsub_d: {
9706 unsigned Opcode;
9707 switch (IntNo) {
9708 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9709 case Intrinsic::x86_sse3_hadd_ps:
9710 case Intrinsic::x86_sse3_hadd_pd:
9711 case Intrinsic::x86_avx_hadd_ps_256:
9712 case Intrinsic::x86_avx_hadd_pd_256:
9713 Opcode = X86ISD::FHADD;
9714 break;
9715 case Intrinsic::x86_sse3_hsub_ps:
9716 case Intrinsic::x86_sse3_hsub_pd:
9717 case Intrinsic::x86_avx_hsub_ps_256:
9718 case Intrinsic::x86_avx_hsub_pd_256:
9719 Opcode = X86ISD::FHSUB;
9720 break;
9721 case Intrinsic::x86_ssse3_phadd_w_128:
9722 case Intrinsic::x86_ssse3_phadd_d_128:
9723 case Intrinsic::x86_avx2_phadd_w:
9724 case Intrinsic::x86_avx2_phadd_d:
9725 Opcode = X86ISD::HADD;
9726 break;
9727 case Intrinsic::x86_ssse3_phsub_w_128:
9728 case Intrinsic::x86_ssse3_phsub_d_128:
9729 case Intrinsic::x86_avx2_phsub_w:
9730 case Intrinsic::x86_avx2_phsub_d:
9731 Opcode = X86ISD::HSUB;
9732 break;
9733 }
9734 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009735 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009736 }
9737
9738 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009739 case Intrinsic::x86_avx2_psllv_d:
9740 case Intrinsic::x86_avx2_psllv_q:
9741 case Intrinsic::x86_avx2_psllv_d_256:
9742 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009743 case Intrinsic::x86_avx2_psrlv_d:
9744 case Intrinsic::x86_avx2_psrlv_q:
9745 case Intrinsic::x86_avx2_psrlv_d_256:
9746 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009747 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009748 case Intrinsic::x86_avx2_psrav_d_256: {
9749 unsigned Opcode;
9750 switch (IntNo) {
9751 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9752 case Intrinsic::x86_avx2_psllv_d:
9753 case Intrinsic::x86_avx2_psllv_q:
9754 case Intrinsic::x86_avx2_psllv_d_256:
9755 case Intrinsic::x86_avx2_psllv_q_256:
9756 Opcode = ISD::SHL;
9757 break;
9758 case Intrinsic::x86_avx2_psrlv_d:
9759 case Intrinsic::x86_avx2_psrlv_q:
9760 case Intrinsic::x86_avx2_psrlv_d_256:
9761 case Intrinsic::x86_avx2_psrlv_q_256:
9762 Opcode = ISD::SRL;
9763 break;
9764 case Intrinsic::x86_avx2_psrav_d:
9765 case Intrinsic::x86_avx2_psrav_d_256:
9766 Opcode = ISD::SRA;
9767 break;
9768 }
9769 return DAG.getNode(Opcode, dl, Op.getValueType(),
9770 Op.getOperand(1), Op.getOperand(2));
9771 }
9772
Craig Topper969ba282012-01-25 06:43:11 +00009773 case Intrinsic::x86_ssse3_pshuf_b_128:
9774 case Intrinsic::x86_avx2_pshuf_b:
9775 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9776 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009777
Craig Topper969ba282012-01-25 06:43:11 +00009778 case Intrinsic::x86_ssse3_psign_b_128:
9779 case Intrinsic::x86_ssse3_psign_w_128:
9780 case Intrinsic::x86_ssse3_psign_d_128:
9781 case Intrinsic::x86_avx2_psign_b:
9782 case Intrinsic::x86_avx2_psign_w:
9783 case Intrinsic::x86_avx2_psign_d:
9784 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9785 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009786
Craig Toppere566cd02012-01-26 07:18:03 +00009787 case Intrinsic::x86_sse41_insertps:
9788 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9789 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009790
Craig Toppere566cd02012-01-26 07:18:03 +00009791 case Intrinsic::x86_avx_vperm2f128_ps_256:
9792 case Intrinsic::x86_avx_vperm2f128_pd_256:
9793 case Intrinsic::x86_avx_vperm2f128_si_256:
9794 case Intrinsic::x86_avx2_vperm2i128:
9795 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9796 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009797
Craig Topperffa6c402012-04-16 07:13:00 +00009798 case Intrinsic::x86_avx2_permd:
9799 case Intrinsic::x86_avx2_permps:
9800 // Operands intentionally swapped. Mask is last operand to intrinsic,
9801 // but second operand for node/intruction.
9802 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9803 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009804
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009805 // ptest and testp intrinsics. The intrinsic these come from are designed to
9806 // return an integer value, not just an instruction so lower it to the ptest
9807 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009808 case Intrinsic::x86_sse41_ptestz:
9809 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009810 case Intrinsic::x86_sse41_ptestnzc:
9811 case Intrinsic::x86_avx_ptestz_256:
9812 case Intrinsic::x86_avx_ptestc_256:
9813 case Intrinsic::x86_avx_ptestnzc_256:
9814 case Intrinsic::x86_avx_vtestz_ps:
9815 case Intrinsic::x86_avx_vtestc_ps:
9816 case Intrinsic::x86_avx_vtestnzc_ps:
9817 case Intrinsic::x86_avx_vtestz_pd:
9818 case Intrinsic::x86_avx_vtestc_pd:
9819 case Intrinsic::x86_avx_vtestnzc_pd:
9820 case Intrinsic::x86_avx_vtestz_ps_256:
9821 case Intrinsic::x86_avx_vtestc_ps_256:
9822 case Intrinsic::x86_avx_vtestnzc_ps_256:
9823 case Intrinsic::x86_avx_vtestz_pd_256:
9824 case Intrinsic::x86_avx_vtestc_pd_256:
9825 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9826 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009827 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009828 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009829 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009830 case Intrinsic::x86_avx_vtestz_ps:
9831 case Intrinsic::x86_avx_vtestz_pd:
9832 case Intrinsic::x86_avx_vtestz_ps_256:
9833 case Intrinsic::x86_avx_vtestz_pd_256:
9834 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009835 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009836 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009837 // ZF = 1
9838 X86CC = X86::COND_E;
9839 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009840 case Intrinsic::x86_avx_vtestc_ps:
9841 case Intrinsic::x86_avx_vtestc_pd:
9842 case Intrinsic::x86_avx_vtestc_ps_256:
9843 case Intrinsic::x86_avx_vtestc_pd_256:
9844 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009845 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009846 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009847 // CF = 1
9848 X86CC = X86::COND_B;
9849 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009850 case Intrinsic::x86_avx_vtestnzc_ps:
9851 case Intrinsic::x86_avx_vtestnzc_pd:
9852 case Intrinsic::x86_avx_vtestnzc_ps_256:
9853 case Intrinsic::x86_avx_vtestnzc_pd_256:
9854 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009855 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009856 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009857 // ZF and CF = 0
9858 X86CC = X86::COND_A;
9859 break;
9860 }
Eric Christopherfd179292009-08-27 18:07:15 +00009861
Eric Christopher71c67532009-07-29 00:28:05 +00009862 SDValue LHS = Op.getOperand(1);
9863 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009864 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9865 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009866 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9867 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9868 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009869 }
Evan Cheng5759f972008-05-04 09:15:50 +00009870
Craig Topper80e46362012-01-23 06:16:53 +00009871 // SSE/AVX shift intrinsics
9872 case Intrinsic::x86_sse2_psll_w:
9873 case Intrinsic::x86_sse2_psll_d:
9874 case Intrinsic::x86_sse2_psll_q:
9875 case Intrinsic::x86_avx2_psll_w:
9876 case Intrinsic::x86_avx2_psll_d:
9877 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +00009878 case Intrinsic::x86_sse2_psrl_w:
9879 case Intrinsic::x86_sse2_psrl_d:
9880 case Intrinsic::x86_sse2_psrl_q:
9881 case Intrinsic::x86_avx2_psrl_w:
9882 case Intrinsic::x86_avx2_psrl_d:
9883 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +00009884 case Intrinsic::x86_sse2_psra_w:
9885 case Intrinsic::x86_sse2_psra_d:
9886 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +00009887 case Intrinsic::x86_avx2_psra_d: {
9888 unsigned Opcode;
9889 switch (IntNo) {
9890 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9891 case Intrinsic::x86_sse2_psll_w:
9892 case Intrinsic::x86_sse2_psll_d:
9893 case Intrinsic::x86_sse2_psll_q:
9894 case Intrinsic::x86_avx2_psll_w:
9895 case Intrinsic::x86_avx2_psll_d:
9896 case Intrinsic::x86_avx2_psll_q:
9897 Opcode = X86ISD::VSHL;
9898 break;
9899 case Intrinsic::x86_sse2_psrl_w:
9900 case Intrinsic::x86_sse2_psrl_d:
9901 case Intrinsic::x86_sse2_psrl_q:
9902 case Intrinsic::x86_avx2_psrl_w:
9903 case Intrinsic::x86_avx2_psrl_d:
9904 case Intrinsic::x86_avx2_psrl_q:
9905 Opcode = X86ISD::VSRL;
9906 break;
9907 case Intrinsic::x86_sse2_psra_w:
9908 case Intrinsic::x86_sse2_psra_d:
9909 case Intrinsic::x86_avx2_psra_w:
9910 case Intrinsic::x86_avx2_psra_d:
9911 Opcode = X86ISD::VSRA;
9912 break;
9913 }
9914 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009915 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009916 }
9917
9918 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +00009919 case Intrinsic::x86_sse2_pslli_w:
9920 case Intrinsic::x86_sse2_pslli_d:
9921 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009922 case Intrinsic::x86_avx2_pslli_w:
9923 case Intrinsic::x86_avx2_pslli_d:
9924 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009925 case Intrinsic::x86_sse2_psrli_w:
9926 case Intrinsic::x86_sse2_psrli_d:
9927 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009928 case Intrinsic::x86_avx2_psrli_w:
9929 case Intrinsic::x86_avx2_psrli_d:
9930 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009931 case Intrinsic::x86_sse2_psrai_w:
9932 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009933 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +00009934 case Intrinsic::x86_avx2_psrai_d: {
9935 unsigned Opcode;
9936 switch (IntNo) {
9937 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9938 case Intrinsic::x86_sse2_pslli_w:
9939 case Intrinsic::x86_sse2_pslli_d:
9940 case Intrinsic::x86_sse2_pslli_q:
9941 case Intrinsic::x86_avx2_pslli_w:
9942 case Intrinsic::x86_avx2_pslli_d:
9943 case Intrinsic::x86_avx2_pslli_q:
9944 Opcode = X86ISD::VSHLI;
9945 break;
9946 case Intrinsic::x86_sse2_psrli_w:
9947 case Intrinsic::x86_sse2_psrli_d:
9948 case Intrinsic::x86_sse2_psrli_q:
9949 case Intrinsic::x86_avx2_psrli_w:
9950 case Intrinsic::x86_avx2_psrli_d:
9951 case Intrinsic::x86_avx2_psrli_q:
9952 Opcode = X86ISD::VSRLI;
9953 break;
9954 case Intrinsic::x86_sse2_psrai_w:
9955 case Intrinsic::x86_sse2_psrai_d:
9956 case Intrinsic::x86_avx2_psrai_w:
9957 case Intrinsic::x86_avx2_psrai_d:
9958 Opcode = X86ISD::VSRAI;
9959 break;
9960 }
9961 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009962 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +00009963 }
9964
Craig Topper4feb6472012-08-06 06:22:36 +00009965 case Intrinsic::x86_sse42_pcmpistria128:
9966 case Intrinsic::x86_sse42_pcmpestria128:
9967 case Intrinsic::x86_sse42_pcmpistric128:
9968 case Intrinsic::x86_sse42_pcmpestric128:
9969 case Intrinsic::x86_sse42_pcmpistrio128:
9970 case Intrinsic::x86_sse42_pcmpestrio128:
9971 case Intrinsic::x86_sse42_pcmpistris128:
9972 case Intrinsic::x86_sse42_pcmpestris128:
9973 case Intrinsic::x86_sse42_pcmpistriz128:
9974 case Intrinsic::x86_sse42_pcmpestriz128: {
9975 unsigned Opcode;
9976 unsigned X86CC;
9977 switch (IntNo) {
9978 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9979 case Intrinsic::x86_sse42_pcmpistria128:
9980 Opcode = X86ISD::PCMPISTRI;
9981 X86CC = X86::COND_A;
9982 break;
9983 case Intrinsic::x86_sse42_pcmpestria128:
9984 Opcode = X86ISD::PCMPESTRI;
9985 X86CC = X86::COND_A;
9986 break;
9987 case Intrinsic::x86_sse42_pcmpistric128:
9988 Opcode = X86ISD::PCMPISTRI;
9989 X86CC = X86::COND_B;
9990 break;
9991 case Intrinsic::x86_sse42_pcmpestric128:
9992 Opcode = X86ISD::PCMPESTRI;
9993 X86CC = X86::COND_B;
9994 break;
9995 case Intrinsic::x86_sse42_pcmpistrio128:
9996 Opcode = X86ISD::PCMPISTRI;
9997 X86CC = X86::COND_O;
9998 break;
9999 case Intrinsic::x86_sse42_pcmpestrio128:
10000 Opcode = X86ISD::PCMPESTRI;
10001 X86CC = X86::COND_O;
10002 break;
10003 case Intrinsic::x86_sse42_pcmpistris128:
10004 Opcode = X86ISD::PCMPISTRI;
10005 X86CC = X86::COND_S;
10006 break;
10007 case Intrinsic::x86_sse42_pcmpestris128:
10008 Opcode = X86ISD::PCMPESTRI;
10009 X86CC = X86::COND_S;
10010 break;
10011 case Intrinsic::x86_sse42_pcmpistriz128:
10012 Opcode = X86ISD::PCMPISTRI;
10013 X86CC = X86::COND_E;
10014 break;
10015 case Intrinsic::x86_sse42_pcmpestriz128:
10016 Opcode = X86ISD::PCMPESTRI;
10017 X86CC = X86::COND_E;
10018 break;
10019 }
10020 SmallVector<SDValue, 5> NewOps;
10021 NewOps.append(Op->op_begin()+1, Op->op_end());
10022 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10023 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10024 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10025 DAG.getConstant(X86CC, MVT::i8),
10026 SDValue(PCMP.getNode(), 1));
10027 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10028 }
Craig Topper6d688152012-08-14 07:43:25 +000010029
Craig Topper4feb6472012-08-06 06:22:36 +000010030 case Intrinsic::x86_sse42_pcmpistri128:
10031 case Intrinsic::x86_sse42_pcmpestri128: {
10032 unsigned Opcode;
10033 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10034 Opcode = X86ISD::PCMPISTRI;
10035 else
10036 Opcode = X86ISD::PCMPESTRI;
10037
10038 SmallVector<SDValue, 5> NewOps;
10039 NewOps.append(Op->op_begin()+1, Op->op_end());
10040 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10041 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10042 }
Craig Topper0e292372012-08-24 04:03:22 +000010043 case Intrinsic::x86_fma_vfmadd_ps:
10044 case Intrinsic::x86_fma_vfmadd_pd:
10045 case Intrinsic::x86_fma_vfmsub_ps:
10046 case Intrinsic::x86_fma_vfmsub_pd:
10047 case Intrinsic::x86_fma_vfnmadd_ps:
10048 case Intrinsic::x86_fma_vfnmadd_pd:
10049 case Intrinsic::x86_fma_vfnmsub_ps:
10050 case Intrinsic::x86_fma_vfnmsub_pd:
10051 case Intrinsic::x86_fma_vfmaddsub_ps:
10052 case Intrinsic::x86_fma_vfmaddsub_pd:
10053 case Intrinsic::x86_fma_vfmsubadd_ps:
10054 case Intrinsic::x86_fma_vfmsubadd_pd:
10055 case Intrinsic::x86_fma_vfmadd_ps_256:
10056 case Intrinsic::x86_fma_vfmadd_pd_256:
10057 case Intrinsic::x86_fma_vfmsub_ps_256:
10058 case Intrinsic::x86_fma_vfmsub_pd_256:
10059 case Intrinsic::x86_fma_vfnmadd_ps_256:
10060 case Intrinsic::x86_fma_vfnmadd_pd_256:
10061 case Intrinsic::x86_fma_vfnmsub_ps_256:
10062 case Intrinsic::x86_fma_vfnmsub_pd_256:
10063 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10064 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10065 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10066 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010067 unsigned Opc;
10068 switch (IntNo) {
10069 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10070 case Intrinsic::x86_fma_vfmadd_ps:
10071 case Intrinsic::x86_fma_vfmadd_pd:
10072 case Intrinsic::x86_fma_vfmadd_ps_256:
10073 case Intrinsic::x86_fma_vfmadd_pd_256:
10074 Opc = X86ISD::FMADD;
10075 break;
10076 case Intrinsic::x86_fma_vfmsub_ps:
10077 case Intrinsic::x86_fma_vfmsub_pd:
10078 case Intrinsic::x86_fma_vfmsub_ps_256:
10079 case Intrinsic::x86_fma_vfmsub_pd_256:
10080 Opc = X86ISD::FMSUB;
10081 break;
10082 case Intrinsic::x86_fma_vfnmadd_ps:
10083 case Intrinsic::x86_fma_vfnmadd_pd:
10084 case Intrinsic::x86_fma_vfnmadd_ps_256:
10085 case Intrinsic::x86_fma_vfnmadd_pd_256:
10086 Opc = X86ISD::FNMADD;
10087 break;
10088 case Intrinsic::x86_fma_vfnmsub_ps:
10089 case Intrinsic::x86_fma_vfnmsub_pd:
10090 case Intrinsic::x86_fma_vfnmsub_ps_256:
10091 case Intrinsic::x86_fma_vfnmsub_pd_256:
10092 Opc = X86ISD::FNMSUB;
10093 break;
10094 case Intrinsic::x86_fma_vfmaddsub_ps:
10095 case Intrinsic::x86_fma_vfmaddsub_pd:
10096 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10097 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10098 Opc = X86ISD::FMADDSUB;
10099 break;
10100 case Intrinsic::x86_fma_vfmsubadd_ps:
10101 case Intrinsic::x86_fma_vfmsubadd_pd:
10102 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10103 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10104 Opc = X86ISD::FMSUBADD;
10105 break;
10106 }
10107
10108 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10109 Op.getOperand(2), Op.getOperand(3));
10110 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010111 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010112}
Evan Cheng72261582005-12-20 06:22:03 +000010113
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010114SDValue
10115X86TargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const {
10116 DebugLoc dl = Op.getDebugLoc();
10117 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10118 switch (IntNo) {
10119 default: return SDValue(); // Don't custom lower most intrinsics.
10120
10121 // RDRAND intrinsics.
10122 case Intrinsic::x86_rdrand_16:
10123 case Intrinsic::x86_rdrand_32:
10124 case Intrinsic::x86_rdrand_64: {
10125 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010126 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10127 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010128
10129 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10130 // return the value from Rand, which is always 0, casted to i32.
10131 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10132 DAG.getConstant(1, Op->getValueType(1)),
10133 DAG.getConstant(X86::COND_B, MVT::i32),
10134 SDValue(Result.getNode(), 1) };
10135 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10136 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10137 Ops, 4);
10138
10139 // Return { result, isValid, chain }.
10140 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010141 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010142 }
10143 }
10144}
10145
Dan Gohmand858e902010-04-17 15:26:15 +000010146SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10147 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010148 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10149 MFI->setReturnAddressIsTaken(true);
10150
Bill Wendling64e87322009-01-16 19:25:27 +000010151 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010152 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010153
10154 if (Depth > 0) {
10155 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10156 SDValue Offset =
10157 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010158 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010159 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010160 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010161 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010162 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010163 }
10164
10165 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010166 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010167 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010168 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010169}
10170
Dan Gohmand858e902010-04-17 15:26:15 +000010171SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010172 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10173 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010174
Owen Andersone50ed302009-08-10 22:56:29 +000010175 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010176 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010177 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10178 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010179 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010180 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010181 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10182 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010183 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010184 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010185}
10186
Dan Gohman475871a2008-07-27 21:46:04 +000010187SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010188 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010189 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010190}
10191
Dan Gohmand858e902010-04-17 15:26:15 +000010192SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010193 SDValue Chain = Op.getOperand(0);
10194 SDValue Offset = Op.getOperand(1);
10195 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010196 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010197
Dan Gohmand8816272010-08-11 18:14:00 +000010198 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10199 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10200 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010201 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010202
Dan Gohmand8816272010-08-11 18:14:00 +000010203 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10204 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010205 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010206 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10207 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010208 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010209
Dale Johannesene4d209d2009-02-03 20:21:25 +000010210 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010211 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010212 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010213}
10214
Duncan Sands4a544a72011-09-06 13:37:06 +000010215SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
10216 SelectionDAG &DAG) const {
10217 return Op.getOperand(0);
10218}
10219
10220SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10221 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010222 SDValue Root = Op.getOperand(0);
10223 SDValue Trmp = Op.getOperand(1); // trampoline
10224 SDValue FPtr = Op.getOperand(2); // nested function
10225 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010226 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010227
Dan Gohman69de1932008-02-06 22:27:42 +000010228 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010229
10230 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010231 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010232
10233 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010234 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10235 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010236
Evan Cheng0e6a0522011-07-18 20:57:22 +000010237 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10238 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010239
10240 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10241
10242 // Load the pointer to the nested function into R11.
10243 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010244 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010245 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010246 Addr, MachinePointerInfo(TrmpAddr),
10247 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010248
Owen Anderson825b72b2009-08-11 20:47:22 +000010249 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10250 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010251 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10252 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010253 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010254
10255 // Load the 'nest' parameter value into R10.
10256 // R10 is specified in X86CallingConv.td
10257 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010258 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10259 DAG.getConstant(10, MVT::i64));
10260 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010261 Addr, MachinePointerInfo(TrmpAddr, 10),
10262 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010263
Owen Anderson825b72b2009-08-11 20:47:22 +000010264 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10265 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010266 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10267 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010268 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010269
10270 // Jump to the nested function.
10271 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010272 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10273 DAG.getConstant(20, MVT::i64));
10274 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010275 Addr, MachinePointerInfo(TrmpAddr, 20),
10276 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010277
10278 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010279 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10280 DAG.getConstant(22, MVT::i64));
10281 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010282 MachinePointerInfo(TrmpAddr, 22),
10283 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010284
Duncan Sands4a544a72011-09-06 13:37:06 +000010285 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010286 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010287 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010288 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010289 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010290 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010291
10292 switch (CC) {
10293 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010294 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010295 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010296 case CallingConv::X86_StdCall: {
10297 // Pass 'nest' parameter in ECX.
10298 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010299 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010300
10301 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010302 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010303 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010304
Chris Lattner58d74912008-03-12 17:45:29 +000010305 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010306 unsigned InRegCount = 0;
10307 unsigned Idx = 1;
10308
10309 for (FunctionType::param_iterator I = FTy->param_begin(),
10310 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010311 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010312 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010313 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010314
10315 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010316 report_fatal_error("Nest register in use - reduce number of inreg"
10317 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010318 }
10319 }
10320 break;
10321 }
10322 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010323 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010324 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010325 // Pass 'nest' parameter in EAX.
10326 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010327 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010328 break;
10329 }
10330
Dan Gohman475871a2008-07-27 21:46:04 +000010331 SDValue OutChains[4];
10332 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010333
Owen Anderson825b72b2009-08-11 20:47:22 +000010334 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10335 DAG.getConstant(10, MVT::i32));
10336 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010337
Chris Lattnera62fe662010-02-05 19:20:30 +000010338 // This is storing the opcode for MOV32ri.
10339 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010340 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010341 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010342 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010343 Trmp, MachinePointerInfo(TrmpAddr),
10344 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010345
Owen Anderson825b72b2009-08-11 20:47:22 +000010346 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10347 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010348 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10349 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010350 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010351
Chris Lattnera62fe662010-02-05 19:20:30 +000010352 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010353 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10354 DAG.getConstant(5, MVT::i32));
10355 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010356 MachinePointerInfo(TrmpAddr, 5),
10357 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010358
Owen Anderson825b72b2009-08-11 20:47:22 +000010359 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10360 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010361 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10362 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010363 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010364
Duncan Sands4a544a72011-09-06 13:37:06 +000010365 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010366 }
10367}
10368
Dan Gohmand858e902010-04-17 15:26:15 +000010369SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10370 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010371 /*
10372 The rounding mode is in bits 11:10 of FPSR, and has the following
10373 settings:
10374 00 Round to nearest
10375 01 Round to -inf
10376 10 Round to +inf
10377 11 Round to 0
10378
10379 FLT_ROUNDS, on the other hand, expects the following:
10380 -1 Undefined
10381 0 Round to 0
10382 1 Round to nearest
10383 2 Round to +inf
10384 3 Round to -inf
10385
10386 To perform the conversion, we do:
10387 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10388 */
10389
10390 MachineFunction &MF = DAG.getMachineFunction();
10391 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010392 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010393 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010394 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010395 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010396
10397 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010398 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010399 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010400
Michael J. Spencerec38de22010-10-10 22:04:20 +000010401
Chris Lattner2156b792010-09-22 01:11:26 +000010402 MachineMemOperand *MMO =
10403 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10404 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010405
Chris Lattner2156b792010-09-22 01:11:26 +000010406 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10407 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10408 DAG.getVTList(MVT::Other),
10409 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010410
10411 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010412 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010413 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010414
10415 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010416 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010417 DAG.getNode(ISD::SRL, DL, MVT::i16,
10418 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010419 CWD, DAG.getConstant(0x800, MVT::i16)),
10420 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010421 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010422 DAG.getNode(ISD::SRL, DL, MVT::i16,
10423 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010424 CWD, DAG.getConstant(0x400, MVT::i16)),
10425 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010426
Dan Gohman475871a2008-07-27 21:46:04 +000010427 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010428 DAG.getNode(ISD::AND, DL, MVT::i16,
10429 DAG.getNode(ISD::ADD, DL, MVT::i16,
10430 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010431 DAG.getConstant(1, MVT::i16)),
10432 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010433
10434
Duncan Sands83ec4b62008-06-06 12:08:01 +000010435 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010436 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010437}
10438
Dan Gohmand858e902010-04-17 15:26:15 +000010439SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010440 EVT VT = Op.getValueType();
10441 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010442 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010443 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010444
10445 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010446 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010447 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010448 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010449 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010450 }
Evan Cheng18efe262007-12-14 02:13:44 +000010451
Evan Cheng152804e2007-12-14 08:30:15 +000010452 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010453 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010454 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010455
10456 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010457 SDValue Ops[] = {
10458 Op,
10459 DAG.getConstant(NumBits+NumBits-1, OpVT),
10460 DAG.getConstant(X86::COND_E, MVT::i8),
10461 Op.getValue(1)
10462 };
10463 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010464
10465 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010466 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010467
Owen Anderson825b72b2009-08-11 20:47:22 +000010468 if (VT == MVT::i8)
10469 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010470 return Op;
10471}
10472
Chandler Carruthacc068e2011-12-24 10:55:54 +000010473SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10474 SelectionDAG &DAG) const {
10475 EVT VT = Op.getValueType();
10476 EVT OpVT = VT;
10477 unsigned NumBits = VT.getSizeInBits();
10478 DebugLoc dl = Op.getDebugLoc();
10479
10480 Op = Op.getOperand(0);
10481 if (VT == MVT::i8) {
10482 // Zero extend to i32 since there is not an i8 bsr.
10483 OpVT = MVT::i32;
10484 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10485 }
10486
10487 // Issue a bsr (scan bits in reverse).
10488 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10489 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10490
10491 // And xor with NumBits-1.
10492 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10493
10494 if (VT == MVT::i8)
10495 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10496 return Op;
10497}
10498
Dan Gohmand858e902010-04-17 15:26:15 +000010499SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010500 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010501 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010502 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010503 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010504
10505 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010506 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010507 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010508
10509 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010510 SDValue Ops[] = {
10511 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010512 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010513 DAG.getConstant(X86::COND_E, MVT::i8),
10514 Op.getValue(1)
10515 };
Chandler Carruth77821022011-12-24 12:12:34 +000010516 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010517}
10518
Craig Topper13894fa2011-08-24 06:14:18 +000010519// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10520// ones, and then concatenate the result back.
10521static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010522 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010523
Craig Topper7a9a28b2012-08-12 02:23:29 +000010524 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010525 "Unsupported value type for operation");
10526
Craig Topper66ddd152012-04-27 22:54:43 +000010527 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010528 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010529
10530 // Extract the LHS vectors
10531 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010532 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10533 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010534
10535 // Extract the RHS vectors
10536 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010537 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10538 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010539
10540 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10541 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10542
10543 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10544 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10545 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10546}
10547
10548SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010549 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010550 Op.getValueType().isInteger() &&
10551 "Only handle AVX 256-bit vector integer operation");
10552 return Lower256IntArith(Op, DAG);
10553}
10554
10555SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010556 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010557 Op.getValueType().isInteger() &&
10558 "Only handle AVX 256-bit vector integer operation");
10559 return Lower256IntArith(Op, DAG);
10560}
10561
10562SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10563 EVT VT = Op.getValueType();
10564
10565 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010566 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010567 return Lower256IntArith(Op, DAG);
10568
Craig Topper5b209e82012-02-05 03:14:49 +000010569 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10570 "Only know how to lower V2I64/V4I64 multiply");
10571
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010572 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010573
Craig Topper5b209e82012-02-05 03:14:49 +000010574 // Ahi = psrlqi(a, 32);
10575 // Bhi = psrlqi(b, 32);
10576 //
10577 // AloBlo = pmuludq(a, b);
10578 // AloBhi = pmuludq(a, Bhi);
10579 // AhiBlo = pmuludq(Ahi, b);
10580
10581 // AloBhi = psllqi(AloBhi, 32);
10582 // AhiBlo = psllqi(AhiBlo, 32);
10583 // return AloBlo + AloBhi + AhiBlo;
10584
Craig Topperaaa643c2011-11-09 07:28:55 +000010585 SDValue A = Op.getOperand(0);
10586 SDValue B = Op.getOperand(1);
10587
Craig Topper5b209e82012-02-05 03:14:49 +000010588 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010589
Craig Topper5b209e82012-02-05 03:14:49 +000010590 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10591 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010592
Craig Topper5b209e82012-02-05 03:14:49 +000010593 // Bit cast to 32-bit vectors for MULUDQ
10594 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10595 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10596 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10597 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10598 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010599
Craig Topper5b209e82012-02-05 03:14:49 +000010600 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10601 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10602 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010603
Craig Topper5b209e82012-02-05 03:14:49 +000010604 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10605 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010606
Dale Johannesene4d209d2009-02-03 20:21:25 +000010607 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010608 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010609}
10610
Nadav Rotem43012222011-05-11 08:12:09 +000010611SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10612
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010613 EVT VT = Op.getValueType();
10614 DebugLoc dl = Op.getDebugLoc();
10615 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010616 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010617 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010618
Craig Topper1accb7e2012-01-10 06:54:16 +000010619 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010620 return SDValue();
10621
Nadav Rotem43012222011-05-11 08:12:09 +000010622 // Optimize shl/srl/sra with constant shift amount.
10623 if (isSplatVector(Amt.getNode())) {
10624 SDValue SclrAmt = Amt->getOperand(0);
10625 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10626 uint64_t ShiftAmt = C->getZExtValue();
10627
Craig Toppered2e13d2012-01-22 19:15:14 +000010628 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10629 (Subtarget->hasAVX2() &&
10630 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10631 if (Op.getOpcode() == ISD::SHL)
10632 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10633 DAG.getConstant(ShiftAmt, MVT::i32));
10634 if (Op.getOpcode() == ISD::SRL)
10635 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10636 DAG.getConstant(ShiftAmt, MVT::i32));
10637 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10638 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10639 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010640 }
10641
Craig Toppered2e13d2012-01-22 19:15:14 +000010642 if (VT == MVT::v16i8) {
10643 if (Op.getOpcode() == ISD::SHL) {
10644 // Make a large shift.
10645 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10646 DAG.getConstant(ShiftAmt, MVT::i32));
10647 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10648 // Zero out the rightmost bits.
10649 SmallVector<SDValue, 16> V(16,
10650 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10651 MVT::i8));
10652 return DAG.getNode(ISD::AND, dl, VT, SHL,
10653 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010654 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010655 if (Op.getOpcode() == ISD::SRL) {
10656 // Make a large shift.
10657 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10658 DAG.getConstant(ShiftAmt, MVT::i32));
10659 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10660 // Zero out the leftmost bits.
10661 SmallVector<SDValue, 16> V(16,
10662 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10663 MVT::i8));
10664 return DAG.getNode(ISD::AND, dl, VT, SRL,
10665 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10666 }
10667 if (Op.getOpcode() == ISD::SRA) {
10668 if (ShiftAmt == 7) {
10669 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010670 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010671 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010672 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010673
Craig Toppered2e13d2012-01-22 19:15:14 +000010674 // R s>> a === ((R u>> a) ^ m) - m
10675 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10676 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10677 MVT::i8));
10678 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10679 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10680 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10681 return Res;
10682 }
Craig Topper731dfd02012-04-23 03:42:40 +000010683 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010684 }
Craig Topper46154eb2011-11-11 07:39:23 +000010685
Craig Topper0d86d462011-11-20 00:12:05 +000010686 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10687 if (Op.getOpcode() == ISD::SHL) {
10688 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010689 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10690 DAG.getConstant(ShiftAmt, MVT::i32));
10691 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010692 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010693 SmallVector<SDValue, 32> V(32,
10694 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10695 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010696 return DAG.getNode(ISD::AND, dl, VT, SHL,
10697 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010698 }
Craig Topper0d86d462011-11-20 00:12:05 +000010699 if (Op.getOpcode() == ISD::SRL) {
10700 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010701 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10702 DAG.getConstant(ShiftAmt, MVT::i32));
10703 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010704 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010705 SmallVector<SDValue, 32> V(32,
10706 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10707 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010708 return DAG.getNode(ISD::AND, dl, VT, SRL,
10709 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10710 }
10711 if (Op.getOpcode() == ISD::SRA) {
10712 if (ShiftAmt == 7) {
10713 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010714 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010715 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010716 }
10717
10718 // R s>> a === ((R u>> a) ^ m) - m
10719 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10720 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10721 MVT::i8));
10722 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10723 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10724 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10725 return Res;
10726 }
Craig Topper731dfd02012-04-23 03:42:40 +000010727 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010728 }
Nadav Rotem43012222011-05-11 08:12:09 +000010729 }
10730 }
10731
10732 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010733 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010734 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10735 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010736
Chris Lattner7302d802012-02-06 21:56:39 +000010737 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10738 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010739 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10740 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010741 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010742 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010743
10744 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010745 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010746 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10747 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10748 }
Nadav Rotem43012222011-05-11 08:12:09 +000010749 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010750 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010751
Nate Begeman51409212010-07-28 00:21:48 +000010752 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010753 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10754 DAG.getConstant(5, MVT::i32));
10755 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010756
Lang Hames8b99c1e2011-12-17 01:08:46 +000010757 // Turn 'a' into a mask suitable for VSELECT
10758 SDValue VSelM = DAG.getConstant(0x80, VT);
10759 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010760 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010761
Lang Hames8b99c1e2011-12-17 01:08:46 +000010762 SDValue CM1 = DAG.getConstant(0x0f, VT);
10763 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010764
Lang Hames8b99c1e2011-12-17 01:08:46 +000010765 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10766 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010767 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10768 DAG.getConstant(4, MVT::i32), DAG);
10769 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010770 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10771
Nate Begeman51409212010-07-28 00:21:48 +000010772 // a += a
10773 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010774 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010775 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010776
Lang Hames8b99c1e2011-12-17 01:08:46 +000010777 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10778 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010779 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10780 DAG.getConstant(2, MVT::i32), DAG);
10781 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010782 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10783
Nate Begeman51409212010-07-28 00:21:48 +000010784 // a += a
10785 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010786 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010787 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010788
Lang Hames8b99c1e2011-12-17 01:08:46 +000010789 // return VSELECT(r, r+r, a);
10790 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010791 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010792 return R;
10793 }
Craig Topper46154eb2011-11-11 07:39:23 +000010794
10795 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010796 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010797 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010798 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10799 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10800
10801 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010802 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10803 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010804
10805 // Recreate the shift amount vectors
10806 SDValue Amt1, Amt2;
10807 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10808 // Constant shift amount
10809 SmallVector<SDValue, 4> Amt1Csts;
10810 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010811 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010812 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010813 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010814 Amt2Csts.push_back(Amt->getOperand(i));
10815
10816 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10817 &Amt1Csts[0], NumElems/2);
10818 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10819 &Amt2Csts[0], NumElems/2);
10820 } else {
10821 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010822 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10823 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010824 }
10825
10826 // Issue new vector shifts for the smaller types
10827 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10828 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10829
10830 // Concatenate the result back
10831 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10832 }
10833
Nate Begeman51409212010-07-28 00:21:48 +000010834 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010835}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010836
Dan Gohmand858e902010-04-17 15:26:15 +000010837SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010838 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10839 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010840 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10841 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010842 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010843 SDValue LHS = N->getOperand(0);
10844 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010845 unsigned BaseOp = 0;
10846 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010847 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010848 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010849 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010850 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010851 // A subtract of one will be selected as a INC. Note that INC doesn't
10852 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010853 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10854 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010855 BaseOp = X86ISD::INC;
10856 Cond = X86::COND_O;
10857 break;
10858 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010859 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010860 Cond = X86::COND_O;
10861 break;
10862 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010863 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010864 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010865 break;
10866 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010867 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10868 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010869 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10870 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010871 BaseOp = X86ISD::DEC;
10872 Cond = X86::COND_O;
10873 break;
10874 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010875 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010876 Cond = X86::COND_O;
10877 break;
10878 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010879 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010880 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010881 break;
10882 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010883 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010884 Cond = X86::COND_O;
10885 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010886 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10887 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10888 MVT::i32);
10889 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010890
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010891 SDValue SetCC =
10892 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10893 DAG.getConstant(X86::COND_O, MVT::i32),
10894 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010895
Dan Gohman6e5fda22011-07-22 18:45:15 +000010896 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010897 }
Bill Wendling74c37652008-12-09 22:08:41 +000010898 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010899
Bill Wendling61edeb52008-12-02 01:06:39 +000010900 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010901 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010902 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010903
Bill Wendling61edeb52008-12-02 01:06:39 +000010904 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010905 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10906 DAG.getConstant(Cond, MVT::i32),
10907 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010908
Dan Gohman6e5fda22011-07-22 18:45:15 +000010909 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010910}
10911
Chad Rosier30450e82011-12-22 22:35:21 +000010912SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10913 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010914 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010915 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10916 EVT VT = Op.getValueType();
10917
Craig Toppered2e13d2012-01-22 19:15:14 +000010918 if (!Subtarget->hasSSE2() || !VT.isVector())
10919 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010920
Craig Toppered2e13d2012-01-22 19:15:14 +000010921 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10922 ExtraVT.getScalarType().getSizeInBits();
10923 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10924
10925 switch (VT.getSimpleVT().SimpleTy) {
10926 default: return SDValue();
10927 case MVT::v8i32:
10928 case MVT::v16i16:
10929 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010930 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010931 if (!Subtarget->hasAVX2()) {
10932 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000010933 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000010934
Craig Toppered2e13d2012-01-22 19:15:14 +000010935 // Extract the LHS vectors
10936 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010937 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10938 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010939
Craig Toppered2e13d2012-01-22 19:15:14 +000010940 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10941 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010942
Craig Toppered2e13d2012-01-22 19:15:14 +000010943 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000010944 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000010945 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10946 ExtraNumElems/2);
10947 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010948
Craig Toppered2e13d2012-01-22 19:15:14 +000010949 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10950 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010951
Craig Toppered2e13d2012-01-22 19:15:14 +000010952 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10953 }
10954 // fall through
10955 case MVT::v4i32:
10956 case MVT::v8i16: {
10957 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10958 Op.getOperand(0), ShAmt, DAG);
10959 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010960 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010961 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010962}
10963
10964
Eric Christopher9a9d2752010-07-22 02:48:34 +000010965SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10966 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010967
Eric Christopher77ed1352011-07-08 00:04:56 +000010968 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10969 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010970 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010971 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010972 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010973 SDValue Ops[] = {
10974 DAG.getRegister(X86::ESP, MVT::i32), // Base
10975 DAG.getTargetConstant(1, MVT::i8), // Scale
10976 DAG.getRegister(0, MVT::i32), // Index
10977 DAG.getTargetConstant(0, MVT::i32), // Disp
10978 DAG.getRegister(0, MVT::i32), // Segment.
10979 Zero,
10980 Chain
10981 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010982 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010983 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10984 array_lengthof(Ops));
10985 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010986 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010987
Eric Christopher9a9d2752010-07-22 02:48:34 +000010988 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010989 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010990 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010991
Chris Lattner132929a2010-08-14 17:26:09 +000010992 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10993 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10994 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10995 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010996
Chris Lattner132929a2010-08-14 17:26:09 +000010997 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10998 if (!Op1 && !Op2 && !Op3 && Op4)
10999 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011000
Chris Lattner132929a2010-08-14 17:26:09 +000011001 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11002 if (Op1 && !Op2 && !Op3 && !Op4)
11003 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011004
11005 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011006 // (MFENCE)>;
11007 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011008}
11009
Eli Friedman14648462011-07-27 22:21:52 +000011010SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
11011 SelectionDAG &DAG) const {
11012 DebugLoc dl = Op.getDebugLoc();
11013 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11014 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11015 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11016 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11017
11018 // The only fence that needs an instruction is a sequentially-consistent
11019 // cross-thread fence.
11020 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11021 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11022 // no-sse2). There isn't any reason to disable it if the target processor
11023 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011024 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011025 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11026
11027 SDValue Chain = Op.getOperand(0);
11028 SDValue Zero = DAG.getConstant(0, MVT::i32);
11029 SDValue Ops[] = {
11030 DAG.getRegister(X86::ESP, MVT::i32), // Base
11031 DAG.getTargetConstant(1, MVT::i8), // Scale
11032 DAG.getRegister(0, MVT::i32), // Index
11033 DAG.getTargetConstant(0, MVT::i32), // Disp
11034 DAG.getRegister(0, MVT::i32), // Segment.
11035 Zero,
11036 Chain
11037 };
11038 SDNode *Res =
11039 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11040 array_lengthof(Ops));
11041 return SDValue(Res, 0);
11042 }
11043
11044 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11045 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11046}
11047
11048
Dan Gohmand858e902010-04-17 15:26:15 +000011049SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000011050 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011051 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011052 unsigned Reg = 0;
11053 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011054 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011055 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011056 case MVT::i8: Reg = X86::AL; size = 1; break;
11057 case MVT::i16: Reg = X86::AX; size = 2; break;
11058 case MVT::i32: Reg = X86::EAX; size = 4; break;
11059 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011060 assert(Subtarget->is64Bit() && "Node not type legal!");
11061 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011062 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011063 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011064 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011065 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011066 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011067 Op.getOperand(1),
11068 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011069 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011070 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011071 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011072 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11073 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11074 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011075 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011076 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011077 return cpOut;
11078}
11079
Duncan Sands1607f052008-12-01 11:39:25 +000011080SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011081 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000011082 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011083 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011084 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011085 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011086 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011087 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11088 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011089 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011090 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11091 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011092 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011093 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011094 rdx.getValue(1)
11095 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011096 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011097}
11098
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011099SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000011100 SelectionDAG &DAG) const {
11101 EVT SrcVT = Op.getOperand(0).getValueType();
11102 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011103 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011104 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011105 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011106 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011107 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011108 // i64 <=> MMX conversions are Legal.
11109 if (SrcVT==MVT::i64 && DstVT.isVector())
11110 return Op;
11111 if (DstVT==MVT::i64 && SrcVT.isVector())
11112 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011113 // MMX <=> MMX conversions are Legal.
11114 if (SrcVT.isVector() && DstVT.isVector())
11115 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011116 // All other conversions need to be expanded.
11117 return SDValue();
11118}
Chris Lattner5b856542010-12-20 00:59:46 +000011119
Dan Gohmand858e902010-04-17 15:26:15 +000011120SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011121 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011122 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011123 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011124 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011125 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011126 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011127 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011128 Node->getOperand(0),
11129 Node->getOperand(1), negOp,
11130 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011131 cast<AtomicSDNode>(Node)->getAlignment(),
11132 cast<AtomicSDNode>(Node)->getOrdering(),
11133 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011134}
11135
Eli Friedman327236c2011-08-24 20:50:09 +000011136static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11137 SDNode *Node = Op.getNode();
11138 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011139 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011140
11141 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011142 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11143 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11144 // (The only way to get a 16-byte store is cmpxchg16b)
11145 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11146 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11147 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011148 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11149 cast<AtomicSDNode>(Node)->getMemoryVT(),
11150 Node->getOperand(0),
11151 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011152 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011153 cast<AtomicSDNode>(Node)->getOrdering(),
11154 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011155 return Swap.getValue(1);
11156 }
11157 // Other atomic stores have a simple pattern.
11158 return Op;
11159}
11160
Chris Lattner5b856542010-12-20 00:59:46 +000011161static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11162 EVT VT = Op.getNode()->getValueType(0);
11163
11164 // Let legalize expand this if it isn't a legal type yet.
11165 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11166 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011167
Chris Lattner5b856542010-12-20 00:59:46 +000011168 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011169
Chris Lattner5b856542010-12-20 00:59:46 +000011170 unsigned Opc;
11171 bool ExtraOp = false;
11172 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011173 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011174 case ISD::ADDC: Opc = X86ISD::ADD; break;
11175 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11176 case ISD::SUBC: Opc = X86ISD::SUB; break;
11177 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11178 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011179
Chris Lattner5b856542010-12-20 00:59:46 +000011180 if (!ExtraOp)
11181 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11182 Op.getOperand(1));
11183 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11184 Op.getOperand(1), Op.getOperand(2));
11185}
11186
Evan Cheng0db9fe62006-04-25 20:13:52 +000011187/// LowerOperation - Provide custom lowering hooks for some operations.
11188///
Dan Gohmand858e902010-04-17 15:26:15 +000011189SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011190 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011191 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011192 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000011193 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000011194 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011195 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
11196 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011197 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011198 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011199 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011200 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11201 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11202 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000011203 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000011204 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011205 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11206 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11207 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011208 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011209 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011210 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011211 case ISD::SHL_PARTS:
11212 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011213 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011214 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011215 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011216 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011217 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011218 case ISD::FABS: return LowerFABS(Op, DAG);
11219 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011220 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011221 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011222 case ISD::SETCC: return LowerSETCC(Op, DAG);
11223 case ISD::SELECT: return LowerSELECT(Op, DAG);
11224 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011225 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011226 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011227 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000011228 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011229 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011230 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011231 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11232 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011233 case ISD::FRAME_TO_ARGS_OFFSET:
11234 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011235 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011236 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011237 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11238 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011239 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011240 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011241 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011242 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011243 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011244 case ISD::SRA:
11245 case ISD::SRL:
11246 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011247 case ISD::SADDO:
11248 case ISD::UADDO:
11249 case ISD::SSUBO:
11250 case ISD::USUBO:
11251 case ISD::SMULO:
11252 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000011253 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011254 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011255 case ISD::ADDC:
11256 case ISD::ADDE:
11257 case ISD::SUBC:
11258 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011259 case ISD::ADD: return LowerADD(Op, DAG);
11260 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011261 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011262}
11263
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011264static void ReplaceATOMIC_LOAD(SDNode *Node,
11265 SmallVectorImpl<SDValue> &Results,
11266 SelectionDAG &DAG) {
11267 DebugLoc dl = Node->getDebugLoc();
11268 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11269
11270 // Convert wide load -> cmpxchg8b/cmpxchg16b
11271 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11272 // (The only way to get a 16-byte load is cmpxchg16b)
11273 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011274 SDValue Zero = DAG.getConstant(0, VT);
11275 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011276 Node->getOperand(0),
11277 Node->getOperand(1), Zero, Zero,
11278 cast<AtomicSDNode>(Node)->getMemOperand(),
11279 cast<AtomicSDNode>(Node)->getOrdering(),
11280 cast<AtomicSDNode>(Node)->getSynchScope());
11281 Results.push_back(Swap.getValue(0));
11282 Results.push_back(Swap.getValue(1));
11283}
11284
Craig Topperc0878702012-08-17 06:55:11 +000011285static void
Duncan Sands1607f052008-12-01 11:39:25 +000011286ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011287 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011288 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011289 assert (Node->getValueType(0) == MVT::i64 &&
11290 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011291
11292 SDValue Chain = Node->getOperand(0);
11293 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011294 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011295 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011296 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011297 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011298 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011299 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011300 SDValue Result =
11301 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11302 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011303 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011304 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011305 Results.push_back(Result.getValue(2));
11306}
11307
Duncan Sands126d9072008-07-04 11:47:58 +000011308/// ReplaceNodeResults - Replace a node with an illegal result type
11309/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011310void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11311 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011312 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011313 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011314 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011315 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011316 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011317 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011318 case ISD::ADDC:
11319 case ISD::ADDE:
11320 case ISD::SUBC:
11321 case ISD::SUBE:
11322 // We don't want to expand or promote these.
11323 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011324 case ISD::FP_TO_SINT:
11325 case ISD::FP_TO_UINT: {
11326 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11327
11328 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11329 return;
11330
Eli Friedman948e95a2009-05-23 09:59:16 +000011331 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011332 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011333 SDValue FIST = Vals.first, StackSlot = Vals.second;
11334 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011335 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011336 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011337 if (StackSlot.getNode() != 0)
11338 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11339 MachinePointerInfo(),
11340 false, false, false, 0));
11341 else
11342 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011343 }
11344 return;
11345 }
11346 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011347 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011348 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011349 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011350 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011351 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011352 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011353 eax.getValue(2));
11354 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11355 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011356 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011357 Results.push_back(edx.getValue(1));
11358 return;
11359 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011360 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011361 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011362 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011363 bool Regs64bit = T == MVT::i128;
11364 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011365 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011366 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11367 DAG.getConstant(0, HalfT));
11368 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11369 DAG.getConstant(1, HalfT));
11370 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11371 Regs64bit ? X86::RAX : X86::EAX,
11372 cpInL, SDValue());
11373 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11374 Regs64bit ? X86::RDX : X86::EDX,
11375 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011376 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011377 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11378 DAG.getConstant(0, HalfT));
11379 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11380 DAG.getConstant(1, HalfT));
11381 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11382 Regs64bit ? X86::RBX : X86::EBX,
11383 swapInL, cpInH.getValue(1));
11384 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011385 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011386 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011387 SDValue Ops[] = { swapInH.getValue(0),
11388 N->getOperand(1),
11389 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011390 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011391 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011392 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11393 X86ISD::LCMPXCHG8_DAG;
11394 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011395 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011396 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11397 Regs64bit ? X86::RAX : X86::EAX,
11398 HalfT, Result.getValue(1));
11399 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11400 Regs64bit ? X86::RDX : X86::EDX,
11401 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011402 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011403 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011404 Results.push_back(cpOutH.getValue(1));
11405 return;
11406 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011407 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011408 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011409 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011410 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011411 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011412 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011413 case ISD::ATOMIC_SWAP: {
11414 unsigned Opc;
11415 switch (N->getOpcode()) {
11416 default: llvm_unreachable("Unexpected opcode");
11417 case ISD::ATOMIC_LOAD_ADD:
11418 Opc = X86ISD::ATOMADD64_DAG;
11419 break;
11420 case ISD::ATOMIC_LOAD_AND:
11421 Opc = X86ISD::ATOMAND64_DAG;
11422 break;
11423 case ISD::ATOMIC_LOAD_NAND:
11424 Opc = X86ISD::ATOMNAND64_DAG;
11425 break;
11426 case ISD::ATOMIC_LOAD_OR:
11427 Opc = X86ISD::ATOMOR64_DAG;
11428 break;
11429 case ISD::ATOMIC_LOAD_SUB:
11430 Opc = X86ISD::ATOMSUB64_DAG;
11431 break;
11432 case ISD::ATOMIC_LOAD_XOR:
11433 Opc = X86ISD::ATOMXOR64_DAG;
11434 break;
11435 case ISD::ATOMIC_SWAP:
11436 Opc = X86ISD::ATOMSWAP64_DAG;
11437 break;
11438 }
11439 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011440 return;
Craig Topperc0878702012-08-17 06:55:11 +000011441 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011442 case ISD::ATOMIC_LOAD:
11443 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011444 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011445}
11446
Evan Cheng72261582005-12-20 06:22:03 +000011447const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11448 switch (Opcode) {
11449 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011450 case X86ISD::BSF: return "X86ISD::BSF";
11451 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011452 case X86ISD::SHLD: return "X86ISD::SHLD";
11453 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011454 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011455 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011456 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011457 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011458 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011459 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011460 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11461 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11462 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011463 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011464 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011465 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011466 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011467 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011468 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011469 case X86ISD::COMI: return "X86ISD::COMI";
11470 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011471 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011472 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011473 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11474 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011475 case X86ISD::CMOV: return "X86ISD::CMOV";
11476 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011477 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011478 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11479 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011480 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011481 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011482 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011483 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011484 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011485 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11486 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011487 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011488 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011489 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011490 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011491 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011492 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11493 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11494 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011495 case X86ISD::HADD: return "X86ISD::HADD";
11496 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011497 case X86ISD::FHADD: return "X86ISD::FHADD";
11498 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011499 case X86ISD::FMAX: return "X86ISD::FMAX";
11500 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011501 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11502 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011503 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11504 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011505 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011506 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011507 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011508 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011509 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011510 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011511 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011512 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11513 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011514 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11515 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11516 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11517 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11518 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11519 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011520 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011521 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011522 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011523 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011524 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11525 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011526 case X86ISD::VSHL: return "X86ISD::VSHL";
11527 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011528 case X86ISD::VSRA: return "X86ISD::VSRA";
11529 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11530 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11531 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011532 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011533 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11534 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011535 case X86ISD::ADD: return "X86ISD::ADD";
11536 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011537 case X86ISD::ADC: return "X86ISD::ADC";
11538 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011539 case X86ISD::SMUL: return "X86ISD::SMUL";
11540 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011541 case X86ISD::INC: return "X86ISD::INC";
11542 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011543 case X86ISD::OR: return "X86ISD::OR";
11544 case X86ISD::XOR: return "X86ISD::XOR";
11545 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011546 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011547 case X86ISD::BLSI: return "X86ISD::BLSI";
11548 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11549 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011550 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011551 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011552 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011553 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11554 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11555 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011556 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011557 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011558 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011559 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011560 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011561 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11562 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011563 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11564 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11565 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011566 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11567 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011568 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11569 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011570 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011571 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011572 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011573 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11574 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011575 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011576 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011577 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011578 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011579 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011580 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011581 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011582 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011583 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011584 case X86ISD::FMADD: return "X86ISD::FMADD";
11585 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11586 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11587 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11588 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11589 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011590 }
11591}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011592
Chris Lattnerc9addb72007-03-30 23:15:24 +000011593// isLegalAddressingMode - Return true if the addressing mode represented
11594// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011595bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011596 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011597 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011598 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011599 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011600
Chris Lattnerc9addb72007-03-30 23:15:24 +000011601 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011602 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011603 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011604
Chris Lattnerc9addb72007-03-30 23:15:24 +000011605 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011606 unsigned GVFlags =
11607 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011608
Chris Lattnerdfed4132009-07-10 07:38:24 +000011609 // If a reference to this global requires an extra load, we can't fold it.
11610 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011611 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011612
Chris Lattnerdfed4132009-07-10 07:38:24 +000011613 // If BaseGV requires a register for the PIC base, we cannot also have a
11614 // BaseReg specified.
11615 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011616 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011617
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011618 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011619 if ((M != CodeModel::Small || R != Reloc::Static) &&
11620 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011621 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011622 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011623
Chris Lattnerc9addb72007-03-30 23:15:24 +000011624 switch (AM.Scale) {
11625 case 0:
11626 case 1:
11627 case 2:
11628 case 4:
11629 case 8:
11630 // These scales always work.
11631 break;
11632 case 3:
11633 case 5:
11634 case 9:
11635 // These scales are formed with basereg+scalereg. Only accept if there is
11636 // no basereg yet.
11637 if (AM.HasBaseReg)
11638 return false;
11639 break;
11640 default: // Other stuff never works.
11641 return false;
11642 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011643
Chris Lattnerc9addb72007-03-30 23:15:24 +000011644 return true;
11645}
11646
11647
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011648bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011649 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011650 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011651 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11652 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011653 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011654 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011655 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011656}
11657
Evan Cheng70e10d32012-07-17 06:53:39 +000011658bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11659 return Imm == (int32_t)Imm;
11660}
11661
11662bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011663 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011664 return Imm == (int32_t)Imm;
11665}
11666
Owen Andersone50ed302009-08-10 22:56:29 +000011667bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011668 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011669 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011670 unsigned NumBits1 = VT1.getSizeInBits();
11671 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011672 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011673 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011674 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011675}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011676
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011677bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011678 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011679 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011680}
11681
Owen Andersone50ed302009-08-10 22:56:29 +000011682bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011683 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011684 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011685}
11686
Owen Andersone50ed302009-08-10 22:56:29 +000011687bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011688 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011689 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011690}
11691
Evan Cheng60c07e12006-07-05 22:17:51 +000011692/// isShuffleMaskLegal - Targets can use this to indicate that they only
11693/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11694/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11695/// are assumed to be legal.
11696bool
Eric Christopherfd179292009-08-27 18:07:15 +000011697X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011698 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011699 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011700 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011701 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011702
Nate Begemana09008b2009-10-19 02:17:23 +000011703 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011704 return (VT.getVectorNumElements() == 2 ||
11705 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11706 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011707 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011708 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011709 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11710 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011711 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011712 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11713 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011714 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11715 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011716}
11717
Dan Gohman7d8143f2008-04-09 20:09:42 +000011718bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011719X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011720 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011721 unsigned NumElts = VT.getVectorNumElements();
11722 // FIXME: This collection of masks seems suspect.
11723 if (NumElts == 2)
11724 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011725 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011726 return (isMOVLMask(Mask, VT) ||
11727 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011728 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11729 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011730 }
11731 return false;
11732}
11733
11734//===----------------------------------------------------------------------===//
11735// X86 Scheduler Hooks
11736//===----------------------------------------------------------------------===//
11737
Mon P Wang63307c32008-05-05 19:05:59 +000011738// private utility function
11739MachineBasicBlock *
11740X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11741 MachineBasicBlock *MBB,
11742 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011743 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011744 unsigned LoadOpc,
11745 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011746 unsigned notOpc,
11747 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011748 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011749 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011750 // For the atomic bitwise operator, we generate
11751 // thisMBB:
11752 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011753 // ld t1 = [bitinstr.addr]
11754 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011755 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011756 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011757 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011758 // bz newMBB
11759 // fallthrough -->nextMBB
11760 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11761 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011762 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011763 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011764
Mon P Wang63307c32008-05-05 19:05:59 +000011765 /// First build the CFG
11766 MachineFunction *F = MBB->getParent();
11767 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011768 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11769 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11770 F->insert(MBBIter, newMBB);
11771 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011772
Dan Gohman14152b42010-07-06 20:24:04 +000011773 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11774 nextMBB->splice(nextMBB->begin(), thisMBB,
11775 llvm::next(MachineBasicBlock::iterator(bInstr)),
11776 thisMBB->end());
11777 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011778
Mon P Wang63307c32008-05-05 19:05:59 +000011779 // Update thisMBB to fall through to newMBB
11780 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011781
Mon P Wang63307c32008-05-05 19:05:59 +000011782 // newMBB jumps to itself and fall through to nextMBB
11783 newMBB->addSuccessor(nextMBB);
11784 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011785
Mon P Wang63307c32008-05-05 19:05:59 +000011786 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011787 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011788 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011789 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011790 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011791 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011792 int numArgs = bInstr->getNumOperands() - 1;
11793 for (int i=0; i < numArgs; ++i)
11794 argOpers[i] = &bInstr->getOperand(i+1);
11795
11796 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011797 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011798 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011799
Dale Johannesen140be2d2008-08-19 18:47:28 +000011800 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011801 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011802 for (int i=0; i <= lastAddrIndx; ++i)
11803 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011804
Dale Johannesen140be2d2008-08-19 18:47:28 +000011805 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011806 assert((argOpers[valArgIndx]->isReg() ||
11807 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011808 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011809 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011810 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011811 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011812 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011813 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011814 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011815
Richard Smith42fc29e2012-04-13 22:47:00 +000011816 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11817 if (Invert) {
11818 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11819 }
11820 else
11821 t3 = t2;
11822
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011823 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011824 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011825
Dale Johannesene4d209d2009-02-03 20:21:25 +000011826 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011827 for (int i=0; i <= lastAddrIndx; ++i)
11828 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011829 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011830 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011831 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11832 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011833
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011834 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011835 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011836
Mon P Wang63307c32008-05-05 19:05:59 +000011837 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011838 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011839
Dan Gohman14152b42010-07-06 20:24:04 +000011840 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011841 return nextMBB;
11842}
11843
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011844// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011845MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011846X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11847 MachineBasicBlock *MBB,
11848 unsigned regOpcL,
11849 unsigned regOpcH,
11850 unsigned immOpcL,
11851 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011852 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011853 // For the atomic bitwise operator, we generate
11854 // thisMBB (instructions are in pairs, except cmpxchg8b)
11855 // ld t1,t2 = [bitinstr.addr]
11856 // newMBB:
11857 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11858 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011859 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011860 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011861 // mov ECX, EBX <- t5, t6
11862 // mov EAX, EDX <- t1, t2
11863 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11864 // mov t3, t4 <- EAX, EDX
11865 // bz newMBB
11866 // result in out1, out2
11867 // fallthrough -->nextMBB
11868
Craig Topperc9099502012-04-20 06:31:50 +000011869 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011870 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011871 const unsigned NotOpc = X86::NOT32r;
11872 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11873 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11874 MachineFunction::iterator MBBIter = MBB;
11875 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011876
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011877 /// First build the CFG
11878 MachineFunction *F = MBB->getParent();
11879 MachineBasicBlock *thisMBB = MBB;
11880 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11881 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11882 F->insert(MBBIter, newMBB);
11883 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011884
Dan Gohman14152b42010-07-06 20:24:04 +000011885 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11886 nextMBB->splice(nextMBB->begin(), thisMBB,
11887 llvm::next(MachineBasicBlock::iterator(bInstr)),
11888 thisMBB->end());
11889 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011890
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011891 // Update thisMBB to fall through to newMBB
11892 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011893
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011894 // newMBB jumps to itself and fall through to nextMBB
11895 newMBB->addSuccessor(nextMBB);
11896 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011897
Dale Johannesene4d209d2009-02-03 20:21:25 +000011898 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011899 // Insert instructions into newMBB based on incoming instruction
11900 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011901 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011902 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011903 MachineOperand& dest1Oper = bInstr->getOperand(0);
11904 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011905 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11906 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011907 argOpers[i] = &bInstr->getOperand(i+2);
11908
Dan Gohman71ea4e52010-05-14 21:01:44 +000011909 // We use some of the operands multiple times, so conservatively just
11910 // clear any kill flags that might be present.
11911 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11912 argOpers[i]->setIsKill(false);
11913 }
11914
Evan Chengad5b52f2010-01-08 19:14:57 +000011915 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011916 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011917
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011918 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011919 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011920 for (int i=0; i <= lastAddrIndx; ++i)
11921 (*MIB).addOperand(*argOpers[i]);
11922 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011923 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011924 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011925 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011926 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011927 MachineOperand newOp3 = *(argOpers[3]);
11928 if (newOp3.isImm())
11929 newOp3.setImm(newOp3.getImm()+4);
11930 else
11931 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011932 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011933 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011934
11935 // t3/4 are defined later, at the bottom of the loop
11936 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11937 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011938 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011939 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011940 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011941 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11942
Evan Cheng306b4ca2010-01-08 23:41:50 +000011943 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000011944 // the PHI instructions.
11945 t1 = dest1Oper.getReg();
11946 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011947
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011948 int valArgIndx = lastAddrIndx + 1;
11949 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011950 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011951 "invalid operand");
11952 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11953 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011954 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011955 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011956 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011957 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011958 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011959 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011960 (*MIB).addOperand(*argOpers[valArgIndx]);
11961 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011962 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011963 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011964 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011965 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011966 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011967 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011968 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011969 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011970 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011971 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011972
Richard Smith42fc29e2012-04-13 22:47:00 +000011973 unsigned t7, t8;
11974 if (Invert) {
11975 t7 = F->getRegInfo().createVirtualRegister(RC);
11976 t8 = F->getRegInfo().createVirtualRegister(RC);
11977 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
11978 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
11979 } else {
11980 t7 = t5;
11981 t8 = t6;
11982 }
11983
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011984 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011985 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011986 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011987 MIB.addReg(t2);
11988
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011989 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000011990 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011991 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000011992 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000011993
Dale Johannesene4d209d2009-02-03 20:21:25 +000011994 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011995 for (int i=0; i <= lastAddrIndx; ++i)
11996 (*MIB).addOperand(*argOpers[i]);
11997
11998 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011999 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12000 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012001
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012002 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012003 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012004 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012005 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012006
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012007 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012008 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012009
Dan Gohman14152b42010-07-06 20:24:04 +000012010 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012011 return nextMBB;
12012}
12013
12014// private utility function
12015MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000012016X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
12017 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000012018 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000012019 // For the atomic min/max operator, we generate
12020 // thisMBB:
12021 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000012022 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000012023 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000012024 // cmp t1, t2
12025 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000012026 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000012027 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
12028 // bz newMBB
12029 // fallthrough -->nextMBB
12030 //
12031 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12032 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012033 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012034 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012035
Mon P Wang63307c32008-05-05 19:05:59 +000012036 /// First build the CFG
12037 MachineFunction *F = MBB->getParent();
12038 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012039 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12040 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12041 F->insert(MBBIter, newMBB);
12042 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012043
Dan Gohman14152b42010-07-06 20:24:04 +000012044 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12045 nextMBB->splice(nextMBB->begin(), thisMBB,
12046 llvm::next(MachineBasicBlock::iterator(mInstr)),
12047 thisMBB->end());
12048 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012049
Mon P Wang63307c32008-05-05 19:05:59 +000012050 // Update thisMBB to fall through to newMBB
12051 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012052
Mon P Wang63307c32008-05-05 19:05:59 +000012053 // newMBB jumps to newMBB and fall through to nextMBB
12054 newMBB->addSuccessor(nextMBB);
12055 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012056
Dale Johannesene4d209d2009-02-03 20:21:25 +000012057 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000012058 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012059 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012060 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000012061 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012062 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000012063 int numArgs = mInstr->getNumOperands() - 1;
12064 for (int i=0; i < numArgs; ++i)
12065 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012066
Mon P Wang63307c32008-05-05 19:05:59 +000012067 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012068 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012069 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000012070
Craig Topperc9099502012-04-20 06:31:50 +000012071 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012072 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000012073 for (int i=0; i <= lastAddrIndx; ++i)
12074 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000012075
Mon P Wang63307c32008-05-05 19:05:59 +000012076 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000012077 assert((argOpers[valArgIndx]->isReg() ||
12078 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000012079 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000012080
Craig Topperc9099502012-04-20 06:31:50 +000012081 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012082 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012083 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012084 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012085 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012086 (*MIB).addOperand(*argOpers[valArgIndx]);
12087
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012088 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012089 MIB.addReg(t1);
12090
Dale Johannesene4d209d2009-02-03 20:21:25 +000012091 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012092 MIB.addReg(t1);
12093 MIB.addReg(t2);
12094
12095 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012096 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012097 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012098 MIB.addReg(t2);
12099 MIB.addReg(t1);
12100
12101 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012102 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012103 for (int i=0; i <= lastAddrIndx; ++i)
12104 (*MIB).addOperand(*argOpers[i]);
12105 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012106 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012107 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12108 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012109
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012110 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012111 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012112
Mon P Wang63307c32008-05-05 19:05:59 +000012113 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012114 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012115
Dan Gohman14152b42010-07-06 20:24:04 +000012116 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012117 return nextMBB;
12118}
12119
Eric Christopherf83a5de2009-08-27 18:08:16 +000012120// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012121// or XMM0_V32I8 in AVX all of this code can be replaced with that
12122// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012123MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012124X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012125 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012126 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012127 "Target must have SSE4.2 or AVX features enabled");
12128
Eric Christopherb120ab42009-08-18 22:50:32 +000012129 DebugLoc dl = MI->getDebugLoc();
12130 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012131 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012132 if (!Subtarget->hasAVX()) {
12133 if (memArg)
12134 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12135 else
12136 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12137 } else {
12138 if (memArg)
12139 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12140 else
12141 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12142 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012143
Eric Christopher41c902f2010-11-30 08:20:21 +000012144 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012145 for (unsigned i = 0; i < numArgs; ++i) {
12146 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012147 if (!(Op.isReg() && Op.isImplicit()))
12148 MIB.addOperand(Op);
12149 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012150 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012151 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012152 .addReg(X86::XMM0);
12153
Dan Gohman14152b42010-07-06 20:24:04 +000012154 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012155 return BB;
12156}
12157
12158MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012159X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012160 DebugLoc dl = MI->getDebugLoc();
12161 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012162
Eric Christopher228232b2010-11-30 07:20:12 +000012163 // Address into RAX/EAX, other two args into ECX, EDX.
12164 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12165 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12166 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12167 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012168 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012169
Eric Christopher228232b2010-11-30 07:20:12 +000012170 unsigned ValOps = X86::AddrNumOperands;
12171 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12172 .addReg(MI->getOperand(ValOps).getReg());
12173 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12174 .addReg(MI->getOperand(ValOps+1).getReg());
12175
12176 // The instruction doesn't actually take any operands though.
12177 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012178
Eric Christopher228232b2010-11-30 07:20:12 +000012179 MI->eraseFromParent(); // The pseudo is gone now.
12180 return BB;
12181}
12182
12183MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012184X86TargetLowering::EmitVAARG64WithCustomInserter(
12185 MachineInstr *MI,
12186 MachineBasicBlock *MBB) const {
12187 // Emit va_arg instruction on X86-64.
12188
12189 // Operands to this pseudo-instruction:
12190 // 0 ) Output : destination address (reg)
12191 // 1-5) Input : va_list address (addr, i64mem)
12192 // 6 ) ArgSize : Size (in bytes) of vararg type
12193 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12194 // 8 ) Align : Alignment of type
12195 // 9 ) EFLAGS (implicit-def)
12196
12197 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12198 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12199
12200 unsigned DestReg = MI->getOperand(0).getReg();
12201 MachineOperand &Base = MI->getOperand(1);
12202 MachineOperand &Scale = MI->getOperand(2);
12203 MachineOperand &Index = MI->getOperand(3);
12204 MachineOperand &Disp = MI->getOperand(4);
12205 MachineOperand &Segment = MI->getOperand(5);
12206 unsigned ArgSize = MI->getOperand(6).getImm();
12207 unsigned ArgMode = MI->getOperand(7).getImm();
12208 unsigned Align = MI->getOperand(8).getImm();
12209
12210 // Memory Reference
12211 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12212 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12213 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12214
12215 // Machine Information
12216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12217 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12218 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12219 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12220 DebugLoc DL = MI->getDebugLoc();
12221
12222 // struct va_list {
12223 // i32 gp_offset
12224 // i32 fp_offset
12225 // i64 overflow_area (address)
12226 // i64 reg_save_area (address)
12227 // }
12228 // sizeof(va_list) = 24
12229 // alignment(va_list) = 8
12230
12231 unsigned TotalNumIntRegs = 6;
12232 unsigned TotalNumXMMRegs = 8;
12233 bool UseGPOffset = (ArgMode == 1);
12234 bool UseFPOffset = (ArgMode == 2);
12235 unsigned MaxOffset = TotalNumIntRegs * 8 +
12236 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12237
12238 /* Align ArgSize to a multiple of 8 */
12239 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12240 bool NeedsAlign = (Align > 8);
12241
12242 MachineBasicBlock *thisMBB = MBB;
12243 MachineBasicBlock *overflowMBB;
12244 MachineBasicBlock *offsetMBB;
12245 MachineBasicBlock *endMBB;
12246
12247 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12248 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12249 unsigned OffsetReg = 0;
12250
12251 if (!UseGPOffset && !UseFPOffset) {
12252 // If we only pull from the overflow region, we don't create a branch.
12253 // We don't need to alter control flow.
12254 OffsetDestReg = 0; // unused
12255 OverflowDestReg = DestReg;
12256
12257 offsetMBB = NULL;
12258 overflowMBB = thisMBB;
12259 endMBB = thisMBB;
12260 } else {
12261 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12262 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12263 // If not, pull from overflow_area. (branch to overflowMBB)
12264 //
12265 // thisMBB
12266 // | .
12267 // | .
12268 // offsetMBB overflowMBB
12269 // | .
12270 // | .
12271 // endMBB
12272
12273 // Registers for the PHI in endMBB
12274 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12275 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12276
12277 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12278 MachineFunction *MF = MBB->getParent();
12279 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12280 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12281 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12282
12283 MachineFunction::iterator MBBIter = MBB;
12284 ++MBBIter;
12285
12286 // Insert the new basic blocks
12287 MF->insert(MBBIter, offsetMBB);
12288 MF->insert(MBBIter, overflowMBB);
12289 MF->insert(MBBIter, endMBB);
12290
12291 // Transfer the remainder of MBB and its successor edges to endMBB.
12292 endMBB->splice(endMBB->begin(), thisMBB,
12293 llvm::next(MachineBasicBlock::iterator(MI)),
12294 thisMBB->end());
12295 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12296
12297 // Make offsetMBB and overflowMBB successors of thisMBB
12298 thisMBB->addSuccessor(offsetMBB);
12299 thisMBB->addSuccessor(overflowMBB);
12300
12301 // endMBB is a successor of both offsetMBB and overflowMBB
12302 offsetMBB->addSuccessor(endMBB);
12303 overflowMBB->addSuccessor(endMBB);
12304
12305 // Load the offset value into a register
12306 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12307 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12308 .addOperand(Base)
12309 .addOperand(Scale)
12310 .addOperand(Index)
12311 .addDisp(Disp, UseFPOffset ? 4 : 0)
12312 .addOperand(Segment)
12313 .setMemRefs(MMOBegin, MMOEnd);
12314
12315 // Check if there is enough room left to pull this argument.
12316 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12317 .addReg(OffsetReg)
12318 .addImm(MaxOffset + 8 - ArgSizeA8);
12319
12320 // Branch to "overflowMBB" if offset >= max
12321 // Fall through to "offsetMBB" otherwise
12322 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12323 .addMBB(overflowMBB);
12324 }
12325
12326 // In offsetMBB, emit code to use the reg_save_area.
12327 if (offsetMBB) {
12328 assert(OffsetReg != 0);
12329
12330 // Read the reg_save_area address.
12331 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12332 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12333 .addOperand(Base)
12334 .addOperand(Scale)
12335 .addOperand(Index)
12336 .addDisp(Disp, 16)
12337 .addOperand(Segment)
12338 .setMemRefs(MMOBegin, MMOEnd);
12339
12340 // Zero-extend the offset
12341 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12342 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12343 .addImm(0)
12344 .addReg(OffsetReg)
12345 .addImm(X86::sub_32bit);
12346
12347 // Add the offset to the reg_save_area to get the final address.
12348 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12349 .addReg(OffsetReg64)
12350 .addReg(RegSaveReg);
12351
12352 // Compute the offset for the next argument
12353 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12354 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12355 .addReg(OffsetReg)
12356 .addImm(UseFPOffset ? 16 : 8);
12357
12358 // Store it back into the va_list.
12359 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12360 .addOperand(Base)
12361 .addOperand(Scale)
12362 .addOperand(Index)
12363 .addDisp(Disp, UseFPOffset ? 4 : 0)
12364 .addOperand(Segment)
12365 .addReg(NextOffsetReg)
12366 .setMemRefs(MMOBegin, MMOEnd);
12367
12368 // Jump to endMBB
12369 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12370 .addMBB(endMBB);
12371 }
12372
12373 //
12374 // Emit code to use overflow area
12375 //
12376
12377 // Load the overflow_area address into a register.
12378 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12379 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12380 .addOperand(Base)
12381 .addOperand(Scale)
12382 .addOperand(Index)
12383 .addDisp(Disp, 8)
12384 .addOperand(Segment)
12385 .setMemRefs(MMOBegin, MMOEnd);
12386
12387 // If we need to align it, do so. Otherwise, just copy the address
12388 // to OverflowDestReg.
12389 if (NeedsAlign) {
12390 // Align the overflow address
12391 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12392 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12393
12394 // aligned_addr = (addr + (align-1)) & ~(align-1)
12395 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12396 .addReg(OverflowAddrReg)
12397 .addImm(Align-1);
12398
12399 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12400 .addReg(TmpReg)
12401 .addImm(~(uint64_t)(Align-1));
12402 } else {
12403 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12404 .addReg(OverflowAddrReg);
12405 }
12406
12407 // Compute the next overflow address after this argument.
12408 // (the overflow address should be kept 8-byte aligned)
12409 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12410 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12411 .addReg(OverflowDestReg)
12412 .addImm(ArgSizeA8);
12413
12414 // Store the new overflow address.
12415 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12416 .addOperand(Base)
12417 .addOperand(Scale)
12418 .addOperand(Index)
12419 .addDisp(Disp, 8)
12420 .addOperand(Segment)
12421 .addReg(NextAddrReg)
12422 .setMemRefs(MMOBegin, MMOEnd);
12423
12424 // If we branched, emit the PHI to the front of endMBB.
12425 if (offsetMBB) {
12426 BuildMI(*endMBB, endMBB->begin(), DL,
12427 TII->get(X86::PHI), DestReg)
12428 .addReg(OffsetDestReg).addMBB(offsetMBB)
12429 .addReg(OverflowDestReg).addMBB(overflowMBB);
12430 }
12431
12432 // Erase the pseudo instruction
12433 MI->eraseFromParent();
12434
12435 return endMBB;
12436}
12437
12438MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012439X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12440 MachineInstr *MI,
12441 MachineBasicBlock *MBB) const {
12442 // Emit code to save XMM registers to the stack. The ABI says that the
12443 // number of registers to save is given in %al, so it's theoretically
12444 // possible to do an indirect jump trick to avoid saving all of them,
12445 // however this code takes a simpler approach and just executes all
12446 // of the stores if %al is non-zero. It's less code, and it's probably
12447 // easier on the hardware branch predictor, and stores aren't all that
12448 // expensive anyway.
12449
12450 // Create the new basic blocks. One block contains all the XMM stores,
12451 // and one block is the final destination regardless of whether any
12452 // stores were performed.
12453 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12454 MachineFunction *F = MBB->getParent();
12455 MachineFunction::iterator MBBIter = MBB;
12456 ++MBBIter;
12457 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12458 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12459 F->insert(MBBIter, XMMSaveMBB);
12460 F->insert(MBBIter, EndMBB);
12461
Dan Gohman14152b42010-07-06 20:24:04 +000012462 // Transfer the remainder of MBB and its successor edges to EndMBB.
12463 EndMBB->splice(EndMBB->begin(), MBB,
12464 llvm::next(MachineBasicBlock::iterator(MI)),
12465 MBB->end());
12466 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12467
Dan Gohmand6708ea2009-08-15 01:38:56 +000012468 // The original block will now fall through to the XMM save block.
12469 MBB->addSuccessor(XMMSaveMBB);
12470 // The XMMSaveMBB will fall through to the end block.
12471 XMMSaveMBB->addSuccessor(EndMBB);
12472
12473 // Now add the instructions.
12474 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12475 DebugLoc DL = MI->getDebugLoc();
12476
12477 unsigned CountReg = MI->getOperand(0).getReg();
12478 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12479 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12480
12481 if (!Subtarget->isTargetWin64()) {
12482 // If %al is 0, branch around the XMM save block.
12483 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012484 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012485 MBB->addSuccessor(EndMBB);
12486 }
12487
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012488 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012489 // In the XMM save block, save all the XMM argument registers.
12490 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12491 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012492 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012493 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012494 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012495 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012496 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012497 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012498 .addFrameIndex(RegSaveFrameIndex)
12499 .addImm(/*Scale=*/1)
12500 .addReg(/*IndexReg=*/0)
12501 .addImm(/*Disp=*/Offset)
12502 .addReg(/*Segment=*/0)
12503 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012504 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012505 }
12506
Dan Gohman14152b42010-07-06 20:24:04 +000012507 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012508
12509 return EndMBB;
12510}
Mon P Wang63307c32008-05-05 19:05:59 +000012511
Lang Hames6e3f7e42012-02-03 01:13:49 +000012512// The EFLAGS operand of SelectItr might be missing a kill marker
12513// because there were multiple uses of EFLAGS, and ISel didn't know
12514// which to mark. Figure out whether SelectItr should have had a
12515// kill marker, and set it if it should. Returns the correct kill
12516// marker value.
12517static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12518 MachineBasicBlock* BB,
12519 const TargetRegisterInfo* TRI) {
12520 // Scan forward through BB for a use/def of EFLAGS.
12521 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12522 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012523 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012524 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012525 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012526 if (mi.definesRegister(X86::EFLAGS))
12527 break; // Should have kill-flag - update below.
12528 }
12529
12530 // If we hit the end of the block, check whether EFLAGS is live into a
12531 // successor.
12532 if (miI == BB->end()) {
12533 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12534 sEnd = BB->succ_end();
12535 sItr != sEnd; ++sItr) {
12536 MachineBasicBlock* succ = *sItr;
12537 if (succ->isLiveIn(X86::EFLAGS))
12538 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012539 }
12540 }
12541
Lang Hames6e3f7e42012-02-03 01:13:49 +000012542 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12543 // out. SelectMI should have a kill flag on EFLAGS.
12544 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012545 return true;
12546}
12547
Evan Cheng60c07e12006-07-05 22:17:51 +000012548MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012549X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012550 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012551 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12552 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012553
Chris Lattner52600972009-09-02 05:57:00 +000012554 // To "insert" a SELECT_CC instruction, we actually have to insert the
12555 // diamond control-flow pattern. The incoming instruction knows the
12556 // destination vreg to set, the condition code register to branch on, the
12557 // true/false values to select between, and a branch opcode to use.
12558 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12559 MachineFunction::iterator It = BB;
12560 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012561
Chris Lattner52600972009-09-02 05:57:00 +000012562 // thisMBB:
12563 // ...
12564 // TrueVal = ...
12565 // cmpTY ccX, r1, r2
12566 // bCC copy1MBB
12567 // fallthrough --> copy0MBB
12568 MachineBasicBlock *thisMBB = BB;
12569 MachineFunction *F = BB->getParent();
12570 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12571 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012572 F->insert(It, copy0MBB);
12573 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012574
Bill Wendling730c07e2010-06-25 20:48:10 +000012575 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12576 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012577 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12578 if (!MI->killsRegister(X86::EFLAGS) &&
12579 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12580 copy0MBB->addLiveIn(X86::EFLAGS);
12581 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012582 }
12583
Dan Gohman14152b42010-07-06 20:24:04 +000012584 // Transfer the remainder of BB and its successor edges to sinkMBB.
12585 sinkMBB->splice(sinkMBB->begin(), BB,
12586 llvm::next(MachineBasicBlock::iterator(MI)),
12587 BB->end());
12588 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12589
12590 // Add the true and fallthrough blocks as its successors.
12591 BB->addSuccessor(copy0MBB);
12592 BB->addSuccessor(sinkMBB);
12593
12594 // Create the conditional branch instruction.
12595 unsigned Opc =
12596 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12597 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12598
Chris Lattner52600972009-09-02 05:57:00 +000012599 // copy0MBB:
12600 // %FalseValue = ...
12601 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012602 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012603
Chris Lattner52600972009-09-02 05:57:00 +000012604 // sinkMBB:
12605 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12606 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012607 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12608 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012609 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12610 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12611
Dan Gohman14152b42010-07-06 20:24:04 +000012612 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012613 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012614}
12615
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012616MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012617X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12618 bool Is64Bit) const {
12619 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12620 DebugLoc DL = MI->getDebugLoc();
12621 MachineFunction *MF = BB->getParent();
12622 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12623
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012624 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012625
12626 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12627 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12628
12629 // BB:
12630 // ... [Till the alloca]
12631 // If stacklet is not large enough, jump to mallocMBB
12632 //
12633 // bumpMBB:
12634 // Allocate by subtracting from RSP
12635 // Jump to continueMBB
12636 //
12637 // mallocMBB:
12638 // Allocate by call to runtime
12639 //
12640 // continueMBB:
12641 // ...
12642 // [rest of original BB]
12643 //
12644
12645 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12646 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12647 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12648
12649 MachineRegisterInfo &MRI = MF->getRegInfo();
12650 const TargetRegisterClass *AddrRegClass =
12651 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12652
12653 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12654 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12655 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012656 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012657 sizeVReg = MI->getOperand(1).getReg(),
12658 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12659
12660 MachineFunction::iterator MBBIter = BB;
12661 ++MBBIter;
12662
12663 MF->insert(MBBIter, bumpMBB);
12664 MF->insert(MBBIter, mallocMBB);
12665 MF->insert(MBBIter, continueMBB);
12666
12667 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12668 (MachineBasicBlock::iterator(MI)), BB->end());
12669 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12670
12671 // Add code to the main basic block to check if the stack limit has been hit,
12672 // and if so, jump to mallocMBB otherwise to bumpMBB.
12673 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012674 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012675 .addReg(tmpSPVReg).addReg(sizeVReg);
12676 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012677 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012678 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012679 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12680
12681 // bumpMBB simply decreases the stack pointer, since we know the current
12682 // stacklet has enough space.
12683 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012684 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012685 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012686 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012687 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12688
12689 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012690 const uint32_t *RegMask =
12691 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012692 if (Is64Bit) {
12693 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12694 .addReg(sizeVReg);
12695 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012696 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012697 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012698 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012699 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012700 } else {
12701 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12702 .addImm(12);
12703 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12704 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012705 .addExternalSymbol("__morestack_allocate_stack_space")
12706 .addRegMask(RegMask)
12707 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012708 }
12709
12710 if (!Is64Bit)
12711 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12712 .addImm(16);
12713
12714 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12715 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12716 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12717
12718 // Set up the CFG correctly.
12719 BB->addSuccessor(bumpMBB);
12720 BB->addSuccessor(mallocMBB);
12721 mallocMBB->addSuccessor(continueMBB);
12722 bumpMBB->addSuccessor(continueMBB);
12723
12724 // Take care of the PHI nodes.
12725 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12726 MI->getOperand(0).getReg())
12727 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12728 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12729
12730 // Delete the original pseudo instruction.
12731 MI->eraseFromParent();
12732
12733 // And we're done.
12734 return continueMBB;
12735}
12736
12737MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012738X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012739 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012740 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12741 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012742
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012743 assert(!Subtarget->isTargetEnvMacho());
12744
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012745 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12746 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012747
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012748 if (Subtarget->isTargetWin64()) {
12749 if (Subtarget->isTargetCygMing()) {
12750 // ___chkstk(Mingw64):
12751 // Clobbers R10, R11, RAX and EFLAGS.
12752 // Updates RSP.
12753 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12754 .addExternalSymbol("___chkstk")
12755 .addReg(X86::RAX, RegState::Implicit)
12756 .addReg(X86::RSP, RegState::Implicit)
12757 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12758 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12759 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12760 } else {
12761 // __chkstk(MSVCRT): does not update stack pointer.
12762 // Clobbers R10, R11 and EFLAGS.
12763 // FIXME: RAX(allocated size) might be reused and not killed.
12764 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12765 .addExternalSymbol("__chkstk")
12766 .addReg(X86::RAX, RegState::Implicit)
12767 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12768 // RAX has the offset to subtracted from RSP.
12769 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12770 .addReg(X86::RSP)
12771 .addReg(X86::RAX);
12772 }
12773 } else {
12774 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012775 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12776
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012777 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12778 .addExternalSymbol(StackProbeSymbol)
12779 .addReg(X86::EAX, RegState::Implicit)
12780 .addReg(X86::ESP, RegState::Implicit)
12781 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12782 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12783 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12784 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012785
Dan Gohman14152b42010-07-06 20:24:04 +000012786 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012787 return BB;
12788}
Chris Lattner52600972009-09-02 05:57:00 +000012789
12790MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012791X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12792 MachineBasicBlock *BB) const {
12793 // This is pretty easy. We're taking the value that we received from
12794 // our load from the relocation, sticking it in either RDI (x86-64)
12795 // or EAX and doing an indirect call. The return value will then
12796 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012797 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012798 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012799 DebugLoc DL = MI->getDebugLoc();
12800 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012801
12802 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012803 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012804
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012805 // Get a register mask for the lowered call.
12806 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12807 // proper register mask.
12808 const uint32_t *RegMask =
12809 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012810 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012811 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12812 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012813 .addReg(X86::RIP)
12814 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012815 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012816 MI->getOperand(3).getTargetFlags())
12817 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012818 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012819 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012820 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012821 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012822 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12823 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012824 .addReg(0)
12825 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012826 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012827 MI->getOperand(3).getTargetFlags())
12828 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012829 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012830 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012831 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012832 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012833 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12834 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012835 .addReg(TII->getGlobalBaseReg(F))
12836 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012837 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012838 MI->getOperand(3).getTargetFlags())
12839 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012840 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012841 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012842 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012843 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012844
Dan Gohman14152b42010-07-06 20:24:04 +000012845 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012846 return BB;
12847}
12848
12849MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012850X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012851 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012852 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012853 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012854 case X86::TAILJMPd64:
12855 case X86::TAILJMPr64:
12856 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012857 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012858 case X86::TCRETURNdi64:
12859 case X86::TCRETURNri64:
12860 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012861 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012862 case X86::WIN_ALLOCA:
12863 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012864 case X86::SEG_ALLOCA_32:
12865 return EmitLoweredSegAlloca(MI, BB, false);
12866 case X86::SEG_ALLOCA_64:
12867 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012868 case X86::TLSCall_32:
12869 case X86::TLSCall_64:
12870 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012871 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012872 case X86::CMOV_FR32:
12873 case X86::CMOV_FR64:
12874 case X86::CMOV_V4F32:
12875 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012876 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012877 case X86::CMOV_V8F32:
12878 case X86::CMOV_V4F64:
12879 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012880 case X86::CMOV_GR16:
12881 case X86::CMOV_GR32:
12882 case X86::CMOV_RFP32:
12883 case X86::CMOV_RFP64:
12884 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012885 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012886
Dale Johannesen849f2142007-07-03 00:53:03 +000012887 case X86::FP32_TO_INT16_IN_MEM:
12888 case X86::FP32_TO_INT32_IN_MEM:
12889 case X86::FP32_TO_INT64_IN_MEM:
12890 case X86::FP64_TO_INT16_IN_MEM:
12891 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012892 case X86::FP64_TO_INT64_IN_MEM:
12893 case X86::FP80_TO_INT16_IN_MEM:
12894 case X86::FP80_TO_INT32_IN_MEM:
12895 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012896 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12897 DebugLoc DL = MI->getDebugLoc();
12898
Evan Cheng60c07e12006-07-05 22:17:51 +000012899 // Change the floating point control register to use "round towards zero"
12900 // mode when truncating to an integer value.
12901 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012902 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012903 addFrameReference(BuildMI(*BB, MI, DL,
12904 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012905
12906 // Load the old value of the high byte of the control word...
12907 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012908 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012909 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012910 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012911
12912 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012913 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012914 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012915
12916 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012917 addFrameReference(BuildMI(*BB, MI, DL,
12918 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012919
12920 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012921 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012922 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012923
12924 // Get the X86 opcode to use.
12925 unsigned Opc;
12926 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012927 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012928 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12929 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12930 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12931 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12932 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12933 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012934 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12935 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12936 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012937 }
12938
12939 X86AddressMode AM;
12940 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012941 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012942 AM.BaseType = X86AddressMode::RegBase;
12943 AM.Base.Reg = Op.getReg();
12944 } else {
12945 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012946 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012947 }
12948 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012949 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012950 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012951 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012952 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012953 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012954 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012955 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012956 AM.GV = Op.getGlobal();
12957 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012958 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012959 }
Dan Gohman14152b42010-07-06 20:24:04 +000012960 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012961 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012962
12963 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012964 addFrameReference(BuildMI(*BB, MI, DL,
12965 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012966
Dan Gohman14152b42010-07-06 20:24:04 +000012967 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012968 return BB;
12969 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012970 // String/text processing lowering.
12971 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012972 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012973 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012974 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012975 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012976 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012977 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000012978 case X86::VPCMPESTRM128MEM: {
12979 unsigned NumArgs;
12980 bool MemArg;
12981 switch (MI->getOpcode()) {
12982 default: llvm_unreachable("illegal opcode!");
12983 case X86::PCMPISTRM128REG:
12984 case X86::VPCMPISTRM128REG:
12985 NumArgs = 3; MemArg = false; break;
12986 case X86::PCMPISTRM128MEM:
12987 case X86::VPCMPISTRM128MEM:
12988 NumArgs = 3; MemArg = true; break;
12989 case X86::PCMPESTRM128REG:
12990 case X86::VPCMPESTRM128REG:
12991 NumArgs = 5; MemArg = false; break;
12992 case X86::PCMPESTRM128MEM:
12993 case X86::VPCMPESTRM128MEM:
12994 NumArgs = 5; MemArg = true; break;
12995 }
12996 return EmitPCMP(MI, BB, NumArgs, MemArg);
12997 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012998
Eric Christopher228232b2010-11-30 07:20:12 +000012999 // Thread synchronization.
13000 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013001 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013002
Eric Christopherb120ab42009-08-18 22:50:32 +000013003 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000013004 case X86::ATOMMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013005 case X86::ATOMMAX32:
Mon P Wang63307c32008-05-05 19:05:59 +000013006 case X86::ATOMUMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013007 case X86::ATOMUMAX32:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013008 case X86::ATOMMIN16:
13009 case X86::ATOMMAX16:
13010 case X86::ATOMUMIN16:
13011 case X86::ATOMUMAX16:
13012 case X86::ATOMMIN64:
13013 case X86::ATOMMAX64:
13014 case X86::ATOMUMIN64:
13015 case X86::ATOMUMAX64: {
13016 unsigned Opc;
13017 switch (MI->getOpcode()) {
13018 default: llvm_unreachable("illegal opcode!");
13019 case X86::ATOMMIN32: Opc = X86::CMOVL32rr; break;
13020 case X86::ATOMMAX32: Opc = X86::CMOVG32rr; break;
13021 case X86::ATOMUMIN32: Opc = X86::CMOVB32rr; break;
13022 case X86::ATOMUMAX32: Opc = X86::CMOVA32rr; break;
13023 case X86::ATOMMIN16: Opc = X86::CMOVL16rr; break;
13024 case X86::ATOMMAX16: Opc = X86::CMOVG16rr; break;
13025 case X86::ATOMUMIN16: Opc = X86::CMOVB16rr; break;
13026 case X86::ATOMUMAX16: Opc = X86::CMOVA16rr; break;
13027 case X86::ATOMMIN64: Opc = X86::CMOVL64rr; break;
13028 case X86::ATOMMAX64: Opc = X86::CMOVG64rr; break;
13029 case X86::ATOMUMIN64: Opc = X86::CMOVB64rr; break;
13030 case X86::ATOMUMAX64: Opc = X86::CMOVA64rr; break;
13031 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
13032 }
13033 return EmitAtomicMinMaxWithCustomInserter(MI, BB, Opc);
13034 }
13035
13036 case X86::ATOMAND32:
13037 case X86::ATOMOR32:
13038 case X86::ATOMXOR32:
13039 case X86::ATOMNAND32: {
13040 bool Invert = false;
13041 unsigned RegOpc, ImmOpc;
13042 switch (MI->getOpcode()) {
13043 default: llvm_unreachable("illegal opcode!");
13044 case X86::ATOMAND32:
13045 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; break;
13046 case X86::ATOMOR32:
13047 RegOpc = X86::OR32rr; ImmOpc = X86::OR32ri; break;
13048 case X86::ATOMXOR32:
13049 RegOpc = X86::XOR32rr; ImmOpc = X86::XOR32ri; break;
13050 case X86::ATOMNAND32:
13051 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; Invert = true; break;
13052 }
13053 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13054 X86::MOV32rm, X86::LCMPXCHG32,
13055 X86::NOT32r, X86::EAX,
13056 &X86::GR32RegClass, Invert);
13057 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013058
13059 case X86::ATOMAND16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013060 case X86::ATOMOR16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013061 case X86::ATOMXOR16:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013062 case X86::ATOMNAND16: {
13063 bool Invert = false;
13064 unsigned RegOpc, ImmOpc;
13065 switch (MI->getOpcode()) {
13066 default: llvm_unreachable("illegal opcode!");
13067 case X86::ATOMAND16:
13068 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; break;
13069 case X86::ATOMOR16:
13070 RegOpc = X86::OR16rr; ImmOpc = X86::OR16ri; break;
13071 case X86::ATOMXOR16:
13072 RegOpc = X86::XOR16rr; ImmOpc = X86::XOR16ri; break;
13073 case X86::ATOMNAND16:
13074 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; Invert = true; break;
13075 }
13076 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13077 X86::MOV16rm, X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013078 X86::NOT16r, X86::AX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013079 &X86::GR16RegClass, Invert);
13080 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013081
13082 case X86::ATOMAND8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013083 case X86::ATOMOR8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013084 case X86::ATOMXOR8:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013085 case X86::ATOMNAND8: {
13086 bool Invert = false;
13087 unsigned RegOpc, ImmOpc;
13088 switch (MI->getOpcode()) {
13089 default: llvm_unreachable("illegal opcode!");
13090 case X86::ATOMAND8:
13091 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; break;
13092 case X86::ATOMOR8:
13093 RegOpc = X86::OR8rr; ImmOpc = X86::OR8ri; break;
13094 case X86::ATOMXOR8:
13095 RegOpc = X86::XOR8rr; ImmOpc = X86::XOR8ri; break;
13096 case X86::ATOMNAND8:
13097 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; Invert = true; break;
13098 }
13099 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13100 X86::MOV8rm, X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013101 X86::NOT8r, X86::AL,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013102 &X86::GR8RegClass, Invert);
13103 }
13104
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013105 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013106 case X86::ATOMAND64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013107 case X86::ATOMOR64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013108 case X86::ATOMXOR64:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013109 case X86::ATOMNAND64: {
13110 bool Invert = false;
13111 unsigned RegOpc, ImmOpc;
13112 switch (MI->getOpcode()) {
13113 default: llvm_unreachable("illegal opcode!");
13114 case X86::ATOMAND64:
13115 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; break;
13116 case X86::ATOMOR64:
13117 RegOpc = X86::OR64rr; ImmOpc = X86::OR64ri32; break;
13118 case X86::ATOMXOR64:
13119 RegOpc = X86::XOR64rr; ImmOpc = X86::XOR64ri32; break;
13120 case X86::ATOMNAND64:
13121 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; Invert = true; break;
13122 }
13123 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13124 X86::MOV64rm, X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013125 X86::NOT64r, X86::RAX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013126 &X86::GR64RegClass, Invert);
13127 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013128
13129 // This group does 64-bit operations on a 32-bit host.
13130 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013131 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013132 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013133 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013134 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013135 case X86::ATOMSUB6432:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013136 case X86::ATOMSWAP6432: {
13137 bool Invert = false;
13138 unsigned RegOpcL, RegOpcH, ImmOpcL, ImmOpcH;
13139 switch (MI->getOpcode()) {
13140 default: llvm_unreachable("illegal opcode!");
13141 case X86::ATOMAND6432:
13142 RegOpcL = RegOpcH = X86::AND32rr;
13143 ImmOpcL = ImmOpcH = X86::AND32ri;
13144 break;
13145 case X86::ATOMOR6432:
13146 RegOpcL = RegOpcH = X86::OR32rr;
13147 ImmOpcL = ImmOpcH = X86::OR32ri;
13148 break;
13149 case X86::ATOMXOR6432:
13150 RegOpcL = RegOpcH = X86::XOR32rr;
13151 ImmOpcL = ImmOpcH = X86::XOR32ri;
13152 break;
13153 case X86::ATOMNAND6432:
13154 RegOpcL = RegOpcH = X86::AND32rr;
13155 ImmOpcL = ImmOpcH = X86::AND32ri;
13156 Invert = true;
13157 break;
13158 case X86::ATOMADD6432:
13159 RegOpcL = X86::ADD32rr; RegOpcH = X86::ADC32rr;
13160 ImmOpcL = X86::ADD32ri; ImmOpcH = X86::ADC32ri;
13161 break;
13162 case X86::ATOMSUB6432:
13163 RegOpcL = X86::SUB32rr; RegOpcH = X86::SBB32rr;
13164 ImmOpcL = X86::SUB32ri; ImmOpcH = X86::SBB32ri;
13165 break;
13166 case X86::ATOMSWAP6432:
13167 RegOpcL = RegOpcH = X86::MOV32rr;
13168 ImmOpcL = ImmOpcH = X86::MOV32ri;
13169 break;
13170 }
13171 return EmitAtomicBit6432WithCustomInserter(MI, BB, RegOpcL, RegOpcH,
13172 ImmOpcL, ImmOpcH, Invert);
13173 }
13174
Dan Gohmand6708ea2009-08-15 01:38:56 +000013175 case X86::VASTART_SAVE_XMM_REGS:
13176 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013177
13178 case X86::VAARG_64:
13179 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013180 }
13181}
13182
13183//===----------------------------------------------------------------------===//
13184// X86 Optimization Hooks
13185//===----------------------------------------------------------------------===//
13186
Dan Gohman475871a2008-07-27 21:46:04 +000013187void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013188 APInt &KnownZero,
13189 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013190 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013191 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013192 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013193 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013194 assert((Opc >= ISD::BUILTIN_OP_END ||
13195 Opc == ISD::INTRINSIC_WO_CHAIN ||
13196 Opc == ISD::INTRINSIC_W_CHAIN ||
13197 Opc == ISD::INTRINSIC_VOID) &&
13198 "Should use MaskedValueIsZero if you don't know whether Op"
13199 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013200
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013201 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013202 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013203 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013204 case X86ISD::ADD:
13205 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013206 case X86ISD::ADC:
13207 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013208 case X86ISD::SMUL:
13209 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013210 case X86ISD::INC:
13211 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013212 case X86ISD::OR:
13213 case X86ISD::XOR:
13214 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013215 // These nodes' second result is a boolean.
13216 if (Op.getResNo() == 0)
13217 break;
13218 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013219 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013220 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013221 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013222 case ISD::INTRINSIC_WO_CHAIN: {
13223 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13224 unsigned NumLoBits = 0;
13225 switch (IntId) {
13226 default: break;
13227 case Intrinsic::x86_sse_movmsk_ps:
13228 case Intrinsic::x86_avx_movmsk_ps_256:
13229 case Intrinsic::x86_sse2_movmsk_pd:
13230 case Intrinsic::x86_avx_movmsk_pd_256:
13231 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013232 case Intrinsic::x86_sse2_pmovmskb_128:
13233 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013234 // High bits of movmskp{s|d}, pmovmskb are known zero.
13235 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013236 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013237 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13238 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13239 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13240 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13241 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13242 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013243 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013244 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013245 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013246 break;
13247 }
13248 }
13249 break;
13250 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013251 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013252}
Chris Lattner259e97c2006-01-31 19:43:35 +000013253
Owen Andersonbc146b02010-09-21 20:42:50 +000013254unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13255 unsigned Depth) const {
13256 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13257 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13258 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013259
Owen Andersonbc146b02010-09-21 20:42:50 +000013260 // Fallback case.
13261 return 1;
13262}
13263
Evan Cheng206ee9d2006-07-07 08:33:52 +000013264/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013265/// node is a GlobalAddress + offset.
13266bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013267 const GlobalValue* &GA,
13268 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013269 if (N->getOpcode() == X86ISD::Wrapper) {
13270 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013271 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013272 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013273 return true;
13274 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013275 }
Evan Chengad4196b2008-05-12 19:56:52 +000013276 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013277}
13278
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013279/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13280/// same as extracting the high 128-bit part of 256-bit vector and then
13281/// inserting the result into the low part of a new 256-bit vector
13282static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13283 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013284 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013285
13286 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013287 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013288 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13289 SVOp->getMaskElt(j) >= 0)
13290 return false;
13291
13292 return true;
13293}
13294
13295/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13296/// same as extracting the low 128-bit part of 256-bit vector and then
13297/// inserting the result into the high part of a new 256-bit vector
13298static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13299 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013300 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013301
13302 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013303 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013304 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13305 SVOp->getMaskElt(j) >= 0)
13306 return false;
13307
13308 return true;
13309}
13310
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013311/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13312static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013313 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013314 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013315 DebugLoc dl = N->getDebugLoc();
13316 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13317 SDValue V1 = SVOp->getOperand(0);
13318 SDValue V2 = SVOp->getOperand(1);
13319 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013320 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013321
13322 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13323 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13324 //
13325 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013326 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013327 // V UNDEF BUILD_VECTOR UNDEF
13328 // \ / \ /
13329 // CONCAT_VECTOR CONCAT_VECTOR
13330 // \ /
13331 // \ /
13332 // RESULT: V + zero extended
13333 //
13334 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13335 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13336 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13337 return SDValue();
13338
13339 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13340 return SDValue();
13341
13342 // To match the shuffle mask, the first half of the mask should
13343 // be exactly the first vector, and all the rest a splat with the
13344 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013345 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013346 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13347 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13348 return SDValue();
13349
Chad Rosier3d1161e2012-01-03 21:05:52 +000013350 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13351 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013352 if (Ld->hasNUsesOfValue(1, 0)) {
13353 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13354 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13355 SDValue ResNode =
13356 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13357 Ld->getMemoryVT(),
13358 Ld->getPointerInfo(),
13359 Ld->getAlignment(),
13360 false/*isVolatile*/, true/*ReadMem*/,
13361 false/*WriteMem*/);
13362 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13363 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013364 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013365
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013366 // Emit a zeroed vector and insert the desired subvector on its
13367 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013368 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013369 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013370 return DCI.CombineTo(N, InsV);
13371 }
13372
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013373 //===--------------------------------------------------------------------===//
13374 // Combine some shuffles into subvector extracts and inserts:
13375 //
13376
13377 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13378 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013379 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13380 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013381 return DCI.CombineTo(N, InsV);
13382 }
13383
13384 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13385 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013386 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13387 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013388 return DCI.CombineTo(N, InsV);
13389 }
13390
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013391 return SDValue();
13392}
13393
13394/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013395static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013396 TargetLowering::DAGCombinerInfo &DCI,
13397 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013398 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013399 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013400
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013401 // Don't create instructions with illegal types after legalize types has run.
13402 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13403 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13404 return SDValue();
13405
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013406 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013407 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013408 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013409 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013410
13411 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013412 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013413 return SDValue();
13414
13415 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13416 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13417 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013418 SmallVector<SDValue, 16> Elts;
13419 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013420 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013421
Nate Begemanfdea31a2010-03-24 20:49:50 +000013422 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013423}
Evan Chengd880b972008-05-09 21:53:03 +000013424
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013425
Craig Topperc16f8512012-04-25 06:39:39 +000013426/// DCI, PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013427/// a sequence of vector shuffle operations.
13428/// It is possible when we truncate 256-bit vector to 128-bit vector
13429
Chad Rosiera20e1e72012-08-01 18:39:17 +000013430SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013431 DAGCombinerInfo &DCI) const {
13432 if (!DCI.isBeforeLegalizeOps())
13433 return SDValue();
13434
Craig Topper3ef43cf2012-04-24 06:36:35 +000013435 if (!Subtarget->hasAVX())
13436 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013437
13438 EVT VT = N->getValueType(0);
13439 SDValue Op = N->getOperand(0);
13440 EVT OpVT = Op.getValueType();
13441 DebugLoc dl = N->getDebugLoc();
13442
13443 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13444
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013445 if (Subtarget->hasAVX2()) {
13446 // AVX2: v4i64 -> v4i32
13447
13448 // VPERMD
13449 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13450
13451 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13452 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13453 ShufMask);
13454
Craig Topperd63fa652012-04-22 18:51:37 +000013455 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13456 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013457 }
13458
13459 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013460 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013461 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013462
13463 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013464 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013465
13466 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13467 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13468
13469 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013470 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013471
Craig Toppercacafd42012-08-14 08:18:43 +000013472 SDValue Undef = DAG.getUNDEF(VT);
13473 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13474 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013475
13476 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013477 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013478
Elena Demikhovsky73252572012-02-01 10:33:05 +000013479 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013480 }
Craig Topperd63fa652012-04-22 18:51:37 +000013481
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013482 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13483
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013484 if (Subtarget->hasAVX2()) {
13485 // AVX2: v8i32 -> v8i16
13486
13487 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013488
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013489 // PSHUFB
13490 SmallVector<SDValue,32> pshufbMask;
13491 for (unsigned i = 0; i < 2; ++i) {
13492 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13493 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13494 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13495 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13496 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13497 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13498 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13499 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13500 for (unsigned j = 0; j < 8; ++j)
13501 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13502 }
Craig Topperd63fa652012-04-22 18:51:37 +000013503 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13504 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013505 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13506
13507 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13508
13509 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013510 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013511 &ShufMask[0]);
13512
Craig Topperd63fa652012-04-22 18:51:37 +000013513 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13514 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013515
13516 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13517 }
13518
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013519 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013520 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013521
13522 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013523 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013524
13525 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13526 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13527
13528 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013529 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13530 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013531
Craig Toppercacafd42012-08-14 08:18:43 +000013532 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13533 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13534 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013535
13536 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13537 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13538
13539 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013540 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013541
Elena Demikhovsky73252572012-02-01 10:33:05 +000013542 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013543 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013544 }
13545
13546 return SDValue();
13547}
13548
Craig Topper89f4e662012-03-20 07:17:59 +000013549/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13550/// specific shuffle of a load can be folded into a single element load.
13551/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13552/// shuffles have been customed lowered so we need to handle those here.
13553static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13554 TargetLowering::DAGCombinerInfo &DCI) {
13555 if (DCI.isBeforeLegalizeOps())
13556 return SDValue();
13557
13558 SDValue InVec = N->getOperand(0);
13559 SDValue EltNo = N->getOperand(1);
13560
13561 if (!isa<ConstantSDNode>(EltNo))
13562 return SDValue();
13563
13564 EVT VT = InVec.getValueType();
13565
13566 bool HasShuffleIntoBitcast = false;
13567 if (InVec.getOpcode() == ISD::BITCAST) {
13568 // Don't duplicate a load with other uses.
13569 if (!InVec.hasOneUse())
13570 return SDValue();
13571 EVT BCVT = InVec.getOperand(0).getValueType();
13572 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13573 return SDValue();
13574 InVec = InVec.getOperand(0);
13575 HasShuffleIntoBitcast = true;
13576 }
13577
13578 if (!isTargetShuffle(InVec.getOpcode()))
13579 return SDValue();
13580
13581 // Don't duplicate a load with other uses.
13582 if (!InVec.hasOneUse())
13583 return SDValue();
13584
13585 SmallVector<int, 16> ShuffleMask;
13586 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013587 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13588 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013589 return SDValue();
13590
13591 // Select the input vector, guarding against out of range extract vector.
13592 unsigned NumElems = VT.getVectorNumElements();
13593 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13594 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13595 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13596 : InVec.getOperand(1);
13597
13598 // If inputs to shuffle are the same for both ops, then allow 2 uses
13599 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13600
13601 if (LdNode.getOpcode() == ISD::BITCAST) {
13602 // Don't duplicate a load with other uses.
13603 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13604 return SDValue();
13605
13606 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13607 LdNode = LdNode.getOperand(0);
13608 }
13609
13610 if (!ISD::isNormalLoad(LdNode.getNode()))
13611 return SDValue();
13612
13613 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13614
13615 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13616 return SDValue();
13617
13618 if (HasShuffleIntoBitcast) {
13619 // If there's a bitcast before the shuffle, check if the load type and
13620 // alignment is valid.
13621 unsigned Align = LN0->getAlignment();
13622 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13623 unsigned NewAlign = TLI.getTargetData()->
13624 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13625
13626 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13627 return SDValue();
13628 }
13629
13630 // All checks match so transform back to vector_shuffle so that DAG combiner
13631 // can finish the job
13632 DebugLoc dl = N->getDebugLoc();
13633
13634 // Create shuffle node taking into account the case that its a unary shuffle
13635 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13636 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13637 InVec.getOperand(0), Shuffle,
13638 &ShuffleMask[0]);
13639 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13640 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13641 EltNo);
13642}
13643
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013644/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13645/// generation and convert it from being a bunch of shuffles and extracts
13646/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013647static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013648 TargetLowering::DAGCombinerInfo &DCI) {
13649 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13650 if (NewOp.getNode())
13651 return NewOp;
13652
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013653 SDValue InputVector = N->getOperand(0);
13654
13655 // Only operate on vectors of 4 elements, where the alternative shuffling
13656 // gets to be more expensive.
13657 if (InputVector.getValueType() != MVT::v4i32)
13658 return SDValue();
13659
13660 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13661 // single use which is a sign-extend or zero-extend, and all elements are
13662 // used.
13663 SmallVector<SDNode *, 4> Uses;
13664 unsigned ExtractedElements = 0;
13665 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13666 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13667 if (UI.getUse().getResNo() != InputVector.getResNo())
13668 return SDValue();
13669
13670 SDNode *Extract = *UI;
13671 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13672 return SDValue();
13673
13674 if (Extract->getValueType(0) != MVT::i32)
13675 return SDValue();
13676 if (!Extract->hasOneUse())
13677 return SDValue();
13678 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13679 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13680 return SDValue();
13681 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13682 return SDValue();
13683
13684 // Record which element was extracted.
13685 ExtractedElements |=
13686 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13687
13688 Uses.push_back(Extract);
13689 }
13690
13691 // If not all the elements were used, this may not be worthwhile.
13692 if (ExtractedElements != 15)
13693 return SDValue();
13694
13695 // Ok, we've now decided to do the transformation.
13696 DebugLoc dl = InputVector.getDebugLoc();
13697
13698 // Store the value to a temporary stack slot.
13699 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013700 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13701 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013702
13703 // Replace each use (extract) with a load of the appropriate element.
13704 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13705 UE = Uses.end(); UI != UE; ++UI) {
13706 SDNode *Extract = *UI;
13707
Nadav Rotem86694292011-05-17 08:31:57 +000013708 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013709 SDValue Idx = Extract->getOperand(1);
13710 unsigned EltSize =
13711 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13712 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013713 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013714 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13715
Nadav Rotem86694292011-05-17 08:31:57 +000013716 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013717 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013718
13719 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013720 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013721 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013722 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013723
13724 // Replace the exact with the load.
13725 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13726 }
13727
13728 // The replacement was made in place; don't return anything.
13729 return SDValue();
13730}
13731
Duncan Sands6bcd2192011-09-17 16:49:39 +000013732/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13733/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013734static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013735 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013736 const X86Subtarget *Subtarget) {
13737 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013738 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013739 // Get the LHS/RHS of the select.
13740 SDValue LHS = N->getOperand(1);
13741 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013742 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013743
Dan Gohman670e5392009-09-21 18:03:22 +000013744 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013745 // instructions match the semantics of the common C idiom x<y?x:y but not
13746 // x<=y?x:y, because of how they handle negative zero (which can be
13747 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013748 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13749 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013750 (Subtarget->hasSSE2() ||
13751 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013752 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013753
Chris Lattner47b4ce82009-03-11 05:48:52 +000013754 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013755 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013756 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13757 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013758 switch (CC) {
13759 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013760 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013761 // Converting this to a min would handle NaNs incorrectly, and swapping
13762 // the operands would cause it to handle comparisons between positive
13763 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013764 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013765 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013766 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13767 break;
13768 std::swap(LHS, RHS);
13769 }
Dan Gohman670e5392009-09-21 18:03:22 +000013770 Opcode = X86ISD::FMIN;
13771 break;
13772 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013773 // Converting this to a min would handle comparisons between positive
13774 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013775 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013776 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13777 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013778 Opcode = X86ISD::FMIN;
13779 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013780 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013781 // Converting this to a min would handle both negative zeros and NaNs
13782 // incorrectly, but we can swap the operands to fix both.
13783 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013784 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013785 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013786 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013787 Opcode = X86ISD::FMIN;
13788 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013789
Dan Gohman670e5392009-09-21 18:03:22 +000013790 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013791 // Converting this to a max would handle comparisons between positive
13792 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013793 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013794 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013795 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013796 Opcode = X86ISD::FMAX;
13797 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013798 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013799 // Converting this to a max would handle NaNs incorrectly, and swapping
13800 // the operands would cause it to handle comparisons between positive
13801 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013802 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013803 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013804 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13805 break;
13806 std::swap(LHS, RHS);
13807 }
Dan Gohman670e5392009-09-21 18:03:22 +000013808 Opcode = X86ISD::FMAX;
13809 break;
13810 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013811 // Converting this to a max would handle both negative zeros and NaNs
13812 // incorrectly, but we can swap the operands to fix both.
13813 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013814 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013815 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013816 case ISD::SETGE:
13817 Opcode = X86ISD::FMAX;
13818 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013819 }
Dan Gohman670e5392009-09-21 18:03:22 +000013820 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013821 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13822 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013823 switch (CC) {
13824 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013825 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013826 // Converting this to a min would handle comparisons between positive
13827 // and negative zero incorrectly, and swapping the operands would
13828 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013829 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013830 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013831 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013832 break;
13833 std::swap(LHS, RHS);
13834 }
Dan Gohman670e5392009-09-21 18:03:22 +000013835 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013836 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013837 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013838 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013839 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013840 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13841 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013842 Opcode = X86ISD::FMIN;
13843 break;
13844 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013845 // Converting this to a min would handle both negative zeros and NaNs
13846 // incorrectly, but we can swap the operands to fix both.
13847 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013848 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013849 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013850 case ISD::SETGE:
13851 Opcode = X86ISD::FMIN;
13852 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013853
Dan Gohman670e5392009-09-21 18:03:22 +000013854 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013855 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013856 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013857 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013858 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013859 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013860 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013861 // Converting this to a max would handle comparisons between positive
13862 // and negative zero incorrectly, and swapping the operands would
13863 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013864 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013865 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013866 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013867 break;
13868 std::swap(LHS, RHS);
13869 }
Dan Gohman670e5392009-09-21 18:03:22 +000013870 Opcode = X86ISD::FMAX;
13871 break;
13872 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013873 // Converting this to a max would handle both negative zeros and NaNs
13874 // incorrectly, but we can swap the operands to fix both.
13875 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013876 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013877 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013878 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013879 Opcode = X86ISD::FMAX;
13880 break;
13881 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013882 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013883
Chris Lattner47b4ce82009-03-11 05:48:52 +000013884 if (Opcode)
13885 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013886 }
Eric Christopherfd179292009-08-27 18:07:15 +000013887
Chris Lattnerd1980a52009-03-12 06:52:53 +000013888 // If this is a select between two integer constants, try to do some
13889 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013890 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13891 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013892 // Don't do this for crazy integer types.
13893 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13894 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013895 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013896 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013897
Chris Lattnercee56e72009-03-13 05:53:31 +000013898 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013899 // Efficiently invertible.
13900 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13901 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13902 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13903 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013904 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013905 }
Eric Christopherfd179292009-08-27 18:07:15 +000013906
Chris Lattnerd1980a52009-03-12 06:52:53 +000013907 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013908 if (FalseC->getAPIntValue() == 0 &&
13909 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013910 if (NeedsCondInvert) // Invert the condition if needed.
13911 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13912 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013913
Chris Lattnerd1980a52009-03-12 06:52:53 +000013914 // Zero extend the condition if needed.
13915 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013916
Chris Lattnercee56e72009-03-13 05:53:31 +000013917 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013918 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013919 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013920 }
Eric Christopherfd179292009-08-27 18:07:15 +000013921
Chris Lattner97a29a52009-03-13 05:22:11 +000013922 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013923 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013924 if (NeedsCondInvert) // Invert the condition if needed.
13925 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13926 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013927
Chris Lattner97a29a52009-03-13 05:22:11 +000013928 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013929 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13930 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013931 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013932 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013933 }
Eric Christopherfd179292009-08-27 18:07:15 +000013934
Chris Lattnercee56e72009-03-13 05:53:31 +000013935 // Optimize cases that will turn into an LEA instruction. This requires
13936 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013937 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013938 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013939 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013940
Chris Lattnercee56e72009-03-13 05:53:31 +000013941 bool isFastMultiplier = false;
13942 if (Diff < 10) {
13943 switch ((unsigned char)Diff) {
13944 default: break;
13945 case 1: // result = add base, cond
13946 case 2: // result = lea base( , cond*2)
13947 case 3: // result = lea base(cond, cond*2)
13948 case 4: // result = lea base( , cond*4)
13949 case 5: // result = lea base(cond, cond*4)
13950 case 8: // result = lea base( , cond*8)
13951 case 9: // result = lea base(cond, cond*8)
13952 isFastMultiplier = true;
13953 break;
13954 }
13955 }
Eric Christopherfd179292009-08-27 18:07:15 +000013956
Chris Lattnercee56e72009-03-13 05:53:31 +000013957 if (isFastMultiplier) {
13958 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13959 if (NeedsCondInvert) // Invert the condition if needed.
13960 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13961 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013962
Chris Lattnercee56e72009-03-13 05:53:31 +000013963 // Zero extend the condition if needed.
13964 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13965 Cond);
13966 // Scale the condition by the difference.
13967 if (Diff != 1)
13968 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13969 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013970
Chris Lattnercee56e72009-03-13 05:53:31 +000013971 // Add the base if non-zero.
13972 if (FalseC->getAPIntValue() != 0)
13973 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13974 SDValue(FalseC, 0));
13975 return Cond;
13976 }
Eric Christopherfd179292009-08-27 18:07:15 +000013977 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013978 }
13979 }
Eric Christopherfd179292009-08-27 18:07:15 +000013980
Evan Cheng56f582d2012-01-04 01:41:39 +000013981 // Canonicalize max and min:
13982 // (x > y) ? x : y -> (x >= y) ? x : y
13983 // (x < y) ? x : y -> (x <= y) ? x : y
13984 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
13985 // the need for an extra compare
13986 // against zero. e.g.
13987 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
13988 // subl %esi, %edi
13989 // testl %edi, %edi
13990 // movl $0, %eax
13991 // cmovgl %edi, %eax
13992 // =>
13993 // xorl %eax, %eax
13994 // subl %esi, $edi
13995 // cmovsl %eax, %edi
13996 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
13997 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13998 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
13999 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14000 switch (CC) {
14001 default: break;
14002 case ISD::SETLT:
14003 case ISD::SETGT: {
14004 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14005 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14006 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14007 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14008 }
14009 }
14010 }
14011
Nadav Rotemcc616562012-01-15 19:27:55 +000014012 // If we know that this node is legal then we know that it is going to be
14013 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14014 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14015 // to simplify previous instructions.
14016 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14017 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014018 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014019 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014020
14021 // Don't optimize vector selects that map to mask-registers.
14022 if (BitWidth == 1)
14023 return SDValue();
14024
Nadav Rotemcc616562012-01-15 19:27:55 +000014025 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14026 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14027
14028 APInt KnownZero, KnownOne;
14029 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14030 DCI.isBeforeLegalizeOps());
14031 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14032 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14033 DCI.CommitTargetLoweringOpt(TLO);
14034 }
14035
Dan Gohman475871a2008-07-27 21:46:04 +000014036 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014037}
14038
Michael Liao2a33cec2012-08-10 19:58:13 +000014039// Check whether a boolean test is testing a boolean value generated by
14040// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14041// code.
14042//
14043// Simplify the following patterns:
14044// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14045// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14046// to (Op EFLAGS Cond)
14047//
14048// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14049// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14050// to (Op EFLAGS !Cond)
14051//
14052// where Op could be BRCOND or CMOV.
14053//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014054static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014055 // Quit if not CMP and SUB with its value result used.
14056 if (Cmp.getOpcode() != X86ISD::CMP &&
14057 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14058 return SDValue();
14059
14060 // Quit if not used as a boolean value.
14061 if (CC != X86::COND_E && CC != X86::COND_NE)
14062 return SDValue();
14063
14064 // Check CMP operands. One of them should be 0 or 1 and the other should be
14065 // an SetCC or extended from it.
14066 SDValue Op1 = Cmp.getOperand(0);
14067 SDValue Op2 = Cmp.getOperand(1);
14068
14069 SDValue SetCC;
14070 const ConstantSDNode* C = 0;
14071 bool needOppositeCond = (CC == X86::COND_E);
14072
14073 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14074 SetCC = Op2;
14075 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14076 SetCC = Op1;
14077 else // Quit if all operands are not constants.
14078 return SDValue();
14079
14080 if (C->getZExtValue() == 1)
14081 needOppositeCond = !needOppositeCond;
14082 else if (C->getZExtValue() != 0)
14083 // Quit if the constant is neither 0 or 1.
14084 return SDValue();
14085
14086 // Skip 'zext' node.
14087 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14088 SetCC = SetCC.getOperand(0);
14089
14090 // Quit if not SETCC.
14091 // FIXME: So far we only handle the boolean value generated from SETCC. If
14092 // there is other ways to generate boolean values, we need handle them here
14093 // as well.
14094 if (SetCC.getOpcode() != X86ISD::SETCC)
14095 return SDValue();
14096
14097 // Set the condition code or opposite one if necessary.
14098 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14099 if (needOppositeCond)
14100 CC = X86::GetOppositeBranchCondition(CC);
14101
14102 return SetCC.getOperand(1);
14103}
14104
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014105/// checkFlaggedOrCombine - DAG combination on X86ISD::OR, i.e. with EFLAGS
14106/// updated. If only flag result is used and the result is evaluated from a
14107/// series of element extraction, try to combine it into a PTEST.
14108static SDValue checkFlaggedOrCombine(SDValue Or, X86::CondCode &CC,
14109 SelectionDAG &DAG,
14110 const X86Subtarget *Subtarget) {
14111 SDNode *N = Or.getNode();
14112 DebugLoc DL = N->getDebugLoc();
14113
14114 // Only SSE4.1 and beyond supports PTEST or like.
14115 if (!Subtarget->hasSSE41())
14116 return SDValue();
14117
14118 if (N->getOpcode() != X86ISD::OR)
14119 return SDValue();
14120
14121 // Quit if the value result of OR is used.
14122 if (N->hasAnyUseOfValue(0))
14123 return SDValue();
14124
14125 // Quit if not used as a boolean value.
14126 if (CC != X86::COND_E && CC != X86::COND_NE)
14127 return SDValue();
14128
14129 SmallVector<SDValue, 8> Opnds;
14130 SDValue VecIn;
14131 EVT VT = MVT::Other;
14132 unsigned Mask = 0;
14133
14134 // Recognize a special case where a vector is casted into wide integer to
14135 // test all 0s.
14136 Opnds.push_back(N->getOperand(0));
14137 Opnds.push_back(N->getOperand(1));
14138
14139 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
14140 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
14141 // BFS traverse all OR'd operands.
14142 if (I->getOpcode() == ISD::OR) {
14143 Opnds.push_back(I->getOperand(0));
14144 Opnds.push_back(I->getOperand(1));
14145 // Re-evaluate the number of nodes to be traversed.
Michael Liao95c22a32012-08-28 23:42:17 +000014146 e += 2; // 2 more nodes (LHS and RHS) are pushed.
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014147 continue;
14148 }
14149
14150 // Quit if a non-EXTRACT_VECTOR_ELT
14151 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14152 return SDValue();
14153
14154 // Quit if without a constant index.
14155 SDValue Idx = I->getOperand(1);
14156 if (!isa<ConstantSDNode>(Idx))
14157 return SDValue();
14158
14159 // Check if all elements are extracted from the same vector.
14160 SDValue ExtractedFromVec = I->getOperand(0);
14161 if (VecIn.getNode() == 0) {
14162 VT = ExtractedFromVec.getValueType();
14163 // FIXME: only 128-bit vector is supported so far.
14164 if (!VT.is128BitVector())
14165 return SDValue();
14166 VecIn = ExtractedFromVec;
14167 } else if (VecIn != ExtractedFromVec)
14168 return SDValue();
14169
14170 // Record the constant index.
14171 Mask |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
14172 }
14173
14174 assert(VT.is128BitVector() && "Only 128-bit vector PTEST is supported so far.");
14175
14176 // Quit if not all elements are used.
14177 if (Mask != (1U << VT.getVectorNumElements()) - 1U)
14178 return SDValue();
14179
14180 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, VecIn, VecIn);
14181}
14182
Chris Lattnerd1980a52009-03-12 06:52:53 +000014183/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14184static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014185 TargetLowering::DAGCombinerInfo &DCI,
14186 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014187 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014188
Chris Lattnerd1980a52009-03-12 06:52:53 +000014189 // If the flag operand isn't dead, don't touch this CMOV.
14190 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14191 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014192
Evan Chengb5a55d92011-05-24 01:48:22 +000014193 SDValue FalseOp = N->getOperand(0);
14194 SDValue TrueOp = N->getOperand(1);
14195 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14196 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014197
Evan Chengb5a55d92011-05-24 01:48:22 +000014198 if (CC == X86::COND_E || CC == X86::COND_NE) {
14199 switch (Cond.getOpcode()) {
14200 default: break;
14201 case X86ISD::BSR:
14202 case X86ISD::BSF:
14203 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14204 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14205 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14206 }
14207 }
14208
Michael Liao2a33cec2012-08-10 19:58:13 +000014209 SDValue Flags;
14210
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014211 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014212 if (Flags.getNode() &&
14213 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014214 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014215 SDValue Ops[] = { FalseOp, TrueOp,
14216 DAG.getConstant(CC, MVT::i8), Flags };
14217 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14218 Ops, array_lengthof(Ops));
14219 }
14220
14221 Flags = checkFlaggedOrCombine(Cond, CC, DAG, Subtarget);
14222 if (Flags.getNode()) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014223 SDValue Ops[] = { FalseOp, TrueOp,
14224 DAG.getConstant(CC, MVT::i8), Flags };
14225 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14226 Ops, array_lengthof(Ops));
14227 }
14228
Chris Lattnerd1980a52009-03-12 06:52:53 +000014229 // If this is a select between two integer constants, try to do some
14230 // optimizations. Note that the operands are ordered the opposite of SELECT
14231 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014232 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14233 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014234 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14235 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014236 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14237 CC = X86::GetOppositeBranchCondition(CC);
14238 std::swap(TrueC, FalseC);
14239 }
Eric Christopherfd179292009-08-27 18:07:15 +000014240
Chris Lattnerd1980a52009-03-12 06:52:53 +000014241 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014242 // This is efficient for any integer data type (including i8/i16) and
14243 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014244 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014245 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14246 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014247
Chris Lattnerd1980a52009-03-12 06:52:53 +000014248 // Zero extend the condition if needed.
14249 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014250
Chris Lattnerd1980a52009-03-12 06:52:53 +000014251 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14252 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014253 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014254 if (N->getNumValues() == 2) // Dead flag value?
14255 return DCI.CombineTo(N, Cond, SDValue());
14256 return Cond;
14257 }
Eric Christopherfd179292009-08-27 18:07:15 +000014258
Chris Lattnercee56e72009-03-13 05:53:31 +000014259 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14260 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014261 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014262 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14263 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014264
Chris Lattner97a29a52009-03-13 05:22:11 +000014265 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014266 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14267 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014268 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14269 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014270
Chris Lattner97a29a52009-03-13 05:22:11 +000014271 if (N->getNumValues() == 2) // Dead flag value?
14272 return DCI.CombineTo(N, Cond, SDValue());
14273 return Cond;
14274 }
Eric Christopherfd179292009-08-27 18:07:15 +000014275
Chris Lattnercee56e72009-03-13 05:53:31 +000014276 // Optimize cases that will turn into an LEA instruction. This requires
14277 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014278 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014279 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014280 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014281
Chris Lattnercee56e72009-03-13 05:53:31 +000014282 bool isFastMultiplier = false;
14283 if (Diff < 10) {
14284 switch ((unsigned char)Diff) {
14285 default: break;
14286 case 1: // result = add base, cond
14287 case 2: // result = lea base( , cond*2)
14288 case 3: // result = lea base(cond, cond*2)
14289 case 4: // result = lea base( , cond*4)
14290 case 5: // result = lea base(cond, cond*4)
14291 case 8: // result = lea base( , cond*8)
14292 case 9: // result = lea base(cond, cond*8)
14293 isFastMultiplier = true;
14294 break;
14295 }
14296 }
Eric Christopherfd179292009-08-27 18:07:15 +000014297
Chris Lattnercee56e72009-03-13 05:53:31 +000014298 if (isFastMultiplier) {
14299 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014300 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14301 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014302 // Zero extend the condition if needed.
14303 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14304 Cond);
14305 // Scale the condition by the difference.
14306 if (Diff != 1)
14307 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14308 DAG.getConstant(Diff, Cond.getValueType()));
14309
14310 // Add the base if non-zero.
14311 if (FalseC->getAPIntValue() != 0)
14312 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14313 SDValue(FalseC, 0));
14314 if (N->getNumValues() == 2) // Dead flag value?
14315 return DCI.CombineTo(N, Cond, SDValue());
14316 return Cond;
14317 }
Eric Christopherfd179292009-08-27 18:07:15 +000014318 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014319 }
14320 }
14321 return SDValue();
14322}
14323
14324
Evan Cheng0b0cd912009-03-28 05:57:29 +000014325/// PerformMulCombine - Optimize a single multiply with constant into two
14326/// in order to implement it with two cheaper instructions, e.g.
14327/// LEA + SHL, LEA + LEA.
14328static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14329 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014330 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14331 return SDValue();
14332
Owen Andersone50ed302009-08-10 22:56:29 +000014333 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014334 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014335 return SDValue();
14336
14337 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14338 if (!C)
14339 return SDValue();
14340 uint64_t MulAmt = C->getZExtValue();
14341 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14342 return SDValue();
14343
14344 uint64_t MulAmt1 = 0;
14345 uint64_t MulAmt2 = 0;
14346 if ((MulAmt % 9) == 0) {
14347 MulAmt1 = 9;
14348 MulAmt2 = MulAmt / 9;
14349 } else if ((MulAmt % 5) == 0) {
14350 MulAmt1 = 5;
14351 MulAmt2 = MulAmt / 5;
14352 } else if ((MulAmt % 3) == 0) {
14353 MulAmt1 = 3;
14354 MulAmt2 = MulAmt / 3;
14355 }
14356 if (MulAmt2 &&
14357 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14358 DebugLoc DL = N->getDebugLoc();
14359
14360 if (isPowerOf2_64(MulAmt2) &&
14361 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14362 // If second multiplifer is pow2, issue it first. We want the multiply by
14363 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14364 // is an add.
14365 std::swap(MulAmt1, MulAmt2);
14366
14367 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014368 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014369 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014370 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014371 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014372 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014373 DAG.getConstant(MulAmt1, VT));
14374
Eric Christopherfd179292009-08-27 18:07:15 +000014375 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014376 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014377 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014378 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014379 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014380 DAG.getConstant(MulAmt2, VT));
14381
14382 // Do not add new nodes to DAG combiner worklist.
14383 DCI.CombineTo(N, NewMul, false);
14384 }
14385 return SDValue();
14386}
14387
Evan Chengad9c0a32009-12-15 00:53:42 +000014388static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14389 SDValue N0 = N->getOperand(0);
14390 SDValue N1 = N->getOperand(1);
14391 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14392 EVT VT = N0.getValueType();
14393
14394 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14395 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014396 if (VT.isInteger() && !VT.isVector() &&
14397 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014398 N0.getOperand(1).getOpcode() == ISD::Constant) {
14399 SDValue N00 = N0.getOperand(0);
14400 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14401 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14402 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14403 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14404 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14405 APInt ShAmt = N1C->getAPIntValue();
14406 Mask = Mask.shl(ShAmt);
14407 if (Mask != 0)
14408 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14409 N00, DAG.getConstant(Mask, VT));
14410 }
14411 }
14412
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014413
14414 // Hardware support for vector shifts is sparse which makes us scalarize the
14415 // vector operations in many cases. Also, on sandybridge ADD is faster than
14416 // shl.
14417 // (shl V, 1) -> add V,V
14418 if (isSplatVector(N1.getNode())) {
14419 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14420 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14421 // We shift all of the values by one. In many cases we do not have
14422 // hardware support for this operation. This is better expressed as an ADD
14423 // of two values.
14424 if (N1C && (1 == N1C->getZExtValue())) {
14425 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14426 }
14427 }
14428
Evan Chengad9c0a32009-12-15 00:53:42 +000014429 return SDValue();
14430}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014431
Nate Begeman740ab032009-01-26 00:52:55 +000014432/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14433/// when possible.
14434static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014435 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014436 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014437 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014438 if (N->getOpcode() == ISD::SHL) {
14439 SDValue V = PerformSHLCombine(N, DAG);
14440 if (V.getNode()) return V;
14441 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014442
Nate Begeman740ab032009-01-26 00:52:55 +000014443 // On X86 with SSE2 support, we can transform this to a vector shift if
14444 // all elements are shifted by the same amount. We can't do this in legalize
14445 // because the a constant vector is typically transformed to a constant pool
14446 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014447 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014448 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014449
Craig Topper7be5dfd2011-11-12 09:58:49 +000014450 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14451 (!Subtarget->hasAVX2() ||
14452 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014453 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014454
Mon P Wang3becd092009-01-28 08:12:05 +000014455 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014456 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014457 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014458 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014459 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14460 unsigned NumElts = VT.getVectorNumElements();
14461 unsigned i = 0;
14462 for (; i != NumElts; ++i) {
14463 SDValue Arg = ShAmtOp.getOperand(i);
14464 if (Arg.getOpcode() == ISD::UNDEF) continue;
14465 BaseShAmt = Arg;
14466 break;
14467 }
Craig Topper37c26772012-01-17 04:44:50 +000014468 // Handle the case where the build_vector is all undef
14469 // FIXME: Should DAG allow this?
14470 if (i == NumElts)
14471 return SDValue();
14472
Mon P Wang3becd092009-01-28 08:12:05 +000014473 for (; i != NumElts; ++i) {
14474 SDValue Arg = ShAmtOp.getOperand(i);
14475 if (Arg.getOpcode() == ISD::UNDEF) continue;
14476 if (Arg != BaseShAmt) {
14477 return SDValue();
14478 }
14479 }
14480 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014481 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014482 SDValue InVec = ShAmtOp.getOperand(0);
14483 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14484 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14485 unsigned i = 0;
14486 for (; i != NumElts; ++i) {
14487 SDValue Arg = InVec.getOperand(i);
14488 if (Arg.getOpcode() == ISD::UNDEF) continue;
14489 BaseShAmt = Arg;
14490 break;
14491 }
14492 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14493 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014494 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014495 if (C->getZExtValue() == SplatIdx)
14496 BaseShAmt = InVec.getOperand(1);
14497 }
14498 }
Mon P Wang845b1892012-02-01 22:15:20 +000014499 if (BaseShAmt.getNode() == 0) {
14500 // Don't create instructions with illegal types after legalize
14501 // types has run.
14502 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14503 !DCI.isBeforeLegalize())
14504 return SDValue();
14505
Mon P Wangefa42202009-09-03 19:56:25 +000014506 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14507 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014508 }
Mon P Wang3becd092009-01-28 08:12:05 +000014509 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014510 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014511
Mon P Wangefa42202009-09-03 19:56:25 +000014512 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014513 if (EltVT.bitsGT(MVT::i32))
14514 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14515 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014516 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014517
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014518 // The shift amount is identical so we can do a vector shift.
14519 SDValue ValOp = N->getOperand(0);
14520 switch (N->getOpcode()) {
14521 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014522 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014523 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014524 switch (VT.getSimpleVT().SimpleTy) {
14525 default: return SDValue();
14526 case MVT::v2i64:
14527 case MVT::v4i32:
14528 case MVT::v8i16:
14529 case MVT::v4i64:
14530 case MVT::v8i32:
14531 case MVT::v16i16:
14532 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14533 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014534 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014535 switch (VT.getSimpleVT().SimpleTy) {
14536 default: return SDValue();
14537 case MVT::v4i32:
14538 case MVT::v8i16:
14539 case MVT::v8i32:
14540 case MVT::v16i16:
14541 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14542 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014543 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014544 switch (VT.getSimpleVT().SimpleTy) {
14545 default: return SDValue();
14546 case MVT::v2i64:
14547 case MVT::v4i32:
14548 case MVT::v8i16:
14549 case MVT::v4i64:
14550 case MVT::v8i32:
14551 case MVT::v16i16:
14552 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14553 }
Nate Begeman740ab032009-01-26 00:52:55 +000014554 }
Nate Begeman740ab032009-01-26 00:52:55 +000014555}
14556
Nate Begemanb65c1752010-12-17 22:55:37 +000014557
Stuart Hastings865f0932011-06-03 23:53:54 +000014558// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14559// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14560// and friends. Likewise for OR -> CMPNEQSS.
14561static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14562 TargetLowering::DAGCombinerInfo &DCI,
14563 const X86Subtarget *Subtarget) {
14564 unsigned opcode;
14565
14566 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14567 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014568 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014569 SDValue N0 = N->getOperand(0);
14570 SDValue N1 = N->getOperand(1);
14571 SDValue CMP0 = N0->getOperand(1);
14572 SDValue CMP1 = N1->getOperand(1);
14573 DebugLoc DL = N->getDebugLoc();
14574
14575 // The SETCCs should both refer to the same CMP.
14576 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14577 return SDValue();
14578
14579 SDValue CMP00 = CMP0->getOperand(0);
14580 SDValue CMP01 = CMP0->getOperand(1);
14581 EVT VT = CMP00.getValueType();
14582
14583 if (VT == MVT::f32 || VT == MVT::f64) {
14584 bool ExpectingFlags = false;
14585 // Check for any users that want flags:
14586 for (SDNode::use_iterator UI = N->use_begin(),
14587 UE = N->use_end();
14588 !ExpectingFlags && UI != UE; ++UI)
14589 switch (UI->getOpcode()) {
14590 default:
14591 case ISD::BR_CC:
14592 case ISD::BRCOND:
14593 case ISD::SELECT:
14594 ExpectingFlags = true;
14595 break;
14596 case ISD::CopyToReg:
14597 case ISD::SIGN_EXTEND:
14598 case ISD::ZERO_EXTEND:
14599 case ISD::ANY_EXTEND:
14600 break;
14601 }
14602
14603 if (!ExpectingFlags) {
14604 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14605 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14606
14607 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14608 X86::CondCode tmp = cc0;
14609 cc0 = cc1;
14610 cc1 = tmp;
14611 }
14612
14613 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14614 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14615 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14616 X86ISD::NodeType NTOperator = is64BitFP ?
14617 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14618 // FIXME: need symbolic constants for these magic numbers.
14619 // See X86ATTInstPrinter.cpp:printSSECC().
14620 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14621 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14622 DAG.getConstant(x86cc, MVT::i8));
14623 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14624 OnesOrZeroesF);
14625 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14626 DAG.getConstant(1, MVT::i32));
14627 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14628 return OneBitOfTruth;
14629 }
14630 }
14631 }
14632 }
14633 return SDValue();
14634}
14635
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014636/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14637/// so it can be folded inside ANDNP.
14638static bool CanFoldXORWithAllOnes(const SDNode *N) {
14639 EVT VT = N->getValueType(0);
14640
14641 // Match direct AllOnes for 128 and 256-bit vectors
14642 if (ISD::isBuildVectorAllOnes(N))
14643 return true;
14644
14645 // Look through a bit convert.
14646 if (N->getOpcode() == ISD::BITCAST)
14647 N = N->getOperand(0).getNode();
14648
14649 // Sometimes the operand may come from a insert_subvector building a 256-bit
14650 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014651 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014652 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14653 SDValue V1 = N->getOperand(0);
14654 SDValue V2 = N->getOperand(1);
14655
14656 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14657 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14658 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14659 ISD::isBuildVectorAllOnes(V2.getNode()))
14660 return true;
14661 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014662
14663 return false;
14664}
14665
Nate Begemanb65c1752010-12-17 22:55:37 +000014666static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14667 TargetLowering::DAGCombinerInfo &DCI,
14668 const X86Subtarget *Subtarget) {
14669 if (DCI.isBeforeLegalizeOps())
14670 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014671
Stuart Hastings865f0932011-06-03 23:53:54 +000014672 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14673 if (R.getNode())
14674 return R;
14675
Craig Topper54a11172011-10-14 07:06:56 +000014676 EVT VT = N->getValueType(0);
14677
Craig Topperb4c94572011-10-21 06:55:01 +000014678 // Create ANDN, BLSI, and BLSR instructions
14679 // BLSI is X & (-X)
14680 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014681 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14682 SDValue N0 = N->getOperand(0);
14683 SDValue N1 = N->getOperand(1);
14684 DebugLoc DL = N->getDebugLoc();
14685
14686 // Check LHS for not
14687 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14688 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14689 // Check RHS for not
14690 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14691 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14692
Craig Topperb4c94572011-10-21 06:55:01 +000014693 // Check LHS for neg
14694 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14695 isZero(N0.getOperand(0)))
14696 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14697
14698 // Check RHS for neg
14699 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14700 isZero(N1.getOperand(0)))
14701 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14702
14703 // Check LHS for X-1
14704 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14705 isAllOnes(N0.getOperand(1)))
14706 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14707
14708 // Check RHS for X-1
14709 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14710 isAllOnes(N1.getOperand(1)))
14711 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14712
Craig Topper54a11172011-10-14 07:06:56 +000014713 return SDValue();
14714 }
14715
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014716 // Want to form ANDNP nodes:
14717 // 1) In the hopes of then easily combining them with OR and AND nodes
14718 // to form PBLEND/PSIGN.
14719 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014720 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014721 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014722
Nate Begemanb65c1752010-12-17 22:55:37 +000014723 SDValue N0 = N->getOperand(0);
14724 SDValue N1 = N->getOperand(1);
14725 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014726
Nate Begemanb65c1752010-12-17 22:55:37 +000014727 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014728 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014729 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14730 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014731 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014732
14733 // Check RHS for vnot
14734 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014735 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14736 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014737 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014738
Nate Begemanb65c1752010-12-17 22:55:37 +000014739 return SDValue();
14740}
14741
Evan Cheng760d1942010-01-04 21:22:48 +000014742static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014743 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014744 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014745 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014746 return SDValue();
14747
Stuart Hastings865f0932011-06-03 23:53:54 +000014748 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14749 if (R.getNode())
14750 return R;
14751
Evan Cheng760d1942010-01-04 21:22:48 +000014752 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014753
Evan Cheng760d1942010-01-04 21:22:48 +000014754 SDValue N0 = N->getOperand(0);
14755 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014756
Nate Begemanb65c1752010-12-17 22:55:37 +000014757 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014758 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014759 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014760 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14761 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014762
Craig Topper1666cb62011-11-19 07:07:26 +000014763 // Canonicalize pandn to RHS
14764 if (N0.getOpcode() == X86ISD::ANDNP)
14765 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014766 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014767 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14768 SDValue Mask = N1.getOperand(0);
14769 SDValue X = N1.getOperand(1);
14770 SDValue Y;
14771 if (N0.getOperand(0) == Mask)
14772 Y = N0.getOperand(1);
14773 if (N0.getOperand(1) == Mask)
14774 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014775
Craig Topper1666cb62011-11-19 07:07:26 +000014776 // Check to see if the mask appeared in both the AND and ANDNP and
14777 if (!Y.getNode())
14778 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014779
Craig Topper1666cb62011-11-19 07:07:26 +000014780 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014781 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014782 if (Mask.getOpcode() == ISD::BITCAST)
14783 Mask = Mask.getOperand(0);
14784 if (X.getOpcode() == ISD::BITCAST)
14785 X = X.getOperand(0);
14786 if (Y.getOpcode() == ISD::BITCAST)
14787 Y = Y.getOperand(0);
14788
Craig Topper1666cb62011-11-19 07:07:26 +000014789 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014790
Craig Toppered2e13d2012-01-22 19:15:14 +000014791 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014792 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14793 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014794 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014795 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014796
14797 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014798 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014799 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14800 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14801 if ((SraAmt + 1) != EltBits)
14802 return SDValue();
14803
14804 DebugLoc DL = N->getDebugLoc();
14805
14806 // Now we know we at least have a plendvb with the mask val. See if
14807 // we can form a psignb/w/d.
14808 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014809 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14810 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014811 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14812 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14813 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014814 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014815 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014816 }
14817 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014818 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014819 return SDValue();
14820
14821 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14822
14823 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14824 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14825 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014826 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014827 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014828 }
14829 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014830
Craig Topper1666cb62011-11-19 07:07:26 +000014831 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14832 return SDValue();
14833
Nate Begemanb65c1752010-12-17 22:55:37 +000014834 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014835 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14836 std::swap(N0, N1);
14837 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14838 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014839 if (!N0.hasOneUse() || !N1.hasOneUse())
14840 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014841
14842 SDValue ShAmt0 = N0.getOperand(1);
14843 if (ShAmt0.getValueType() != MVT::i8)
14844 return SDValue();
14845 SDValue ShAmt1 = N1.getOperand(1);
14846 if (ShAmt1.getValueType() != MVT::i8)
14847 return SDValue();
14848 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14849 ShAmt0 = ShAmt0.getOperand(0);
14850 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14851 ShAmt1 = ShAmt1.getOperand(0);
14852
14853 DebugLoc DL = N->getDebugLoc();
14854 unsigned Opc = X86ISD::SHLD;
14855 SDValue Op0 = N0.getOperand(0);
14856 SDValue Op1 = N1.getOperand(0);
14857 if (ShAmt0.getOpcode() == ISD::SUB) {
14858 Opc = X86ISD::SHRD;
14859 std::swap(Op0, Op1);
14860 std::swap(ShAmt0, ShAmt1);
14861 }
14862
Evan Cheng8b1190a2010-04-28 01:18:01 +000014863 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014864 if (ShAmt1.getOpcode() == ISD::SUB) {
14865 SDValue Sum = ShAmt1.getOperand(0);
14866 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014867 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14868 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14869 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14870 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014871 return DAG.getNode(Opc, DL, VT,
14872 Op0, Op1,
14873 DAG.getNode(ISD::TRUNCATE, DL,
14874 MVT::i8, ShAmt0));
14875 }
14876 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14877 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14878 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014879 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014880 return DAG.getNode(Opc, DL, VT,
14881 N0.getOperand(0), N1.getOperand(0),
14882 DAG.getNode(ISD::TRUNCATE, DL,
14883 MVT::i8, ShAmt0));
14884 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014885
Evan Cheng760d1942010-01-04 21:22:48 +000014886 return SDValue();
14887}
14888
Manman Ren92363622012-06-07 22:39:10 +000014889// Generate NEG and CMOV for integer abs.
14890static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14891 EVT VT = N->getValueType(0);
14892
14893 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14894 // 8-bit integer abs to NEG and CMOV.
14895 if (VT.isInteger() && VT.getSizeInBits() == 8)
14896 return SDValue();
14897
14898 SDValue N0 = N->getOperand(0);
14899 SDValue N1 = N->getOperand(1);
14900 DebugLoc DL = N->getDebugLoc();
14901
14902 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
14903 // and change it to SUB and CMOV.
14904 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
14905 N0.getOpcode() == ISD::ADD &&
14906 N0.getOperand(1) == N1 &&
14907 N1.getOpcode() == ISD::SRA &&
14908 N1.getOperand(0) == N0.getOperand(0))
14909 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
14910 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
14911 // Generate SUB & CMOV.
14912 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
14913 DAG.getConstant(0, VT), N0.getOperand(0));
14914
14915 SDValue Ops[] = { N0.getOperand(0), Neg,
14916 DAG.getConstant(X86::COND_GE, MVT::i8),
14917 SDValue(Neg.getNode(), 1) };
14918 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
14919 Ops, array_lengthof(Ops));
14920 }
14921 return SDValue();
14922}
14923
Craig Topper3738ccd2011-12-27 06:27:23 +000014924// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014925static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14926 TargetLowering::DAGCombinerInfo &DCI,
14927 const X86Subtarget *Subtarget) {
14928 if (DCI.isBeforeLegalizeOps())
14929 return SDValue();
14930
Manman Ren45d53b82012-06-08 18:58:26 +000014931 if (Subtarget->hasCMov()) {
14932 SDValue RV = performIntegerAbsCombine(N, DAG);
14933 if (RV.getNode())
14934 return RV;
14935 }
Manman Ren92363622012-06-07 22:39:10 +000014936
14937 // Try forming BMI if it is available.
14938 if (!Subtarget->hasBMI())
14939 return SDValue();
14940
Craig Topperb4c94572011-10-21 06:55:01 +000014941 EVT VT = N->getValueType(0);
14942
14943 if (VT != MVT::i32 && VT != MVT::i64)
14944 return SDValue();
14945
Craig Topper3738ccd2011-12-27 06:27:23 +000014946 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
14947
Craig Topperb4c94572011-10-21 06:55:01 +000014948 // Create BLSMSK instructions by finding X ^ (X-1)
14949 SDValue N0 = N->getOperand(0);
14950 SDValue N1 = N->getOperand(1);
14951 DebugLoc DL = N->getDebugLoc();
14952
14953 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14954 isAllOnes(N0.getOperand(1)))
14955 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14956
14957 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14958 isAllOnes(N1.getOperand(1)))
14959 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14960
14961 return SDValue();
14962}
14963
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014964/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14965static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014966 TargetLowering::DAGCombinerInfo &DCI,
14967 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014968 LoadSDNode *Ld = cast<LoadSDNode>(N);
14969 EVT RegVT = Ld->getValueType(0);
14970 EVT MemVT = Ld->getMemoryVT();
14971 DebugLoc dl = Ld->getDebugLoc();
14972 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14973
14974 ISD::LoadExtType Ext = Ld->getExtensionType();
14975
Nadav Rotemca6f2962011-09-18 19:00:23 +000014976 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014977 // shuffle. We need SSE4 for the shuffles.
14978 // TODO: It is possible to support ZExt by zeroing the undef values
14979 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000014980 if (RegVT.isVector() && RegVT.isInteger() &&
14981 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014982 assert(MemVT != RegVT && "Cannot extend to the same type");
14983 assert(MemVT.isVector() && "Must load a vector from memory");
14984
14985 unsigned NumElems = RegVT.getVectorNumElements();
14986 unsigned RegSz = RegVT.getSizeInBits();
14987 unsigned MemSz = MemVT.getSizeInBits();
14988 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014989
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014990 // All sizes must be a power of two.
14991 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
14992 return SDValue();
14993
14994 // Attempt to load the original value using scalar loads.
14995 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014996 MVT SclrLoadTy = MVT::i8;
14997 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14998 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14999 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015000 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015001 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015002 }
15003 }
15004
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015005 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15006 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15007 (64 <= MemSz))
15008 SclrLoadTy = MVT::f64;
15009
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015010 // Calculate the number of scalar loads that we need to perform
15011 // in order to load our vector from memory.
15012 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015013
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015014 // Represent our vector as a sequence of elements which are the
15015 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015016 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15017 RegSz/SclrLoadTy.getSizeInBits());
15018
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015019 // Represent the data using the same element type that is stored in
15020 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015021 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15022 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015023
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015024 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15025 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015026
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015027 // We can't shuffle using an illegal type.
15028 if (!TLI.isTypeLegal(WideVecVT))
15029 return SDValue();
15030
15031 SmallVector<SDValue, 8> Chains;
15032 SDValue Ptr = Ld->getBasePtr();
15033 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15034 TLI.getPointerTy());
15035 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15036
15037 for (unsigned i = 0; i < NumLoads; ++i) {
15038 // Perform a single load.
15039 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15040 Ptr, Ld->getPointerInfo(),
15041 Ld->isVolatile(), Ld->isNonTemporal(),
15042 Ld->isInvariant(), Ld->getAlignment());
15043 Chains.push_back(ScalarLoad.getValue(1));
15044 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15045 // another round of DAGCombining.
15046 if (i == 0)
15047 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15048 else
15049 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15050 ScalarLoad, DAG.getIntPtrConstant(i));
15051
15052 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15053 }
15054
15055 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15056 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015057
15058 // Bitcast the loaded value to a vector of the original element type, in
15059 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015060 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015061 unsigned SizeRatio = RegSz/MemSz;
15062
15063 // Redistribute the loaded elements into the different locations.
15064 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015065 for (unsigned i = 0; i != NumElems; ++i)
15066 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015067
15068 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015069 DAG.getUNDEF(WideVecVT),
15070 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015071
15072 // Bitcast to the requested type.
15073 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15074 // Replace the original load with the new sequence
15075 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015076 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015077 }
15078
15079 return SDValue();
15080}
15081
Chris Lattner149a4e52008-02-22 02:09:43 +000015082/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015083static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015084 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015085 StoreSDNode *St = cast<StoreSDNode>(N);
15086 EVT VT = St->getValue().getValueType();
15087 EVT StVT = St->getMemoryVT();
15088 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015089 SDValue StoredVal = St->getOperand(1);
15090 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15091
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015092 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015093 // On Sandy Bridge, 256-bit memory operations are executed by two
15094 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15095 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015096 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015097 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15098 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015099 SDValue Value0 = StoredVal.getOperand(0);
15100 SDValue Value1 = StoredVal.getOperand(1);
15101
15102 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15103 SDValue Ptr0 = St->getBasePtr();
15104 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15105
15106 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15107 St->getPointerInfo(), St->isVolatile(),
15108 St->isNonTemporal(), St->getAlignment());
15109 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15110 St->getPointerInfo(), St->isVolatile(),
15111 St->isNonTemporal(), St->getAlignment());
15112 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15113 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015114
15115 // Optimize trunc store (of multiple scalars) to shuffle and store.
15116 // First, pack all of the elements in one place. Next, store to memory
15117 // in fewer chunks.
15118 if (St->isTruncatingStore() && VT.isVector()) {
15119 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15120 unsigned NumElems = VT.getVectorNumElements();
15121 assert(StVT != VT && "Cannot truncate to the same type");
15122 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15123 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15124
15125 // From, To sizes and ElemCount must be pow of two
15126 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015127 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015128 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015129 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015130
Nadav Rotem614061b2011-08-10 19:30:14 +000015131 unsigned SizeRatio = FromSz / ToSz;
15132
15133 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15134
15135 // Create a type on which we perform the shuffle
15136 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15137 StVT.getScalarType(), NumElems*SizeRatio);
15138
15139 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15140
15141 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15142 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015143 for (unsigned i = 0; i != NumElems; ++i)
15144 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015145
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015146 // Can't shuffle using an illegal type.
15147 if (!TLI.isTypeLegal(WideVecVT))
15148 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015149
15150 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015151 DAG.getUNDEF(WideVecVT),
15152 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015153 // At this point all of the data is stored at the bottom of the
15154 // register. We now need to save it to mem.
15155
15156 // Find the largest store unit
15157 MVT StoreType = MVT::i8;
15158 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15159 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15160 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015161 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015162 StoreType = Tp;
15163 }
15164
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015165 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15166 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15167 (64 <= NumElems * ToSz))
15168 StoreType = MVT::f64;
15169
Nadav Rotem614061b2011-08-10 19:30:14 +000015170 // Bitcast the original vector into a vector of store-size units
15171 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015172 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015173 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15174 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15175 SmallVector<SDValue, 8> Chains;
15176 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15177 TLI.getPointerTy());
15178 SDValue Ptr = St->getBasePtr();
15179
15180 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015181 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015182 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15183 StoreType, ShuffWide,
15184 DAG.getIntPtrConstant(i));
15185 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15186 St->getPointerInfo(), St->isVolatile(),
15187 St->isNonTemporal(), St->getAlignment());
15188 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15189 Chains.push_back(Ch);
15190 }
15191
15192 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15193 Chains.size());
15194 }
15195
15196
Chris Lattner149a4e52008-02-22 02:09:43 +000015197 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15198 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015199 // A preferable solution to the general problem is to figure out the right
15200 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015201
15202 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015203 if (VT.getSizeInBits() != 64)
15204 return SDValue();
15205
Devang Patel578efa92009-06-05 21:57:13 +000015206 const Function *F = DAG.getMachineFunction().getFunction();
15207 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015208 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015209 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015210 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015211 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015212 isa<LoadSDNode>(St->getValue()) &&
15213 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15214 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015215 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015216 LoadSDNode *Ld = 0;
15217 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015218 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015219 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015220 // Must be a store of a load. We currently handle two cases: the load
15221 // is a direct child, and it's under an intervening TokenFactor. It is
15222 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015223 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015224 Ld = cast<LoadSDNode>(St->getChain());
15225 else if (St->getValue().hasOneUse() &&
15226 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015227 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015228 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015229 TokenFactorIndex = i;
15230 Ld = cast<LoadSDNode>(St->getValue());
15231 } else
15232 Ops.push_back(ChainVal->getOperand(i));
15233 }
15234 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015235
Evan Cheng536e6672009-03-12 05:59:15 +000015236 if (!Ld || !ISD::isNormalLoad(Ld))
15237 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015238
Evan Cheng536e6672009-03-12 05:59:15 +000015239 // If this is not the MMX case, i.e. we are just turning i64 load/store
15240 // into f64 load/store, avoid the transformation if there are multiple
15241 // uses of the loaded value.
15242 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15243 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015244
Evan Cheng536e6672009-03-12 05:59:15 +000015245 DebugLoc LdDL = Ld->getDebugLoc();
15246 DebugLoc StDL = N->getDebugLoc();
15247 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15248 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15249 // pair instead.
15250 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015251 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015252 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15253 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015254 Ld->isNonTemporal(), Ld->isInvariant(),
15255 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015256 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015257 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015258 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015259 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015260 Ops.size());
15261 }
Evan Cheng536e6672009-03-12 05:59:15 +000015262 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015263 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015264 St->isVolatile(), St->isNonTemporal(),
15265 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015266 }
Evan Cheng536e6672009-03-12 05:59:15 +000015267
15268 // Otherwise, lower to two pairs of 32-bit loads / stores.
15269 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015270 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15271 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015272
Owen Anderson825b72b2009-08-11 20:47:22 +000015273 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015274 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015275 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015276 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015277 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015278 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015279 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015280 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015281 MinAlign(Ld->getAlignment(), 4));
15282
15283 SDValue NewChain = LoLd.getValue(1);
15284 if (TokenFactorIndex != -1) {
15285 Ops.push_back(LoLd);
15286 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015287 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015288 Ops.size());
15289 }
15290
15291 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015292 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15293 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015294
15295 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015296 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015297 St->isVolatile(), St->isNonTemporal(),
15298 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015299 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015300 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015301 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015302 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015303 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015304 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015305 }
Dan Gohman475871a2008-07-27 21:46:04 +000015306 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015307}
15308
Duncan Sands17470be2011-09-22 20:15:48 +000015309/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15310/// and return the operands for the horizontal operation in LHS and RHS. A
15311/// horizontal operation performs the binary operation on successive elements
15312/// of its first operand, then on successive elements of its second operand,
15313/// returning the resulting values in a vector. For example, if
15314/// A = < float a0, float a1, float a2, float a3 >
15315/// and
15316/// B = < float b0, float b1, float b2, float b3 >
15317/// then the result of doing a horizontal operation on A and B is
15318/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15319/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15320/// A horizontal-op B, for some already available A and B, and if so then LHS is
15321/// set to A, RHS to B, and the routine returns 'true'.
15322/// Note that the binary operation should have the property that if one of the
15323/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015324static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015325 // Look for the following pattern: if
15326 // A = < float a0, float a1, float a2, float a3 >
15327 // B = < float b0, float b1, float b2, float b3 >
15328 // and
15329 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15330 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15331 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15332 // which is A horizontal-op B.
15333
15334 // At least one of the operands should be a vector shuffle.
15335 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15336 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15337 return false;
15338
15339 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015340
15341 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15342 "Unsupported vector type for horizontal add/sub");
15343
15344 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15345 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015346 unsigned NumElts = VT.getVectorNumElements();
15347 unsigned NumLanes = VT.getSizeInBits()/128;
15348 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015349 assert((NumLaneElts % 2 == 0) &&
15350 "Vector type should have an even number of elements in each lane");
15351 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015352
15353 // View LHS in the form
15354 // LHS = VECTOR_SHUFFLE A, B, LMask
15355 // If LHS is not a shuffle then pretend it is the shuffle
15356 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15357 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15358 // type VT.
15359 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015360 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015361 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15362 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15363 A = LHS.getOperand(0);
15364 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15365 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015366 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15367 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015368 } else {
15369 if (LHS.getOpcode() != ISD::UNDEF)
15370 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015371 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015372 LMask[i] = i;
15373 }
15374
15375 // Likewise, view RHS in the form
15376 // RHS = VECTOR_SHUFFLE C, D, RMask
15377 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015378 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015379 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15380 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15381 C = RHS.getOperand(0);
15382 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15383 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015384 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15385 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015386 } else {
15387 if (RHS.getOpcode() != ISD::UNDEF)
15388 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015389 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015390 RMask[i] = i;
15391 }
15392
15393 // Check that the shuffles are both shuffling the same vectors.
15394 if (!(A == C && B == D) && !(A == D && B == C))
15395 return false;
15396
15397 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15398 if (!A.getNode() && !B.getNode())
15399 return false;
15400
15401 // If A and B occur in reverse order in RHS, then "swap" them (which means
15402 // rewriting the mask).
15403 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015404 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015405
15406 // At this point LHS and RHS are equivalent to
15407 // LHS = VECTOR_SHUFFLE A, B, LMask
15408 // RHS = VECTOR_SHUFFLE A, B, RMask
15409 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015410 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015411 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015412
Craig Topperf8363302011-12-02 08:18:41 +000015413 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015414 if (LIdx < 0 || RIdx < 0 ||
15415 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15416 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015417 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015418
Craig Topperf8363302011-12-02 08:18:41 +000015419 // Check that successive elements are being operated on. If not, this is
15420 // not a horizontal operation.
15421 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15422 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015423 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015424 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015425 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015426 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015427 }
15428
15429 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15430 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15431 return true;
15432}
15433
15434/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15435static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15436 const X86Subtarget *Subtarget) {
15437 EVT VT = N->getValueType(0);
15438 SDValue LHS = N->getOperand(0);
15439 SDValue RHS = N->getOperand(1);
15440
15441 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015442 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015443 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015444 isHorizontalBinOp(LHS, RHS, true))
15445 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15446 return SDValue();
15447}
15448
15449/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15450static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15451 const X86Subtarget *Subtarget) {
15452 EVT VT = N->getValueType(0);
15453 SDValue LHS = N->getOperand(0);
15454 SDValue RHS = N->getOperand(1);
15455
15456 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015457 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015458 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015459 isHorizontalBinOp(LHS, RHS, false))
15460 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15461 return SDValue();
15462}
15463
Chris Lattner6cf73262008-01-25 06:14:17 +000015464/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15465/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015466static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015467 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15468 // F[X]OR(0.0, x) -> x
15469 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015470 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15471 if (C->getValueAPF().isPosZero())
15472 return N->getOperand(1);
15473 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15474 if (C->getValueAPF().isPosZero())
15475 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015476 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015477}
15478
Nadav Rotemd60cb112012-08-19 13:06:16 +000015479/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15480/// X86ISD::FMAX nodes.
15481static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15482 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15483
15484 // Only perform optimizations if UnsafeMath is used.
15485 if (!DAG.getTarget().Options.UnsafeFPMath)
15486 return SDValue();
15487
15488 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015489 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015490 unsigned NewOp = 0;
15491 switch (N->getOpcode()) {
15492 default: llvm_unreachable("unknown opcode");
15493 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15494 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15495 }
15496
15497 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15498 N->getOperand(0), N->getOperand(1));
15499}
15500
15501
Chris Lattneraf723b92008-01-25 05:46:26 +000015502/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015503static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015504 // FAND(0.0, x) -> 0.0
15505 // FAND(x, 0.0) -> 0.0
15506 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15507 if (C->getValueAPF().isPosZero())
15508 return N->getOperand(0);
15509 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15510 if (C->getValueAPF().isPosZero())
15511 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015512 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015513}
15514
Dan Gohmane5af2d32009-01-29 01:59:02 +000015515static SDValue PerformBTCombine(SDNode *N,
15516 SelectionDAG &DAG,
15517 TargetLowering::DAGCombinerInfo &DCI) {
15518 // BT ignores high bits in the bit index operand.
15519 SDValue Op1 = N->getOperand(1);
15520 if (Op1.hasOneUse()) {
15521 unsigned BitWidth = Op1.getValueSizeInBits();
15522 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15523 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015524 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15525 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015526 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015527 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15528 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15529 DCI.CommitTargetLoweringOpt(TLO);
15530 }
15531 return SDValue();
15532}
Chris Lattner83e6c992006-10-04 06:57:07 +000015533
Eli Friedman7a5e5552009-06-07 06:52:44 +000015534static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15535 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015536 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015537 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015538 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015539 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015540 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015541 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015542 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015543 }
15544 return SDValue();
15545}
15546
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015547static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15548 TargetLowering::DAGCombinerInfo &DCI,
15549 const X86Subtarget *Subtarget) {
15550 if (!DCI.isBeforeLegalizeOps())
15551 return SDValue();
15552
Craig Topper3ef43cf2012-04-24 06:36:35 +000015553 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015554 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015555
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015556 EVT VT = N->getValueType(0);
15557 SDValue Op = N->getOperand(0);
15558 EVT OpVT = Op.getValueType();
15559 DebugLoc dl = N->getDebugLoc();
15560
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015561 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15562 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015563
Craig Topper3ef43cf2012-04-24 06:36:35 +000015564 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015565 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015566
15567 // Optimize vectors in AVX mode
15568 // Sign extend v8i16 to v8i32 and
15569 // v4i32 to v4i64
15570 //
15571 // Divide input vector into two parts
15572 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15573 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15574 // concat the vectors to original VT
15575
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015576 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015577 SDValue Undef = DAG.getUNDEF(OpVT);
15578
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015579 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015580 for (unsigned i = 0; i != NumElems/2; ++i)
15581 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015582
Craig Toppercacafd42012-08-14 08:18:43 +000015583 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015584
15585 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015586 for (unsigned i = 0; i != NumElems/2; ++i)
15587 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015588
Craig Toppercacafd42012-08-14 08:18:43 +000015589 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015590
Craig Topper3ef43cf2012-04-24 06:36:35 +000015591 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015592 VT.getVectorNumElements()/2);
15593
Craig Topper3ef43cf2012-04-24 06:36:35 +000015594 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015595 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15596
15597 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15598 }
15599 return SDValue();
15600}
15601
Michael Liaof6c24ee2012-08-10 14:39:24 +000015602static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015603 const X86Subtarget* Subtarget) {
15604 DebugLoc dl = N->getDebugLoc();
15605 EVT VT = N->getValueType(0);
15606
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015607 // Let legalize expand this if it isn't a legal type yet.
15608 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15609 return SDValue();
15610
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015611 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015612 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15613 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015614 return SDValue();
15615
15616 SDValue A = N->getOperand(0);
15617 SDValue B = N->getOperand(1);
15618 SDValue C = N->getOperand(2);
15619
15620 bool NegA = (A.getOpcode() == ISD::FNEG);
15621 bool NegB = (B.getOpcode() == ISD::FNEG);
15622 bool NegC = (C.getOpcode() == ISD::FNEG);
15623
Michael Liaof6c24ee2012-08-10 14:39:24 +000015624 // Negative multiplication when NegA xor NegB
15625 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015626 if (NegA)
15627 A = A.getOperand(0);
15628 if (NegB)
15629 B = B.getOperand(0);
15630 if (NegC)
15631 C = C.getOperand(0);
15632
15633 unsigned Opcode;
15634 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015635 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015636 else
Craig Topperbf404372012-08-31 15:40:30 +000015637 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15638
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015639 return DAG.getNode(Opcode, dl, VT, A, B, C);
15640}
15641
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015642static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015643 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015644 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015645 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15646 // (and (i32 x86isd::setcc_carry), 1)
15647 // This eliminates the zext. This transformation is necessary because
15648 // ISD::SETCC is always legalized to i8.
15649 DebugLoc dl = N->getDebugLoc();
15650 SDValue N0 = N->getOperand(0);
15651 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015652 EVT OpVT = N0.getValueType();
15653
Evan Cheng2e489c42009-12-16 00:53:11 +000015654 if (N0.getOpcode() == ISD::AND &&
15655 N0.hasOneUse() &&
15656 N0.getOperand(0).hasOneUse()) {
15657 SDValue N00 = N0.getOperand(0);
15658 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15659 return SDValue();
15660 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15661 if (!C || C->getZExtValue() != 1)
15662 return SDValue();
15663 return DAG.getNode(ISD::AND, dl, VT,
15664 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15665 N00.getOperand(0), N00.getOperand(1)),
15666 DAG.getConstant(1, VT));
15667 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015668
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015669 // Optimize vectors in AVX mode:
15670 //
15671 // v8i16 -> v8i32
15672 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15673 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15674 // Concat upper and lower parts.
15675 //
15676 // v4i32 -> v4i64
15677 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15678 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15679 // Concat upper and lower parts.
15680 //
Craig Topperc16f8512012-04-25 06:39:39 +000015681 if (!DCI.isBeforeLegalizeOps())
15682 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015683
Craig Topperc16f8512012-04-25 06:39:39 +000015684 if (!Subtarget->hasAVX())
15685 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015686
Craig Topperc16f8512012-04-25 06:39:39 +000015687 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15688 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015689
Craig Topperc16f8512012-04-25 06:39:39 +000015690 if (Subtarget->hasAVX2())
15691 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015692
Craig Topperc16f8512012-04-25 06:39:39 +000015693 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15694 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15695 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015696
Craig Topperc16f8512012-04-25 06:39:39 +000015697 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15698 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015699
Craig Topperc16f8512012-04-25 06:39:39 +000015700 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15701 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15702
15703 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015704 }
15705
Evan Cheng2e489c42009-12-16 00:53:11 +000015706 return SDValue();
15707}
15708
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015709// Optimize x == -y --> x+y == 0
15710// x != -y --> x+y != 0
15711static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15712 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15713 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015714 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015715
15716 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15717 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15718 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15719 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15720 LHS.getValueType(), RHS, LHS.getOperand(1));
15721 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15722 addV, DAG.getConstant(0, addV.getValueType()), CC);
15723 }
15724 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15725 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15726 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15727 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15728 RHS.getValueType(), LHS, RHS.getOperand(1));
15729 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15730 addV, DAG.getConstant(0, addV.getValueType()), CC);
15731 }
15732 return SDValue();
15733}
15734
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015735// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015736static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15737 TargetLowering::DAGCombinerInfo &DCI,
15738 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015739 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015740 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15741 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015742
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015743 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15744 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15745 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015746 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015747 return DAG.getNode(ISD::AND, DL, MVT::i8,
15748 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015749 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015750 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015751
Michael Liao2a33cec2012-08-10 19:58:13 +000015752 SDValue Flags;
15753
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015754 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15755 if (Flags.getNode()) {
15756 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15757 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15758 }
15759
15760 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015761 if (Flags.getNode()) {
15762 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15763 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15764 }
15765
15766 return SDValue();
15767}
15768
15769// Optimize branch condition evaluation.
15770//
15771static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15772 TargetLowering::DAGCombinerInfo &DCI,
15773 const X86Subtarget *Subtarget) {
15774 DebugLoc DL = N->getDebugLoc();
15775 SDValue Chain = N->getOperand(0);
15776 SDValue Dest = N->getOperand(1);
15777 SDValue EFLAGS = N->getOperand(3);
15778 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15779
15780 SDValue Flags;
15781
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015782 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15783 if (Flags.getNode()) {
15784 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15785 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15786 Flags);
15787 }
15788
15789 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015790 if (Flags.getNode()) {
15791 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15792 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15793 Flags);
15794 }
15795
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015796 return SDValue();
15797}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015798
Craig Topper7fd5e162012-04-24 06:02:29 +000015799static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015800 SDValue Op0 = N->getOperand(0);
15801 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015802
15803 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015804 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015805 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015806 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015807 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15808 // Notice that we use SINT_TO_FP because we know that the high bits
15809 // are zero and SINT_TO_FP is better supported by the hardware.
15810 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15811 }
15812
15813 return SDValue();
15814}
15815
Benjamin Kramer1396c402011-06-18 11:09:41 +000015816static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15817 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015818 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015819 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015820
15821 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015822 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015823 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015824 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015825 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15826 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15827 }
15828
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015829 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15830 // a 32-bit target where SSE doesn't support i64->FP operations.
15831 if (Op0.getOpcode() == ISD::LOAD) {
15832 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15833 EVT VT = Ld->getValueType(0);
15834 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15835 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15836 !XTLI->getSubtarget()->is64Bit() &&
15837 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015838 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15839 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015840 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15841 return FILDChain;
15842 }
15843 }
15844 return SDValue();
15845}
15846
Craig Topper7fd5e162012-04-24 06:02:29 +000015847static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15848 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015849
15850 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015851 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15852 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015853 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015854 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15855 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15856 }
15857
15858 return SDValue();
15859}
15860
Chris Lattner23a01992010-12-20 01:37:09 +000015861// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15862static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15863 X86TargetLowering::DAGCombinerInfo &DCI) {
15864 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15865 // the result is either zero or one (depending on the input carry bit).
15866 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15867 if (X86::isZeroNode(N->getOperand(0)) &&
15868 X86::isZeroNode(N->getOperand(1)) &&
15869 // We don't have a good way to replace an EFLAGS use, so only do this when
15870 // dead right now.
15871 SDValue(N, 1).use_empty()) {
15872 DebugLoc DL = N->getDebugLoc();
15873 EVT VT = N->getValueType(0);
15874 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15875 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15876 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15877 DAG.getConstant(X86::COND_B,MVT::i8),
15878 N->getOperand(2)),
15879 DAG.getConstant(1, VT));
15880 return DCI.CombineTo(N, Res1, CarryOut);
15881 }
15882
15883 return SDValue();
15884}
15885
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015886// fold (add Y, (sete X, 0)) -> adc 0, Y
15887// (add Y, (setne X, 0)) -> sbb -1, Y
15888// (sub (sete X, 0), Y) -> sbb 0, Y
15889// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015890static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015891 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015892
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015893 // Look through ZExts.
15894 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15895 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15896 return SDValue();
15897
15898 SDValue SetCC = Ext.getOperand(0);
15899 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
15900 return SDValue();
15901
15902 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
15903 if (CC != X86::COND_E && CC != X86::COND_NE)
15904 return SDValue();
15905
15906 SDValue Cmp = SetCC.getOperand(1);
15907 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000015908 !X86::isZeroNode(Cmp.getOperand(1)) ||
15909 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015910 return SDValue();
15911
15912 SDValue CmpOp0 = Cmp.getOperand(0);
15913 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
15914 DAG.getConstant(1, CmpOp0.getValueType()));
15915
15916 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
15917 if (CC == X86::COND_NE)
15918 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
15919 DL, OtherVal.getValueType(), OtherVal,
15920 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
15921 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
15922 DL, OtherVal.getValueType(), OtherVal,
15923 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
15924}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015925
Craig Topper54f952a2011-11-19 09:02:40 +000015926/// PerformADDCombine - Do target-specific dag combines on integer adds.
15927static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
15928 const X86Subtarget *Subtarget) {
15929 EVT VT = N->getValueType(0);
15930 SDValue Op0 = N->getOperand(0);
15931 SDValue Op1 = N->getOperand(1);
15932
15933 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015934 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000015935 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000015936 isHorizontalBinOp(Op0, Op1, true))
15937 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
15938
15939 return OptimizeConditionalInDecrement(N, DAG);
15940}
15941
15942static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
15943 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015944 SDValue Op0 = N->getOperand(0);
15945 SDValue Op1 = N->getOperand(1);
15946
15947 // X86 can't encode an immediate LHS of a sub. See if we can push the
15948 // negation into a preceding instruction.
15949 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015950 // If the RHS of the sub is a XOR with one use and a constant, invert the
15951 // immediate. Then add one to the LHS of the sub so we can turn
15952 // X-Y -> X+~Y+1, saving one register.
15953 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
15954 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000015955 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015956 EVT VT = Op0.getValueType();
15957 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
15958 Op1.getOperand(0),
15959 DAG.getConstant(~XorC, VT));
15960 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000015961 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015962 }
15963 }
15964
Craig Topper54f952a2011-11-19 09:02:40 +000015965 // Try to synthesize horizontal adds from adds of shuffles.
15966 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000015967 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000015968 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
15969 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000015970 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
15971
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015972 return OptimizeConditionalInDecrement(N, DAG);
15973}
15974
Dan Gohman475871a2008-07-27 21:46:04 +000015975SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000015976 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000015977 SelectionDAG &DAG = DCI.DAG;
15978 switch (N->getOpcode()) {
15979 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015980 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000015981 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000015982 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000015983 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015984 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000015985 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
15986 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000015987 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000015988 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000015989 case ISD::SHL:
15990 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000015991 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000015992 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000015993 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000015994 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015995 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000015996 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000015997 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015998 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000015999 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016000 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16001 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016002 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016003 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016004 case X86ISD::FMIN:
16005 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016006 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016007 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016008 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016009 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016010 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016011 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000016012 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016013 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016014 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016015 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016016 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016017 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016018 case X86ISD::UNPCKH:
16019 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016020 case X86ISD::MOVHLPS:
16021 case X86ISD::MOVLHPS:
16022 case X86ISD::PSHUFD:
16023 case X86ISD::PSHUFHW:
16024 case X86ISD::PSHUFLW:
16025 case X86ISD::MOVSS:
16026 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016027 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016028 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016029 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016030 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016031 }
16032
Dan Gohman475871a2008-07-27 21:46:04 +000016033 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016034}
16035
Evan Chenge5b51ac2010-04-17 06:13:15 +000016036/// isTypeDesirableForOp - Return true if the target has native support for
16037/// the specified value type and it is 'desirable' to use the type for the
16038/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16039/// instruction encodings are longer and some i16 instructions are slow.
16040bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16041 if (!isTypeLegal(VT))
16042 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016043 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016044 return true;
16045
16046 switch (Opc) {
16047 default:
16048 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016049 case ISD::LOAD:
16050 case ISD::SIGN_EXTEND:
16051 case ISD::ZERO_EXTEND:
16052 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016053 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016054 case ISD::SRL:
16055 case ISD::SUB:
16056 case ISD::ADD:
16057 case ISD::MUL:
16058 case ISD::AND:
16059 case ISD::OR:
16060 case ISD::XOR:
16061 return false;
16062 }
16063}
16064
16065/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016066/// beneficial for dag combiner to promote the specified node. If true, it
16067/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016068bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016069 EVT VT = Op.getValueType();
16070 if (VT != MVT::i16)
16071 return false;
16072
Evan Cheng4c26e932010-04-19 19:29:22 +000016073 bool Promote = false;
16074 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016075 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016076 default: break;
16077 case ISD::LOAD: {
16078 LoadSDNode *LD = cast<LoadSDNode>(Op);
16079 // If the non-extending load has a single use and it's not live out, then it
16080 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016081 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16082 Op.hasOneUse()*/) {
16083 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16084 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16085 // The only case where we'd want to promote LOAD (rather then it being
16086 // promoted as an operand is when it's only use is liveout.
16087 if (UI->getOpcode() != ISD::CopyToReg)
16088 return false;
16089 }
16090 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016091 Promote = true;
16092 break;
16093 }
16094 case ISD::SIGN_EXTEND:
16095 case ISD::ZERO_EXTEND:
16096 case ISD::ANY_EXTEND:
16097 Promote = true;
16098 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016099 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016100 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016101 SDValue N0 = Op.getOperand(0);
16102 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016103 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016104 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016105 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016106 break;
16107 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016108 case ISD::ADD:
16109 case ISD::MUL:
16110 case ISD::AND:
16111 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016112 case ISD::XOR:
16113 Commute = true;
16114 // fallthrough
16115 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016116 SDValue N0 = Op.getOperand(0);
16117 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016118 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016119 return false;
16120 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016121 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016122 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016123 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016124 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016125 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016126 }
16127 }
16128
16129 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016130 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016131}
16132
Evan Cheng60c07e12006-07-05 22:17:51 +000016133//===----------------------------------------------------------------------===//
16134// X86 Inline Assembly Support
16135//===----------------------------------------------------------------------===//
16136
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016137namespace {
16138 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016139 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016140 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016141
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016142 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016143 StringRef piece(*args[i]);
16144 if (!s.startswith(piece)) // Check if the piece matches.
16145 return false;
16146
16147 s = s.substr(piece.size());
16148 StringRef::size_type pos = s.find_first_not_of(" \t");
16149 if (pos == 0) // We matched a prefix.
16150 return false;
16151
16152 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016153 }
16154
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016155 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016156 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016157 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016158}
16159
Chris Lattnerb8105652009-07-20 17:51:36 +000016160bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16161 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016162
16163 std::string AsmStr = IA->getAsmString();
16164
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016165 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16166 if (!Ty || Ty->getBitWidth() % 16 != 0)
16167 return false;
16168
Chris Lattnerb8105652009-07-20 17:51:36 +000016169 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016170 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016171 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016172
16173 switch (AsmPieces.size()) {
16174 default: return false;
16175 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016176 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016177 // we will turn this bswap into something that will be lowered to logical
16178 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16179 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016180 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016181 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16182 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16183 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16184 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16185 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16186 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016187 // No need to check constraints, nothing other than the equivalent of
16188 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016189 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016190 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016191
Chris Lattnerb8105652009-07-20 17:51:36 +000016192 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016193 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016194 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016195 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16196 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016197 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016198 const std::string &ConstraintsStr = IA->getConstraintString();
16199 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016200 std::sort(AsmPieces.begin(), AsmPieces.end());
16201 if (AsmPieces.size() == 4 &&
16202 AsmPieces[0] == "~{cc}" &&
16203 AsmPieces[1] == "~{dirflag}" &&
16204 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016205 AsmPieces[3] == "~{fpsr}")
16206 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016207 }
16208 break;
16209 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016210 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016211 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016212 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16213 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16214 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016215 AsmPieces.clear();
16216 const std::string &ConstraintsStr = IA->getConstraintString();
16217 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16218 std::sort(AsmPieces.begin(), AsmPieces.end());
16219 if (AsmPieces.size() == 4 &&
16220 AsmPieces[0] == "~{cc}" &&
16221 AsmPieces[1] == "~{dirflag}" &&
16222 AsmPieces[2] == "~{flags}" &&
16223 AsmPieces[3] == "~{fpsr}")
16224 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016225 }
Evan Cheng55d42002011-01-08 01:24:27 +000016226
16227 if (CI->getType()->isIntegerTy(64)) {
16228 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16229 if (Constraints.size() >= 2 &&
16230 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16231 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16232 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016233 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16234 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16235 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016236 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016237 }
16238 }
16239 break;
16240 }
16241 return false;
16242}
16243
16244
16245
Chris Lattnerf4dff842006-07-11 02:54:03 +000016246/// getConstraintType - Given a constraint letter, return the type of
16247/// constraint it is for this target.
16248X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016249X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16250 if (Constraint.size() == 1) {
16251 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016252 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016253 case 'q':
16254 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016255 case 'f':
16256 case 't':
16257 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016258 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016259 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016260 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016261 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016262 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016263 case 'a':
16264 case 'b':
16265 case 'c':
16266 case 'd':
16267 case 'S':
16268 case 'D':
16269 case 'A':
16270 return C_Register;
16271 case 'I':
16272 case 'J':
16273 case 'K':
16274 case 'L':
16275 case 'M':
16276 case 'N':
16277 case 'G':
16278 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016279 case 'e':
16280 case 'Z':
16281 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016282 default:
16283 break;
16284 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016285 }
Chris Lattner4234f572007-03-25 02:14:49 +000016286 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016287}
16288
John Thompson44ab89e2010-10-29 17:29:13 +000016289/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016290/// This object must already have been set up with the operand type
16291/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016292TargetLowering::ConstraintWeight
16293 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016294 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016295 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016296 Value *CallOperandVal = info.CallOperandVal;
16297 // If we don't have a value, we can't do a match,
16298 // but allow it at the lowest weight.
16299 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016300 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016301 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016302 // Look at the constraint type.
16303 switch (*constraint) {
16304 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016305 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16306 case 'R':
16307 case 'q':
16308 case 'Q':
16309 case 'a':
16310 case 'b':
16311 case 'c':
16312 case 'd':
16313 case 'S':
16314 case 'D':
16315 case 'A':
16316 if (CallOperandVal->getType()->isIntegerTy())
16317 weight = CW_SpecificReg;
16318 break;
16319 case 'f':
16320 case 't':
16321 case 'u':
16322 if (type->isFloatingPointTy())
16323 weight = CW_SpecificReg;
16324 break;
16325 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016326 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016327 weight = CW_SpecificReg;
16328 break;
16329 case 'x':
16330 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016331 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016332 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016333 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016334 break;
16335 case 'I':
16336 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16337 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016338 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016339 }
16340 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016341 case 'J':
16342 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16343 if (C->getZExtValue() <= 63)
16344 weight = CW_Constant;
16345 }
16346 break;
16347 case 'K':
16348 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16349 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16350 weight = CW_Constant;
16351 }
16352 break;
16353 case 'L':
16354 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16355 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16356 weight = CW_Constant;
16357 }
16358 break;
16359 case 'M':
16360 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16361 if (C->getZExtValue() <= 3)
16362 weight = CW_Constant;
16363 }
16364 break;
16365 case 'N':
16366 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16367 if (C->getZExtValue() <= 0xff)
16368 weight = CW_Constant;
16369 }
16370 break;
16371 case 'G':
16372 case 'C':
16373 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16374 weight = CW_Constant;
16375 }
16376 break;
16377 case 'e':
16378 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16379 if ((C->getSExtValue() >= -0x80000000LL) &&
16380 (C->getSExtValue() <= 0x7fffffffLL))
16381 weight = CW_Constant;
16382 }
16383 break;
16384 case 'Z':
16385 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16386 if (C->getZExtValue() <= 0xffffffff)
16387 weight = CW_Constant;
16388 }
16389 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016390 }
16391 return weight;
16392}
16393
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016394/// LowerXConstraint - try to replace an X constraint, which matches anything,
16395/// with another that has more specific requirements based on the type of the
16396/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016397const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016398LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016399 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16400 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016401 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016402 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016403 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016404 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016405 return "x";
16406 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016407
Chris Lattner5e764232008-04-26 23:02:14 +000016408 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016409}
16410
Chris Lattner48884cd2007-08-25 00:47:38 +000016411/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16412/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016413void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016414 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016415 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016416 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016417 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016418
Eric Christopher100c8332011-06-02 23:16:42 +000016419 // Only support length 1 constraints for now.
16420 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016421
Eric Christopher100c8332011-06-02 23:16:42 +000016422 char ConstraintLetter = Constraint[0];
16423 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016424 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016425 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016427 if (C->getZExtValue() <= 31) {
16428 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016429 break;
16430 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016431 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016432 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016433 case 'J':
16434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016435 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016436 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16437 break;
16438 }
16439 }
16440 return;
16441 case 'K':
16442 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016443 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016444 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16445 break;
16446 }
16447 }
16448 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016449 case 'N':
16450 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016451 if (C->getZExtValue() <= 255) {
16452 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016453 break;
16454 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016455 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016456 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016457 case 'e': {
16458 // 32-bit signed value
16459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016460 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16461 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016462 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016463 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016464 break;
16465 }
16466 // FIXME gcc accepts some relocatable values here too, but only in certain
16467 // memory models; it's complicated.
16468 }
16469 return;
16470 }
16471 case 'Z': {
16472 // 32-bit unsigned value
16473 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016474 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16475 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016476 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16477 break;
16478 }
16479 }
16480 // FIXME gcc accepts some relocatable values here too, but only in certain
16481 // memory models; it's complicated.
16482 return;
16483 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016484 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016485 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016486 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016487 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016488 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016489 break;
16490 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016491
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016492 // In any sort of PIC mode addresses need to be computed at runtime by
16493 // adding in a register or some sort of table lookup. These can't
16494 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016495 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016496 return;
16497
Chris Lattnerdc43a882007-05-03 16:52:29 +000016498 // If we are in non-pic codegen mode, we allow the address of a global (with
16499 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016500 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016501 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016502
Chris Lattner49921962009-05-08 18:23:14 +000016503 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16504 while (1) {
16505 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16506 Offset += GA->getOffset();
16507 break;
16508 } else if (Op.getOpcode() == ISD::ADD) {
16509 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16510 Offset += C->getZExtValue();
16511 Op = Op.getOperand(0);
16512 continue;
16513 }
16514 } else if (Op.getOpcode() == ISD::SUB) {
16515 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16516 Offset += -C->getZExtValue();
16517 Op = Op.getOperand(0);
16518 continue;
16519 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016520 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016521
Chris Lattner49921962009-05-08 18:23:14 +000016522 // Otherwise, this isn't something we can handle, reject it.
16523 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016524 }
Eric Christopherfd179292009-08-27 18:07:15 +000016525
Dan Gohman46510a72010-04-15 01:51:59 +000016526 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016527 // If we require an extra load to get this address, as in PIC mode, we
16528 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016529 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16530 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016531 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016532
Devang Patel0d881da2010-07-06 22:08:15 +000016533 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16534 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016535 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016536 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016537 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016538
Gabor Greifba36cb52008-08-28 21:40:38 +000016539 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016540 Ops.push_back(Result);
16541 return;
16542 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016543 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016544}
16545
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016546std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016547X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016548 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016549 // First, see if this is a constraint that directly corresponds to an LLVM
16550 // register class.
16551 if (Constraint.size() == 1) {
16552 // GCC Constraint Letters
16553 switch (Constraint[0]) {
16554 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016555 // TODO: Slight differences here in allocation order and leaving
16556 // RIP in the class. Do they matter any more here than they do
16557 // in the normal allocation?
16558 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16559 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016560 if (VT == MVT::i32 || VT == MVT::f32)
16561 return std::make_pair(0U, &X86::GR32RegClass);
16562 if (VT == MVT::i16)
16563 return std::make_pair(0U, &X86::GR16RegClass);
16564 if (VT == MVT::i8 || VT == MVT::i1)
16565 return std::make_pair(0U, &X86::GR8RegClass);
16566 if (VT == MVT::i64 || VT == MVT::f64)
16567 return std::make_pair(0U, &X86::GR64RegClass);
16568 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016569 }
16570 // 32-bit fallthrough
16571 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016572 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016573 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16574 if (VT == MVT::i16)
16575 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16576 if (VT == MVT::i8 || VT == MVT::i1)
16577 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16578 if (VT == MVT::i64)
16579 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016580 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016581 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016582 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016583 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016584 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016585 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016586 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016587 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016588 return std::make_pair(0U, &X86::GR32RegClass);
16589 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016590 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016591 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016592 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016593 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016594 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016595 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016596 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16597 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016598 case 'f': // FP Stack registers.
16599 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16600 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016601 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016602 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016603 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016604 return std::make_pair(0U, &X86::RFP64RegClass);
16605 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016606 case 'y': // MMX_REGS if MMX allowed.
16607 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016608 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016609 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016610 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016611 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016612 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016613 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016614
Owen Anderson825b72b2009-08-11 20:47:22 +000016615 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016616 default: break;
16617 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016618 case MVT::f32:
16619 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016620 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016621 case MVT::f64:
16622 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016623 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016624 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016625 case MVT::v16i8:
16626 case MVT::v8i16:
16627 case MVT::v4i32:
16628 case MVT::v2i64:
16629 case MVT::v4f32:
16630 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016631 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016632 // AVX types.
16633 case MVT::v32i8:
16634 case MVT::v16i16:
16635 case MVT::v8i32:
16636 case MVT::v4i64:
16637 case MVT::v8f32:
16638 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016639 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016640 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016641 break;
16642 }
16643 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016644
Chris Lattnerf76d1802006-07-31 23:26:50 +000016645 // Use the default implementation in TargetLowering to convert the register
16646 // constraint into a member of a register class.
16647 std::pair<unsigned, const TargetRegisterClass*> Res;
16648 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016649
16650 // Not found as a standard register?
16651 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016652 // Map st(0) -> st(7) -> ST0
16653 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16654 tolower(Constraint[1]) == 's' &&
16655 tolower(Constraint[2]) == 't' &&
16656 Constraint[3] == '(' &&
16657 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16658 Constraint[5] == ')' &&
16659 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016660
Chris Lattner56d77c72009-09-13 22:41:48 +000016661 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016662 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016663 return Res;
16664 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016665
Chris Lattner56d77c72009-09-13 22:41:48 +000016666 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016667 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016668 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016669 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016670 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016671 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016672
16673 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016674 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016675 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016676 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016677 return Res;
16678 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016679
Dale Johannesen330169f2008-11-13 21:52:36 +000016680 // 'A' means EAX + EDX.
16681 if (Constraint == "A") {
16682 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016683 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016684 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016685 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016686 return Res;
16687 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016688
Chris Lattnerf76d1802006-07-31 23:26:50 +000016689 // Otherwise, check to see if this is a register class of the wrong value
16690 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16691 // turn into {ax},{dx}.
16692 if (Res.second->hasType(VT))
16693 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016694
Chris Lattnerf76d1802006-07-31 23:26:50 +000016695 // All of the single-register GCC register classes map their values onto
16696 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16697 // really want an 8-bit or 32-bit register, map to the appropriate register
16698 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016699 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016700 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016701 unsigned DestReg = 0;
16702 switch (Res.first) {
16703 default: break;
16704 case X86::AX: DestReg = X86::AL; break;
16705 case X86::DX: DestReg = X86::DL; break;
16706 case X86::CX: DestReg = X86::CL; break;
16707 case X86::BX: DestReg = X86::BL; break;
16708 }
16709 if (DestReg) {
16710 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016711 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016712 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016713 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016714 unsigned DestReg = 0;
16715 switch (Res.first) {
16716 default: break;
16717 case X86::AX: DestReg = X86::EAX; break;
16718 case X86::DX: DestReg = X86::EDX; break;
16719 case X86::CX: DestReg = X86::ECX; break;
16720 case X86::BX: DestReg = X86::EBX; break;
16721 case X86::SI: DestReg = X86::ESI; break;
16722 case X86::DI: DestReg = X86::EDI; break;
16723 case X86::BP: DestReg = X86::EBP; break;
16724 case X86::SP: DestReg = X86::ESP; break;
16725 }
16726 if (DestReg) {
16727 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016728 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016729 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016730 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016731 unsigned DestReg = 0;
16732 switch (Res.first) {
16733 default: break;
16734 case X86::AX: DestReg = X86::RAX; break;
16735 case X86::DX: DestReg = X86::RDX; break;
16736 case X86::CX: DestReg = X86::RCX; break;
16737 case X86::BX: DestReg = X86::RBX; break;
16738 case X86::SI: DestReg = X86::RSI; break;
16739 case X86::DI: DestReg = X86::RDI; break;
16740 case X86::BP: DestReg = X86::RBP; break;
16741 case X86::SP: DestReg = X86::RSP; break;
16742 }
16743 if (DestReg) {
16744 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016745 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016746 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016747 }
Craig Topperc9099502012-04-20 06:31:50 +000016748 } else if (Res.second == &X86::FR32RegClass ||
16749 Res.second == &X86::FR64RegClass ||
16750 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016751 // Handle references to XMM physical registers that got mapped into the
16752 // wrong class. This can happen with constraints like {xmm0} where the
16753 // target independent register mapper will just pick the first match it can
16754 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016755
16756 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016757 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016758 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016759 Res.second = &X86::FR64RegClass;
16760 else if (X86::VR128RegClass.hasType(VT))
16761 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016762 else if (X86::VR256RegClass.hasType(VT))
16763 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016764 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016765
Chris Lattnerf76d1802006-07-31 23:26:50 +000016766 return Res;
16767}