blob: ec9f6751e1efb086195106f93799625e008d1baa [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000019#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/VariadicFunction.h"
Evan Cheng55d42002011-01-08 01:24:27 +000026#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Function.h"
37#include "llvm/IR/GlobalAlias.h"
38#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/LLVMContext.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000042#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000044#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/MC/MCSymbol.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000161
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000162 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000163 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000164
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000166 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
168 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000169 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000170 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
171 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000172
Eric Christopherde5e1012011-03-11 01:05:58 +0000173 // For 64-bit since we have so many registers use the ILP scheduler, for
174 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000175 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000176 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000177 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000178 else if (Subtarget->is64Bit())
179 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000180 else
181 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000182 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000183
Preston Gurd2e2efd92012-09-04 18:22:17 +0000184 // Bypass i32 with i8 on Atom when compiling with O2
185 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
Preston Gurd8d662b52012-10-04 21:33:40 +0000186 addBypassSlowDiv(32, 8);
Preston Gurd2e2efd92012-09-04 18:22:17 +0000187
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000188 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000189 // Setup Windows compiler runtime calls.
190 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000191 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000192 setLibcallName(RTLIB::SREM_I64, "_allrem");
193 setLibcallName(RTLIB::UREM_I64, "_aullrem");
194 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000196 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000197 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
198 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000200
201 // The _ftol2 runtime function has an unusual calling conv, which
202 // is modeled by a special pseudo-instruction.
203 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
204 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
206 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000207 }
208
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000209 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000210 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 setUseUnderscoreSetJmp(false);
212 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000213 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 // MS runtime is weird: it exports _setjmp, but longjmp!
215 setUseUnderscoreSetJmp(true);
216 setUseUnderscoreLongJmp(false);
217 } else {
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(true);
220 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000223 addRegisterClass(MVT::i8, &X86::GR8RegClass);
224 addRegisterClass(MVT::i16, &X86::GR16RegClass);
225 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000227 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000230
Scott Michelfdc40a02009-02-17 22:15:04 +0000231 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000233 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000235 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
237 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000238
239 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
243 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000246
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
248 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
250 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000252
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000256 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000257 // We have an algorithm for SSE2->double, and we turn this into a
258 // 64-bit FILD followed by conditional FADD for other targets.
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 // We have an algorithm for SSE2, and we turn this into a 64-bit
261 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000262 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
265 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
266 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
268 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000269
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000270 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000271 // SSE has no i16 to fp conversion, only i32
272 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000283 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284
Dale Johannesen73328d12007-09-19 23:55:34 +0000285 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
286 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
288 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000289
Evan Cheng02568ff2006-01-30 22:13:22 +0000290 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
291 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
293 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000294
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000295 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000297 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000299 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000302 }
303
304 // Handle FP_TO_UINT by promoting the destination to a larger signed
305 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
307 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000309
Evan Cheng25ab6902006-09-08 06:48:29 +0000310 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000313 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000314 // Since AVX is a superset of SSE3, only check for SSE here.
315 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 // Expand FP_TO_UINT into a select.
317 // FIXME: We would like to use a Custom expander here eventually to do
318 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000321 // With SSE3 we can use fisttpll to convert to a signed i64; without
322 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000325
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000326 if (isTargetFTOL()) {
327 // Use the _ftol2 runtime function, which has a pseudo-instruction
328 // to handle its weird calling convention.
329 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
330 }
331
Chris Lattner399610a2006-12-05 18:22:22 +0000332 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000333 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
335 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000336 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000338 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000339 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000340 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000341 }
Chris Lattner21f66852005-12-23 05:15:23 +0000342
Dan Gohmanb00ee212008-02-18 19:34:53 +0000343 // Scalar integer divide and remainder are lowered to use operations that
344 // produce two results, to match the available instructions. This exposes
345 // the two-result form to trivial CSE, which is able to combine x/y and x%y
346 // into a single instruction.
347 //
348 // Scalar integer multiply-high is also lowered to use two-result
349 // operations, to match the available instructions. However, plain multiply
350 // (low) operations are left as Legal, as there are single-result
351 // instructions for this in x86. Using the two-result multiply instructions
352 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000353 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000354 MVT VT = IntVTs[i];
355 setOperationAction(ISD::MULHS, VT, Expand);
356 setOperationAction(ISD::MULHU, VT, Expand);
357 setOperationAction(ISD::SDIV, VT, Expand);
358 setOperationAction(ISD::UDIV, VT, Expand);
359 setOperationAction(ISD::SREM, VT, Expand);
360 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000361
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000362 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000363 setOperationAction(ISD::ADDC, VT, Custom);
364 setOperationAction(ISD::ADDE, VT, Custom);
365 setOperationAction(ISD::SUBC, VT, Custom);
366 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000368
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
370 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
371 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
372 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
378 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
379 setOperationAction(ISD::FREM , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f64 , Expand);
381 setOperationAction(ISD::FREM , MVT::f80 , Expand);
382 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000383
Chandler Carruth77821022011-12-24 12:12:34 +0000384 // Promote the i8 variants and force them on up to i32 which has a shorter
385 // encoding.
386 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
387 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
388 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
389 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000390 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000391 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
393 if (Subtarget->is64Bit())
394 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000395 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000396 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
400 }
Craig Topper37f21672011-10-11 06:44:02 +0000401
402 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000403 // When promoting the i8 variants, force them to i32 for a shorter
404 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000405 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000406 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
408 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
411 if (Subtarget->is64Bit())
412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000413 } else {
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000417 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
420 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000422 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
423 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
425
Benjamin Kramer1292c222010-12-04 20:32:23 +0000426 if (Subtarget->hasPOPCNT()) {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
428 } else {
429 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
432 if (Subtarget->is64Bit())
433 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
434 }
435
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
437 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000438
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000439 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000441 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000442 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000443 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
448 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000449 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000454 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000456 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000457 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000459 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
460 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000461 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000462 // other SjLj exception interfaces are implemented and please don't build
463 // your own exception handling based on them.
464 // LLVM/Clang supports zero-cost DWARF exception handling.
465 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
466 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000467
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000468 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
472 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000473 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
475 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000476 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000477 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
479 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
480 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
481 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000482 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000483 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000484 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
486 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
487 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000488 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
490 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
491 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000492 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493
Craig Topper1accb7e2012-01-10 06:54:16 +0000494 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000496
Eric Christopher9a9d2752010-07-22 02:48:34 +0000497 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000498 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000499
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000500 // On X86 and X86-64, atomic operations are lowered to locked instructions.
501 // Locked instructions, in turn, have implicit fence semantics (all memory
502 // operations are flushed before issuing the locked instruction, and they
503 // are not buffered), so we can fold away the common pattern of
504 // fence-atomic-fence.
505 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000506
Mon P Wang63307c32008-05-05 19:05:59 +0000507 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000508 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000509 MVT VT = IntVTs[i];
510 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000512 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000513 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000514
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000515 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000516 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000524 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
527 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000528 }
529
Eli Friedman43f51ae2011-08-26 21:21:21 +0000530 if (Subtarget->hasCmpxchg16b()) {
531 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
532 }
533
Evan Cheng3c992d22006-03-07 02:02:57 +0000534 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000535 if (!Subtarget->isTargetDarwin() &&
536 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000537 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000539 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
542 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
543 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
544 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000545 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000546 setExceptionPointerRegister(X86::RAX);
547 setExceptionSelectorRegister(X86::RDX);
548 } else {
549 setExceptionPointerRegister(X86::EAX);
550 setExceptionSelectorRegister(X86::EDX);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
553 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000554
Duncan Sands4a544a72011-09-06 13:37:06 +0000555 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
556 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000557
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000559 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000560
Nate Begemanacc398c2006-01-25 18:21:52 +0000561 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::VASTART , MVT::Other, Custom);
563 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000564 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VAARG , MVT::Other, Custom);
566 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000567 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VAARG , MVT::Other, Expand);
569 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000570 }
Evan Chengae642192007-03-02 23:16:35 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
573 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000574
575 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
576 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
577 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000578 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000579 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
580 MVT::i64 : MVT::i32, Custom);
581 else
582 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
583 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000584
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000585 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000587 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000588 addRegisterClass(MVT::f32, &X86::FR32RegClass);
589 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590
Evan Cheng223547a2006-01-31 22:28:30 +0000591 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FABS , MVT::f64, Custom);
593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f64, Custom);
597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000598
Evan Cheng68c47cb2007-01-05 07:55:56 +0000599 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
601 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000602
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000603 // Lower this to FGETSIGNx86 plus an AND.
604 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
605 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
606
Evan Chengd25e9e82006-02-02 00:28:23 +0000607 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612
Chris Lattnera54aa942006-01-29 06:26:08 +0000613 // Expand FP immediates into loads from the stack, except for the special
614 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 addLegalFPImmediate(APFloat(+0.0)); // xorpd
616 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000617 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 // Use SSE for f32, x87 for f64.
619 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000620 addRegisterClass(MVT::f32, &X86::FR32RegClass);
621 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625
626 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
631 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634
635 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f32, Expand);
637 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
Nate Begemane1795842008-02-14 08:57:00 +0000639 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000640 addLegalFPImmediate(APFloat(+0.0f)); // xorps
641 addLegalFPImmediate(APFloat(+0.0)); // FLD0
642 addLegalFPImmediate(APFloat(+1.0)); // FLD1
643 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
644 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
645
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000646 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
648 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000650 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000653 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
654 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
657 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000662 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000664 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000666 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000667 addLegalFPImmediate(APFloat(+0.0)); // FLD0
668 addLegalFPImmediate(APFloat(+1.0)); // FLD1
669 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
670 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000671 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
672 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
673 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
674 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Cameron Zwarich33390842011-07-08 21:39:21 +0000677 // We don't support FMA.
678 setOperationAction(ISD::FMA, MVT::f64, Expand);
679 setOperationAction(ISD::FMA, MVT::f32, Expand);
680
Dale Johannesen59a58732007-08-05 18:49:15 +0000681 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000682 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000683 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
685 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000687 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000688 addLegalFPImmediate(TmpFlt); // FLD0
689 TmpFlt.changeSign();
690 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000691
692 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000693 APFloat TmpFlt2(+1.0);
694 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
695 &ignored);
696 addLegalFPImmediate(TmpFlt2); // FLD1
697 TmpFlt2.changeSign();
698 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
699 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000700
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000701 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
703 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000704 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000705
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000706 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
707 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
708 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
709 setOperationAction(ISD::FRINT, MVT::f80, Expand);
710 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000711 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000712 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000713
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000714 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
716 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000718
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FLOG, MVT::f80, Expand);
720 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
722 setOperationAction(ISD::FEXP, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000724
Mon P Wangf007a8b2008-11-06 05:31:54 +0000725 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000726 // (for widening) or expand (for scalarization). Then we will selectively
727 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000728 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
729 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000730 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000731 setOperationAction(ISD::ADD , VT, Expand);
732 setOperationAction(ISD::SUB , VT, Expand);
733 setOperationAction(ISD::FADD, VT, Expand);
734 setOperationAction(ISD::FNEG, VT, Expand);
735 setOperationAction(ISD::FSUB, VT, Expand);
736 setOperationAction(ISD::MUL , VT, Expand);
737 setOperationAction(ISD::FMUL, VT, Expand);
738 setOperationAction(ISD::SDIV, VT, Expand);
739 setOperationAction(ISD::UDIV, VT, Expand);
740 setOperationAction(ISD::FDIV, VT, Expand);
741 setOperationAction(ISD::SREM, VT, Expand);
742 setOperationAction(ISD::UREM, VT, Expand);
743 setOperationAction(ISD::LOAD, VT, Expand);
744 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
746 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
747 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
748 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
749 setOperationAction(ISD::FABS, VT, Expand);
750 setOperationAction(ISD::FSIN, VT, Expand);
751 setOperationAction(ISD::FCOS, VT, Expand);
752 setOperationAction(ISD::FREM, VT, Expand);
753 setOperationAction(ISD::FMA, VT, Expand);
754 setOperationAction(ISD::FPOWI, VT, Expand);
755 setOperationAction(ISD::FSQRT, VT, Expand);
756 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
757 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000758 setOperationAction(ISD::FCEIL, VT, Expand);
759 setOperationAction(ISD::FTRUNC, VT, Expand);
760 setOperationAction(ISD::FRINT, VT, Expand);
761 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000762 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
763 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
764 setOperationAction(ISD::SDIVREM, VT, Expand);
765 setOperationAction(ISD::UDIVREM, VT, Expand);
766 setOperationAction(ISD::FPOW, VT, Expand);
767 setOperationAction(ISD::CTPOP, VT, Expand);
768 setOperationAction(ISD::CTTZ, VT, Expand);
769 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
770 setOperationAction(ISD::CTLZ, VT, Expand);
771 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
772 setOperationAction(ISD::SHL, VT, Expand);
773 setOperationAction(ISD::SRA, VT, Expand);
774 setOperationAction(ISD::SRL, VT, Expand);
775 setOperationAction(ISD::ROTL, VT, Expand);
776 setOperationAction(ISD::ROTR, VT, Expand);
777 setOperationAction(ISD::BSWAP, VT, Expand);
778 setOperationAction(ISD::SETCC, VT, Expand);
779 setOperationAction(ISD::FLOG, VT, Expand);
780 setOperationAction(ISD::FLOG2, VT, Expand);
781 setOperationAction(ISD::FLOG10, VT, Expand);
782 setOperationAction(ISD::FEXP, VT, Expand);
783 setOperationAction(ISD::FEXP2, VT, Expand);
784 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
785 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
786 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
787 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
788 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
789 setOperationAction(ISD::TRUNCATE, VT, Expand);
790 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
791 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
792 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
793 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000794 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
795 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000796 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000797 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000798 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
799 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
800 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000801 }
802
Evan Chengc7ce29b2009-02-13 22:36:38 +0000803 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
804 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000805 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000806 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000807 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Dale Johannesen0488fb62010-09-30 23:57:10 +0000810 // MMX-sized vectors (other than x86mmx) are expected to be expanded
811 // into smaller operations.
812 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
813 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
814 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
815 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
816 setOperationAction(ISD::AND, MVT::v8i8, Expand);
817 setOperationAction(ISD::AND, MVT::v4i16, Expand);
818 setOperationAction(ISD::AND, MVT::v2i32, Expand);
819 setOperationAction(ISD::AND, MVT::v1i64, Expand);
820 setOperationAction(ISD::OR, MVT::v8i8, Expand);
821 setOperationAction(ISD::OR, MVT::v4i16, Expand);
822 setOperationAction(ISD::OR, MVT::v2i32, Expand);
823 setOperationAction(ISD::OR, MVT::v1i64, Expand);
824 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
825 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
826 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
827 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
831 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
833 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
834 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
835 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
836 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000837 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
838 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
839 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
840 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000841
Craig Topper1accb7e2012-01-10 06:54:16 +0000842 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
846 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
847 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
848 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
849 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
850 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000851 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
853 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
854 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
855 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
856 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000857 }
858
Craig Topper1accb7e2012-01-10 06:54:16 +0000859 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000860 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000861
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
863 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000864 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
865 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
866 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
867 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000868
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
870 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
871 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
872 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000873 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
875 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
876 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
877 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
878 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
879 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
880 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
881 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
882 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
883 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
884 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
885 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000886 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000887
Nadav Rotem354efd82011-09-18 14:57:03 +0000888 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000889 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
890 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
891 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
894 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000900 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000901 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000902 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000904 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000905 // Do not attempt to custom lower non-128-bit vectors
906 if (!VT.is128BitVector())
907 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000908 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
910 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000911 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
914 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
915 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
916 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
917 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
918 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000919
Nate Begemancdd1eec2008-02-12 22:51:28 +0000920 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000923 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000925 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000926 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000927 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000928
929 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000930 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000931 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000932
Craig Topper0d1f1762012-08-12 00:34:56 +0000933 setOperationAction(ISD::AND, VT, Promote);
934 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
935 setOperationAction(ISD::OR, VT, Promote);
936 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
937 setOperationAction(ISD::XOR, VT, Promote);
938 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
939 setOperationAction(ISD::LOAD, VT, Promote);
940 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
941 setOperationAction(ISD::SELECT, VT, Promote);
942 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000943 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000944
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000946
Evan Cheng2c3ae372006-04-12 21:21:57 +0000947 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
949 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
950 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
951 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
954 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000955
Michael Liaoa7554632012-10-23 17:36:08 +0000956 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
957 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000958 // As there is no 64-bit GPR available, we need build a special custom
959 // sequence to convert from v2i32 to v2f32.
960 if (!Subtarget->is64Bit())
961 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000962
Michael Liao9d796db2012-10-10 16:32:15 +0000963 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000964 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000965
Michael Liaob8150d82012-09-10 18:33:51 +0000966 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000967 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000968
Craig Topperd0a31172012-01-10 06:37:29 +0000969 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000970 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
971 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
972 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
973 setOperationAction(ISD::FRINT, MVT::f32, Legal);
974 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
975 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
976 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
977 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
978 setOperationAction(ISD::FRINT, MVT::f64, Legal);
979 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
980
Craig Topper12fb5c62012-09-08 17:42:27 +0000981 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000982 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
983 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
984 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
985 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000986 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000987 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
988 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
989 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
990 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000991
Nate Begeman14d12ca2008-02-11 04:19:36 +0000992 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000994
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000995 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
996 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
997 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
998 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
999 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001000
Nate Begeman14d12ca2008-02-11 04:19:36 +00001001 // i8 and i16 vectors are custom , because the source register and source
1002 // source memory operand types are not the same width. f32 vectors are
1003 // custom since the immediate controlling the insert encodes additional
1004 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1006 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1007 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1008 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001009
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1012 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1013 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001014
Pete Coopera77214a2011-11-14 19:38:42 +00001015 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001016 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001017 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001018 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1019 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001020 }
1021 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001022
Craig Topper1accb7e2012-01-10 06:54:16 +00001023 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001024 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001025 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001026
Nadav Rotem43012222011-05-11 08:12:09 +00001027 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001028 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001029
Nadav Rotem43012222011-05-11 08:12:09 +00001030 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001031 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001032
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001033 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001034 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1035 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1036
1037 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1038 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1039
1040 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1041 } else {
1042 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1043 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1044
1045 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1046 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1047
1048 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1049 }
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001050 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1051 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001052 }
1053
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001054 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001055 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1056 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1057 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1058 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1059 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1060 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001061
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1064 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001065
Owen Anderson825b72b2009-08-11 20:47:22 +00001066 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1067 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1068 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1069 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1070 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001071 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001072 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1073 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1074 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1075 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001077 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001078
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1080 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1081 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1082 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1083 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001084 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001085 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1086 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1087 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1088 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001090 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001091
Michael Liaobedcbd42012-10-16 18:14:11 +00001092 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001093 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001094
1095 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1096
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001097 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1098 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001099 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001100
Michael Liaoa7554632012-10-23 17:36:08 +00001101 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1102 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1103 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1104
Michael Liaob8150d82012-09-10 18:33:51 +00001105 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1106
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001107 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1108 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1109
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001110 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1111 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1112
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001113 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001114 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001115
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001116 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1117
Duncan Sands28b77e92011-09-06 19:07:46 +00001118 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1119 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1120 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1121 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001122
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001123 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1124 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1125 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1126
Craig Topperaaa643c2011-11-09 07:28:55 +00001127 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1128 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1129 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1130 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001131
Nadav Rotem0509db22012-12-28 05:45:24 +00001132 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1133 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1134 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1135 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1136 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1137 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001138
Craig Topperbf404372012-08-31 15:40:30 +00001139 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001140 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1141 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1142 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1143 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1144 setOperationAction(ISD::FMA, MVT::f32, Legal);
1145 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001146 }
Craig Topper880ef452012-08-11 22:34:26 +00001147
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001148 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001149 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1150 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1151 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1152 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001153
Craig Topperaaa643c2011-11-09 07:28:55 +00001154 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1155 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1156 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1157 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001158
Craig Topperaaa643c2011-11-09 07:28:55 +00001159 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1160 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1161 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001162 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001163
1164 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001165
1166 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1167 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1168
1169 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1170 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1171
1172 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001173
1174 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001175 } else {
1176 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1177 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1178 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1179 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1180
1181 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1182 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1183 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1184 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1185
1186 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1187 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1188 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1189 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001190
1191 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1192 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1193
1194 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1195 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1196
1197 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001198 }
Craig Topper13894fa2011-08-24 06:14:18 +00001199
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001200 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001201 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1202 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001203 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001204
1205 // Extract subvector is special because the value type
1206 // (result) is 128-bit but the source is 256-bit wide.
1207 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001208 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001209
1210 // Do not attempt to custom lower other non-256-bit vectors
1211 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001212 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001213
Craig Topper0d1f1762012-08-12 00:34:56 +00001214 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1215 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1216 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1217 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1218 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1219 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1220 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001221 }
1222
David Greene54d8eba2011-01-27 22:38:56 +00001223 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001224 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001225 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001226
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001227 // Do not attempt to promote non-256-bit vectors
1228 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001229 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001230
Craig Topper0d1f1762012-08-12 00:34:56 +00001231 setOperationAction(ISD::AND, VT, Promote);
1232 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1233 setOperationAction(ISD::OR, VT, Promote);
1234 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1235 setOperationAction(ISD::XOR, VT, Promote);
1236 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1237 setOperationAction(ISD::LOAD, VT, Promote);
1238 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1239 setOperationAction(ISD::SELECT, VT, Promote);
1240 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001241 }
David Greene9b9838d2009-06-29 16:47:10 +00001242 }
1243
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001244 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1245 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001246 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1247 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001248 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1249 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001250 }
1251
Evan Cheng6be2c582006-04-05 23:38:46 +00001252 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001254 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001255
Eli Friedman962f5492010-06-02 19:35:46 +00001256 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1257 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001258 //
Eli Friedman962f5492010-06-02 19:35:46 +00001259 // FIXME: We really should do custom legalization for addition and
1260 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1261 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001262 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1263 // Add/Sub/Mul with overflow operations are custom lowered.
1264 MVT VT = IntVTs[i];
1265 setOperationAction(ISD::SADDO, VT, Custom);
1266 setOperationAction(ISD::UADDO, VT, Custom);
1267 setOperationAction(ISD::SSUBO, VT, Custom);
1268 setOperationAction(ISD::USUBO, VT, Custom);
1269 setOperationAction(ISD::SMULO, VT, Custom);
1270 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001271 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001272
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001273 // There are no 8-bit 3-address imul/mul instructions
1274 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1275 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001276
Evan Chengd54f2d52009-03-31 19:38:51 +00001277 if (!Subtarget->is64Bit()) {
1278 // These libcalls are not available in 32-bit.
1279 setLibcallName(RTLIB::SHL_I128, 0);
1280 setLibcallName(RTLIB::SRL_I128, 0);
1281 setLibcallName(RTLIB::SRA_I128, 0);
1282 }
1283
Evan Cheng206ee9d2006-07-07 08:33:52 +00001284 // We have target-specific dag combine patterns for the following nodes:
1285 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001286 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001287 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001288 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001289 setTargetDAGCombine(ISD::SHL);
1290 setTargetDAGCombine(ISD::SRA);
1291 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001292 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001293 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001294 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001295 setTargetDAGCombine(ISD::FADD);
1296 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001297 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001298 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001299 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001300 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001301 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001302 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001303 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001304 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001305 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001306 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001307 if (Subtarget->is64Bit())
1308 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001309 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001310
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001311 computeRegisterProperties();
1312
Evan Cheng05219282011-01-06 06:52:41 +00001313 // On Darwin, -Os means optimize for size without hurting performance,
1314 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001315 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001316 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001317 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001318 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1319 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1320 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001321 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001322 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001323
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001324 // Predictable cmov don't hurt on atom because it's in-order.
1325 predictableSelectIsExpensive = !Subtarget->isAtom();
1326
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001327 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001328}
1329
Duncan Sands28b77e92011-09-06 19:07:46 +00001330EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1331 if (!VT.isVector()) return MVT::i8;
1332 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001333}
1334
Evan Cheng29286502008-01-23 23:17:41 +00001335/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1336/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001337static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001338 if (MaxAlign == 16)
1339 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001340 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001341 if (VTy->getBitWidth() == 128)
1342 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001343 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001344 unsigned EltAlign = 0;
1345 getMaxByValAlign(ATy->getElementType(), EltAlign);
1346 if (EltAlign > MaxAlign)
1347 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001348 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001349 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1350 unsigned EltAlign = 0;
1351 getMaxByValAlign(STy->getElementType(i), EltAlign);
1352 if (EltAlign > MaxAlign)
1353 MaxAlign = EltAlign;
1354 if (MaxAlign == 16)
1355 break;
1356 }
1357 }
Evan Cheng29286502008-01-23 23:17:41 +00001358}
1359
1360/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1361/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001362/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1363/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001364unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001365 if (Subtarget->is64Bit()) {
1366 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001367 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001368 if (TyAlign > 8)
1369 return TyAlign;
1370 return 8;
1371 }
1372
Evan Cheng29286502008-01-23 23:17:41 +00001373 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001374 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001375 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001376 return Align;
1377}
Chris Lattner2b02a442007-02-25 08:29:00 +00001378
Evan Chengf0df0312008-05-15 08:39:06 +00001379/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001380/// and store operations as a result of memset, memcpy, and memmove
1381/// lowering. If DstAlign is zero that means it's safe to destination
1382/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1383/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001384/// probably because the source does not need to be loaded. If 'IsMemset' is
1385/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1386/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1387/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001388/// It returns EVT::Other if the type should be determined using generic
1389/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001390EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001391X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1392 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001393 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001394 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001395 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001396 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001397 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001398 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1399 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001400 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001401 (Subtarget->isUnalignedMemAccessFast() ||
1402 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001403 (SrcAlign == 0 || SrcAlign >= 16)))) {
1404 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001405 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001406 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001407 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001408 return MVT::v8f32;
1409 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001410 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001411 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001412 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001413 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001414 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001415 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001416 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001417 // Do not use f64 to lower memcpy if source is string constant. It's
1418 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001419 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001420 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001421 }
Evan Chengf0df0312008-05-15 08:39:06 +00001422 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 return MVT::i64;
1424 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001425}
1426
Evan Cheng7d342672012-12-12 01:32:07 +00001427bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001428 if (VT == MVT::f32)
1429 return X86ScalarSSEf32;
1430 else if (VT == MVT::f64)
1431 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001432 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001433}
1434
Evan Cheng376642e2012-12-10 23:21:26 +00001435bool
1436X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1437 if (Fast)
1438 *Fast = Subtarget->isUnalignedMemAccessFast();
1439 return true;
1440}
1441
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001442/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1443/// current function. The returned value is a member of the
1444/// MachineJumpTableInfo::JTEntryKind enum.
1445unsigned X86TargetLowering::getJumpTableEncoding() const {
1446 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1447 // symbol.
1448 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1449 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001450 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001451
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001452 // Otherwise, use the normal jump table encoding heuristics.
1453 return TargetLowering::getJumpTableEncoding();
1454}
1455
Chris Lattnerc64daab2010-01-26 05:02:42 +00001456const MCExpr *
1457X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1458 const MachineBasicBlock *MBB,
1459 unsigned uid,MCContext &Ctx) const{
1460 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1461 Subtarget->isPICStyleGOT());
1462 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1463 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001464 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1465 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001466}
1467
Evan Chengcc415862007-11-09 01:32:10 +00001468/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1469/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001470SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001471 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001472 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001473 // This doesn't have DebugLoc associated with it, but is not really the
1474 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001475 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001476 return Table;
1477}
1478
Chris Lattner589c6f62010-01-26 06:28:43 +00001479/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1480/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1481/// MCExpr.
1482const MCExpr *X86TargetLowering::
1483getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1484 MCContext &Ctx) const {
1485 // X86-64 uses RIP relative addressing based on the jump table label.
1486 if (Subtarget->isPICStyleRIPRel())
1487 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1488
1489 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001490 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001491}
1492
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001493// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001494std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001495X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001496 const TargetRegisterClass *RRC = 0;
1497 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001498 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001499 default:
1500 return TargetLowering::findRepresentativeClass(VT);
1501 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001502 RRC = Subtarget->is64Bit() ?
1503 (const TargetRegisterClass*)&X86::GR64RegClass :
1504 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001505 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001506 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001507 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001508 break;
1509 case MVT::f32: case MVT::f64:
1510 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1511 case MVT::v4f32: case MVT::v2f64:
1512 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1513 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001514 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001515 break;
1516 }
1517 return std::make_pair(RRC, Cost);
1518}
1519
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001520bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1521 unsigned &Offset) const {
1522 if (!Subtarget->isTargetLinux())
1523 return false;
1524
1525 if (Subtarget->is64Bit()) {
1526 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1527 Offset = 0x28;
1528 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1529 AddressSpace = 256;
1530 else
1531 AddressSpace = 257;
1532 } else {
1533 // %gs:0x14 on i386
1534 Offset = 0x14;
1535 AddressSpace = 256;
1536 }
1537 return true;
1538}
1539
Chris Lattner2b02a442007-02-25 08:29:00 +00001540//===----------------------------------------------------------------------===//
1541// Return Value Calling Convention Implementation
1542//===----------------------------------------------------------------------===//
1543
Chris Lattner59ed56b2007-02-28 04:55:35 +00001544#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001545
Michael J. Spencerec38de22010-10-10 22:04:20 +00001546bool
Eric Christopher471e4222011-06-08 23:55:35 +00001547X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001548 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001549 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001550 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001551 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001552 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001553 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001554 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001555}
1556
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557SDValue
1558X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001559 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001561 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001562 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001563 MachineFunction &MF = DAG.getMachineFunction();
1564 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattner9774c912007-02-27 05:28:59 +00001566 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001567 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568 RVLocs, *DAG.getContext());
1569 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Evan Chengdcea1632010-02-04 02:40:39 +00001571 // Add the regs to the liveout set for the function.
1572 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1573 for (unsigned i = 0; i != RVLocs.size(); ++i)
1574 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1575 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Dan Gohman475871a2008-07-27 21:46:04 +00001577 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001578
Dan Gohman475871a2008-07-27 21:46:04 +00001579 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001580 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1581 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001582 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1583 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001584
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001585 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001586 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1587 CCValAssign &VA = RVLocs[i];
1588 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001589 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001590 EVT ValVT = ValToCopy.getValueType();
1591
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001592 // Promote values to the appropriate types
1593 if (VA.getLocInfo() == CCValAssign::SExt)
1594 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1595 else if (VA.getLocInfo() == CCValAssign::ZExt)
1596 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1597 else if (VA.getLocInfo() == CCValAssign::AExt)
1598 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1599 else if (VA.getLocInfo() == CCValAssign::BCvt)
1600 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1601
Dale Johannesenc4510512010-09-24 19:05:48 +00001602 // If this is x86-64, and we disabled SSE, we can't return FP values,
1603 // or SSE or MMX vectors.
1604 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1605 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001606 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001607 report_fatal_error("SSE register return with SSE disabled");
1608 }
1609 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1610 // llvm-gcc has never done it right and no one has noticed, so this
1611 // should be OK for now.
1612 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001613 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001614 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001615
Chris Lattner447ff682008-03-11 03:23:40 +00001616 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1617 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001618 if (VA.getLocReg() == X86::ST0 ||
1619 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001620 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1621 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001622 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001623 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001624 RetOps.push_back(ValToCopy);
1625 // Don't emit a copytoreg.
1626 continue;
1627 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001628
Evan Cheng242b38b2009-02-23 09:03:22 +00001629 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1630 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001631 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001632 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001633 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001634 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001635 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1636 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001637 // If we don't have SSE2 available, convert to v4f32 so the generated
1638 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001639 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001640 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001641 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001642 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001643 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001644
Dale Johannesendd64c412009-02-04 00:33:20 +00001645 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001646 Flag = Chain.getValue(1);
1647 }
Dan Gohman61a92132008-04-21 23:59:07 +00001648
1649 // The x86-64 ABI for returning structs by value requires that we copy
1650 // the sret argument into %rax for the return. We saved the argument into
1651 // a virtual register in the entry block, so now we copy the value out
1652 // and into %rax.
1653 if (Subtarget->is64Bit() &&
1654 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1655 MachineFunction &MF = DAG.getMachineFunction();
1656 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1657 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001658 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001659 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001660 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001661
Dale Johannesendd64c412009-02-04 00:33:20 +00001662 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001663 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001664
1665 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001666 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001667 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001668
Chris Lattner447ff682008-03-11 03:23:40 +00001669 RetOps[0] = Chain; // Update chain.
1670
1671 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001672 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001673 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001674
1675 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001677}
1678
Evan Chengbf010eb2012-04-10 01:51:00 +00001679bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001680 if (N->getNumValues() != 1)
1681 return false;
1682 if (!N->hasNUsesOfValue(1, 0))
1683 return false;
1684
Evan Chengbf010eb2012-04-10 01:51:00 +00001685 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001686 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001687 if (Copy->getOpcode() == ISD::CopyToReg) {
1688 // If the copy has a glue operand, we conservatively assume it isn't safe to
1689 // perform a tail call.
1690 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1691 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001692 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001693 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001694 return false;
1695
Evan Cheng1bf891a2010-12-01 22:59:46 +00001696 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001697 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001698 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001699 if (UI->getOpcode() != X86ISD::RET_FLAG)
1700 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001701 HasRet = true;
1702 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001703
Evan Chengbf010eb2012-04-10 01:51:00 +00001704 if (!HasRet)
1705 return false;
1706
1707 Chain = TCChain;
1708 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001709}
1710
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001711MVT
1712X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001713 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001714 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001715 // TODO: Is this also valid on 32-bit?
1716 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001717 ReturnMVT = MVT::i8;
1718 else
1719 ReturnMVT = MVT::i32;
1720
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001721 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001722 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001723}
1724
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725/// LowerCallResult - Lower the result values of a call into the
1726/// appropriate copies out of appropriate physical registers.
1727///
1728SDValue
1729X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001730 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001731 const SmallVectorImpl<ISD::InputArg> &Ins,
1732 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001733 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001734
Chris Lattnere32bbf62007-02-28 07:09:55 +00001735 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001736 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001737 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001738 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001739 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001741
Chris Lattner3085e152007-02-25 08:59:22 +00001742 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001743 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001744 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001745 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001746
Torok Edwin3f142c32009-02-01 18:15:56 +00001747 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001748 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001749 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001750 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001751 }
1752
Evan Cheng79fb3b42009-02-20 20:43:02 +00001753 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001754
1755 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001756 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001757 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001758 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001759 // instead.
1760 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1761 // If we prefer to use the value in xmm registers, copy it out as f80 and
1762 // use a truncate to move it from fp stack reg to xmm reg.
1763 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001764 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001765 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1766 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001767 Val = Chain.getValue(0);
1768
1769 // Round the f80 to the right size, which also moves it to the appropriate
1770 // xmm register.
1771 if (CopyVT != VA.getValVT())
1772 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1773 // This truncation won't change the value.
1774 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001775 } else {
1776 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1777 CopyVT, InFlag).getValue(1);
1778 Val = Chain.getValue(0);
1779 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001780 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001782 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001783
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001785}
1786
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001787//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001788// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001789//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001790// StdCall calling convention seems to be standard for many Windows' API
1791// routines and around. It differs from C calling convention just a little:
1792// callee should clean up the stack, not caller. Symbols should be also
1793// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001794// For info on fast calling convention see Fast Calling Convention (tail call)
1795// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001796
Dan Gohman98ca4f22009-08-05 01:29:28 +00001797/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001798/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001799enum StructReturnType {
1800 NotStructReturn,
1801 RegStructReturn,
1802 StackStructReturn
1803};
1804static StructReturnType
1805callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001807 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001808
Rafael Espindola1cee7102012-07-25 13:41:10 +00001809 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1810 if (!Flags.isSRet())
1811 return NotStructReturn;
1812 if (Flags.isInReg())
1813 return RegStructReturn;
1814 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001815}
1816
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001817/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001818/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001819static StructReturnType
1820argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001821 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001822 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001823
Rafael Espindola1cee7102012-07-25 13:41:10 +00001824 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1825 if (!Flags.isSRet())
1826 return NotStructReturn;
1827 if (Flags.isInReg())
1828 return RegStructReturn;
1829 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001830}
1831
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001832/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1833/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001834/// the specific parameter attribute. The copy will be passed as a byval
1835/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001836static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001837CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001838 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1839 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001840 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001841
Dale Johannesendd64c412009-02-04 00:33:20 +00001842 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001843 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001844 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001845}
1846
Chris Lattner29689432010-03-11 00:22:57 +00001847/// IsTailCallConvention - Return true if the calling convention is one that
1848/// supports tail call optimization.
1849static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001850 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1851 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001852}
1853
Evan Cheng485fafc2011-03-21 01:19:09 +00001854bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001855 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001856 return false;
1857
1858 CallSite CS(CI);
1859 CallingConv::ID CalleeCC = CS.getCallingConv();
1860 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1861 return false;
1862
1863 return true;
1864}
1865
Evan Cheng0c439eb2010-01-27 00:07:07 +00001866/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1867/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001868static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1869 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001870 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001871}
1872
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873SDValue
1874X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001875 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 const SmallVectorImpl<ISD::InputArg> &Ins,
1877 DebugLoc dl, SelectionDAG &DAG,
1878 const CCValAssign &VA,
1879 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001880 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001881 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001882 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001883 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1884 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001885 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001886 EVT ValVT;
1887
1888 // If value is passed by pointer we have address passed instead of the value
1889 // itself.
1890 if (VA.getLocInfo() == CCValAssign::Indirect)
1891 ValVT = VA.getLocVT();
1892 else
1893 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001894
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001895 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001896 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001897 // In case of tail call optimization mark all arguments mutable. Since they
1898 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001899 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001900 unsigned Bytes = Flags.getByValSize();
1901 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1902 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001903 return DAG.getFrameIndex(FI, getPointerTy());
1904 } else {
1905 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001906 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001907 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1908 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001909 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001910 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001911 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001912}
1913
Dan Gohman475871a2008-07-27 21:46:04 +00001914SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001915X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001916 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001917 bool isVarArg,
1918 const SmallVectorImpl<ISD::InputArg> &Ins,
1919 DebugLoc dl,
1920 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001921 SmallVectorImpl<SDValue> &InVals)
1922 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001923 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001924 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001925
Gordon Henriksen86737662008-01-05 16:56:59 +00001926 const Function* Fn = MF.getFunction();
1927 if (Fn->hasExternalLinkage() &&
1928 Subtarget->isTargetCygMing() &&
1929 Fn->getName() == "main")
1930 FuncInfo->setForceFramePointer(true);
1931
Evan Cheng1bc78042006-04-26 01:20:17 +00001932 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001933 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001934 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001935 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001936
Chris Lattner29689432010-03-11 00:22:57 +00001937 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001938 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001939
Chris Lattner638402b2007-02-28 07:00:42 +00001940 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001941 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001942 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001944
1945 // Allocate shadow area for Win64
1946 if (IsWin64) {
1947 CCInfo.AllocateStack(32, 8);
1948 }
1949
Duncan Sands45907662010-10-31 13:21:44 +00001950 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001951
Chris Lattnerf39f7712007-02-28 05:46:49 +00001952 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001953 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001954 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1955 CCValAssign &VA = ArgLocs[i];
1956 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1957 // places.
1958 assert(VA.getValNo() != LastVal &&
1959 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001960 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001961 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001962
Chris Lattnerf39f7712007-02-28 05:46:49 +00001963 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001964 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001965 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001966 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001967 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001968 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001969 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001971 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001972 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001973 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001974 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001975 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001976 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001977 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001978 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001979 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001980 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001981 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001982
Devang Patel68e6bee2011-02-21 23:21:26 +00001983 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Chris Lattnerf39f7712007-02-28 05:46:49 +00001986 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1987 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1988 // right size.
1989 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001990 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001991 DAG.getValueType(VA.getValVT()));
1992 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001993 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001994 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001995 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001996 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001997
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001998 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001999 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002000 if (RegVT.isVector())
2001 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2002 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002003 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002004 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002005 } else {
2006 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002007 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002008 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002009
2010 // If value is passed via pointer - do a load.
2011 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002012 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002013 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002014
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002016 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002017
Dan Gohman61a92132008-04-21 23:59:07 +00002018 // The x86-64 ABI for returning structs by value requires that we copy
2019 // the sret argument into %rax for the return. Save the argument into
2020 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002021 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002022 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2023 unsigned Reg = FuncInfo->getSRetReturnReg();
2024 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002025 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00002026 FuncInfo->setSRetReturnReg(Reg);
2027 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002030 }
2031
Chris Lattnerf39f7712007-02-28 05:46:49 +00002032 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002033 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002034 if (FuncIsMadeTailCallSafe(CallConv,
2035 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002036 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002037
Evan Cheng1bc78042006-04-26 01:20:17 +00002038 // If the function takes variable number of arguments, make a frame index for
2039 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002040 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002041 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2042 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002043 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002044 }
2045 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002046 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2047
2048 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002049 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002050 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002051 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002052 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002053 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2054 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002055 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2057 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2058 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002059 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002060 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002061
2062 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002063 // The XMM registers which might contain var arg parameters are shadowed
2064 // in their paired GPR. So we only need to save the GPR to their home
2065 // slots.
2066 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002067 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002068 } else {
2069 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2070 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002071
Chad Rosier30450e82011-12-22 22:35:21 +00002072 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2073 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002074 }
2075 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2076 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002077
Bill Wendling831737d2012-12-30 10:32:01 +00002078 bool NoImplicitFloatOps = Fn->getAttributes().
2079 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002080 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002081 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002082 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2083 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002084 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002085 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002086 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002087 // Kernel mode asks for SSE to be disabled, so don't push them
2088 // on the stack.
2089 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002090
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002091 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002092 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002093 // Get to the caller-allocated home save location. Add 8 to account
2094 // for the return address.
2095 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002096 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002097 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002098 // Fixup to set vararg frame on shadow area (4 x i64).
2099 if (NumIntRegs < 4)
2100 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002101 } else {
2102 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002103 // registers, then we must store them to their spots on the stack so
2104 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002105 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2106 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2107 FuncInfo->setRegSaveFrameIndex(
2108 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002109 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002110 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002111
Gordon Henriksen86737662008-01-05 16:56:59 +00002112 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002114 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2115 getPointerTy());
2116 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002117 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002118 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2119 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002120 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002121 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002123 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002124 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002125 MachinePointerInfo::getFixedStack(
2126 FuncInfo->getRegSaveFrameIndex(), Offset),
2127 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002128 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002129 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002130 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002131
Dan Gohmanface41a2009-08-16 21:24:25 +00002132 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2133 // Now store the XMM (fp + vector) parameter registers.
2134 SmallVector<SDValue, 11> SaveXMMOps;
2135 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002136
Craig Topperc9099502012-04-20 06:31:50 +00002137 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002138 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2139 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002140
Dan Gohman1e93df62010-04-17 14:41:14 +00002141 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2142 FuncInfo->getRegSaveFrameIndex()));
2143 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2144 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002145
Dan Gohmanface41a2009-08-16 21:24:25 +00002146 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002147 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002148 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002149 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2150 SaveXMMOps.push_back(Val);
2151 }
2152 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2153 MVT::Other,
2154 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002155 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002156
2157 if (!MemOps.empty())
2158 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2159 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002160 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002162
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002164 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2165 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002166 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002167 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002168 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002169 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002170 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002171 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002172 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002173 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002174
Gordon Henriksen86737662008-01-05 16:56:59 +00002175 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002176 // RegSaveFrameIndex is X86-64 only.
2177 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002178 if (CallConv == CallingConv::X86_FastCall ||
2179 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002180 // fastcc functions can't have varargs.
2181 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002182 }
Evan Cheng25caf632006-05-23 21:06:34 +00002183
Rafael Espindola76927d752011-08-30 19:39:58 +00002184 FuncInfo->setArgumentStackSize(StackSize);
2185
Dan Gohman98ca4f22009-08-05 01:29:28 +00002186 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002187}
2188
Dan Gohman475871a2008-07-27 21:46:04 +00002189SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002190X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2191 SDValue StackPtr, SDValue Arg,
2192 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002193 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002194 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002195 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002196 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002197 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002198 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002199 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002200
2201 return DAG.getStore(Chain, dl, Arg, PtrOff,
2202 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002203 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002204}
2205
Bill Wendling64e87322009-01-16 19:25:27 +00002206/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002207/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002208SDValue
2209X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002210 SDValue &OutRetAddr, SDValue Chain,
2211 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002212 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002213 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002214 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002215 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002216
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002217 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002218 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002219 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002220 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002221}
2222
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002223/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002224/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002225static SDValue
2226EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002227 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2228 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002229 // Store the return address to the appropriate stack slot.
2230 if (!FPDiff) return Chain;
2231 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002232 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002233 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002234 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002236 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002237 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238 return Chain;
2239}
2240
Dan Gohman98ca4f22009-08-05 01:29:28 +00002241SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002242X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002243 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002244 SelectionDAG &DAG = CLI.DAG;
2245 DebugLoc &dl = CLI.DL;
2246 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2247 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2248 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2249 SDValue Chain = CLI.Chain;
2250 SDValue Callee = CLI.Callee;
2251 CallingConv::ID CallConv = CLI.CallConv;
2252 bool &isTailCall = CLI.IsTailCall;
2253 bool isVarArg = CLI.IsVarArg;
2254
Dan Gohman98ca4f22009-08-05 01:29:28 +00002255 MachineFunction &MF = DAG.getMachineFunction();
2256 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002257 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002258 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002259 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002260 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002261
Nick Lewycky22de16d2012-01-19 00:34:10 +00002262 if (MF.getTarget().Options.DisableTailCalls)
2263 isTailCall = false;
2264
Evan Cheng5f941932010-02-05 02:21:12 +00002265 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002266 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002267 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002268 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002269 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002270 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002271
2272 // Sibcalls are automatically detected tailcalls which do not require
2273 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002274 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002275 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002276
2277 if (isTailCall)
2278 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002279 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002280
Chris Lattner29689432010-03-11 00:22:57 +00002281 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002282 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002283
Chris Lattner638402b2007-02-28 07:00:42 +00002284 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002285 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002286 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002287 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002288
2289 // Allocate shadow area for Win64
2290 if (IsWin64) {
2291 CCInfo.AllocateStack(32, 8);
2292 }
2293
Duncan Sands45907662010-10-31 13:21:44 +00002294 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002295
Chris Lattner423c5f42007-02-28 05:31:48 +00002296 // Get a count of how many bytes are to be pushed on the stack.
2297 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002298 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002299 // This is a sibcall. The memory operands are available in caller's
2300 // own caller's stack.
2301 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002302 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2303 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002304 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002305
Gordon Henriksen86737662008-01-05 16:56:59 +00002306 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002307 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002308 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002309 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2310 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2311
Gordon Henriksen86737662008-01-05 16:56:59 +00002312 FPDiff = NumBytesCallerPushed - NumBytes;
2313
2314 // Set the delta of movement of the returnaddr stackslot.
2315 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002316 if (FPDiff < X86Info->getTCReturnAddrDelta())
2317 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002318 }
2319
Evan Chengf22f9b32010-02-06 03:28:46 +00002320 if (!IsSibcall)
2321 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002322
Dan Gohman475871a2008-07-27 21:46:04 +00002323 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002324 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002325 if (isTailCall && FPDiff)
2326 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2327 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002328
Dan Gohman475871a2008-07-27 21:46:04 +00002329 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2330 SmallVector<SDValue, 8> MemOpChains;
2331 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002332
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002333 // Walk the register/memloc assignments, inserting copies/loads. In the case
2334 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002335 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2336 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002337 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002338 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002339 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002340 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002341
Chris Lattner423c5f42007-02-28 05:31:48 +00002342 // Promote the value if needed.
2343 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002344 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002345 case CCValAssign::Full: break;
2346 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002347 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002348 break;
2349 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002350 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002351 break;
2352 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002353 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002354 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002355 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2357 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002358 } else
2359 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2360 break;
2361 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002362 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002363 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002364 case CCValAssign::Indirect: {
2365 // Store the argument.
2366 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002367 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002368 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002369 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002370 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002371 Arg = SpillSlot;
2372 break;
2373 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002375
Chris Lattner423c5f42007-02-28 05:31:48 +00002376 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002377 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2378 if (isVarArg && IsWin64) {
2379 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2380 // shadow reg if callee is a varargs function.
2381 unsigned ShadowReg = 0;
2382 switch (VA.getLocReg()) {
2383 case X86::XMM0: ShadowReg = X86::RCX; break;
2384 case X86::XMM1: ShadowReg = X86::RDX; break;
2385 case X86::XMM2: ShadowReg = X86::R8; break;
2386 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002387 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002388 if (ShadowReg)
2389 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002390 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002391 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002392 assert(VA.isMemLoc());
2393 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002394 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2395 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002396 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2397 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002398 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002399 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Evan Cheng32fe1032006-05-25 00:59:30 +00002401 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002402 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002403 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002404
Chris Lattner88e1fd52009-07-09 04:24:46 +00002405 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002406 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2407 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002408 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002409 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2410 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002411 } else {
2412 // If we are tail calling and generating PIC/GOT style code load the
2413 // address of the callee into ECX. The value in ecx is used as target of
2414 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2415 // for tail calls on PIC/GOT architectures. Normally we would just put the
2416 // address of GOT into ebx and then call target@PLT. But for tail calls
2417 // ebx would be restored (since ebx is callee saved) before jumping to the
2418 // target@PLT.
2419
2420 // Note: The actual moving to ECX is done further down.
2421 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2422 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2423 !G->getGlobal()->hasProtectedVisibility())
2424 Callee = LowerGlobalAddress(Callee, DAG);
2425 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002426 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002427 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002428 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002429
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002430 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002431 // From AMD64 ABI document:
2432 // For calls that may call functions that use varargs or stdargs
2433 // (prototype-less calls or calls to functions containing ellipsis (...) in
2434 // the declaration) %al is used as hidden argument to specify the number
2435 // of SSE registers used. The contents of %al do not need to match exactly
2436 // the number of registers, but must be an ubound on the number of SSE
2437 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002438
Gordon Henriksen86737662008-01-05 16:56:59 +00002439 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002440 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002441 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2442 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2443 };
2444 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002445 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002446 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002447
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002448 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2449 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002450 }
2451
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002452 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002453 if (isTailCall) {
2454 // Force all the incoming stack arguments to be loaded from the stack
2455 // before any new outgoing arguments are stored to the stack, because the
2456 // outgoing stack slots may alias the incoming argument stack slots, and
2457 // the alias isn't otherwise explicit. This is slightly more conservative
2458 // than necessary, because it means that each store effectively depends
2459 // on every argument instead of just those arguments it would clobber.
2460 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2461
Dan Gohman475871a2008-07-27 21:46:04 +00002462 SmallVector<SDValue, 8> MemOpChains2;
2463 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002464 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002465 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002466 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2467 CCValAssign &VA = ArgLocs[i];
2468 if (VA.isRegLoc())
2469 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002470 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002471 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002473 // Create frame index.
2474 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002475 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002476 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002477 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002478
Duncan Sands276dcbd2008-03-21 09:14:45 +00002479 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002480 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002481 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002482 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002483 StackPtr = DAG.getCopyFromReg(Chain, dl,
2484 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002485 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002486 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002487
Dan Gohman98ca4f22009-08-05 01:29:28 +00002488 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2489 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002490 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002491 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002492 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002493 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002494 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002495 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002496 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002497 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002498 }
2499 }
2500
2501 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002502 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002503 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002504
2505 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002506 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2507 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002508 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002509 }
2510
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002511 // Build a sequence of copy-to-reg nodes chained together with token chain
2512 // and flag operands which copy the outgoing args into registers.
2513 SDValue InFlag;
2514 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2515 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2516 RegsToPass[i].second, InFlag);
2517 InFlag = Chain.getValue(1);
2518 }
2519
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002520 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2521 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2522 // In the 64-bit large code model, we have to make all calls
2523 // through a register, since the call instruction's 32-bit
2524 // pc-relative offset may not be large enough to hold the whole
2525 // address.
2526 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002527 // If the callee is a GlobalAddress node (quite common, every direct call
2528 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2529 // it.
2530
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002531 // We should use extra load for direct calls to dllimported functions in
2532 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002533 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002534 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002535 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002536 bool ExtraLoad = false;
2537 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002538
Chris Lattner48a7d022009-07-09 05:02:21 +00002539 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2540 // external symbols most go through the PLT in PIC mode. If the symbol
2541 // has hidden or protected visibility, or if it is static or local, then
2542 // we don't need to use the PLT - we can directly call it.
2543 if (Subtarget->isTargetELF() &&
2544 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002545 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002546 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002547 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002548 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002549 (!Subtarget->getTargetTriple().isMacOSX() ||
2550 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002551 // PC-relative references to external symbols should go through $stub,
2552 // unless we're building with the leopard linker or later, which
2553 // automatically synthesizes these stubs.
2554 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002555 } else if (Subtarget->isPICStyleRIPRel() &&
2556 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002557 cast<Function>(GV)->getAttributes().
2558 hasAttribute(AttributeSet::FunctionIndex,
2559 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002560 // If the function is marked as non-lazy, generate an indirect call
2561 // which loads from the GOT directly. This avoids runtime overhead
2562 // at the cost of eager binding (and one extra byte of encoding).
2563 OpFlags = X86II::MO_GOTPCREL;
2564 WrapperKind = X86ISD::WrapperRIP;
2565 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002566 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002567
Devang Patel0d881da2010-07-06 22:08:15 +00002568 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002569 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002570
2571 // Add a wrapper if needed.
2572 if (WrapperKind != ISD::DELETED_NODE)
2573 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2574 // Add extra indirection if needed.
2575 if (ExtraLoad)
2576 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2577 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002578 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002579 }
Bill Wendling056292f2008-09-16 21:48:12 +00002580 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002581 unsigned char OpFlags = 0;
2582
Evan Cheng1bf891a2010-12-01 22:59:46 +00002583 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2584 // external symbols should go through the PLT.
2585 if (Subtarget->isTargetELF() &&
2586 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2587 OpFlags = X86II::MO_PLT;
2588 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002589 (!Subtarget->getTargetTriple().isMacOSX() ||
2590 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002591 // PC-relative references to external symbols should go through $stub,
2592 // unless we're building with the leopard linker or later, which
2593 // automatically synthesizes these stubs.
2594 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002595 }
Eric Christopherfd179292009-08-27 18:07:15 +00002596
Chris Lattner48a7d022009-07-09 05:02:21 +00002597 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2598 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002599 }
2600
Chris Lattnerd96d0722007-02-25 06:40:16 +00002601 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002602 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002603 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002604
Evan Chengf22f9b32010-02-06 03:28:46 +00002605 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002606 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2607 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002608 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002609 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002610
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002611 Ops.push_back(Chain);
2612 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002613
Dan Gohman98ca4f22009-08-05 01:29:28 +00002614 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002615 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002616
Gordon Henriksen86737662008-01-05 16:56:59 +00002617 // Add argument registers to the end of the list so that they are known live
2618 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002619 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2620 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2621 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002622
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002623 // Add a register mask operand representing the call-preserved registers.
2624 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2625 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2626 assert(Mask && "Missing call preserved mask for calling convention");
2627 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002628
Gabor Greifba36cb52008-08-28 21:40:38 +00002629 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002630 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002631
Dan Gohman98ca4f22009-08-05 01:29:28 +00002632 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002633 // We used to do:
2634 //// If this is the first return lowered for this function, add the regs
2635 //// to the liveout set for the function.
2636 // This isn't right, although it's probably harmless on x86; liveouts
2637 // should be computed from returns not tail calls. Consider a void
2638 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002639 return DAG.getNode(X86ISD::TC_RETURN, dl,
2640 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002641 }
2642
Dale Johannesenace16102009-02-03 19:33:06 +00002643 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002644 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002645
Chris Lattner2d297092006-05-23 18:50:38 +00002646 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002647 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002648 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2649 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002650 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002651 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002652 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002653 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002654 // pops the hidden struct pointer, so we have to push it back.
2655 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002656 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002657 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002658 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002659 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002660
Gordon Henriksenae636f82008-01-03 16:47:34 +00002661 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002662 if (!IsSibcall) {
2663 Chain = DAG.getCALLSEQ_END(Chain,
2664 DAG.getIntPtrConstant(NumBytes, true),
2665 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2666 true),
2667 InFlag);
2668 InFlag = Chain.getValue(1);
2669 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002670
Chris Lattner3085e152007-02-25 08:59:22 +00002671 // Handle result values, copying them out of physregs into vregs that we
2672 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002673 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2674 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002675}
2676
Evan Cheng25ab6902006-09-08 06:48:29 +00002677//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002678// Fast Calling Convention (tail call) implementation
2679//===----------------------------------------------------------------------===//
2680
2681// Like std call, callee cleans arguments, convention except that ECX is
2682// reserved for storing the tail called function address. Only 2 registers are
2683// free for argument passing (inreg). Tail call optimization is performed
2684// provided:
2685// * tailcallopt is enabled
2686// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002687// On X86_64 architecture with GOT-style position independent code only local
2688// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002689// To keep the stack aligned according to platform abi the function
2690// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2691// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002692// If a tail called function callee has more arguments than the caller the
2693// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002694// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002695// original REtADDR, but before the saved framepointer or the spilled registers
2696// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2697// stack layout:
2698// arg1
2699// arg2
2700// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002701// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002702// move area ]
2703// (possible EBP)
2704// ESI
2705// EDI
2706// local1 ..
2707
2708/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2709/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002710unsigned
2711X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2712 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002713 MachineFunction &MF = DAG.getMachineFunction();
2714 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002715 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002716 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002717 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002718 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002719 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002720 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2721 // Number smaller than 12 so just add the difference.
2722 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2723 } else {
2724 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002725 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002726 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002727 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002728 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002729}
2730
Evan Cheng5f941932010-02-05 02:21:12 +00002731/// MatchingStackOffset - Return true if the given stack call argument is
2732/// already available in the same position (relatively) of the caller's
2733/// incoming argument stack.
2734static
2735bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2736 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2737 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002738 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2739 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002740 if (Arg.getOpcode() == ISD::CopyFromReg) {
2741 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002742 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002743 return false;
2744 MachineInstr *Def = MRI->getVRegDef(VR);
2745 if (!Def)
2746 return false;
2747 if (!Flags.isByVal()) {
2748 if (!TII->isLoadFromStackSlot(Def, FI))
2749 return false;
2750 } else {
2751 unsigned Opcode = Def->getOpcode();
2752 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2753 Def->getOperand(1).isFI()) {
2754 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002755 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002756 } else
2757 return false;
2758 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002759 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2760 if (Flags.isByVal())
2761 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002762 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002763 // define @foo(%struct.X* %A) {
2764 // tail call @bar(%struct.X* byval %A)
2765 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002766 return false;
2767 SDValue Ptr = Ld->getBasePtr();
2768 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2769 if (!FINode)
2770 return false;
2771 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002772 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002773 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002774 FI = FINode->getIndex();
2775 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002776 } else
2777 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002778
Evan Cheng4cae1332010-03-05 08:38:04 +00002779 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002780 if (!MFI->isFixedObjectIndex(FI))
2781 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002782 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002783}
2784
Dan Gohman98ca4f22009-08-05 01:29:28 +00002785/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2786/// for tail call optimization. Targets which want to do tail call
2787/// optimization should implement this function.
2788bool
2789X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002790 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002791 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002792 bool isCalleeStructRet,
2793 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002794 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002795 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002796 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002797 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002798 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002799 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002800 CalleeCC != CallingConv::C)
2801 return false;
2802
Evan Cheng7096ae42010-01-29 06:45:59 +00002803 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002804 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002805 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002806
2807 // If the function return type is x86_fp80 and the callee return type is not,
2808 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2809 // perform a tailcall optimization here.
2810 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2811 return false;
2812
Evan Cheng13617962010-04-30 01:12:32 +00002813 CallingConv::ID CallerCC = CallerF->getCallingConv();
2814 bool CCMatch = CallerCC == CalleeCC;
2815
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002816 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002817 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002818 return true;
2819 return false;
2820 }
2821
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002822 // Look for obvious safe cases to perform tail call optimization that do not
2823 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002824
Evan Cheng2c12cb42010-03-26 16:26:03 +00002825 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2826 // emit a special epilogue.
2827 if (RegInfo->needsStackRealignment(MF))
2828 return false;
2829
Evan Chenga375d472010-03-15 18:54:48 +00002830 // Also avoid sibcall optimization if either caller or callee uses struct
2831 // return semantics.
2832 if (isCalleeStructRet || isCallerStructRet)
2833 return false;
2834
Chad Rosier2416da32011-06-24 21:15:36 +00002835 // An stdcall caller is expected to clean up its arguments; the callee
2836 // isn't going to do that.
2837 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2838 return false;
2839
Chad Rosier871f6642011-05-18 19:59:50 +00002840 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002841 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002842 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002843
2844 // Optimizing for varargs on Win64 is unlikely to be safe without
2845 // additional testing.
2846 if (Subtarget->isTargetWin64())
2847 return false;
2848
Chad Rosier871f6642011-05-18 19:59:50 +00002849 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002850 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002851 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002852
Chad Rosier871f6642011-05-18 19:59:50 +00002853 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2854 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2855 if (!ArgLocs[i].isRegLoc())
2856 return false;
2857 }
2858
Chad Rosier30450e82011-12-22 22:35:21 +00002859 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2860 // stack. Therefore, if it's not used by the call it is not safe to optimize
2861 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002862 bool Unused = false;
2863 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2864 if (!Ins[i].Used) {
2865 Unused = true;
2866 break;
2867 }
2868 }
2869 if (Unused) {
2870 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002871 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002872 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002873 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002874 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002875 CCValAssign &VA = RVLocs[i];
2876 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2877 return false;
2878 }
2879 }
2880
Evan Cheng13617962010-04-30 01:12:32 +00002881 // If the calling conventions do not match, then we'd better make sure the
2882 // results are returned in the same way as what the caller expects.
2883 if (!CCMatch) {
2884 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002885 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002886 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002887 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2888
2889 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002890 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002891 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002892 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2893
2894 if (RVLocs1.size() != RVLocs2.size())
2895 return false;
2896 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2897 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2898 return false;
2899 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2900 return false;
2901 if (RVLocs1[i].isRegLoc()) {
2902 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2903 return false;
2904 } else {
2905 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2906 return false;
2907 }
2908 }
2909 }
2910
Evan Chenga6bff982010-01-30 01:22:00 +00002911 // If the callee takes no arguments then go on to check the results of the
2912 // call.
2913 if (!Outs.empty()) {
2914 // Check if stack adjustment is needed. For now, do not do this if any
2915 // argument is passed on the stack.
2916 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002917 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002918 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002919
2920 // Allocate shadow area for Win64
2921 if (Subtarget->isTargetWin64()) {
2922 CCInfo.AllocateStack(32, 8);
2923 }
2924
Duncan Sands45907662010-10-31 13:21:44 +00002925 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002926 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002927 MachineFunction &MF = DAG.getMachineFunction();
2928 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2929 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002930
2931 // Check if the arguments are already laid out in the right way as
2932 // the caller's fixed stack objects.
2933 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002934 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2935 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002936 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002937 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2938 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002939 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002940 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002941 if (VA.getLocInfo() == CCValAssign::Indirect)
2942 return false;
2943 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002944 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2945 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002946 return false;
2947 }
2948 }
2949 }
Evan Cheng9c044672010-05-29 01:35:22 +00002950
2951 // If the tailcall address may be in a register, then make sure it's
2952 // possible to register allocate for it. In 32-bit, the call address can
2953 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002954 // callee-saved registers are restored. These happen to be the same
2955 // registers used to pass 'inreg' arguments so watch out for those.
2956 if (!Subtarget->is64Bit() &&
2957 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002958 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002959 unsigned NumInRegs = 0;
2960 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2961 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002962 if (!VA.isRegLoc())
2963 continue;
2964 unsigned Reg = VA.getLocReg();
2965 switch (Reg) {
2966 default: break;
2967 case X86::EAX: case X86::EDX: case X86::ECX:
2968 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002969 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002970 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002971 }
2972 }
2973 }
Evan Chenga6bff982010-01-30 01:22:00 +00002974 }
Evan Chengb1712452010-01-27 06:25:16 +00002975
Evan Cheng86809cc2010-02-03 03:28:02 +00002976 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002977}
2978
Dan Gohman3df24e62008-09-03 23:12:08 +00002979FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002980X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2981 const TargetLibraryInfo *libInfo) const {
2982 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002983}
2984
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002985//===----------------------------------------------------------------------===//
2986// Other Lowering Hooks
2987//===----------------------------------------------------------------------===//
2988
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002989static bool MayFoldLoad(SDValue Op) {
2990 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2991}
2992
2993static bool MayFoldIntoStore(SDValue Op) {
2994 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2995}
2996
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002997static bool isTargetShuffle(unsigned Opcode) {
2998 switch(Opcode) {
2999 default: return false;
3000 case X86ISD::PSHUFD:
3001 case X86ISD::PSHUFHW:
3002 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003003 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003004 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003005 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003006 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003007 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003008 case X86ISD::MOVLPS:
3009 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003010 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003011 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003012 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003013 case X86ISD::MOVSS:
3014 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003015 case X86ISD::UNPCKL:
3016 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003017 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003018 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003019 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003020 return true;
3021 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003022}
3023
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003024static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003025 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003026 switch(Opc) {
3027 default: llvm_unreachable("Unknown x86 shuffle node");
3028 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003029 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003030 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003031 return DAG.getNode(Opc, dl, VT, V1);
3032 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003033}
3034
3035static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003036 SDValue V1, unsigned TargetMask,
3037 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003038 switch(Opc) {
3039 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003040 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003041 case X86ISD::PSHUFHW:
3042 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003043 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003044 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003045 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3046 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003047}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003048
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003049static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003050 SDValue V1, SDValue V2, unsigned TargetMask,
3051 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003052 switch(Opc) {
3053 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003054 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003055 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003056 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003057 return DAG.getNode(Opc, dl, VT, V1, V2,
3058 DAG.getConstant(TargetMask, MVT::i8));
3059 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003060}
3061
3062static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3063 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3064 switch(Opc) {
3065 default: llvm_unreachable("Unknown x86 shuffle node");
3066 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003067 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003068 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003069 case X86ISD::MOVLPS:
3070 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003071 case X86ISD::MOVSS:
3072 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003073 case X86ISD::UNPCKL:
3074 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003075 return DAG.getNode(Opc, dl, VT, V1, V2);
3076 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003077}
3078
Dan Gohmand858e902010-04-17 15:26:15 +00003079SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003080 MachineFunction &MF = DAG.getMachineFunction();
3081 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3082 int ReturnAddrIndex = FuncInfo->getRAIndex();
3083
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003084 if (ReturnAddrIndex == 0) {
3085 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003086 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003087 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003088 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003089 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003090 }
3091
Evan Cheng25ab6902006-09-08 06:48:29 +00003092 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003093}
3094
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003095bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3096 bool hasSymbolicDisplacement) {
3097 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003098 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003099 return false;
3100
3101 // If we don't have a symbolic displacement - we don't have any extra
3102 // restrictions.
3103 if (!hasSymbolicDisplacement)
3104 return true;
3105
3106 // FIXME: Some tweaks might be needed for medium code model.
3107 if (M != CodeModel::Small && M != CodeModel::Kernel)
3108 return false;
3109
3110 // For small code model we assume that latest object is 16MB before end of 31
3111 // bits boundary. We may also accept pretty large negative constants knowing
3112 // that all objects are in the positive half of address space.
3113 if (M == CodeModel::Small && Offset < 16*1024*1024)
3114 return true;
3115
3116 // For kernel code model we know that all object resist in the negative half
3117 // of 32bits address space. We may not accept negative offsets, since they may
3118 // be just off and we may accept pretty large positive ones.
3119 if (M == CodeModel::Kernel && Offset > 0)
3120 return true;
3121
3122 return false;
3123}
3124
Evan Chengef41ff62011-06-23 17:54:54 +00003125/// isCalleePop - Determines whether the callee is required to pop its
3126/// own arguments. Callee pop is necessary to support tail calls.
3127bool X86::isCalleePop(CallingConv::ID CallingConv,
3128 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3129 if (IsVarArg)
3130 return false;
3131
3132 switch (CallingConv) {
3133 default:
3134 return false;
3135 case CallingConv::X86_StdCall:
3136 return !is64Bit;
3137 case CallingConv::X86_FastCall:
3138 return !is64Bit;
3139 case CallingConv::X86_ThisCall:
3140 return !is64Bit;
3141 case CallingConv::Fast:
3142 return TailCallOpt;
3143 case CallingConv::GHC:
3144 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003145 case CallingConv::HiPE:
3146 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003147 }
3148}
3149
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003150/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3151/// specific condition code, returning the condition code and the LHS/RHS of the
3152/// comparison to make.
3153static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3154 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003155 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003156 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3157 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3158 // X > -1 -> X == 0, jump !sign.
3159 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003160 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003161 }
3162 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003163 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003164 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003165 }
3166 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003167 // X < 1 -> X <= 0
3168 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003169 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003170 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003171 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003172
Evan Chengd9558e02006-01-06 00:43:03 +00003173 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003174 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003175 case ISD::SETEQ: return X86::COND_E;
3176 case ISD::SETGT: return X86::COND_G;
3177 case ISD::SETGE: return X86::COND_GE;
3178 case ISD::SETLT: return X86::COND_L;
3179 case ISD::SETLE: return X86::COND_LE;
3180 case ISD::SETNE: return X86::COND_NE;
3181 case ISD::SETULT: return X86::COND_B;
3182 case ISD::SETUGT: return X86::COND_A;
3183 case ISD::SETULE: return X86::COND_BE;
3184 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003185 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003186 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003187
Chris Lattner4c78e022008-12-23 23:42:27 +00003188 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003189
Chris Lattner4c78e022008-12-23 23:42:27 +00003190 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003191 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3192 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003193 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3194 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003195 }
3196
Chris Lattner4c78e022008-12-23 23:42:27 +00003197 switch (SetCCOpcode) {
3198 default: break;
3199 case ISD::SETOLT:
3200 case ISD::SETOLE:
3201 case ISD::SETUGT:
3202 case ISD::SETUGE:
3203 std::swap(LHS, RHS);
3204 break;
3205 }
3206
3207 // On a floating point condition, the flags are set as follows:
3208 // ZF PF CF op
3209 // 0 | 0 | 0 | X > Y
3210 // 0 | 0 | 1 | X < Y
3211 // 1 | 0 | 0 | X == Y
3212 // 1 | 1 | 1 | unordered
3213 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003214 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003215 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003216 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003217 case ISD::SETOLT: // flipped
3218 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003219 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003220 case ISD::SETOLE: // flipped
3221 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003222 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003223 case ISD::SETUGT: // flipped
3224 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003225 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003226 case ISD::SETUGE: // flipped
3227 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003228 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003229 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003230 case ISD::SETNE: return X86::COND_NE;
3231 case ISD::SETUO: return X86::COND_P;
3232 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003233 case ISD::SETOEQ:
3234 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003235 }
Evan Chengd9558e02006-01-06 00:43:03 +00003236}
3237
Evan Cheng4a460802006-01-11 00:33:36 +00003238/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3239/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003240/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003241static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003242 switch (X86CC) {
3243 default:
3244 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003245 case X86::COND_B:
3246 case X86::COND_BE:
3247 case X86::COND_E:
3248 case X86::COND_P:
3249 case X86::COND_A:
3250 case X86::COND_AE:
3251 case X86::COND_NE:
3252 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003253 return true;
3254 }
3255}
3256
Evan Chengeb2f9692009-10-27 19:56:55 +00003257/// isFPImmLegal - Returns true if the target can instruction select the
3258/// specified FP immediate natively. If false, the legalizer will
3259/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003260bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003261 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3262 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3263 return true;
3264 }
3265 return false;
3266}
3267
Nate Begeman9008ca62009-04-27 18:41:29 +00003268/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3269/// the specified range (L, H].
3270static bool isUndefOrInRange(int Val, int Low, int Hi) {
3271 return (Val < 0) || (Val >= Low && Val < Hi);
3272}
3273
3274/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3275/// specified value.
3276static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003277 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003278}
3279
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003280/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003281/// from position Pos and ending in Pos+Size, falls within the specified
3282/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003283static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003284 unsigned Pos, unsigned Size, int Low) {
3285 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003286 if (!isUndefOrEqual(Mask[i], Low))
3287 return false;
3288 return true;
3289}
3290
Nate Begeman9008ca62009-04-27 18:41:29 +00003291/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3292/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3293/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003294static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003295 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003296 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003298 return (Mask[0] < 2 && Mask[1] < 2);
3299 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003300}
3301
Nate Begeman9008ca62009-04-27 18:41:29 +00003302/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3303/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003304static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3305 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003306 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003307
Nate Begeman9008ca62009-04-27 18:41:29 +00003308 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003309 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3310 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003311
Evan Cheng506d3df2006-03-29 23:07:14 +00003312 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003313 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003314 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003315 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003316
Craig Toppera9a568a2012-05-02 08:03:44 +00003317 if (VT == MVT::v16i16) {
3318 // Lower quadword copied in order or undef.
3319 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3320 return false;
3321
3322 // Upper quadword shuffled.
3323 for (unsigned i = 12; i != 16; ++i)
3324 if (!isUndefOrInRange(Mask[i], 12, 16))
3325 return false;
3326 }
3327
Evan Cheng506d3df2006-03-29 23:07:14 +00003328 return true;
3329}
3330
Nate Begeman9008ca62009-04-27 18:41:29 +00003331/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3332/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003333static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3334 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003335 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003336
Rafael Espindola15684b22009-04-24 12:40:33 +00003337 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003338 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3339 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003340
Rafael Espindola15684b22009-04-24 12:40:33 +00003341 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003342 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003343 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003344 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003345
Craig Toppera9a568a2012-05-02 08:03:44 +00003346 if (VT == MVT::v16i16) {
3347 // Upper quadword copied in order.
3348 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3349 return false;
3350
3351 // Lower quadword shuffled.
3352 for (unsigned i = 8; i != 12; ++i)
3353 if (!isUndefOrInRange(Mask[i], 8, 12))
3354 return false;
3355 }
3356
Rafael Espindola15684b22009-04-24 12:40:33 +00003357 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003358}
3359
Nate Begemana09008b2009-10-19 02:17:23 +00003360/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3361/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003362static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3363 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003364 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3365 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003366 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003367
Craig Topper0e2037b2012-01-20 05:53:00 +00003368 unsigned NumElts = VT.getVectorNumElements();
3369 unsigned NumLanes = VT.getSizeInBits()/128;
3370 unsigned NumLaneElts = NumElts/NumLanes;
3371
3372 // Do not handle 64-bit element shuffles with palignr.
3373 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003374 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003375
Craig Topper0e2037b2012-01-20 05:53:00 +00003376 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3377 unsigned i;
3378 for (i = 0; i != NumLaneElts; ++i) {
3379 if (Mask[i+l] >= 0)
3380 break;
3381 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003382
Craig Topper0e2037b2012-01-20 05:53:00 +00003383 // Lane is all undef, go to next lane
3384 if (i == NumLaneElts)
3385 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003386
Craig Topper0e2037b2012-01-20 05:53:00 +00003387 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003388
Craig Topper0e2037b2012-01-20 05:53:00 +00003389 // Make sure its in this lane in one of the sources
3390 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3391 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003392 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003393
3394 // If not lane 0, then we must match lane 0
3395 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3396 return false;
3397
3398 // Correct second source to be contiguous with first source
3399 if (Start >= (int)NumElts)
3400 Start -= NumElts - NumLaneElts;
3401
3402 // Make sure we're shifting in the right direction.
3403 if (Start <= (int)(i+l))
3404 return false;
3405
3406 Start -= i;
3407
3408 // Check the rest of the elements to see if they are consecutive.
3409 for (++i; i != NumLaneElts; ++i) {
3410 int Idx = Mask[i+l];
3411
3412 // Make sure its in this lane
3413 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3414 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3415 return false;
3416
3417 // If not lane 0, then we must match lane 0
3418 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3419 return false;
3420
3421 if (Idx >= (int)NumElts)
3422 Idx -= NumElts - NumLaneElts;
3423
3424 if (!isUndefOrEqual(Idx, Start+i))
3425 return false;
3426
3427 }
Nate Begemana09008b2009-10-19 02:17:23 +00003428 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003429
Nate Begemana09008b2009-10-19 02:17:23 +00003430 return true;
3431}
3432
Craig Topper1a7700a2012-01-19 08:19:12 +00003433/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3434/// the two vector operands have swapped position.
3435static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3436 unsigned NumElems) {
3437 for (unsigned i = 0; i != NumElems; ++i) {
3438 int idx = Mask[i];
3439 if (idx < 0)
3440 continue;
3441 else if (idx < (int)NumElems)
3442 Mask[i] = idx + NumElems;
3443 else
3444 Mask[i] = idx - NumElems;
3445 }
3446}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003447
Craig Topper1a7700a2012-01-19 08:19:12 +00003448/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3449/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3450/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3451/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003452static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003453 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003454 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003455 return false;
3456
Craig Topper1a7700a2012-01-19 08:19:12 +00003457 unsigned NumElems = VT.getVectorNumElements();
3458 unsigned NumLanes = VT.getSizeInBits()/128;
3459 unsigned NumLaneElems = NumElems/NumLanes;
3460
3461 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003462 return false;
3463
3464 // VSHUFPSY divides the resulting vector into 4 chunks.
3465 // The sources are also splitted into 4 chunks, and each destination
3466 // chunk must come from a different source chunk.
3467 //
3468 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3469 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3470 //
3471 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3472 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3473 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003474 // VSHUFPDY divides the resulting vector into 4 chunks.
3475 // The sources are also splitted into 4 chunks, and each destination
3476 // chunk must come from a different source chunk.
3477 //
3478 // SRC1 => X3 X2 X1 X0
3479 // SRC2 => Y3 Y2 Y1 Y0
3480 //
3481 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3482 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003483 unsigned HalfLaneElems = NumLaneElems/2;
3484 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3485 for (unsigned i = 0; i != NumLaneElems; ++i) {
3486 int Idx = Mask[i+l];
3487 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3488 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3489 return false;
3490 // For VSHUFPSY, the mask of the second half must be the same as the
3491 // first but with the appropriate offsets. This works in the same way as
3492 // VPERMILPS works with masks.
3493 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3494 continue;
3495 if (!isUndefOrEqual(Idx, Mask[i]+l))
3496 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003497 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003498 }
3499
3500 return true;
3501}
3502
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003503/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3504/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003505static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003506 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003507 return false;
3508
Craig Topper7a9a28b2012-08-12 02:23:29 +00003509 unsigned NumElems = VT.getVectorNumElements();
3510
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003511 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003512 return false;
3513
Evan Cheng2064a2b2006-03-28 06:50:32 +00003514 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003515 return isUndefOrEqual(Mask[0], 6) &&
3516 isUndefOrEqual(Mask[1], 7) &&
3517 isUndefOrEqual(Mask[2], 2) &&
3518 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003519}
3520
Nate Begeman0b10b912009-11-07 23:17:15 +00003521/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3522/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3523/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003524static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003525 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003526 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003527
Craig Topper7a9a28b2012-08-12 02:23:29 +00003528 unsigned NumElems = VT.getVectorNumElements();
3529
Nate Begeman0b10b912009-11-07 23:17:15 +00003530 if (NumElems != 4)
3531 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003532
Craig Topperdd637ae2012-02-19 05:41:45 +00003533 return isUndefOrEqual(Mask[0], 2) &&
3534 isUndefOrEqual(Mask[1], 3) &&
3535 isUndefOrEqual(Mask[2], 2) &&
3536 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003537}
3538
Evan Cheng5ced1d82006-04-06 23:23:56 +00003539/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3540/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003541static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003542 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003543 return false;
3544
Craig Topperdd637ae2012-02-19 05:41:45 +00003545 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003546
Evan Cheng5ced1d82006-04-06 23:23:56 +00003547 if (NumElems != 2 && NumElems != 4)
3548 return false;
3549
Chad Rosier238ae312012-04-30 17:47:15 +00003550 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003551 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003552 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003553
Chad Rosier238ae312012-04-30 17:47:15 +00003554 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003555 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003556 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003557
3558 return true;
3559}
3560
Nate Begeman0b10b912009-11-07 23:17:15 +00003561/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3562/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003563static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003564 if (!VT.is128BitVector())
3565 return false;
3566
Craig Topperdd637ae2012-02-19 05:41:45 +00003567 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003568
Craig Topper7a9a28b2012-08-12 02:23:29 +00003569 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003570 return false;
3571
Chad Rosier238ae312012-04-30 17:47:15 +00003572 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003573 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003574 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003575
Chad Rosier238ae312012-04-30 17:47:15 +00003576 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3577 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003578 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003579
3580 return true;
3581}
3582
Elena Demikhovsky15963732012-06-26 08:04:10 +00003583//
3584// Some special combinations that can be optimized.
3585//
3586static
3587SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3588 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003589 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003590 DebugLoc dl = SVOp->getDebugLoc();
3591
3592 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3593 return SDValue();
3594
3595 ArrayRef<int> Mask = SVOp->getMask();
3596
3597 // These are the special masks that may be optimized.
3598 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3599 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3600 bool MatchEvenMask = true;
3601 bool MatchOddMask = true;
3602 for (int i=0; i<8; ++i) {
3603 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3604 MatchEvenMask = false;
3605 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3606 MatchOddMask = false;
3607 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003608
Elena Demikhovsky32510202012-09-04 12:49:02 +00003609 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003610 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003611
Elena Demikhovsky15963732012-06-26 08:04:10 +00003612 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3613
Elena Demikhovsky32510202012-09-04 12:49:02 +00003614 SDValue Op0 = SVOp->getOperand(0);
3615 SDValue Op1 = SVOp->getOperand(1);
3616
3617 if (MatchEvenMask) {
3618 // Shift the second operand right to 32 bits.
3619 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3620 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3621 } else {
3622 // Shift the first operand left to 32 bits.
3623 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3624 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3625 }
3626 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3627 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003628}
3629
Evan Cheng0038e592006-03-28 00:39:58 +00003630/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3631/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003632static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003633 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003634 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003635
3636 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3637 "Unsupported vector type for unpckh");
3638
Craig Topper5a529e42013-01-18 06:44:29 +00003639 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003640 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003641 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003642
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003643 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3644 // independently on 128-bit lanes.
3645 unsigned NumLanes = VT.getSizeInBits()/128;
3646 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003647
Craig Topper94438ba2011-12-16 08:06:31 +00003648 for (unsigned l = 0; l != NumLanes; ++l) {
3649 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3650 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003651 i += 2, ++j) {
3652 int BitI = Mask[i];
3653 int BitI1 = Mask[i+1];
3654 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003655 return false;
David Greenea20244d2011-03-02 17:23:43 +00003656 if (V2IsSplat) {
3657 if (!isUndefOrEqual(BitI1, NumElts))
3658 return false;
3659 } else {
3660 if (!isUndefOrEqual(BitI1, j + NumElts))
3661 return false;
3662 }
Evan Cheng39623da2006-04-20 08:58:49 +00003663 }
Evan Cheng0038e592006-03-28 00:39:58 +00003664 }
David Greenea20244d2011-03-02 17:23:43 +00003665
Evan Cheng0038e592006-03-28 00:39:58 +00003666 return true;
3667}
3668
Evan Cheng4fcb9222006-03-28 02:43:26 +00003669/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3670/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003671static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003672 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003673 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003674
3675 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3676 "Unsupported vector type for unpckh");
3677
Craig Topper5a529e42013-01-18 06:44:29 +00003678 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003679 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003680 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003681
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003682 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3683 // independently on 128-bit lanes.
3684 unsigned NumLanes = VT.getSizeInBits()/128;
3685 unsigned NumLaneElts = NumElts/NumLanes;
3686
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003687 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003688 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3689 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003690 int BitI = Mask[i];
3691 int BitI1 = Mask[i+1];
3692 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003693 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003694 if (V2IsSplat) {
3695 if (isUndefOrEqual(BitI1, NumElts))
3696 return false;
3697 } else {
3698 if (!isUndefOrEqual(BitI1, j+NumElts))
3699 return false;
3700 }
Evan Cheng39623da2006-04-20 08:58:49 +00003701 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003702 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003703 return true;
3704}
3705
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003706/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3707/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3708/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003709static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003710 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003711 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003712
3713 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3714 "Unsupported vector type for unpckh");
3715
Craig Topper5a529e42013-01-18 06:44:29 +00003716 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003717 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003718 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003719
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003720 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3721 // FIXME: Need a better way to get rid of this, there's no latency difference
3722 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3723 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003724 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003725 return false;
3726
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003727 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3728 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003729 unsigned NumLanes = VT.getSizeInBits()/128;
3730 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003731
Craig Topper94438ba2011-12-16 08:06:31 +00003732 for (unsigned l = 0; l != NumLanes; ++l) {
3733 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3734 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003735 i += 2, ++j) {
3736 int BitI = Mask[i];
3737 int BitI1 = Mask[i+1];
3738
3739 if (!isUndefOrEqual(BitI, j))
3740 return false;
3741 if (!isUndefOrEqual(BitI1, j))
3742 return false;
3743 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003744 }
David Greenea20244d2011-03-02 17:23:43 +00003745
Rafael Espindola15684b22009-04-24 12:40:33 +00003746 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003747}
3748
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003749/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3750/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3751/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003752static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003753 unsigned NumElts = VT.getVectorNumElements();
3754
3755 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3756 "Unsupported vector type for unpckh");
3757
Craig Topper5a529e42013-01-18 06:44:29 +00003758 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003759 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003760 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003761
Craig Topper94438ba2011-12-16 08:06:31 +00003762 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3763 // independently on 128-bit lanes.
3764 unsigned NumLanes = VT.getSizeInBits()/128;
3765 unsigned NumLaneElts = NumElts/NumLanes;
3766
3767 for (unsigned l = 0; l != NumLanes; ++l) {
3768 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3769 i != (l+1)*NumLaneElts; i += 2, ++j) {
3770 int BitI = Mask[i];
3771 int BitI1 = Mask[i+1];
3772 if (!isUndefOrEqual(BitI, j))
3773 return false;
3774 if (!isUndefOrEqual(BitI1, j))
3775 return false;
3776 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003777 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003778 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003779}
3780
Evan Cheng017dcc62006-04-21 01:05:10 +00003781/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3782/// specifies a shuffle of elements that is suitable for input to MOVSS,
3783/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003784static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003785 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003786 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003787 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003788 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003789
Craig Topperc612d792012-01-02 09:17:37 +00003790 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003791
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003793 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003794
Craig Topperc612d792012-01-02 09:17:37 +00003795 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003797 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003798
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003799 return true;
3800}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003801
Craig Topper70b883b2011-11-28 10:14:51 +00003802/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003803/// as permutations between 128-bit chunks or halves. As an example: this
3804/// shuffle bellow:
3805/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3806/// The first half comes from the second half of V1 and the second half from the
3807/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003808static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3809 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003810 return false;
3811
3812 // The shuffle result is divided into half A and half B. In total the two
3813 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3814 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003815 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003816 bool MatchA = false, MatchB = false;
3817
3818 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003819 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003820 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3821 MatchA = true;
3822 break;
3823 }
3824 }
3825
3826 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003827 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003828 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3829 MatchB = true;
3830 break;
3831 }
3832 }
3833
3834 return MatchA && MatchB;
3835}
3836
Craig Topper70b883b2011-11-28 10:14:51 +00003837/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3838/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003839static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003840 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003841
Craig Topperc612d792012-01-02 09:17:37 +00003842 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003843
Craig Topperc612d792012-01-02 09:17:37 +00003844 unsigned FstHalf = 0, SndHalf = 0;
3845 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003846 if (SVOp->getMaskElt(i) > 0) {
3847 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3848 break;
3849 }
3850 }
Craig Topperc612d792012-01-02 09:17:37 +00003851 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003852 if (SVOp->getMaskElt(i) > 0) {
3853 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3854 break;
3855 }
3856 }
3857
3858 return (FstHalf | (SndHalf << 4));
3859}
3860
Craig Topper70b883b2011-11-28 10:14:51 +00003861/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003862/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3863/// Note that VPERMIL mask matching is different depending whether theunderlying
3864/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3865/// to the same elements of the low, but to the higher half of the source.
3866/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003867/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003868static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3869 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003870 return false;
3871
Craig Topperc612d792012-01-02 09:17:37 +00003872 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003873 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003874 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003875 return false;
3876
Craig Topperc612d792012-01-02 09:17:37 +00003877 unsigned NumLanes = VT.getSizeInBits()/128;
3878 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003879 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003880 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003881 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003882 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003883 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003884 continue;
3885 // VPERMILPS handling
3886 if (Mask[i] < 0)
3887 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003888 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003889 return false;
3890 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003891 }
3892
3893 return true;
3894}
3895
Craig Topper5aaffa82012-02-19 02:53:47 +00003896/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003897/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003898/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003899static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003901 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003902 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003903
3904 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003905 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003906 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003907
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003909 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003910
Craig Topperc612d792012-01-02 09:17:37 +00003911 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3913 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3914 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003915 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003916
Evan Cheng39623da2006-04-20 08:58:49 +00003917 return true;
3918}
3919
Evan Chengd9539472006-04-14 21:59:03 +00003920/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3921/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003922/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003923static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003924 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003925 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003926 return false;
3927
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003928 unsigned NumElems = VT.getVectorNumElements();
3929
Craig Topper5a529e42013-01-18 06:44:29 +00003930 if ((VT.is128BitVector() && NumElems != 4) ||
3931 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003932 return false;
3933
3934 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003935 for (unsigned i = 0; i != NumElems; i += 2)
3936 if (!isUndefOrEqual(Mask[i], i+1) ||
3937 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003939
3940 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003941}
3942
3943/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3944/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003945/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003946static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003947 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003948 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003949 return false;
3950
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003951 unsigned NumElems = VT.getVectorNumElements();
3952
Craig Topper5a529e42013-01-18 06:44:29 +00003953 if ((VT.is128BitVector() && NumElems != 4) ||
3954 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003955 return false;
3956
3957 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003958 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003959 if (!isUndefOrEqual(Mask[i], i) ||
3960 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003962
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003963 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003964}
3965
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003966/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3967/// specifies a shuffle of elements that is suitable for input to 256-bit
3968/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003969static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3970 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003971 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003972
Craig Topper7a9a28b2012-08-12 02:23:29 +00003973 unsigned NumElts = VT.getVectorNumElements();
3974 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003975 return false;
3976
Craig Topperc612d792012-01-02 09:17:37 +00003977 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003978 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003979 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003980 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003981 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003982 return false;
3983 return true;
3984}
3985
Evan Cheng0b457f02008-09-25 20:50:48 +00003986/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003987/// specifies a shuffle of elements that is suitable for input to 128-bit
3988/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003989static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003990 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003991 return false;
3992
Craig Topperc612d792012-01-02 09:17:37 +00003993 unsigned e = VT.getVectorNumElements() / 2;
3994 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003995 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003996 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003997 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003998 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003999 return false;
4000 return true;
4001}
4002
David Greenec38a03e2011-02-03 15:50:00 +00004003/// isVEXTRACTF128Index - Return true if the specified
4004/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4005/// suitable for input to VEXTRACTF128.
4006bool X86::isVEXTRACTF128Index(SDNode *N) {
4007 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4008 return false;
4009
4010 // The index should be aligned on a 128-bit boundary.
4011 uint64_t Index =
4012 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4013
Craig Topper5141d972013-01-18 08:41:28 +00004014 MVT VT = N->getValueType(0).getSimpleVT();
4015 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004016 bool Result = (Index * ElSize) % 128 == 0;
4017
4018 return Result;
4019}
4020
David Greeneccacdc12011-02-04 16:08:29 +00004021/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4022/// operand specifies a subvector insert that is suitable for input to
4023/// VINSERTF128.
4024bool X86::isVINSERTF128Index(SDNode *N) {
4025 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4026 return false;
4027
4028 // The index should be aligned on a 128-bit boundary.
4029 uint64_t Index =
4030 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4031
Craig Topper5141d972013-01-18 08:41:28 +00004032 MVT VT = N->getValueType(0).getSimpleVT();
4033 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004034 bool Result = (Index * ElSize) % 128 == 0;
4035
4036 return Result;
4037}
4038
Evan Cheng63d33002006-03-22 08:01:21 +00004039/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004040/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004041/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004042static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004043 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004044
Craig Topper1a7700a2012-01-19 08:19:12 +00004045 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4046 "Unsupported vector type for PSHUF/SHUFP");
4047
4048 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4049 // independently on 128-bit lanes.
4050 unsigned NumElts = VT.getVectorNumElements();
4051 unsigned NumLanes = VT.getSizeInBits()/128;
4052 unsigned NumLaneElts = NumElts/NumLanes;
4053
4054 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4055 "Only supports 2 or 4 elements per lane");
4056
4057 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004058 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004059 for (unsigned i = 0; i != NumElts; ++i) {
4060 int Elt = N->getMaskElt(i);
4061 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004062 Elt &= NumLaneElts - 1;
4063 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004064 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004065 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004066
Evan Cheng63d33002006-03-22 08:01:21 +00004067 return Mask;
4068}
4069
Evan Cheng506d3df2006-03-29 23:07:14 +00004070/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004071/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004072static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004073 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004074
4075 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4076 "Unsupported vector type for PSHUFHW");
4077
4078 unsigned NumElts = VT.getVectorNumElements();
4079
Evan Cheng506d3df2006-03-29 23:07:14 +00004080 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004081 for (unsigned l = 0; l != NumElts; l += 8) {
4082 // 8 nodes per lane, but we only care about the last 4.
4083 for (unsigned i = 0; i < 4; ++i) {
4084 int Elt = N->getMaskElt(l+i+4);
4085 if (Elt < 0) continue;
4086 Elt &= 0x3; // only 2-bits.
4087 Mask |= Elt << (i * 2);
4088 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004089 }
Craig Topper6b28d352012-05-03 07:12:59 +00004090
Evan Cheng506d3df2006-03-29 23:07:14 +00004091 return Mask;
4092}
4093
4094/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004095/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004096static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004097 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004098
4099 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4100 "Unsupported vector type for PSHUFHW");
4101
4102 unsigned NumElts = VT.getVectorNumElements();
4103
Evan Cheng506d3df2006-03-29 23:07:14 +00004104 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004105 for (unsigned l = 0; l != NumElts; l += 8) {
4106 // 8 nodes per lane, but we only care about the first 4.
4107 for (unsigned i = 0; i < 4; ++i) {
4108 int Elt = N->getMaskElt(l+i);
4109 if (Elt < 0) continue;
4110 Elt &= 0x3; // only 2-bits
4111 Mask |= Elt << (i * 2);
4112 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004113 }
Craig Topper6b28d352012-05-03 07:12:59 +00004114
Evan Cheng506d3df2006-03-29 23:07:14 +00004115 return Mask;
4116}
4117
Nate Begemana09008b2009-10-19 02:17:23 +00004118/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4119/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004120static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004121 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004122 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004123
Craig Topper0e2037b2012-01-20 05:53:00 +00004124 unsigned NumElts = VT.getVectorNumElements();
4125 unsigned NumLanes = VT.getSizeInBits()/128;
4126 unsigned NumLaneElts = NumElts/NumLanes;
4127
4128 int Val = 0;
4129 unsigned i;
4130 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004131 Val = SVOp->getMaskElt(i);
4132 if (Val >= 0)
4133 break;
4134 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004135 if (Val >= (int)NumElts)
4136 Val -= NumElts - NumLaneElts;
4137
Eli Friedman63f8dde2011-07-25 21:36:45 +00004138 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004139 return (Val - i) * EltSize;
4140}
4141
David Greenec38a03e2011-02-03 15:50:00 +00004142/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4143/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4144/// instructions.
4145unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4146 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4147 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4148
4149 uint64_t Index =
4150 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4151
Craig Toppercfcab212013-01-19 08:27:45 +00004152 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4153 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004154
4155 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004156 return Index / NumElemsPerChunk;
4157}
4158
David Greeneccacdc12011-02-04 16:08:29 +00004159/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4160/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4161/// instructions.
4162unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4163 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4164 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4165
4166 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004167 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004168
Craig Toppercfcab212013-01-19 08:27:45 +00004169 MVT VecVT = N->getValueType(0).getSimpleVT();
4170 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004171
4172 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004173 return Index / NumElemsPerChunk;
4174}
4175
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004176/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4177/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4178/// Handles 256-bit.
4179static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004180 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004181
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004182 unsigned NumElts = VT.getVectorNumElements();
4183
Craig Topper095c5282012-04-15 23:48:57 +00004184 assert((VT.is256BitVector() && NumElts == 4) &&
4185 "Unsupported vector type for VPERMQ/VPERMPD");
4186
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004187 unsigned Mask = 0;
4188 for (unsigned i = 0; i != NumElts; ++i) {
4189 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004190 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004191 continue;
4192 Mask |= Elt << (i*2);
4193 }
4194
4195 return Mask;
4196}
Evan Cheng37b73872009-07-30 08:33:02 +00004197/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4198/// constant +0.0.
4199bool X86::isZeroNode(SDValue Elt) {
4200 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004201 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004202 (isa<ConstantFPSDNode>(Elt) &&
4203 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4204}
4205
Nate Begeman9008ca62009-04-27 18:41:29 +00004206/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4207/// their permute mask.
4208static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4209 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004210 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004211 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004212 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004213
Nate Begeman5a5ca152009-04-29 05:20:52 +00004214 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004215 int Idx = SVOp->getMaskElt(i);
4216 if (Idx >= 0) {
4217 if (Idx < (int)NumElems)
4218 Idx += NumElems;
4219 else
4220 Idx -= NumElems;
4221 }
4222 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004223 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4225 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004226}
4227
Evan Cheng533a0aa2006-04-19 20:35:22 +00004228/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4229/// match movhlps. The lower half elements should come from upper half of
4230/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004231/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004232static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004233 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004234 return false;
4235 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004236 return false;
4237 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004238 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004239 return false;
4240 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004241 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004242 return false;
4243 return true;
4244}
4245
Evan Cheng5ced1d82006-04-06 23:23:56 +00004246/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004247/// is promoted to a vector. It also returns the LoadSDNode by reference if
4248/// required.
4249static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004250 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4251 return false;
4252 N = N->getOperand(0).getNode();
4253 if (!ISD::isNON_EXTLoad(N))
4254 return false;
4255 if (LD)
4256 *LD = cast<LoadSDNode>(N);
4257 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004258}
4259
Dan Gohman65fd6562011-11-03 21:49:52 +00004260// Test whether the given value is a vector value which will be legalized
4261// into a load.
4262static bool WillBeConstantPoolLoad(SDNode *N) {
4263 if (N->getOpcode() != ISD::BUILD_VECTOR)
4264 return false;
4265
4266 // Check for any non-constant elements.
4267 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4268 switch (N->getOperand(i).getNode()->getOpcode()) {
4269 case ISD::UNDEF:
4270 case ISD::ConstantFP:
4271 case ISD::Constant:
4272 break;
4273 default:
4274 return false;
4275 }
4276
4277 // Vectors of all-zeros and all-ones are materialized with special
4278 // instructions rather than being loaded.
4279 return !ISD::isBuildVectorAllZeros(N) &&
4280 !ISD::isBuildVectorAllOnes(N);
4281}
4282
Evan Cheng533a0aa2006-04-19 20:35:22 +00004283/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4284/// match movlp{s|d}. The lower half elements should come from lower half of
4285/// V1 (and in order), and the upper half elements should come from the upper
4286/// half of V2 (and in order). And since V1 will become the source of the
4287/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004288static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004289 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004290 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004291 return false;
4292
Evan Cheng466685d2006-10-09 20:57:25 +00004293 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004294 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004295 // Is V2 is a vector load, don't do this transformation. We will try to use
4296 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004297 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004298 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004299
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004300 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004301
Evan Cheng533a0aa2006-04-19 20:35:22 +00004302 if (NumElems != 2 && NumElems != 4)
4303 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004304 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004305 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004306 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004307 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004308 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004309 return false;
4310 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004311}
4312
Evan Cheng39623da2006-04-20 08:58:49 +00004313/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4314/// all the same.
4315static bool isSplatVector(SDNode *N) {
4316 if (N->getOpcode() != ISD::BUILD_VECTOR)
4317 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004318
Dan Gohman475871a2008-07-27 21:46:04 +00004319 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004320 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4321 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004322 return false;
4323 return true;
4324}
4325
Evan Cheng213d2cf2007-05-17 18:45:50 +00004326/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004327/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004328/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004329static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004330 SDValue V1 = N->getOperand(0);
4331 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004332 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4333 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004334 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004335 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004336 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004337 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4338 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004339 if (Opc != ISD::BUILD_VECTOR ||
4340 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 return false;
4342 } else if (Idx >= 0) {
4343 unsigned Opc = V1.getOpcode();
4344 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4345 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004346 if (Opc != ISD::BUILD_VECTOR ||
4347 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004348 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004349 }
4350 }
4351 return true;
4352}
4353
4354/// getZeroVector - Returns a vector of specified type with all zero elements.
4355///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004356static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004357 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004358 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004359
Dale Johannesen0488fb62010-09-30 23:57:10 +00004360 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004361 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004362 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004363 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004364 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004365 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4366 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4367 } else { // SSE1
4368 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4369 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4370 }
Craig Topper5a529e42013-01-18 06:44:29 +00004371 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004372 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004373 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4374 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4375 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4376 } else {
4377 // 256-bit logic and arithmetic instructions in AVX are all
4378 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4379 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4380 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4381 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4382 }
Craig Topper9d352402012-04-23 07:24:41 +00004383 } else
4384 llvm_unreachable("Unexpected vector type");
4385
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004386 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004387}
4388
Chris Lattner8a594482007-11-25 00:24:49 +00004389/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004390/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4391/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4392/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004393static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004394 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004395 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004396
Owen Anderson825b72b2009-08-11 20:47:22 +00004397 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004398 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004399 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004400 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004401 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4402 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4403 } else { // AVX
4404 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004405 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004406 }
Craig Topper5a529e42013-01-18 06:44:29 +00004407 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004408 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004409 } else
4410 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004411
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004412 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004413}
4414
Evan Cheng39623da2006-04-20 08:58:49 +00004415/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4416/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004417static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004418 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004419 if (Mask[i] > (int)NumElems) {
4420 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004421 }
Evan Cheng39623da2006-04-20 08:58:49 +00004422 }
Evan Cheng39623da2006-04-20 08:58:49 +00004423}
4424
Evan Cheng017dcc62006-04-21 01:05:10 +00004425/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4426/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004427static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 SDValue V2) {
4429 unsigned NumElems = VT.getVectorNumElements();
4430 SmallVector<int, 8> Mask;
4431 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004432 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004433 Mask.push_back(i);
4434 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004435}
4436
Nate Begeman9008ca62009-04-27 18:41:29 +00004437/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004438static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 SDValue V2) {
4440 unsigned NumElems = VT.getVectorNumElements();
4441 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004442 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004443 Mask.push_back(i);
4444 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004445 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004446 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004447}
4448
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004449/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004450static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 SDValue V2) {
4452 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004454 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004455 Mask.push_back(i + Half);
4456 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004457 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004459}
4460
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004461// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004462// a generic shuffle instruction because the target has no such instructions.
4463// Generate shuffles which repeat i16 and i8 several times until they can be
4464// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004465static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004467 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004468 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004469
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 while (NumElems > 4) {
4471 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004472 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004475 EltNo -= NumElems/2;
4476 }
4477 NumElems >>= 1;
4478 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004479 return V;
4480}
Eric Christopherfd179292009-08-27 18:07:15 +00004481
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004482/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4483static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4484 EVT VT = V.getValueType();
4485 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004486
Craig Topper5a529e42013-01-18 06:44:29 +00004487 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004488 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004489 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004490 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4491 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004492 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004493 // To use VPERMILPS to splat scalars, the second half of indicies must
4494 // refer to the higher part, which is a duplication of the lower one,
4495 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004496 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4497 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004498
4499 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4500 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4501 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004502 } else
4503 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004504
4505 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4506}
4507
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004508/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004509static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4510 EVT SrcVT = SV->getValueType(0);
4511 SDValue V1 = SV->getOperand(0);
4512 DebugLoc dl = SV->getDebugLoc();
4513
4514 int EltNo = SV->getSplatIndex();
4515 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004516 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004517
Craig Topper5a529e42013-01-18 06:44:29 +00004518 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4519 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004520
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004521 // Extract the 128-bit part containing the splat element and update
4522 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004523 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004524 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4525 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004526 EltNo -= NumElems/2;
4527 }
4528
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004529 // All i16 and i8 vector types can't be used directly by a generic shuffle
4530 // instruction because the target has no such instruction. Generate shuffles
4531 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004532 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004533 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004534 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004535 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004536
4537 // Recreate the 256-bit vector and place the same 128-bit vector
4538 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004539 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004540 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004541 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004542 }
4543
4544 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004545}
4546
Evan Chengba05f722006-04-21 23:03:30 +00004547/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004548/// vector of zero or undef vector. This produces a shuffle where the low
4549/// element of V2 is swizzled into the zero/undef vector, landing at element
4550/// 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 +00004551static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004552 bool IsZero,
4553 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004554 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004555 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004556 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004557 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004558 unsigned NumElems = VT.getVectorNumElements();
4559 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004560 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 // If this is the insertion idx, put the low elt of V2 here.
4562 MaskVec.push_back(i == Idx ? NumElems : i);
4563 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004564}
4565
Craig Toppera1ffc682012-03-20 06:42:26 +00004566/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4567/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004568/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004569static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004570 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004571 unsigned NumElems = VT.getVectorNumElements();
4572 SDValue ImmN;
4573
Craig Topper89f4e662012-03-20 07:17:59 +00004574 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004575 switch(N->getOpcode()) {
4576 case X86ISD::SHUFP:
4577 ImmN = N->getOperand(N->getNumOperands()-1);
4578 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4579 break;
4580 case X86ISD::UNPCKH:
4581 DecodeUNPCKHMask(VT, Mask);
4582 break;
4583 case X86ISD::UNPCKL:
4584 DecodeUNPCKLMask(VT, Mask);
4585 break;
4586 case X86ISD::MOVHLPS:
4587 DecodeMOVHLPSMask(NumElems, Mask);
4588 break;
4589 case X86ISD::MOVLHPS:
4590 DecodeMOVLHPSMask(NumElems, Mask);
4591 break;
4592 case X86ISD::PSHUFD:
4593 case X86ISD::VPERMILP:
4594 ImmN = N->getOperand(N->getNumOperands()-1);
4595 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004596 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004597 break;
4598 case X86ISD::PSHUFHW:
4599 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004600 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004601 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004602 break;
4603 case X86ISD::PSHUFLW:
4604 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004605 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004606 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004607 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004608 case X86ISD::VPERMI:
4609 ImmN = N->getOperand(N->getNumOperands()-1);
4610 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4611 IsUnary = true;
4612 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004613 case X86ISD::MOVSS:
4614 case X86ISD::MOVSD: {
4615 // The index 0 always comes from the first element of the second source,
4616 // this is why MOVSS and MOVSD are used in the first place. The other
4617 // elements come from the other positions of the first source vector
4618 Mask.push_back(NumElems);
4619 for (unsigned i = 1; i != NumElems; ++i) {
4620 Mask.push_back(i);
4621 }
4622 break;
4623 }
4624 case X86ISD::VPERM2X128:
4625 ImmN = N->getOperand(N->getNumOperands()-1);
4626 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004627 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004628 break;
4629 case X86ISD::MOVDDUP:
4630 case X86ISD::MOVLHPD:
4631 case X86ISD::MOVLPD:
4632 case X86ISD::MOVLPS:
4633 case X86ISD::MOVSHDUP:
4634 case X86ISD::MOVSLDUP:
4635 case X86ISD::PALIGN:
4636 // Not yet implemented
4637 return false;
4638 default: llvm_unreachable("unknown target shuffle node");
4639 }
4640
4641 return true;
4642}
4643
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004644/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4645/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004646static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004647 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004648 if (Depth == 6)
4649 return SDValue(); // Limit search depth.
4650
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651 SDValue V = SDValue(N, 0);
4652 EVT VT = V.getValueType();
4653 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654
4655 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4656 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004657 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658
Craig Topper3d092db2012-03-21 02:14:01 +00004659 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660 return DAG.getUNDEF(VT.getVectorElementType());
4661
Craig Topperd156dc12012-02-06 07:17:51 +00004662 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004663 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4664 : SV->getOperand(1);
4665 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004666 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004667
4668 // Recurse into target specific vector shuffles to find scalars.
4669 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004670 MVT ShufVT = V.getValueType().getSimpleVT();
4671 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004672 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004673 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004674
Craig Topperd978c542012-05-06 19:46:21 +00004675 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004676 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004677
Craig Topper3d092db2012-03-21 02:14:01 +00004678 int Elt = ShuffleMask[Index];
4679 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004680 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004681
Craig Topper3d092db2012-03-21 02:14:01 +00004682 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004683 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004684 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004685 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004686 }
4687
4688 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004689 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004690 V = V.getOperand(0);
4691 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004692 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004693
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004694 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004695 return SDValue();
4696 }
4697
4698 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4699 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004700 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004701
4702 if (V.getOpcode() == ISD::BUILD_VECTOR)
4703 return V.getOperand(Index);
4704
4705 return SDValue();
4706}
4707
4708/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4709/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004710/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004711static
Craig Topper3d092db2012-03-21 02:14:01 +00004712unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004713 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004714 unsigned i;
4715 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004716 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004717 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004718 if (!(Elt.getNode() &&
4719 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4720 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004721 }
4722
4723 return i;
4724}
4725
Craig Topper3d092db2012-03-21 02:14:01 +00004726/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4727/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004728/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4729static
Craig Topper3d092db2012-03-21 02:14:01 +00004730bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4731 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4732 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004733 bool SeenV1 = false;
4734 bool SeenV2 = false;
4735
Craig Topper3d092db2012-03-21 02:14:01 +00004736 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004737 int Idx = SVOp->getMaskElt(i);
4738 // Ignore undef indicies
4739 if (Idx < 0)
4740 continue;
4741
Craig Topper3d092db2012-03-21 02:14:01 +00004742 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004743 SeenV1 = true;
4744 else
4745 SeenV2 = true;
4746
4747 // Only accept consecutive elements from the same vector
4748 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4749 return false;
4750 }
4751
4752 OpNum = SeenV1 ? 0 : 1;
4753 return true;
4754}
4755
4756/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4757/// logical left shift of a vector.
4758static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4759 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4760 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4761 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4762 false /* check zeros from right */, DAG);
4763 unsigned OpSrc;
4764
4765 if (!NumZeros)
4766 return false;
4767
4768 // Considering the elements in the mask that are not consecutive zeros,
4769 // check if they consecutively come from only one of the source vectors.
4770 //
4771 // V1 = {X, A, B, C} 0
4772 // \ \ \ /
4773 // vector_shuffle V1, V2 <1, 2, 3, X>
4774 //
4775 if (!isShuffleMaskConsecutive(SVOp,
4776 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004777 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004778 NumZeros, // Where to start looking in the src vector
4779 NumElems, // Number of elements in vector
4780 OpSrc)) // Which source operand ?
4781 return false;
4782
4783 isLeft = false;
4784 ShAmt = NumZeros;
4785 ShVal = SVOp->getOperand(OpSrc);
4786 return true;
4787}
4788
4789/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4790/// logical left shift of a vector.
4791static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4792 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4793 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4794 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4795 true /* check zeros from left */, DAG);
4796 unsigned OpSrc;
4797
4798 if (!NumZeros)
4799 return false;
4800
4801 // Considering the elements in the mask that are not consecutive zeros,
4802 // check if they consecutively come from only one of the source vectors.
4803 //
4804 // 0 { A, B, X, X } = V2
4805 // / \ / /
4806 // vector_shuffle V1, V2 <X, X, 4, 5>
4807 //
4808 if (!isShuffleMaskConsecutive(SVOp,
4809 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004810 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004811 0, // Where to start looking in the src vector
4812 NumElems, // Number of elements in vector
4813 OpSrc)) // Which source operand ?
4814 return false;
4815
4816 isLeft = true;
4817 ShAmt = NumZeros;
4818 ShVal = SVOp->getOperand(OpSrc);
4819 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004820}
4821
4822/// isVectorShift - Returns true if the shuffle can be implemented as a
4823/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004824static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004825 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004826 // Although the logic below support any bitwidth size, there are no
4827 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004828 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004829 return false;
4830
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004831 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4832 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4833 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004834
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004835 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004836}
4837
Evan Chengc78d3b42006-04-24 18:01:45 +00004838/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4839///
Dan Gohman475871a2008-07-27 21:46:04 +00004840static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004841 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004842 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004843 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004844 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004846 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004847
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004848 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004849 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004850 bool First = true;
4851 for (unsigned i = 0; i < 16; ++i) {
4852 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4853 if (ThisIsNonZero && First) {
4854 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004855 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004856 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004858 First = false;
4859 }
4860
4861 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004862 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4864 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004865 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004867 }
4868 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4870 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4871 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004872 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004874 } else
4875 ThisElt = LastElt;
4876
Gabor Greifba36cb52008-08-28 21:40:38 +00004877 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004879 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 }
4881 }
4882
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004883 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004884}
4885
Bill Wendlinga348c562007-03-22 18:42:45 +00004886/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004887///
Dan Gohman475871a2008-07-27 21:46:04 +00004888static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004889 unsigned NumNonZero, unsigned NumZero,
4890 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004891 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004892 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004893 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004894 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004895
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004896 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004897 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004898 bool First = true;
4899 for (unsigned i = 0; i < 8; ++i) {
4900 bool isNonZero = (NonZeros & (1 << i)) != 0;
4901 if (isNonZero) {
4902 if (First) {
4903 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004904 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004905 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004906 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004907 First = false;
4908 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004909 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004910 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004911 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004912 }
4913 }
4914
4915 return V;
4916}
4917
Evan Chengf26ffe92008-05-29 08:22:04 +00004918/// getVShift - Return a vector logical shift node.
4919///
Owen Andersone50ed302009-08-10 22:56:29 +00004920static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004921 unsigned NumBits, SelectionDAG &DAG,
4922 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004923 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004924 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004925 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004926 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4927 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004928 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004929 DAG.getConstant(NumBits,
4930 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004931}
4932
Dan Gohman475871a2008-07-27 21:46:04 +00004933SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004934X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004935 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004936
Evan Chengc3630942009-12-09 21:00:30 +00004937 // Check if the scalar load can be widened into a vector load. And if
4938 // the address is "base + cst" see if the cst can be "absorbed" into
4939 // the shuffle mask.
4940 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4941 SDValue Ptr = LD->getBasePtr();
4942 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4943 return SDValue();
4944 EVT PVT = LD->getValueType(0);
4945 if (PVT != MVT::i32 && PVT != MVT::f32)
4946 return SDValue();
4947
4948 int FI = -1;
4949 int64_t Offset = 0;
4950 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4951 FI = FINode->getIndex();
4952 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004953 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004954 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4955 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4956 Offset = Ptr.getConstantOperandVal(1);
4957 Ptr = Ptr.getOperand(0);
4958 } else {
4959 return SDValue();
4960 }
4961
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004962 // FIXME: 256-bit vector instructions don't require a strict alignment,
4963 // improve this code to support it better.
4964 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004965 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004966 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004967 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004968 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004969 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004970 // Can't change the alignment. FIXME: It's possible to compute
4971 // the exact stack offset and reference FI + adjust offset instead.
4972 // If someone *really* cares about this. That's the way to implement it.
4973 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004974 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004975 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004976 }
4977 }
4978
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004979 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004980 // Ptr + (Offset & ~15).
4981 if (Offset < 0)
4982 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004983 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004984 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004985 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004986 if (StartOffset)
4987 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4988 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4989
4990 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004991 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004992
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004993 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4994 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004995 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004996 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004997
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004998 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004999 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005000 Mask.push_back(EltNo);
5001
Craig Toppercc3000632012-01-30 07:50:31 +00005002 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005003 }
5004
5005 return SDValue();
5006}
5007
Michael J. Spencerec38de22010-10-10 22:04:20 +00005008/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5009/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005010/// load which has the same value as a build_vector whose operands are 'elts'.
5011///
5012/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005013///
Nate Begeman1449f292010-03-24 22:19:06 +00005014/// FIXME: we'd also like to handle the case where the last elements are zero
5015/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5016/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005017static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005018 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005019 EVT EltVT = VT.getVectorElementType();
5020 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005021
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 LoadSDNode *LDBase = NULL;
5023 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005024
Nate Begeman1449f292010-03-24 22:19:06 +00005025 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005026 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005027 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005028 for (unsigned i = 0; i < NumElems; ++i) {
5029 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005030
Nate Begemanfdea31a2010-03-24 20:49:50 +00005031 if (!Elt.getNode() ||
5032 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5033 return SDValue();
5034 if (!LDBase) {
5035 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5036 return SDValue();
5037 LDBase = cast<LoadSDNode>(Elt.getNode());
5038 LastLoadedElt = i;
5039 continue;
5040 }
5041 if (Elt.getOpcode() == ISD::UNDEF)
5042 continue;
5043
5044 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5045 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5046 return SDValue();
5047 LastLoadedElt = i;
5048 }
Nate Begeman1449f292010-03-24 22:19:06 +00005049
5050 // If we have found an entire vector of loads and undefs, then return a large
5051 // load of the entire vector width starting at the base pointer. If we found
5052 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005053 if (LastLoadedElt == NumElems - 1) {
5054 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005055 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005056 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005057 LDBase->isVolatile(), LDBase->isNonTemporal(),
5058 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005059 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005060 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005061 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005062 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005063 }
5064 if (NumElems == 4 && LastLoadedElt == 1 &&
5065 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005066 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5067 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005068 SDValue ResNode =
5069 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5070 LDBase->getPointerInfo(),
5071 LDBase->getAlignment(),
5072 false/*isVolatile*/, true/*ReadMem*/,
5073 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005074
5075 // Make sure the newly-created LOAD is in the same position as LDBase in
5076 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5077 // update uses of LDBase's output chain to use the TokenFactor.
5078 if (LDBase->hasAnyUseOfValue(1)) {
5079 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5080 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5081 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5082 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5083 SDValue(ResNode.getNode(), 1));
5084 }
5085
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005086 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005087 }
5088 return SDValue();
5089}
5090
Nadav Rotem9d68b062012-04-08 12:54:54 +00005091/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5092/// to generate a splat value for the following cases:
5093/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005094/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005095/// a scalar load, or a constant.
5096/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005097/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005098SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005099X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005100 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005101 return SDValue();
5102
Craig Topper45e1c752013-01-20 00:38:18 +00005103 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005104 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005105
Craig Topper5da8a802012-05-04 05:49:51 +00005106 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5107 "Unsupported vector type for broadcast.");
5108
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005109 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005110 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005111
Nadav Rotem9d68b062012-04-08 12:54:54 +00005112 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005113 default:
5114 // Unknown pattern found.
5115 return SDValue();
5116
5117 case ISD::BUILD_VECTOR: {
5118 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005119 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005120 return SDValue();
5121
Nadav Rotem9d68b062012-04-08 12:54:54 +00005122 Ld = Op.getOperand(0);
5123 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5124 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005125
5126 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005127 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005128 // Constants may have multiple users.
5129 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005130 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005131 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005132 }
5133
5134 case ISD::VECTOR_SHUFFLE: {
5135 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5136
5137 // Shuffles must have a splat mask where the first element is
5138 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005139 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005140 return SDValue();
5141
5142 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005143 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005144 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5145
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005146 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005147 return SDValue();
5148
5149 // Use the register form of the broadcast instruction available on AVX2.
5150 if (VT.is256BitVector())
5151 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5152 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5153 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154
5155 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005156 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005157 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005158
5159 // The scalar_to_vector node and the suspected
5160 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005161 // Constants may have multiple users.
5162 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005163 return SDValue();
5164 break;
5165 }
5166 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005167
Craig Topper7a9a28b2012-08-12 02:23:29 +00005168 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005169
5170 // Handle the broadcasting a single constant scalar from the constant pool
5171 // into a vector. On Sandybridge it is still better to load a constant vector
5172 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005173 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005174 EVT CVT = Ld.getValueType();
5175 assert(!CVT.isVector() && "Must not broadcast a vector type");
5176 unsigned ScalarSize = CVT.getSizeInBits();
5177
Craig Topper5da8a802012-05-04 05:49:51 +00005178 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005179 const Constant *C = 0;
5180 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5181 C = CI->getConstantIntValue();
5182 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5183 C = CF->getConstantFPValue();
5184
5185 assert(C && "Invalid constant type");
5186
Nadav Rotem154819d2012-04-09 07:45:58 +00005187 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005188 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005189 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005190 MachinePointerInfo::getConstantPool(),
5191 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005192
Nadav Rotem9d68b062012-04-08 12:54:54 +00005193 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5194 }
5195 }
5196
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005197 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005198 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5199
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005200 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005201 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005202 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5203 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5204
5205 // The scalar source must be a normal load.
5206 if (!IsLoad)
5207 return SDValue();
5208
Craig Topper5da8a802012-05-04 05:49:51 +00005209 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005210 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005211
Craig Toppera9376332012-01-10 08:23:59 +00005212 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005213 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005214 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005215 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005216 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005217 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005218
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005219 // Unsupported broadcast.
5220 return SDValue();
5221}
5222
Evan Chengc3630942009-12-09 21:00:30 +00005223SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005224X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5225 EVT VT = Op.getValueType();
5226
5227 // Skip if insert_vec_elt is not supported.
5228 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5229 return SDValue();
5230
5231 DebugLoc DL = Op.getDebugLoc();
5232 unsigned NumElems = Op.getNumOperands();
5233
5234 SDValue VecIn1;
5235 SDValue VecIn2;
5236 SmallVector<unsigned, 4> InsertIndices;
5237 SmallVector<int, 8> Mask(NumElems, -1);
5238
5239 for (unsigned i = 0; i != NumElems; ++i) {
5240 unsigned Opc = Op.getOperand(i).getOpcode();
5241
5242 if (Opc == ISD::UNDEF)
5243 continue;
5244
5245 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5246 // Quit if more than 1 elements need inserting.
5247 if (InsertIndices.size() > 1)
5248 return SDValue();
5249
5250 InsertIndices.push_back(i);
5251 continue;
5252 }
5253
5254 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5255 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5256
5257 // Quit if extracted from vector of different type.
5258 if (ExtractedFromVec.getValueType() != VT)
5259 return SDValue();
5260
5261 // Quit if non-constant index.
5262 if (!isa<ConstantSDNode>(ExtIdx))
5263 return SDValue();
5264
5265 if (VecIn1.getNode() == 0)
5266 VecIn1 = ExtractedFromVec;
5267 else if (VecIn1 != ExtractedFromVec) {
5268 if (VecIn2.getNode() == 0)
5269 VecIn2 = ExtractedFromVec;
5270 else if (VecIn2 != ExtractedFromVec)
5271 // Quit if more than 2 vectors to shuffle
5272 return SDValue();
5273 }
5274
5275 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5276
5277 if (ExtractedFromVec == VecIn1)
5278 Mask[i] = Idx;
5279 else if (ExtractedFromVec == VecIn2)
5280 Mask[i] = Idx + NumElems;
5281 }
5282
5283 if (VecIn1.getNode() == 0)
5284 return SDValue();
5285
5286 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5287 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5288 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5289 unsigned Idx = InsertIndices[i];
5290 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5291 DAG.getIntPtrConstant(Idx));
5292 }
5293
5294 return NV;
5295}
5296
5297SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005298X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005299 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005300
Craig Topper45e1c752013-01-20 00:38:18 +00005301 MVT VT = Op.getValueType().getSimpleVT();
5302 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005303 unsigned NumElems = Op.getNumOperands();
5304
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005305 // Vectors containing all zeros can be matched by pxor and xorps later
5306 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5307 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5308 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005309 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005310 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005311
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005312 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005313 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005315 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005316 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5317 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005318 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005319 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005320 return Op;
5321
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005322 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005323 }
5324
Nadav Rotem154819d2012-04-09 07:45:58 +00005325 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005326 if (Broadcast.getNode())
5327 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005328
Owen Andersone50ed302009-08-10 22:56:29 +00005329 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005330
Evan Cheng0db9fe62006-04-25 20:13:52 +00005331 unsigned NumZero = 0;
5332 unsigned NumNonZero = 0;
5333 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005334 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005335 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005336 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005337 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005338 if (Elt.getOpcode() == ISD::UNDEF)
5339 continue;
5340 Values.insert(Elt);
5341 if (Elt.getOpcode() != ISD::Constant &&
5342 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005343 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005344 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005345 NumZero++;
5346 else {
5347 NonZeros |= (1 << i);
5348 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005349 }
5350 }
5351
Chris Lattner97a2a562010-08-26 05:24:29 +00005352 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5353 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005354 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005355
Chris Lattner67f453a2008-03-09 05:42:06 +00005356 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005357 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005358 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005359 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005360
Chris Lattner62098042008-03-09 01:05:04 +00005361 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5362 // the value are obviously zero, truncate the value to i32 and do the
5363 // insertion that way. Only do this if the value is non-constant or if the
5364 // value is a constant being inserted into element 0. It is cheaper to do
5365 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005366 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005367 (!IsAllConstants || Idx == 0)) {
5368 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005369 // Handle SSE only.
5370 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5371 EVT VecVT = MVT::v4i32;
5372 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005373
Chris Lattner62098042008-03-09 01:05:04 +00005374 // Truncate the value (which may itself be a constant) to i32, and
5375 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005376 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005377 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005378 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005379
Chris Lattner62098042008-03-09 01:05:04 +00005380 // Now we have our 32-bit value zero extended in the low element of
5381 // a vector. If Idx != 0, swizzle it into place.
5382 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005383 SmallVector<int, 4> Mask;
5384 Mask.push_back(Idx);
5385 for (unsigned i = 1; i != VecElts; ++i)
5386 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005387 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005388 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005389 }
Craig Topper07a27622012-01-22 03:07:48 +00005390 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005391 }
5392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005393
Chris Lattner19f79692008-03-08 22:59:52 +00005394 // If we have a constant or non-constant insertion into the low element of
5395 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5396 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005397 // depending on what the source datatype is.
5398 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005399 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005400 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005401
5402 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005403 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005404 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005405 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005406 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5407 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005408 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005409 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005410 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5411 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005412 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005413 }
5414
5415 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005416 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005417 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005418 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005419 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005420 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005421 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005422 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005423 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005424 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005425 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005426 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005427 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005428
5429 // Is it a vector logical left shift?
5430 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005431 X86::isZeroNode(Op.getOperand(0)) &&
5432 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005433 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005434 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005435 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005436 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005437 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005438 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005439
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005440 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005441 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442
Chris Lattner19f79692008-03-08 22:59:52 +00005443 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5444 // is a non-constant being inserted into an element other than the low one,
5445 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5446 // movd/movss) to move this into the low element, then shuffle it into
5447 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005448 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005449 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005450
Evan Cheng0db9fe62006-04-25 20:13:52 +00005451 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005452 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005453 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005454 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005455 MaskVec.push_back(i == Idx ? 0 : 1);
5456 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005457 }
5458 }
5459
Chris Lattner67f453a2008-03-09 05:42:06 +00005460 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005461 if (Values.size() == 1) {
5462 if (EVTBits == 32) {
5463 // Instead of a shuffle like this:
5464 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5465 // Check if it's possible to issue this instead.
5466 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5467 unsigned Idx = CountTrailingZeros_32(NonZeros);
5468 SDValue Item = Op.getOperand(Idx);
5469 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5470 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5471 }
Dan Gohman475871a2008-07-27 21:46:04 +00005472 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005473 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005474
Dan Gohmana3941172007-07-24 22:55:08 +00005475 // A vector full of immediates; various special cases are already
5476 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005477 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005478 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005479
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005480 // For AVX-length vectors, build the individual 128-bit pieces and use
5481 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005482 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005483 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005484 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005485 V.push_back(Op.getOperand(i));
5486
5487 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5488
5489 // Build both the lower and upper subvector.
5490 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5491 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5492 NumElems/2);
5493
5494 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005495 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005496 }
5497
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005498 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005499 if (EVTBits == 64) {
5500 if (NumNonZero == 1) {
5501 // One half is zero or undef.
5502 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005503 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005504 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005505 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005506 }
Dan Gohman475871a2008-07-27 21:46:04 +00005507 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005508 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005509
5510 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005511 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005512 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005513 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005514 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005515 }
5516
Bill Wendling826f36f2007-03-28 00:57:11 +00005517 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005518 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005519 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005520 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005521 }
5522
5523 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005524 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005525 if (NumElems == 4 && NumZero > 0) {
5526 for (unsigned i = 0; i < 4; ++i) {
5527 bool isZero = !(NonZeros & (1 << i));
5528 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005529 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005530 else
Dale Johannesenace16102009-02-03 19:33:06 +00005531 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005532 }
5533
5534 for (unsigned i = 0; i < 2; ++i) {
5535 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5536 default: break;
5537 case 0:
5538 V[i] = V[i*2]; // Must be a zero vector.
5539 break;
5540 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005541 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005542 break;
5543 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005544 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005545 break;
5546 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005547 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005548 break;
5549 }
5550 }
5551
Benjamin Kramer9c683542012-01-30 15:16:21 +00005552 bool Reverse1 = (NonZeros & 0x3) == 2;
5553 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5554 int MaskVec[] = {
5555 Reverse1 ? 1 : 0,
5556 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005557 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5558 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005559 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005560 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005561 }
5562
Craig Topper7a9a28b2012-08-12 02:23:29 +00005563 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005564 // Check for a build vector of consecutive loads.
5565 for (unsigned i = 0; i < NumElems; ++i)
5566 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005567
Nate Begemanfdea31a2010-03-24 20:49:50 +00005568 // Check for elements which are consecutive loads.
5569 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5570 if (LD.getNode())
5571 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005572
Michael Liaofacace82012-10-19 17:15:18 +00005573 // Check for a build vector from mostly shuffle plus few inserting.
5574 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5575 if (Sh.getNode())
5576 return Sh;
5577
Michael J. Spencerec38de22010-10-10 22:04:20 +00005578 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005579 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005580 SDValue Result;
5581 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5582 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5583 else
5584 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005585
Chris Lattner24faf612010-08-28 17:59:08 +00005586 for (unsigned i = 1; i < NumElems; ++i) {
5587 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5588 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005589 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005590 }
5591 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005593
Chris Lattner6e80e442010-08-28 17:15:43 +00005594 // Otherwise, expand into a number of unpckl*, start by extending each of
5595 // our (non-undef) elements to the full vector width with the element in the
5596 // bottom slot of the vector (which generates no code for SSE).
5597 for (unsigned i = 0; i < NumElems; ++i) {
5598 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5599 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5600 else
5601 V[i] = DAG.getUNDEF(VT);
5602 }
5603
5604 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005605 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5606 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5607 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005608 unsigned EltStride = NumElems >> 1;
5609 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005610 for (unsigned i = 0; i < EltStride; ++i) {
5611 // If V[i+EltStride] is undef and this is the first round of mixing,
5612 // then it is safe to just drop this shuffle: V[i] is already in the
5613 // right place, the one element (since it's the first round) being
5614 // inserted as undef can be dropped. This isn't safe for successive
5615 // rounds because they will permute elements within both vectors.
5616 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5617 EltStride == NumElems/2)
5618 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005619
Chris Lattner6e80e442010-08-28 17:15:43 +00005620 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005621 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005622 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005623 }
5624 return V[0];
5625 }
Dan Gohman475871a2008-07-27 21:46:04 +00005626 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005627}
5628
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005629// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5630// to create 256-bit vectors from two other 128-bit ones.
5631static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5632 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005633 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005634
Craig Topper7a9a28b2012-08-12 02:23:29 +00005635 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005636
5637 SDValue V1 = Op.getOperand(0);
5638 SDValue V2 = Op.getOperand(1);
5639 unsigned NumElems = ResVT.getVectorNumElements();
5640
Craig Topper4c7972d2012-04-22 18:15:59 +00005641 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005642}
5643
Craig Topper55b24052012-09-11 06:15:32 +00005644static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005645 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005646
5647 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5648 // from two other 128-bit ones.
5649 return LowerAVXCONCAT_VECTORS(Op, DAG);
5650}
5651
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005652// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005653static SDValue
5654LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5655 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005656 SDValue V1 = SVOp->getOperand(0);
5657 SDValue V2 = SVOp->getOperand(1);
5658 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005659 MVT VT = SVOp->getValueType(0).getSimpleVT();
5660 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005661 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005662
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005663 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5664 return SDValue();
5665 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005666 return SDValue();
5667
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005668 // Check the mask for BLEND and build the value.
5669 unsigned MaskValue = 0;
5670 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
5671 unsigned NumLanes = (NumElems-1)/8 + 1;
5672 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005673
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005674 // Blend for v16i16 should be symetric for the both lanes.
5675 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005676
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005677 int SndLaneEltIdx = (NumLanes == 2) ?
5678 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005679 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005680
5681 if ((EltIdx == -1 || EltIdx == (int)i) &&
5682 (SndLaneEltIdx == -1 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
5683 continue;
5684
5685 if (((unsigned)EltIdx == (i + NumElems)) &&
5686 (SndLaneEltIdx == -1 ||
5687 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5688 MaskValue |= (1<<i);
5689 else
Craig Topper1842ba02012-04-23 06:38:28 +00005690 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005691 }
5692
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005693 // Convert i32 vectors to floating point if it is not AVX2.
5694 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
5695 EVT BlendVT = VT;
5696 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
5697 BlendVT = EVT::getVectorVT(*DAG.getContext(),
5698 EVT::getFloatingPointVT(EltVT.getSizeInBits()),
5699 NumElems);
5700 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5701 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5702 }
5703
5704 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5705 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005706 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005707}
5708
Nate Begemanb9a47b82009-02-23 08:49:38 +00005709// v8i16 shuffles - Prefer shuffles in the following order:
5710// 1. [all] pshuflw, pshufhw, optional move
5711// 2. [ssse3] 1 x pshufb
5712// 3. [ssse3] 2 x pshufb + 1 x por
5713// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005714static SDValue
5715LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5716 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005717 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005718 SDValue V1 = SVOp->getOperand(0);
5719 SDValue V2 = SVOp->getOperand(1);
5720 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005722
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 // Determine if more than 1 of the words in each of the low and high quadwords
5724 // of the result come from the same quadword of one of the two inputs. Undef
5725 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005726 unsigned LoQuad[] = { 0, 0, 0, 0 };
5727 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005728 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005730 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005731 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 MaskVals.push_back(EltIdx);
5733 if (EltIdx < 0) {
5734 ++Quad[0];
5735 ++Quad[1];
5736 ++Quad[2];
5737 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005738 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 }
5740 ++Quad[EltIdx / 4];
5741 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005742 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005743
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005745 unsigned MaxQuad = 1;
5746 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 if (LoQuad[i] > MaxQuad) {
5748 BestLoQuad = i;
5749 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005750 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005751 }
5752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005754 MaxQuad = 1;
5755 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 if (HiQuad[i] > MaxQuad) {
5757 BestHiQuad = i;
5758 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005759 }
5760 }
5761
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005763 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 // single pshufb instruction is necessary. If There are more than 2 input
5765 // quads, disable the next transformation since it does not help SSSE3.
5766 bool V1Used = InputQuads[0] || InputQuads[1];
5767 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005768 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005770 BestLoQuad = InputQuads[0] ? 0 : 1;
5771 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 }
5773 if (InputQuads.count() > 2) {
5774 BestLoQuad = -1;
5775 BestHiQuad = -1;
5776 }
5777 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005778
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5780 // the shuffle mask. If a quad is scored as -1, that means that it contains
5781 // words from all 4 input quadwords.
5782 SDValue NewV;
5783 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005784 int MaskV[] = {
5785 BestLoQuad < 0 ? 0 : BestLoQuad,
5786 BestHiQuad < 0 ? 1 : BestHiQuad
5787 };
Eric Christopherfd179292009-08-27 18:07:15 +00005788 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005789 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5790 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5791 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005792
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5794 // source words for the shuffle, to aid later transformations.
5795 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005796 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005797 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005799 if (idx != (int)i)
5800 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005802 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 AllWordsInNewV = false;
5804 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005805 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005806
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5808 if (AllWordsInNewV) {
5809 for (int i = 0; i != 8; ++i) {
5810 int idx = MaskVals[i];
5811 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005812 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005813 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 if ((idx != i) && idx < 4)
5815 pshufhw = false;
5816 if ((idx != i) && idx > 3)
5817 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005818 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 V1 = NewV;
5820 V2Used = false;
5821 BestLoQuad = 0;
5822 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005823 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005824
Nate Begemanb9a47b82009-02-23 08:49:38 +00005825 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5826 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005827 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005828 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5829 unsigned TargetMask = 0;
5830 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005831 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005832 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5833 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5834 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005835 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005836 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005837 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005838 }
Eric Christopherfd179292009-08-27 18:07:15 +00005839
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 // If we have SSSE3, and all words of the result are from 1 input vector,
5841 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5842 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005843 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005844 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005845
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005847 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 // mask, and elements that come from V1 in the V2 mask, so that the two
5849 // results can be OR'd together.
5850 bool TwoInputs = V1Used && V2Used;
5851 for (unsigned i = 0; i != 8; ++i) {
5852 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005853 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5854 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005855 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005856 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005858 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005859 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005860 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005861 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005863 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005864
Nate Begemanb9a47b82009-02-23 08:49:38 +00005865 // Calculate the shuffle mask for the second input, shuffle it, and
5866 // OR it with the first shuffled input.
5867 pshufbMask.clear();
5868 for (unsigned i = 0; i != 8; ++i) {
5869 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005870 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5871 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5872 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5873 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005875 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005876 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005877 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 MVT::v16i8, &pshufbMask[0], 16));
5879 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005880 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 }
5882
5883 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5884 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005885 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005887 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 for (int i = 0; i != 4; ++i) {
5889 int idx = MaskVals[i];
5890 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 InOrder.set(i);
5892 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005893 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005894 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 }
5896 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005898 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005899
Craig Topperdd637ae2012-02-19 05:41:45 +00005900 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5901 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005902 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005903 NewV.getOperand(0),
5904 getShufflePSHUFLWImmediate(SVOp), DAG);
5905 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 }
Eric Christopherfd179292009-08-27 18:07:15 +00005907
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5909 // and update MaskVals with the new element order.
5910 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005911 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 for (unsigned i = 4; i != 8; ++i) {
5913 int idx = MaskVals[i];
5914 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 InOrder.set(i);
5916 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005917 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 }
5920 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005922 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005923
Craig Topperdd637ae2012-02-19 05:41:45 +00005924 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5925 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005926 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005927 NewV.getOperand(0),
5928 getShufflePSHUFHWImmediate(SVOp), DAG);
5929 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 }
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 // In case BestHi & BestLo were both -1, which means each quadword has a word
5933 // from each of the four input quadwords, calculate the InOrder bitvector now
5934 // before falling through to the insert/extract cleanup.
5935 if (BestLoQuad == -1 && BestHiQuad == -1) {
5936 NewV = V1;
5937 for (int i = 0; i != 8; ++i)
5938 if (MaskVals[i] < 0 || MaskVals[i] == i)
5939 InOrder.set(i);
5940 }
Eric Christopherfd179292009-08-27 18:07:15 +00005941
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 // The other elements are put in the right place using pextrw and pinsrw.
5943 for (unsigned i = 0; i != 8; ++i) {
5944 if (InOrder[i])
5945 continue;
5946 int EltIdx = MaskVals[i];
5947 if (EltIdx < 0)
5948 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005949 SDValue ExtOp = (EltIdx < 8) ?
5950 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5951 DAG.getIntPtrConstant(EltIdx)) :
5952 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005954 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 DAG.getIntPtrConstant(i));
5956 }
5957 return NewV;
5958}
5959
5960// v16i8 shuffles - Prefer shuffles in the following order:
5961// 1. [ssse3] 1 x pshufb
5962// 2. [ssse3] 2 x pshufb + 1 x por
5963// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5964static
Nate Begeman9008ca62009-04-27 18:41:29 +00005965SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005966 SelectionDAG &DAG,
5967 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005968 SDValue V1 = SVOp->getOperand(0);
5969 SDValue V2 = SVOp->getOperand(1);
5970 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005971 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005972
Nate Begemanb9a47b82009-02-23 08:49:38 +00005973 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005974 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005975 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005976
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005978 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005980
Nate Begemanb9a47b82009-02-23 08:49:38 +00005981 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005982 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005983 //
5984 // Otherwise, we have elements from both input vectors, and must zero out
5985 // elements that come from V2 in the first mask, and V1 in the second mask
5986 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 for (unsigned i = 0; i != 16; ++i) {
5988 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005989 if (EltIdx < 0 || EltIdx >= 16)
5990 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005991 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005992 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005993 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005994 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005995 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005996
5997 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5998 // the 2nd operand if it's undefined or zero.
5999 if (V2.getOpcode() == ISD::UNDEF ||
6000 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006001 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006002
Nate Begemanb9a47b82009-02-23 08:49:38 +00006003 // Calculate the shuffle mask for the second input, shuffle it, and
6004 // OR it with the first shuffled input.
6005 pshufbMask.clear();
6006 for (unsigned i = 0; i != 16; ++i) {
6007 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006008 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006009 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006012 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006013 MVT::v16i8, &pshufbMask[0], 16));
6014 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006015 }
Eric Christopherfd179292009-08-27 18:07:15 +00006016
Nate Begemanb9a47b82009-02-23 08:49:38 +00006017 // No SSSE3 - Calculate in place words and then fix all out of place words
6018 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6019 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006020 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6021 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006022 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006023 for (int i = 0; i != 8; ++i) {
6024 int Elt0 = MaskVals[i*2];
6025 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006026
Nate Begemanb9a47b82009-02-23 08:49:38 +00006027 // This word of the result is all undef, skip it.
6028 if (Elt0 < 0 && Elt1 < 0)
6029 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006030
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006032 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006033 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006034
Nate Begemanb9a47b82009-02-23 08:49:38 +00006035 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6036 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6037 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006038
6039 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6040 // using a single extract together, load it and store it.
6041 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006042 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006043 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006044 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006045 DAG.getIntPtrConstant(i));
6046 continue;
6047 }
6048
Nate Begemanb9a47b82009-02-23 08:49:38 +00006049 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006050 // source byte is not also odd, shift the extracted word left 8 bits
6051 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006052 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006053 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006054 DAG.getIntPtrConstant(Elt1 / 2));
6055 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006056 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006057 DAG.getConstant(8,
6058 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006059 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006060 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6061 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006062 }
6063 // If Elt0 is defined, extract it from the appropriate source. If the
6064 // source byte is not also even, shift the extracted word right 8 bits. If
6065 // Elt1 was also defined, OR the extracted values together before
6066 // inserting them in the result.
6067 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006069 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6070 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006071 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006072 DAG.getConstant(8,
6073 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006074 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006075 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6076 DAG.getConstant(0x00FF, MVT::i16));
6077 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006078 : InsElt0;
6079 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006080 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006081 DAG.getIntPtrConstant(i));
6082 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006083 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006084}
6085
Elena Demikhovsky41789462012-09-06 12:42:01 +00006086// v32i8 shuffles - Translate to VPSHUFB if possible.
6087static
6088SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006089 const X86Subtarget *Subtarget,
6090 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006091 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006092 SDValue V1 = SVOp->getOperand(0);
6093 SDValue V2 = SVOp->getOperand(1);
6094 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006095 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006096
6097 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006098 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6099 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006100
Michael Liao471b9172012-10-03 23:43:52 +00006101 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006102 // (1) one of input vector is undefined or zeroinitializer.
6103 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6104 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006105 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006106 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006107 return SDValue();
6108
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006109 if (V1IsAllZero && !V2IsAllZero) {
6110 CommuteVectorShuffleMask(MaskVals, 32);
6111 V1 = V2;
6112 }
6113 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006114 for (unsigned i = 0; i != 32; i++) {
6115 int EltIdx = MaskVals[i];
6116 if (EltIdx < 0 || EltIdx >= 32)
6117 EltIdx = 0x80;
6118 else {
6119 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6120 // Cross lane is not allowed.
6121 return SDValue();
6122 EltIdx &= 0xf;
6123 }
6124 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6125 }
6126 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6127 DAG.getNode(ISD::BUILD_VECTOR, dl,
6128 MVT::v32i8, &pshufbMask[0], 32));
6129}
6130
Evan Cheng7a831ce2007-12-15 03:00:47 +00006131/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006132/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006133/// done when every pair / quad of shuffle mask elements point to elements in
6134/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006135/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006136static
Nate Begeman9008ca62009-04-27 18:41:29 +00006137SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006138 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006139 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006140 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006141 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006142 MVT NewVT;
6143 unsigned Scale;
6144 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006145 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006146 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6147 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6148 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6149 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6150 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6151 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006152 }
6153
Nate Begeman9008ca62009-04-27 18:41:29 +00006154 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006155 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006156 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006157 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006158 int EltIdx = SVOp->getMaskElt(i+j);
6159 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006160 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006161 if (StartIdx < 0)
6162 StartIdx = (EltIdx / Scale);
6163 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006164 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006165 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006166 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006167 }
6168
Craig Topper11ac1f82012-05-04 04:08:44 +00006169 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6170 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006171 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006172}
6173
Evan Chengd880b972008-05-09 21:53:03 +00006174/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006175///
Craig Topperf84b7502013-01-20 00:50:58 +00006176static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006177 SDValue SrcOp, SelectionDAG &DAG,
6178 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006179 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006180 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006181 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006182 LD = dyn_cast<LoadSDNode>(SrcOp);
6183 if (!LD) {
6184 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6185 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006186 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006187 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006188 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006189 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006190 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006191 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006192 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006193 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006194 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6195 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6196 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006197 SrcOp.getOperand(0)
6198 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006199 }
6200 }
6201 }
6202
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006203 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006204 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006205 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006206 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006207}
6208
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006209/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6210/// which could not be matched by any known target speficic shuffle
6211static SDValue
6212LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006213
6214 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6215 if (NewOp.getNode())
6216 return NewOp;
6217
Craig Topper657a99c2013-01-19 23:36:09 +00006218 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006219
Craig Topper8f35c132012-01-20 09:29:03 +00006220 unsigned NumElems = VT.getVectorNumElements();
6221 unsigned NumLaneElems = NumElems / 2;
6222
Craig Topper8f35c132012-01-20 09:29:03 +00006223 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006224 MVT EltVT = VT.getVectorElementType();
6225 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006226 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006227
Craig Topper9a2b6e12012-04-06 07:45:23 +00006228 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006229 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006230 // Build a shuffle mask for the output, discovering on the fly which
6231 // input vectors to use as shuffle operands (recorded in InputUsed).
6232 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006233 // out with UseBuildVector set.
6234 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006235 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006236 unsigned LaneStart = l * NumLaneElems;
6237 for (unsigned i = 0; i != NumLaneElems; ++i) {
6238 // The mask element. This indexes into the input.
6239 int Idx = SVOp->getMaskElt(i+LaneStart);
6240 if (Idx < 0) {
6241 // the mask element does not index into any input vector.
6242 Mask.push_back(-1);
6243 continue;
6244 }
Craig Topper8f35c132012-01-20 09:29:03 +00006245
Craig Topper9a2b6e12012-04-06 07:45:23 +00006246 // The input vector this mask element indexes into.
6247 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006248
Craig Topper9a2b6e12012-04-06 07:45:23 +00006249 // Turn the index into an offset from the start of the input vector.
6250 Idx -= Input * NumLaneElems;
6251
6252 // Find or create a shuffle vector operand to hold this input.
6253 unsigned OpNo;
6254 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6255 if (InputUsed[OpNo] == Input)
6256 // This input vector is already an operand.
6257 break;
6258 if (InputUsed[OpNo] < 0) {
6259 // Create a new operand for this input vector.
6260 InputUsed[OpNo] = Input;
6261 break;
6262 }
6263 }
6264
6265 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006266 // More than two input vectors used! Give up on trying to create a
6267 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6268 UseBuildVector = true;
6269 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006270 }
6271
6272 // Add the mask index for the new shuffle vector.
6273 Mask.push_back(Idx + OpNo * NumLaneElems);
6274 }
6275
Craig Topper8ae97ba2012-05-21 06:40:16 +00006276 if (UseBuildVector) {
6277 SmallVector<SDValue, 16> SVOps;
6278 for (unsigned i = 0; i != NumLaneElems; ++i) {
6279 // The mask element. This indexes into the input.
6280 int Idx = SVOp->getMaskElt(i+LaneStart);
6281 if (Idx < 0) {
6282 SVOps.push_back(DAG.getUNDEF(EltVT));
6283 continue;
6284 }
6285
6286 // The input vector this mask element indexes into.
6287 int Input = Idx / NumElems;
6288
6289 // Turn the index into an offset from the start of the input vector.
6290 Idx -= Input * NumElems;
6291
6292 // Extract the vector element by hand.
6293 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6294 SVOp->getOperand(Input),
6295 DAG.getIntPtrConstant(Idx)));
6296 }
6297
6298 // Construct the output using a BUILD_VECTOR.
6299 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6300 SVOps.size());
6301 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006302 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006303 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006304 } else {
6305 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006306 (InputUsed[0] % 2) * NumLaneElems,
6307 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006308 // If only one input was used, use an undefined vector for the other.
6309 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6310 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006311 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006312 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006313 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006314 }
6315
6316 Mask.clear();
6317 }
Craig Topper8f35c132012-01-20 09:29:03 +00006318
6319 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006320 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006321}
6322
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006323/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6324/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006325static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006326LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006327 SDValue V1 = SVOp->getOperand(0);
6328 SDValue V2 = SVOp->getOperand(1);
6329 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006330 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006331
Craig Topper7a9a28b2012-08-12 02:23:29 +00006332 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006333
Benjamin Kramer9c683542012-01-30 15:16:21 +00006334 std::pair<int, int> Locs[4];
6335 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006336 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006337
Evan Chengace3c172008-07-22 21:13:36 +00006338 unsigned NumHi = 0;
6339 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006340 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006341 int Idx = PermMask[i];
6342 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006343 Locs[i] = std::make_pair(-1, -1);
6344 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006345 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6346 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006347 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006348 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006349 NumLo++;
6350 } else {
6351 Locs[i] = std::make_pair(1, NumHi);
6352 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006353 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006354 NumHi++;
6355 }
6356 }
6357 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006358
Evan Chengace3c172008-07-22 21:13:36 +00006359 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006360 // If no more than two elements come from either vector. This can be
6361 // implemented with two shuffles. First shuffle gather the elements.
6362 // The second shuffle, which takes the first shuffle as both of its
6363 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006364 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006365
Benjamin Kramer9c683542012-01-30 15:16:21 +00006366 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006367
Benjamin Kramer9c683542012-01-30 15:16:21 +00006368 for (unsigned i = 0; i != 4; ++i)
6369 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006370 unsigned Idx = (i < 2) ? 0 : 4;
6371 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006372 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006373 }
Evan Chengace3c172008-07-22 21:13:36 +00006374
Nate Begeman9008ca62009-04-27 18:41:29 +00006375 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006376 }
6377
6378 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006379 // Otherwise, we must have three elements from one vector, call it X, and
6380 // one element from the other, call it Y. First, use a shufps to build an
6381 // intermediate vector with the one element from Y and the element from X
6382 // that will be in the same half in the final destination (the indexes don't
6383 // matter). Then, use a shufps to build the final vector, taking the half
6384 // containing the element from Y from the intermediate, and the other half
6385 // from X.
6386 if (NumHi == 3) {
6387 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006388 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006389 std::swap(V1, V2);
6390 }
6391
6392 // Find the element from V2.
6393 unsigned HiIndex;
6394 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006395 int Val = PermMask[HiIndex];
6396 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006397 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006398 if (Val >= 4)
6399 break;
6400 }
6401
Nate Begeman9008ca62009-04-27 18:41:29 +00006402 Mask1[0] = PermMask[HiIndex];
6403 Mask1[1] = -1;
6404 Mask1[2] = PermMask[HiIndex^1];
6405 Mask1[3] = -1;
6406 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006407
6408 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006409 Mask1[0] = PermMask[0];
6410 Mask1[1] = PermMask[1];
6411 Mask1[2] = HiIndex & 1 ? 6 : 4;
6412 Mask1[3] = HiIndex & 1 ? 4 : 6;
6413 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006414 }
Craig Topper69947b92012-04-23 06:57:04 +00006415
6416 Mask1[0] = HiIndex & 1 ? 2 : 0;
6417 Mask1[1] = HiIndex & 1 ? 0 : 2;
6418 Mask1[2] = PermMask[2];
6419 Mask1[3] = PermMask[3];
6420 if (Mask1[2] >= 0)
6421 Mask1[2] += 4;
6422 if (Mask1[3] >= 0)
6423 Mask1[3] += 4;
6424 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006425 }
6426
6427 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006428 int LoMask[] = { -1, -1, -1, -1 };
6429 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006430
Benjamin Kramer9c683542012-01-30 15:16:21 +00006431 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006432 unsigned MaskIdx = 0;
6433 unsigned LoIdx = 0;
6434 unsigned HiIdx = 2;
6435 for (unsigned i = 0; i != 4; ++i) {
6436 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006437 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006438 MaskIdx = 1;
6439 LoIdx = 0;
6440 HiIdx = 2;
6441 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006442 int Idx = PermMask[i];
6443 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006444 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006445 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006446 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006447 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006448 LoIdx++;
6449 } else {
6450 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006451 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006452 HiIdx++;
6453 }
6454 }
6455
Nate Begeman9008ca62009-04-27 18:41:29 +00006456 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6457 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006458 int MaskOps[] = { -1, -1, -1, -1 };
6459 for (unsigned i = 0; i != 4; ++i)
6460 if (Locs[i].first != -1)
6461 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006462 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006463}
6464
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006465static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006466 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006467 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006468
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006469 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6470 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006471 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6472 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6473 // BUILD_VECTOR (load), undef
6474 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006475
6476 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006477}
6478
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006479static
Evan Cheng835580f2010-10-07 20:50:20 +00006480SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6481 EVT VT = Op.getValueType();
6482
6483 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006484 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6485 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006486 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6487 V1, DAG));
6488}
6489
6490static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006491SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006492 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006493 SDValue V1 = Op.getOperand(0);
6494 SDValue V2 = Op.getOperand(1);
6495 EVT VT = Op.getValueType();
6496
6497 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6498
Craig Topper1accb7e2012-01-10 06:54:16 +00006499 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006500 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6501
Evan Cheng0899f5c2011-08-31 02:05:24 +00006502 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6503 return DAG.getNode(ISD::BITCAST, dl, VT,
6504 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6505 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6506 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006507}
6508
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006509static
6510SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6511 SDValue V1 = Op.getOperand(0);
6512 SDValue V2 = Op.getOperand(1);
6513 EVT VT = Op.getValueType();
6514
6515 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6516 "unsupported shuffle type");
6517
6518 if (V2.getOpcode() == ISD::UNDEF)
6519 V2 = V1;
6520
6521 // v4i32 or v4f32
6522 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6523}
6524
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006525static
Craig Topper1accb7e2012-01-10 06:54:16 +00006526SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006527 SDValue V1 = Op.getOperand(0);
6528 SDValue V2 = Op.getOperand(1);
6529 EVT VT = Op.getValueType();
6530 unsigned NumElems = VT.getVectorNumElements();
6531
6532 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6533 // operand of these instructions is only memory, so check if there's a
6534 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6535 // same masks.
6536 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006537
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006538 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006539 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006540 CanFoldLoad = true;
6541
6542 // When V1 is a load, it can be folded later into a store in isel, example:
6543 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6544 // turns into:
6545 // (MOVLPSmr addr:$src1, VR128:$src2)
6546 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006547 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006548 CanFoldLoad = true;
6549
Dan Gohman65fd6562011-11-03 21:49:52 +00006550 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006551 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006552 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006553 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6554
6555 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006556 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006557 if (SVOp->getMaskElt(1) != -1)
6558 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006559 }
6560
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006561 // movl and movlp will both match v2i64, but v2i64 is never matched by
6562 // movl earlier because we make it strict to avoid messing with the movlp load
6563 // folding logic (see the code above getMOVLP call). Match it here then,
6564 // this is horrible, but will stay like this until we move all shuffle
6565 // matching to x86 specific nodes. Note that for the 1st condition all
6566 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006567 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006568 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6569 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006570 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006571 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006572 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006573 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006574
6575 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6576
6577 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006578 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006579 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006580}
6581
Michael Liaod9d09602012-10-23 17:34:00 +00006582// Reduce a vector shuffle to zext.
6583SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006584X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006585 // PMOVZX is only available from SSE41.
6586 if (!Subtarget->hasSSE41())
6587 return SDValue();
6588
6589 EVT VT = Op.getValueType();
6590
6591 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006592 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006593 return SDValue();
6594
6595 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6596 DebugLoc DL = Op.getDebugLoc();
6597 SDValue V1 = Op.getOperand(0);
6598 SDValue V2 = Op.getOperand(1);
6599 unsigned NumElems = VT.getVectorNumElements();
6600
6601 // Extending is an unary operation and the element type of the source vector
6602 // won't be equal to or larger than i64.
6603 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6604 VT.getVectorElementType() == MVT::i64)
6605 return SDValue();
6606
6607 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6608 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006609 while ((1U << Shift) < NumElems) {
6610 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006611 break;
6612 Shift += 1;
6613 // The maximal ratio is 8, i.e. from i8 to i64.
6614 if (Shift > 3)
6615 return SDValue();
6616 }
6617
6618 // Check the shuffle mask.
6619 unsigned Mask = (1U << Shift) - 1;
6620 for (unsigned i = 0; i != NumElems; ++i) {
6621 int EltIdx = SVOp->getMaskElt(i);
6622 if ((i & Mask) != 0 && EltIdx != -1)
6623 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006624 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006625 return SDValue();
6626 }
6627
6628 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6629 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6630 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6631
6632 if (!isTypeLegal(NVT))
6633 return SDValue();
6634
6635 // Simplify the operand as it's prepared to be fed into shuffle.
6636 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6637 if (V1.getOpcode() == ISD::BITCAST &&
6638 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6639 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6640 V1.getOperand(0)
6641 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6642 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6643 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006644 ConstantSDNode *CIdx =
6645 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006646 // If it's foldable, i.e. normal load with single use, we will let code
6647 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006648 if (CIdx && CIdx->getZExtValue() == 0 &&
6649 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006650 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6651 }
6652
6653 return DAG.getNode(ISD::BITCAST, DL, VT,
6654 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6655}
6656
Nadav Rotem154819d2012-04-09 07:45:58 +00006657SDValue
6658X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006659 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006660 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006661 DebugLoc dl = Op.getDebugLoc();
6662 SDValue V1 = Op.getOperand(0);
6663 SDValue V2 = Op.getOperand(1);
6664
6665 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006666 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006667
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006668 // Handle splat operations
6669 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006670 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006671
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006672 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006673 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006674 if (Broadcast.getNode())
6675 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006676
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006677 // Handle splats by matching through known shuffle masks
Craig Topper5a529e42013-01-18 06:44:29 +00006678 if ((VT.is128BitVector() && NumElem <= 4) ||
6679 (VT.is256BitVector() && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006680 return SDValue();
6681
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006682 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006683 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006684 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006685
Michael Liaod9d09602012-10-23 17:34:00 +00006686 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006687 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006688 if (NewOp.getNode())
6689 return NewOp;
6690
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006691 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6692 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006693 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6694 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006695 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006696 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006697 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006698 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006699 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006700 // FIXME: Figure out a cleaner way to do this.
6701 // Try to make use of movq to zero out the top part.
6702 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006703 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006704 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006705 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006706 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6707 NewVT, true, false))
6708 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006709 DAG, Subtarget, dl);
6710 }
6711 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006712 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006713 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006714 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006715 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6716 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6717 DAG, Subtarget, dl);
6718 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006719 }
6720 }
6721 return SDValue();
6722}
6723
Dan Gohman475871a2008-07-27 21:46:04 +00006724SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006725X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006726 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006727 SDValue V1 = Op.getOperand(0);
6728 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006729 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006730 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006731 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006732 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006733 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006734 bool V1IsSplat = false;
6735 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006736 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006737 bool HasFp256 = Subtarget->hasFp256();
6738 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006739 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006740 bool OptForSize = MF.getFunction()->getAttributes().
6741 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006742
Craig Topper3426a3e2011-11-14 06:46:21 +00006743 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006744
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006745 if (V1IsUndef && V2IsUndef)
6746 return DAG.getUNDEF(VT);
6747
6748 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006749
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006750 // Vector shuffle lowering takes 3 steps:
6751 //
6752 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6753 // narrowing and commutation of operands should be handled.
6754 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6755 // shuffle nodes.
6756 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6757 // so the shuffle can be broken into other shuffles and the legalizer can
6758 // try the lowering again.
6759 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006760 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006761 // be matched during isel, all of them must be converted to a target specific
6762 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006763
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006764 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6765 // narrowing and commutation of operands should be handled. The actual code
6766 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006767 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006768 if (NewOp.getNode())
6769 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006770
Craig Topper5aaffa82012-02-19 02:53:47 +00006771 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6772
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006773 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6774 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006775 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006776 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006777 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006778 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006779
Craig Topperdd637ae2012-02-19 05:41:45 +00006780 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006781 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006782 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006783
Craig Topperdd637ae2012-02-19 05:41:45 +00006784 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006785 return getMOVHighToLow(Op, dl, DAG);
6786
6787 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006788 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006789 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006790 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006791
Craig Topper5aaffa82012-02-19 02:53:47 +00006792 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006793 // The actual implementation will match the mask in the if above and then
6794 // during isel it can match several different instructions, not only pshufd
6795 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006796 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6797 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006798
Craig Topper5aaffa82012-02-19 02:53:47 +00006799 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006800
Craig Topper1accb7e2012-01-10 06:54:16 +00006801 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006802 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6803
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006804 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6805 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6806 DAG);
6807
Craig Topperb3982da2011-12-31 23:50:21 +00006808 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006809 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006810 }
Eric Christopherfd179292009-08-27 18:07:15 +00006811
Evan Chengf26ffe92008-05-29 08:22:04 +00006812 // Check if this can be converted into a logical shift.
6813 bool isLeft = false;
6814 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006815 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006816 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006817 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006818 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006819 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006820 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006821 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006822 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006823 }
Eric Christopherfd179292009-08-27 18:07:15 +00006824
Craig Topper5aaffa82012-02-19 02:53:47 +00006825 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006826 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006827 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006828 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006829 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006830 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6831
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006832 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006833 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6834 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006835 }
Eric Christopherfd179292009-08-27 18:07:15 +00006836
Nate Begeman9008ca62009-04-27 18:41:29 +00006837 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006838 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006839 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006840
Craig Topperdd637ae2012-02-19 05:41:45 +00006841 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006842 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006843
Craig Topperdd637ae2012-02-19 05:41:45 +00006844 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006845 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006846
Craig Topperdd637ae2012-02-19 05:41:45 +00006847 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006848 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006849
Craig Topperdd637ae2012-02-19 05:41:45 +00006850 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006851 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006852
Craig Topperdd637ae2012-02-19 05:41:45 +00006853 if (ShouldXformToMOVHLPS(M, VT) ||
6854 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006855 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006856
Evan Chengf26ffe92008-05-29 08:22:04 +00006857 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006858 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006859 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006860 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006861 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006862 }
Eric Christopherfd179292009-08-27 18:07:15 +00006863
Evan Cheng9eca5e82006-10-25 21:49:50 +00006864 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006865 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6866 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006867 V1IsSplat = isSplatVector(V1.getNode());
6868 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006869
Chris Lattner8a594482007-11-25 00:24:49 +00006870 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006871 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6872 CommuteVectorShuffleMask(M, NumElems);
6873 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006874 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006875 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006876 }
6877
Craig Topperbeabc6c2011-12-05 06:56:46 +00006878 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006879 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006880 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006881 return V1;
6882 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6883 // the instruction selector will not match, so get a canonical MOVL with
6884 // swapped operands to undo the commute.
6885 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006886 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006887
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006888 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006889 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006890
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006891 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006892 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006893
Evan Cheng9bbbb982006-10-25 20:48:19 +00006894 if (V2IsSplat) {
6895 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006896 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006897 // new vector_shuffle with the corrected mask.p
6898 SmallVector<int, 8> NewMask(M.begin(), M.end());
6899 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006900 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006901 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006902 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006903 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006904 }
6905
Evan Cheng9eca5e82006-10-25 21:49:50 +00006906 if (Commuted) {
6907 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006908 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006909 CommuteVectorShuffleMask(M, NumElems);
6910 std::swap(V1, V2);
6911 std::swap(V1IsSplat, V2IsSplat);
6912 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006913
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006914 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006915 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006916
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006917 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006918 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006919 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920
Nate Begeman9008ca62009-04-27 18:41:29 +00006921 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006922 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006923 return CommuteVectorShuffle(SVOp, DAG);
6924
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006925 // The checks below are all present in isShuffleMaskLegal, but they are
6926 // inlined here right now to enable us to directly emit target specific
6927 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006928
Craig Topper0e2037b2012-01-20 05:53:00 +00006929 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006930 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006931 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006932 DAG);
6933
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006934 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6935 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006936 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006937 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006938 }
6939
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006940 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006941 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006942 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006943 DAG);
6944
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006945 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006946 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006947 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006948 DAG);
6949
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006950 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006951 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006952 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006953
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006954 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006955 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006956 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006957 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006958
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006959 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006960 // Generate target specific nodes for 128 or 256-bit shuffles only
6961 // supported in the AVX instruction set.
6962 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006963
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006964 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006965 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006966 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6967
Craig Topper70b883b2011-11-28 10:14:51 +00006968 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006969 if (isVPERMILPMask(M, VT, HasFp256)) {
6970 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006971 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006972 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006973 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006974 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006975 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006976
Craig Topper70b883b2011-11-28 10:14:51 +00006977 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006978 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00006979 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006980 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006981
Craig Topper1842ba02012-04-23 06:38:28 +00006982 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006983 if (BlendOp.getNode())
6984 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006985
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006986 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006987 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006988 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006989 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006990 }
Craig Topper92040742012-04-16 06:43:40 +00006991 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6992 &permclMask[0], 8);
6993 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006994 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006995 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006996 }
Craig Topper095c5282012-04-15 23:48:57 +00006997
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006998 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00006999 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007000 getShuffleCLImmediate(SVOp), DAG);
7001
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007002 //===--------------------------------------------------------------------===//
7003 // Since no target specific shuffle was selected for this generic one,
7004 // lower it into other known shuffles. FIXME: this isn't true yet, but
7005 // this is the plan.
7006 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007007
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007008 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7009 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007010 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007011 if (NewOp.getNode())
7012 return NewOp;
7013 }
7014
7015 if (VT == MVT::v16i8) {
7016 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7017 if (NewOp.getNode())
7018 return NewOp;
7019 }
7020
Elena Demikhovsky41789462012-09-06 12:42:01 +00007021 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007022 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007023 if (NewOp.getNode())
7024 return NewOp;
7025 }
7026
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007027 // Handle all 128-bit wide vectors with 4 elements, and match them with
7028 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007029 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007030 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7031
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007032 // Handle general 256-bit shuffles
7033 if (VT.is256BitVector())
7034 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7035
Dan Gohman475871a2008-07-27 21:46:04 +00007036 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007037}
7038
Craig Topperf84b7502013-01-20 00:50:58 +00007039static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007040 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007041 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007042
Craig Topper45e1c752013-01-20 00:38:18 +00007043 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007044 return SDValue();
7045
Duncan Sands83ec4b62008-06-06 12:08:01 +00007046 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007047 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007048 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007050 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007051 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007052 }
7053
7054 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007055 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7056 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7057 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007058 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7059 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007060 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007062 Op.getOperand(0)),
7063 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007064 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007065 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007067 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007068 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007069 }
7070
7071 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007072 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7073 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007074 // result has a single use which is a store or a bitcast to i32. And in
7075 // the case of a store, it's not worth it if the index is a constant 0,
7076 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007077 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007078 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007079 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007080 if ((User->getOpcode() != ISD::STORE ||
7081 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7082 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007083 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007085 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007087 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007088 Op.getOperand(0)),
7089 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007090 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007091 }
7092
7093 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007094 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007095 if (isa<ConstantSDNode>(Op.getOperand(1)))
7096 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007097 }
Dan Gohman475871a2008-07-27 21:46:04 +00007098 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007099}
7100
Dan Gohman475871a2008-07-27 21:46:04 +00007101SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007102X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7103 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007104 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007105 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007106
David Greene74a579d2011-02-10 16:57:36 +00007107 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007108 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007109
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007110 // If this is a 256-bit vector result, first extract the 128-bit vector and
7111 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007112 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007113 DebugLoc dl = Op.getNode()->getDebugLoc();
7114 unsigned NumElems = VecVT.getVectorNumElements();
7115 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007116 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7117
7118 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007119 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007120
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007121 if (IdxVal >= NumElems/2)
7122 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007123 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007124 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007125 }
7126
Craig Topper7a9a28b2012-08-12 02:23:29 +00007127 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007128
Craig Topperd0a31172012-01-10 06:37:29 +00007129 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007130 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007131 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007132 return Res;
7133 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007134
Craig Topper45e1c752013-01-20 00:38:18 +00007135 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007136 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007137 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007138 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007139 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007140 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007141 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007142 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7143 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007144 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007145 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007146 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007147 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007148 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007149 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007150 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007151 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007152 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007153 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007154 }
7155
7156 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007157 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007158 if (Idx == 0)
7159 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007160
Evan Cheng0db9fe62006-04-25 20:13:52 +00007161 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007162 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007163 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007164 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007165 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007166 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007167 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007168 }
7169
7170 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007171 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7172 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7173 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007174 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007175 if (Idx == 0)
7176 return Op;
7177
7178 // UNPCKHPD the element to the lowest double word, then movsd.
7179 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7180 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007181 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007182 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007183 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007184 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007185 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007186 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007187 }
7188
Dan Gohman475871a2008-07-27 21:46:04 +00007189 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007190}
7191
Craig Topperf84b7502013-01-20 00:50:58 +00007192static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007193 MVT VT = Op.getValueType().getSimpleVT();
7194 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007195 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007196
Dan Gohman475871a2008-07-27 21:46:04 +00007197 SDValue N0 = Op.getOperand(0);
7198 SDValue N1 = Op.getOperand(1);
7199 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007200
Craig Topper7a9a28b2012-08-12 02:23:29 +00007201 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007202 return SDValue();
7203
Dan Gohman8a55ce42009-09-23 21:02:20 +00007204 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007205 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007206 unsigned Opc;
7207 if (VT == MVT::v8i16)
7208 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007209 else if (VT == MVT::v16i8)
7210 Opc = X86ISD::PINSRB;
7211 else
7212 Opc = X86ISD::PINSRB;
7213
Nate Begeman14d12ca2008-02-11 04:19:36 +00007214 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7215 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007216 if (N1.getValueType() != MVT::i32)
7217 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7218 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007219 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007220 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007221 }
7222
7223 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007224 // Bits [7:6] of the constant are the source select. This will always be
7225 // zero here. The DAG Combiner may combine an extract_elt index into these
7226 // bits. For example (insert (extract, 3), 2) could be matched by putting
7227 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007228 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007229 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007230 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007231 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007232 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007233 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007234 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007235 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007236 }
7237
7238 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007239 // PINSR* works with constant index.
7240 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007241 }
Dan Gohman475871a2008-07-27 21:46:04 +00007242 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007243}
7244
Dan Gohman475871a2008-07-27 21:46:04 +00007245SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007246X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007247 MVT VT = Op.getValueType().getSimpleVT();
7248 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007249
David Greene6b381262011-02-09 15:32:06 +00007250 DebugLoc dl = Op.getDebugLoc();
7251 SDValue N0 = Op.getOperand(0);
7252 SDValue N1 = Op.getOperand(1);
7253 SDValue N2 = Op.getOperand(2);
7254
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007255 // If this is a 256-bit vector result, first extract the 128-bit vector,
7256 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007257 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007258 if (!isa<ConstantSDNode>(N2))
7259 return SDValue();
7260
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007261 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007262 unsigned NumElems = VT.getVectorNumElements();
7263 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007264 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007265
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007266 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007267 bool Upper = IdxVal >= NumElems/2;
7268 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7269 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007270
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007271 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007272 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007273 }
7274
Craig Topperd0a31172012-01-10 06:37:29 +00007275 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007276 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7277
Dan Gohman8a55ce42009-09-23 21:02:20 +00007278 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007279 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007280
Dan Gohman8a55ce42009-09-23 21:02:20 +00007281 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007282 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7283 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007284 if (N1.getValueType() != MVT::i32)
7285 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7286 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007287 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007288 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007289 }
Dan Gohman475871a2008-07-27 21:46:04 +00007290 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007291}
7292
Craig Topper55b24052012-09-11 06:15:32 +00007293static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007294 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007295 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007296 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007297
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007298 // If this is a 256-bit vector result, first insert into a 128-bit
7299 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007300 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007301 // Insert into a 128-bit vector.
7302 EVT VT128 = EVT::getVectorVT(*Context,
7303 OpVT.getVectorElementType(),
7304 OpVT.getVectorNumElements() / 2);
7305
7306 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7307
7308 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007309 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007310 }
7311
Craig Topperd77d2fe2012-04-29 20:22:05 +00007312 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007313 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007315
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007317 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007318 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007319 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007320}
7321
David Greene91585092011-01-26 15:38:49 +00007322// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7323// a simple subregister reference or explicit instructions to grab
7324// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007325static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7326 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007327 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007328 DebugLoc dl = Op.getNode()->getDebugLoc();
7329 SDValue Vec = Op.getNode()->getOperand(0);
7330 SDValue Idx = Op.getNode()->getOperand(1);
7331
Craig Topper7a9a28b2012-08-12 02:23:29 +00007332 if (Op.getNode()->getValueType(0).is128BitVector() &&
7333 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007334 isa<ConstantSDNode>(Idx)) {
7335 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7336 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007337 }
David Greene91585092011-01-26 15:38:49 +00007338 }
7339 return SDValue();
7340}
7341
David Greenecfe33c42011-01-26 19:13:22 +00007342// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7343// simple superregister reference or explicit instructions to insert
7344// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007345static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7346 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007347 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007348 DebugLoc dl = Op.getNode()->getDebugLoc();
7349 SDValue Vec = Op.getNode()->getOperand(0);
7350 SDValue SubVec = Op.getNode()->getOperand(1);
7351 SDValue Idx = Op.getNode()->getOperand(2);
7352
Craig Topper7a9a28b2012-08-12 02:23:29 +00007353 if (Op.getNode()->getValueType(0).is256BitVector() &&
7354 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007355 isa<ConstantSDNode>(Idx)) {
7356 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7357 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007358 }
7359 }
7360 return SDValue();
7361}
7362
Bill Wendling056292f2008-09-16 21:48:12 +00007363// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7364// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7365// one of the above mentioned nodes. It has to be wrapped because otherwise
7366// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7367// be used to form addressing mode. These wrapped nodes will be selected
7368// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007369SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007370X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007371 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007372
Chris Lattner41621a22009-06-26 19:22:52 +00007373 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7374 // global base reg.
7375 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007376 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007377 CodeModel::Model M = getTargetMachine().getCodeModel();
7378
Chris Lattner4f066492009-07-11 20:29:19 +00007379 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007380 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007381 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007382 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007383 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007384 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007385 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007386
Evan Cheng1606e8e2009-03-13 07:51:59 +00007387 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007388 CP->getAlignment(),
7389 CP->getOffset(), OpFlag);
7390 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007391 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007392 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007393 if (OpFlag) {
7394 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007395 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007396 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007397 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007398 }
7399
7400 return Result;
7401}
7402
Dan Gohmand858e902010-04-17 15:26:15 +00007403SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007404 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007405
Chris Lattner18c59872009-06-27 04:16:01 +00007406 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7407 // global base reg.
7408 unsigned char OpFlag = 0;
7409 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007410 CodeModel::Model M = getTargetMachine().getCodeModel();
7411
Chris Lattner4f066492009-07-11 20:29:19 +00007412 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007413 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007414 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007415 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007416 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007417 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007418 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007419
Chris Lattner18c59872009-06-27 04:16:01 +00007420 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7421 OpFlag);
7422 DebugLoc DL = JT->getDebugLoc();
7423 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007424
Chris Lattner18c59872009-06-27 04:16:01 +00007425 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007426 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007427 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7428 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007429 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007430 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007431
Chris Lattner18c59872009-06-27 04:16:01 +00007432 return Result;
7433}
7434
7435SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007436X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007437 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007438
Chris Lattner18c59872009-06-27 04:16:01 +00007439 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7440 // global base reg.
7441 unsigned char OpFlag = 0;
7442 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007443 CodeModel::Model M = getTargetMachine().getCodeModel();
7444
Chris Lattner4f066492009-07-11 20:29:19 +00007445 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007446 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7447 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7448 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007449 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007450 } else if (Subtarget->isPICStyleGOT()) {
7451 OpFlag = X86II::MO_GOT;
7452 } else if (Subtarget->isPICStyleStubPIC()) {
7453 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7454 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7455 OpFlag = X86II::MO_DARWIN_NONLAZY;
7456 }
Eric Christopherfd179292009-08-27 18:07:15 +00007457
Chris Lattner18c59872009-06-27 04:16:01 +00007458 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007459
Chris Lattner18c59872009-06-27 04:16:01 +00007460 DebugLoc DL = Op.getDebugLoc();
7461 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007462
Chris Lattner18c59872009-06-27 04:16:01 +00007463 // With PIC, the address is actually $g + Offset.
7464 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007465 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007466 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7467 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007468 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007469 Result);
7470 }
Eric Christopherfd179292009-08-27 18:07:15 +00007471
Eli Friedman586272d2011-08-11 01:48:05 +00007472 // For symbols that require a load from a stub to get the address, emit the
7473 // load.
7474 if (isGlobalStubReference(OpFlag))
7475 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007476 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007477
Chris Lattner18c59872009-06-27 04:16:01 +00007478 return Result;
7479}
7480
Dan Gohman475871a2008-07-27 21:46:04 +00007481SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007482X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007483 // Create the TargetBlockAddressAddress node.
7484 unsigned char OpFlags =
7485 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007486 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007487 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007488 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007489 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007490 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7491 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007492
Dan Gohmanf705adb2009-10-30 01:28:02 +00007493 if (Subtarget->isPICStyleRIPRel() &&
7494 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007495 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7496 else
7497 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007498
Dan Gohman29cbade2009-11-20 23:18:13 +00007499 // With PIC, the address is actually $g + Offset.
7500 if (isGlobalRelativeToPICBase(OpFlags)) {
7501 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7502 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7503 Result);
7504 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007505
7506 return Result;
7507}
7508
7509SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007510X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007511 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007512 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007513 // Create the TargetGlobalAddress node, folding in the constant
7514 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007515 unsigned char OpFlags =
7516 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007517 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007518 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007519 if (OpFlags == X86II::MO_NO_FLAG &&
7520 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007521 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007522 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007523 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007524 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007525 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007526 }
Eric Christopherfd179292009-08-27 18:07:15 +00007527
Chris Lattner4f066492009-07-11 20:29:19 +00007528 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007529 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007530 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7531 else
7532 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007533
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007534 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007535 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007536 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7537 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007538 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007539 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007540
Chris Lattner36c25012009-07-10 07:34:39 +00007541 // For globals that require a load from a stub to get the address, emit the
7542 // load.
7543 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007544 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007545 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007546
Dan Gohman6520e202008-10-18 02:06:02 +00007547 // If there was a non-zero offset that we didn't fold, create an explicit
7548 // addition for it.
7549 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007550 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007551 DAG.getConstant(Offset, getPointerTy()));
7552
Evan Cheng0db9fe62006-04-25 20:13:52 +00007553 return Result;
7554}
7555
Evan Chengda43bcf2008-09-24 00:05:32 +00007556SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007557X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007558 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007559 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007560 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007561}
7562
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007563static SDValue
7564GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007565 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007566 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007567 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007568 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007569 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007570 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007571 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007572 GA->getOffset(),
7573 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007574
7575 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7576 : X86ISD::TLSADDR;
7577
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007578 if (InFlag) {
7579 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007580 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007581 } else {
7582 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007583 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007584 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007585
7586 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007587 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007588
Rafael Espindola15f1b662009-04-24 12:59:40 +00007589 SDValue Flag = Chain.getValue(1);
7590 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007591}
7592
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007593// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007594static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007595LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007596 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007597 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007598 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7599 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007600 DAG.getNode(X86ISD::GlobalBaseReg,
7601 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007602 InFlag = Chain.getValue(1);
7603
Chris Lattnerb903bed2009-06-26 21:20:29 +00007604 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007605}
7606
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007607// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007608static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007609LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007610 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007611 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7612 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007613}
7614
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007615static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7616 SelectionDAG &DAG,
7617 const EVT PtrVT,
7618 bool is64Bit) {
7619 DebugLoc dl = GA->getDebugLoc();
7620
7621 // Get the start address of the TLS block for this module.
7622 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7623 .getInfo<X86MachineFunctionInfo>();
7624 MFI->incNumLocalDynamicTLSAccesses();
7625
7626 SDValue Base;
7627 if (is64Bit) {
7628 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7629 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7630 } else {
7631 SDValue InFlag;
7632 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7633 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7634 InFlag = Chain.getValue(1);
7635 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7636 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7637 }
7638
7639 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7640 // of Base.
7641
7642 // Build x@dtpoff.
7643 unsigned char OperandFlags = X86II::MO_DTPOFF;
7644 unsigned WrapperKind = X86ISD::Wrapper;
7645 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7646 GA->getValueType(0),
7647 GA->getOffset(), OperandFlags);
7648 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7649
7650 // Add x@dtpoff with the base.
7651 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7652}
7653
Hans Wennborg228756c2012-05-11 10:11:01 +00007654// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007655static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007656 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007657 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007658 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007659
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007660 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7661 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7662 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007663
Michael J. Spencerec38de22010-10-10 22:04:20 +00007664 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007665 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007666 MachinePointerInfo(Ptr),
7667 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007668
Chris Lattnerb903bed2009-06-26 21:20:29 +00007669 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007670 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7671 // initialexec.
7672 unsigned WrapperKind = X86ISD::Wrapper;
7673 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007674 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007675 } else if (model == TLSModel::InitialExec) {
7676 if (is64Bit) {
7677 OperandFlags = X86II::MO_GOTTPOFF;
7678 WrapperKind = X86ISD::WrapperRIP;
7679 } else {
7680 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7681 }
Chris Lattner18c59872009-06-27 04:16:01 +00007682 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007683 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007684 }
Eric Christopherfd179292009-08-27 18:07:15 +00007685
Hans Wennborg228756c2012-05-11 10:11:01 +00007686 // emit "addl x@ntpoff,%eax" (local exec)
7687 // or "addl x@indntpoff,%eax" (initial exec)
7688 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007689 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007690 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007691 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007692 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007693
Hans Wennborg228756c2012-05-11 10:11:01 +00007694 if (model == TLSModel::InitialExec) {
7695 if (isPIC && !is64Bit) {
7696 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7697 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7698 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007699 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007700
7701 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7702 MachinePointerInfo::getGOT(), false, false, false,
7703 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007704 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007705
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007706 // The address of the thread local variable is the add of the thread
7707 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007708 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007709}
7710
Dan Gohman475871a2008-07-27 21:46:04 +00007711SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007712X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007713
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007714 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007715 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007716
Eric Christopher30ef0e52010-06-03 04:07:48 +00007717 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007718 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007719
Eric Christopher30ef0e52010-06-03 04:07:48 +00007720 switch (model) {
7721 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007722 if (Subtarget->is64Bit())
7723 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7724 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007725 case TLSModel::LocalDynamic:
7726 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7727 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007728 case TLSModel::InitialExec:
7729 case TLSModel::LocalExec:
7730 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007731 Subtarget->is64Bit(),
7732 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007733 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007734 llvm_unreachable("Unknown TLS model.");
7735 }
7736
7737 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007738 // Darwin only has one model of TLS. Lower to that.
7739 unsigned char OpFlag = 0;
7740 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7741 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007742
Eric Christopher30ef0e52010-06-03 04:07:48 +00007743 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7744 // global base reg.
7745 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7746 !Subtarget->is64Bit();
7747 if (PIC32)
7748 OpFlag = X86II::MO_TLVP_PIC_BASE;
7749 else
7750 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007751 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007752 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007753 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007754 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007755 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007756
Eric Christopher30ef0e52010-06-03 04:07:48 +00007757 // With PIC32, the address is actually $g + Offset.
7758 if (PIC32)
7759 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7760 DAG.getNode(X86ISD::GlobalBaseReg,
7761 DebugLoc(), getPointerTy()),
7762 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007763
Eric Christopher30ef0e52010-06-03 04:07:48 +00007764 // Lowering the machine isd will make sure everything is in the right
7765 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007766 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007767 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007768 SDValue Args[] = { Chain, Offset };
7769 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007770
Eric Christopher30ef0e52010-06-03 04:07:48 +00007771 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7772 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7773 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007774
Eric Christopher30ef0e52010-06-03 04:07:48 +00007775 // And our return value (tls address) is in the standard call return value
7776 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007777 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007778 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7779 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007780 }
7781
7782 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007783 // Just use the implicit TLS architecture
7784 // Need to generate someting similar to:
7785 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7786 // ; from TEB
7787 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7788 // mov rcx, qword [rdx+rcx*8]
7789 // mov eax, .tls$:tlsvar
7790 // [rax+rcx] contains the address
7791 // Windows 64bit: gs:0x58
7792 // Windows 32bit: fs:__tls_array
7793
7794 // If GV is an alias then use the aliasee for determining
7795 // thread-localness.
7796 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7797 GV = GA->resolveAliasedGlobal(false);
7798 DebugLoc dl = GA->getDebugLoc();
7799 SDValue Chain = DAG.getEntryNode();
7800
7801 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7802 // %gs:0x58 (64-bit).
7803 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7804 ? Type::getInt8PtrTy(*DAG.getContext(),
7805 256)
7806 : Type::getInt32PtrTy(*DAG.getContext(),
7807 257));
7808
7809 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7810 Subtarget->is64Bit()
7811 ? DAG.getIntPtrConstant(0x58)
7812 : DAG.getExternalSymbol("_tls_array",
7813 getPointerTy()),
7814 MachinePointerInfo(Ptr),
7815 false, false, false, 0);
7816
7817 // Load the _tls_index variable
7818 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7819 if (Subtarget->is64Bit())
7820 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7821 IDX, MachinePointerInfo(), MVT::i32,
7822 false, false, 0);
7823 else
7824 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7825 false, false, false, 0);
7826
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007827 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007828 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007829 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7830
7831 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7832 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7833 false, false, false, 0);
7834
7835 // Get the offset of start of .tls section
7836 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7837 GA->getValueType(0),
7838 GA->getOffset(), X86II::MO_SECREL);
7839 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7840
7841 // The address of the thread local variable is the add of the thread
7842 // pointer with the offset of the variable.
7843 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007844 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007845
David Blaikie4d6ccb52012-01-20 21:51:11 +00007846 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007847}
7848
Chad Rosierb90d2a92012-01-03 23:19:12 +00007849/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7850/// and take a 2 x i32 value to shift plus a shift amount.
7851SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007852 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007853 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007854 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007855 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007856 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007857 SDValue ShOpLo = Op.getOperand(0);
7858 SDValue ShOpHi = Op.getOperand(1);
7859 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007860 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007861 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007862 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007863
Dan Gohman475871a2008-07-27 21:46:04 +00007864 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007865 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007866 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7867 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007868 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007869 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7870 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007871 }
Evan Chenge3413162006-01-09 18:33:28 +00007872
Owen Anderson825b72b2009-08-11 20:47:22 +00007873 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7874 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007875 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007877
Dan Gohman475871a2008-07-27 21:46:04 +00007878 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007879 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007880 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7881 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007882
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007883 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007884 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7885 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007886 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007887 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7888 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007889 }
7890
Dan Gohman475871a2008-07-27 21:46:04 +00007891 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007892 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007893}
Evan Chenga3195e82006-01-12 22:54:21 +00007894
Dan Gohmand858e902010-04-17 15:26:15 +00007895SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7896 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007897 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007898
Dale Johannesen0488fb62010-09-30 23:57:10 +00007899 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007900 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007901
Owen Anderson825b72b2009-08-11 20:47:22 +00007902 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007903 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007904
Eli Friedman36df4992009-05-27 00:47:34 +00007905 // These are really Legal; return the operand so the caller accepts it as
7906 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007907 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007908 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007909 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007910 Subtarget->is64Bit()) {
7911 return Op;
7912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007913
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007914 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007915 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007916 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007917 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007918 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007919 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007920 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007921 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007922 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007923 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7924}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007925
Owen Andersone50ed302009-08-10 22:56:29 +00007926SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007927 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007928 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007929 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007930 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007931 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007932 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007933 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007934 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007935 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007936 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007937
Chris Lattner492a43e2010-09-22 01:28:21 +00007938 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007939
Stuart Hastings84be9582011-06-02 15:57:11 +00007940 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7941 MachineMemOperand *MMO;
7942 if (FI) {
7943 int SSFI = FI->getIndex();
7944 MMO =
7945 DAG.getMachineFunction()
7946 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7947 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7948 } else {
7949 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7950 StackSlot = StackSlot.getOperand(1);
7951 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007952 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007953 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7954 X86ISD::FILD, DL,
7955 Tys, Ops, array_lengthof(Ops),
7956 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007957
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007958 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007959 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007960 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007961
7962 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7963 // shouldn't be necessary except that RFP cannot be live across
7964 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007965 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007966 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7967 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007968 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007969 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007970 SDValue Ops[] = {
7971 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7972 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007973 MachineMemOperand *MMO =
7974 DAG.getMachineFunction()
7975 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007976 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007977
Chris Lattner492a43e2010-09-22 01:28:21 +00007978 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7979 Ops, array_lengthof(Ops),
7980 Op.getValueType(), MMO);
7981 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007982 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007983 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007984 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007985
Evan Cheng0db9fe62006-04-25 20:13:52 +00007986 return Result;
7987}
7988
Bill Wendling8b8a6362009-01-17 03:56:04 +00007989// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007990SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7991 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007992 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007993 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007994 movq %rax, %xmm0
7995 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7996 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7997 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007998 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007999 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008000 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008001 addpd %xmm1, %xmm0
8002 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008003 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008004
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008005 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008006 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008007
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008008 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008009 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8010 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008011 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008012
Chris Lattner97484792012-01-25 09:56:22 +00008013 SmallVector<Constant*,2> CV1;
8014 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008015 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008016 CV1.push_back(
8017 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8018 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008019 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008020
Bill Wendling397ae212012-01-05 02:13:20 +00008021 // Load the 64-bit value into an XMM register.
8022 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8023 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008024 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008025 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008026 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008027 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8028 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8029 CLod0);
8030
Owen Anderson825b72b2009-08-11 20:47:22 +00008031 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008032 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008033 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008034 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008035 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008036 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008037
Craig Topperd0a31172012-01-10 06:37:29 +00008038 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008039 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8040 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8041 } else {
8042 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8043 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8044 S2F, 0x4E, DAG);
8045 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8046 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8047 Sub);
8048 }
8049
8050 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008051 DAG.getIntPtrConstant(0));
8052}
8053
Bill Wendling8b8a6362009-01-17 03:56:04 +00008054// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008055SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8056 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008057 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008058 // FP constant to bias correct the final result.
8059 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008060 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008061
8062 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008064 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008065
Eli Friedmanf3704762011-08-29 21:15:46 +00008066 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008067 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008068
Owen Anderson825b72b2009-08-11 20:47:22 +00008069 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008070 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008071 DAG.getIntPtrConstant(0));
8072
8073 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008074 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008075 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008076 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008078 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008079 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 MVT::v2f64, Bias)));
8081 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008082 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008083 DAG.getIntPtrConstant(0));
8084
8085 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008086 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008087
8088 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008089 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008090
Craig Topper69947b92012-04-23 06:57:04 +00008091 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008092 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008093 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008094 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008095 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008096
8097 // Handle final rounding.
8098 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008099}
8100
Michael Liaoa7554632012-10-23 17:36:08 +00008101SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8102 SelectionDAG &DAG) const {
8103 SDValue N0 = Op.getOperand(0);
8104 EVT SVT = N0.getValueType();
8105 DebugLoc dl = Op.getDebugLoc();
8106
8107 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8108 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8109 "Custom UINT_TO_FP is not supported!");
8110
8111 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8112 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8113 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8114}
8115
Dan Gohmand858e902010-04-17 15:26:15 +00008116SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8117 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008118 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008119 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008120
Michael Liaoa7554632012-10-23 17:36:08 +00008121 if (Op.getValueType().isVector())
8122 return lowerUINT_TO_FP_vec(Op, DAG);
8123
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008124 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008125 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8126 // the optimization here.
8127 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008128 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008129
Owen Andersone50ed302009-08-10 22:56:29 +00008130 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008131 EVT DstVT = Op.getValueType();
8132 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008133 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008134 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008135 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008136 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008137 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008138
8139 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008140 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008141 if (SrcVT == MVT::i32) {
8142 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8143 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8144 getPointerTy(), StackSlot, WordOff);
8145 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008146 StackSlot, MachinePointerInfo(),
8147 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008148 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008149 OffsetSlot, MachinePointerInfo(),
8150 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008151 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8152 return Fild;
8153 }
8154
8155 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8156 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008157 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008158 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008159 // For i64 source, we need to add the appropriate power of 2 if the input
8160 // was negative. This is the same as the optimization in
8161 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8162 // we must be careful to do the computation in x87 extended precision, not
8163 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008164 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8165 MachineMemOperand *MMO =
8166 DAG.getMachineFunction()
8167 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8168 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008169
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008170 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8171 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008172 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8173 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008174
8175 APInt FF(32, 0x5F800000ULL);
8176
8177 // Check whether the sign bit is set.
8178 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8179 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8180 ISD::SETLT);
8181
8182 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8183 SDValue FudgePtr = DAG.getConstantPool(
8184 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8185 getPointerTy());
8186
8187 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8188 SDValue Zero = DAG.getIntPtrConstant(0);
8189 SDValue Four = DAG.getIntPtrConstant(4);
8190 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8191 Zero, Four);
8192 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8193
8194 // Load the value out, extending it from f32 to f80.
8195 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008196 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008197 FudgePtr, MachinePointerInfo::getConstantPool(),
8198 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008199 // Extend everything to 80 bits to force it to be done on x87.
8200 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8201 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008202}
8203
Dan Gohman475871a2008-07-27 21:46:04 +00008204std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008205FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008206 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008207
Owen Andersone50ed302009-08-10 22:56:29 +00008208 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008209
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008210 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008211 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8212 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008213 }
8214
Owen Anderson825b72b2009-08-11 20:47:22 +00008215 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8216 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008217 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008218
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008219 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008220 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008221 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008222 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008223 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008224 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008225 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008226 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008227
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008228 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8229 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008230 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008231 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008232 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008233 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008234
Evan Cheng0db9fe62006-04-25 20:13:52 +00008235 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008236 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8237 Opc = X86ISD::WIN_FTOL;
8238 else
8239 switch (DstTy.getSimpleVT().SimpleTy) {
8240 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8241 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8242 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8243 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8244 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008245
Dan Gohman475871a2008-07-27 21:46:04 +00008246 SDValue Chain = DAG.getEntryNode();
8247 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008248 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008249 // FIXME This causes a redundant load/store if the SSE-class value is already
8250 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008251 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008252 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008253 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008254 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008255 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008256 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008257 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008258 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008259 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008260
Chris Lattner492a43e2010-09-22 01:28:21 +00008261 MachineMemOperand *MMO =
8262 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8263 MachineMemOperand::MOLoad, MemSize, MemSize);
8264 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8265 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008266 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008267 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008268 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8269 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008270
Chris Lattner07290932010-09-22 01:05:16 +00008271 MachineMemOperand *MMO =
8272 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8273 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008274
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008275 if (Opc != X86ISD::WIN_FTOL) {
8276 // Build the FP_TO_INT*_IN_MEM
8277 SDValue Ops[] = { Chain, Value, StackSlot };
8278 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8279 Ops, 3, DstTy, MMO);
8280 return std::make_pair(FIST, StackSlot);
8281 } else {
8282 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8283 DAG.getVTList(MVT::Other, MVT::Glue),
8284 Chain, Value);
8285 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8286 MVT::i32, ftol.getValue(1));
8287 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8288 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008289 SDValue Ops[] = { eax, edx };
8290 SDValue pair = IsReplace
8291 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8292 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008293 return std::make_pair(pair, SDValue());
8294 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008295}
8296
Nadav Rotem0509db22012-12-28 05:45:24 +00008297static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8298 const X86Subtarget *Subtarget) {
8299 EVT VT = Op->getValueType(0);
8300 SDValue In = Op->getOperand(0);
8301 EVT InVT = In.getValueType();
8302 DebugLoc dl = Op->getDebugLoc();
8303
8304 // Optimize vectors in AVX mode:
8305 //
8306 // v8i16 -> v8i32
8307 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8308 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8309 // Concat upper and lower parts.
8310 //
8311 // v4i32 -> v4i64
8312 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8313 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8314 // Concat upper and lower parts.
8315 //
8316
8317 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8318 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8319 return SDValue();
8320
8321 if (Subtarget->hasInt256())
8322 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8323
8324 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8325 SDValue Undef = DAG.getUNDEF(InVT);
8326 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8327 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8328 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8329
8330 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8331 VT.getVectorNumElements()/2);
8332
8333 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8334 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8335
8336 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8337}
8338
8339SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8340 SelectionDAG &DAG) const {
8341 if (Subtarget->hasFp256()) {
8342 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8343 if (Res.getNode())
8344 return Res;
8345 }
8346
8347 return SDValue();
8348}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008349SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8350 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008351 DebugLoc DL = Op.getDebugLoc();
8352 EVT VT = Op.getValueType();
8353 SDValue In = Op.getOperand(0);
8354 EVT SVT = In.getValueType();
8355
Nadav Rotem0509db22012-12-28 05:45:24 +00008356 if (Subtarget->hasFp256()) {
8357 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8358 if (Res.getNode())
8359 return Res;
8360 }
8361
Michael Liaoa7554632012-10-23 17:36:08 +00008362 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8363 VT.getVectorNumElements() != SVT.getVectorNumElements())
8364 return SDValue();
8365
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008366 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008367
8368 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008369 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008370 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8371
8372 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8373 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8374 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008375 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8376 DAG.getUNDEF(MVT::v8i16),
8377 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008378
8379 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8380}
8381
Michael Liaobedcbd42012-10-16 18:14:11 +00008382SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8383 DebugLoc DL = Op.getDebugLoc();
8384 EVT VT = Op.getValueType();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008385 SDValue In = Op.getOperand(0);
8386 EVT SVT = In.getValueType();
Michael Liaobedcbd42012-10-16 18:14:11 +00008387
Nadav Rotem3c22a442012-12-27 07:45:10 +00008388 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8389 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8390 if (Subtarget->hasInt256()) {
8391 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8392 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8393 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8394 ShufMask);
8395 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8396 DAG.getIntPtrConstant(0));
8397 }
8398
8399 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8400 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8401 DAG.getIntPtrConstant(0));
8402 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8403 DAG.getIntPtrConstant(2));
8404
8405 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8406 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8407
8408 // The PSHUFD mask:
8409 static const int ShufMask1[] = {0, 2, 0, 0};
8410 SDValue Undef = DAG.getUNDEF(VT);
8411 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8412 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8413
8414 // The MOVLHPS mask:
8415 static const int ShufMask2[] = {0, 1, 4, 5};
8416 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8417 }
8418
8419 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8420 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8421 if (Subtarget->hasInt256()) {
8422 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8423
8424 SmallVector<SDValue,32> pshufbMask;
8425 for (unsigned i = 0; i < 2; ++i) {
8426 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8427 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8428 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8429 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8430 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8431 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8432 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8433 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8434 for (unsigned j = 0; j < 8; ++j)
8435 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8436 }
8437 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8438 &pshufbMask[0], 32);
8439 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8440 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8441
8442 static const int ShufMask[] = {0, 2, -1, -1};
8443 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8444 &ShufMask[0]);
8445 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8446 DAG.getIntPtrConstant(0));
8447 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8448 }
8449
8450 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8451 DAG.getIntPtrConstant(0));
8452
8453 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8454 DAG.getIntPtrConstant(4));
8455
8456 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8457 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8458
8459 // The PSHUFB mask:
8460 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8461 -1, -1, -1, -1, -1, -1, -1, -1};
8462
8463 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8464 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8465 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8466
8467 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8468 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8469
8470 // The MOVLHPS Mask:
8471 static const int ShufMask2[] = {0, 1, 4, 5};
8472 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8473 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8474 }
8475
8476 // Handle truncation of V256 to V128 using shuffles.
8477 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008478 return SDValue();
8479
Nadav Rotem3c22a442012-12-27 07:45:10 +00008480 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8481 "Invalid op");
8482 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008483
8484 unsigned NumElems = VT.getVectorNumElements();
8485 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8486 NumElems * 2);
8487
Michael Liaobedcbd42012-10-16 18:14:11 +00008488 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8489 // Prepare truncation shuffle mask
8490 for (unsigned i = 0; i != NumElems; ++i)
8491 MaskVec[i] = i * 2;
8492 SDValue V = DAG.getVectorShuffle(NVT, DL,
8493 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8494 DAG.getUNDEF(NVT), &MaskVec[0]);
8495 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8496 DAG.getIntPtrConstant(0));
8497}
8498
Dan Gohmand858e902010-04-17 15:26:15 +00008499SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8500 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008501 if (Op.getValueType().isVector()) {
8502 if (Op.getValueType() == MVT::v8i16)
8503 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8504 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8505 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008506 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008507 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008508
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008509 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8510 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008511 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008512 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8513 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008514
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008515 if (StackSlot.getNode())
8516 // Load the result.
8517 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8518 FIST, StackSlot, MachinePointerInfo(),
8519 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008520
8521 // The node is the result.
8522 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008523}
8524
Dan Gohmand858e902010-04-17 15:26:15 +00008525SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8526 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008527 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8528 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008529 SDValue FIST = Vals.first, StackSlot = Vals.second;
8530 assert(FIST.getNode() && "Unexpected failure");
8531
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008532 if (StackSlot.getNode())
8533 // Load the result.
8534 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8535 FIST, StackSlot, MachinePointerInfo(),
8536 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008537
8538 // The node is the result.
8539 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008540}
8541
Michael Liao9d796db2012-10-10 16:32:15 +00008542SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8543 SelectionDAG &DAG) const {
8544 DebugLoc DL = Op.getDebugLoc();
8545 EVT VT = Op.getValueType();
8546 SDValue In = Op.getOperand(0);
8547 EVT SVT = In.getValueType();
8548
8549 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8550
8551 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8552 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8553 In, DAG.getUNDEF(SVT)));
8554}
8555
Craig Topper43620672012-09-08 07:31:51 +00008556SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008557 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008558 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008559 EVT VT = Op.getValueType();
8560 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008561 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8562 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008563 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008564 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008565 }
Craig Topper43620672012-09-08 07:31:51 +00008566 Constant *C;
8567 if (EltVT == MVT::f64)
8568 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8569 else
8570 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8571 C = ConstantVector::getSplat(NumElts, C);
8572 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8573 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008574 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008575 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008576 false, false, false, Alignment);
8577 if (VT.isVector()) {
8578 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8579 return DAG.getNode(ISD::BITCAST, dl, VT,
8580 DAG.getNode(ISD::AND, dl, ANDVT,
8581 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8582 Op.getOperand(0)),
8583 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8584 }
Dale Johannesenace16102009-02-03 19:33:06 +00008585 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008586}
8587
Dan Gohmand858e902010-04-17 15:26:15 +00008588SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008589 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008590 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008591 EVT VT = Op.getValueType();
8592 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008593 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8594 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008595 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008596 NumElts = VT.getVectorNumElements();
8597 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008598 Constant *C;
8599 if (EltVT == MVT::f64)
8600 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8601 else
8602 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8603 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008604 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8605 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008606 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008607 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008608 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008609 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008610 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008611 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008612 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008613 DAG.getNode(ISD::BITCAST, dl, XORVT,
8614 Op.getOperand(0)),
8615 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008616 }
Craig Topper69947b92012-04-23 06:57:04 +00008617
8618 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008619}
8620
Dan Gohmand858e902010-04-17 15:26:15 +00008621SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008622 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008623 SDValue Op0 = Op.getOperand(0);
8624 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008625 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008626 EVT VT = Op.getValueType();
8627 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008628
8629 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008630 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008631 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008632 SrcVT = VT;
8633 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008634 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008635 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008636 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008637 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008638 }
8639
8640 // At this point the operands and the result should have the same
8641 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008642
Evan Cheng68c47cb2007-01-05 07:55:56 +00008643 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008644 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008645 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008646 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8647 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008648 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008649 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8650 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8651 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8652 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008653 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008654 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008655 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008656 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008657 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008658 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008659 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008660
8661 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008662 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008663 // Op0 is MVT::f32, Op1 is MVT::f64.
8664 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8665 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8666 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008667 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008668 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008669 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008670 }
8671
Evan Cheng73d6cf12007-01-05 21:37:56 +00008672 // Clear first operand sign bit.
8673 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008674 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008675 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8676 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008677 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008678 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8679 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8680 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8681 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008682 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008683 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008684 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008685 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008686 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008687 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008688 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008689
8690 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008691 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008692}
8693
Craig Topper55b24052012-09-11 06:15:32 +00008694static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008695 SDValue N0 = Op.getOperand(0);
8696 DebugLoc dl = Op.getDebugLoc();
8697 EVT VT = Op.getValueType();
8698
8699 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8700 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8701 DAG.getConstant(1, VT));
8702 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8703}
8704
Michael Liaof966e4e2012-09-13 20:24:54 +00008705// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8706//
8707SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8708 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8709
8710 if (!Subtarget->hasSSE41())
8711 return SDValue();
8712
8713 if (!Op->hasOneUse())
8714 return SDValue();
8715
8716 SDNode *N = Op.getNode();
8717 DebugLoc DL = N->getDebugLoc();
8718
8719 SmallVector<SDValue, 8> Opnds;
8720 DenseMap<SDValue, unsigned> VecInMap;
8721 EVT VT = MVT::Other;
8722
8723 // Recognize a special case where a vector is casted into wide integer to
8724 // test all 0s.
8725 Opnds.push_back(N->getOperand(0));
8726 Opnds.push_back(N->getOperand(1));
8727
8728 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8729 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8730 // BFS traverse all OR'd operands.
8731 if (I->getOpcode() == ISD::OR) {
8732 Opnds.push_back(I->getOperand(0));
8733 Opnds.push_back(I->getOperand(1));
8734 // Re-evaluate the number of nodes to be traversed.
8735 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8736 continue;
8737 }
8738
8739 // Quit if a non-EXTRACT_VECTOR_ELT
8740 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8741 return SDValue();
8742
8743 // Quit if without a constant index.
8744 SDValue Idx = I->getOperand(1);
8745 if (!isa<ConstantSDNode>(Idx))
8746 return SDValue();
8747
8748 SDValue ExtractedFromVec = I->getOperand(0);
8749 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8750 if (M == VecInMap.end()) {
8751 VT = ExtractedFromVec.getValueType();
8752 // Quit if not 128/256-bit vector.
8753 if (!VT.is128BitVector() && !VT.is256BitVector())
8754 return SDValue();
8755 // Quit if not the same type.
8756 if (VecInMap.begin() != VecInMap.end() &&
8757 VT != VecInMap.begin()->first.getValueType())
8758 return SDValue();
8759 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8760 }
8761 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8762 }
8763
8764 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008765 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008766
8767 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8768 SmallVector<SDValue, 8> VecIns;
8769
8770 for (DenseMap<SDValue, unsigned>::const_iterator
8771 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8772 // Quit if not all elements are used.
8773 if (I->second != FullMask)
8774 return SDValue();
8775 VecIns.push_back(I->first);
8776 }
8777
8778 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8779
8780 // Cast all vectors into TestVT for PTEST.
8781 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8782 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8783
8784 // If more than one full vectors are evaluated, OR them first before PTEST.
8785 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8786 // Each iteration will OR 2 nodes and append the result until there is only
8787 // 1 node left, i.e. the final OR'd value of all vectors.
8788 SDValue LHS = VecIns[Slot];
8789 SDValue RHS = VecIns[Slot + 1];
8790 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8791 }
8792
8793 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8794 VecIns.back(), VecIns.back());
8795}
8796
Dan Gohman076aee32009-03-04 19:44:21 +00008797/// Emit nodes that will be selected as "test Op0,Op0", or something
8798/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008799SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008800 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008801 DebugLoc dl = Op.getDebugLoc();
8802
Dan Gohman31125812009-03-07 01:58:32 +00008803 // CF and OF aren't always set the way we want. Determine which
8804 // of these we need.
8805 bool NeedCF = false;
8806 bool NeedOF = false;
8807 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008808 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008809 case X86::COND_A: case X86::COND_AE:
8810 case X86::COND_B: case X86::COND_BE:
8811 NeedCF = true;
8812 break;
8813 case X86::COND_G: case X86::COND_GE:
8814 case X86::COND_L: case X86::COND_LE:
8815 case X86::COND_O: case X86::COND_NO:
8816 NeedOF = true;
8817 break;
Dan Gohman31125812009-03-07 01:58:32 +00008818 }
8819
Dan Gohman076aee32009-03-04 19:44:21 +00008820 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008821 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8822 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008823 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8824 // Emit a CMP with 0, which is the TEST pattern.
8825 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8826 DAG.getConstant(0, Op.getValueType()));
8827
8828 unsigned Opcode = 0;
8829 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008830
8831 // Truncate operations may prevent the merge of the SETCC instruction
8832 // and the arithmetic intruction before it. Attempt to truncate the operands
8833 // of the arithmetic instruction and use a reduced bit-width instruction.
8834 bool NeedTruncation = false;
8835 SDValue ArithOp = Op;
8836 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8837 SDValue Arith = Op->getOperand(0);
8838 // Both the trunc and the arithmetic op need to have one user each.
8839 if (Arith->hasOneUse())
8840 switch (Arith.getOpcode()) {
8841 default: break;
8842 case ISD::ADD:
8843 case ISD::SUB:
8844 case ISD::AND:
8845 case ISD::OR:
8846 case ISD::XOR: {
8847 NeedTruncation = true;
8848 ArithOp = Arith;
8849 }
8850 }
8851 }
8852
8853 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8854 // which may be the result of a CAST. We use the variable 'Op', which is the
8855 // non-casted variable when we check for possible users.
8856 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008857 case ISD::ADD:
8858 // Due to an isel shortcoming, be conservative if this add is likely to be
8859 // selected as part of a load-modify-store instruction. When the root node
8860 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8861 // uses of other nodes in the match, such as the ADD in this case. This
8862 // leads to the ADD being left around and reselected, with the result being
8863 // two adds in the output. Alas, even if none our users are stores, that
8864 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8865 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8866 // climbing the DAG back to the root, and it doesn't seem to be worth the
8867 // effort.
8868 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008869 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8870 if (UI->getOpcode() != ISD::CopyToReg &&
8871 UI->getOpcode() != ISD::SETCC &&
8872 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008873 goto default_case;
8874
8875 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008876 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008877 // An add of one will be selected as an INC.
8878 if (C->getAPIntValue() == 1) {
8879 Opcode = X86ISD::INC;
8880 NumOperands = 1;
8881 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008882 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008883
8884 // An add of negative one (subtract of one) will be selected as a DEC.
8885 if (C->getAPIntValue().isAllOnesValue()) {
8886 Opcode = X86ISD::DEC;
8887 NumOperands = 1;
8888 break;
8889 }
Dan Gohman076aee32009-03-04 19:44:21 +00008890 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008891
8892 // Otherwise use a regular EFLAGS-setting add.
8893 Opcode = X86ISD::ADD;
8894 NumOperands = 2;
8895 break;
8896 case ISD::AND: {
8897 // If the primary and result isn't used, don't bother using X86ISD::AND,
8898 // because a TEST instruction will be better.
8899 bool NonFlagUse = false;
8900 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8901 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8902 SDNode *User = *UI;
8903 unsigned UOpNo = UI.getOperandNo();
8904 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8905 // Look pass truncate.
8906 UOpNo = User->use_begin().getOperandNo();
8907 User = *User->use_begin();
8908 }
8909
8910 if (User->getOpcode() != ISD::BRCOND &&
8911 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008912 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008913 NonFlagUse = true;
8914 break;
8915 }
Dan Gohman076aee32009-03-04 19:44:21 +00008916 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008917
8918 if (!NonFlagUse)
8919 break;
8920 }
8921 // FALL THROUGH
8922 case ISD::SUB:
8923 case ISD::OR:
8924 case ISD::XOR:
8925 // Due to the ISEL shortcoming noted above, be conservative if this op is
8926 // likely to be selected as part of a load-modify-store instruction.
8927 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8928 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8929 if (UI->getOpcode() == ISD::STORE)
8930 goto default_case;
8931
8932 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008933 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008934 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008935 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008936 case ISD::XOR: Opcode = X86ISD::XOR; break;
8937 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008938 case ISD::OR: {
8939 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8940 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8941 if (EFLAGS.getNode())
8942 return EFLAGS;
8943 }
8944 Opcode = X86ISD::OR;
8945 break;
8946 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008947 }
8948
8949 NumOperands = 2;
8950 break;
8951 case X86ISD::ADD:
8952 case X86ISD::SUB:
8953 case X86ISD::INC:
8954 case X86ISD::DEC:
8955 case X86ISD::OR:
8956 case X86ISD::XOR:
8957 case X86ISD::AND:
8958 return SDValue(Op.getNode(), 1);
8959 default:
8960 default_case:
8961 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008962 }
8963
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008964 // If we found that truncation is beneficial, perform the truncation and
8965 // update 'Op'.
8966 if (NeedTruncation) {
8967 EVT VT = Op.getValueType();
8968 SDValue WideVal = Op->getOperand(0);
8969 EVT WideVT = WideVal.getValueType();
8970 unsigned ConvertedOp = 0;
8971 // Use a target machine opcode to prevent further DAGCombine
8972 // optimizations that may separate the arithmetic operations
8973 // from the setcc node.
8974 switch (WideVal.getOpcode()) {
8975 default: break;
8976 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8977 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8978 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8979 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8980 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8981 }
8982
8983 if (ConvertedOp) {
8984 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8985 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8986 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8987 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8988 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8989 }
8990 }
8991 }
8992
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008993 if (Opcode == 0)
8994 // Emit a CMP with 0, which is the TEST pattern.
8995 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8996 DAG.getConstant(0, Op.getValueType()));
8997
8998 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8999 SmallVector<SDValue, 4> Ops;
9000 for (unsigned i = 0; i != NumOperands; ++i)
9001 Ops.push_back(Op.getOperand(i));
9002
9003 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9004 DAG.ReplaceAllUsesWith(Op, New);
9005 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009006}
9007
9008/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9009/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009010SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009011 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009012 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9013 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009014 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009015
9016 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009017 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9018 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9019 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9020 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9021 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9022 Op0, Op1);
9023 return SDValue(Sub.getNode(), 1);
9024 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009025 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009026}
9027
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009028/// Convert a comparison if required by the subtarget.
9029SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9030 SelectionDAG &DAG) const {
9031 // If the subtarget does not support the FUCOMI instruction, floating-point
9032 // comparisons have to be converted.
9033 if (Subtarget->hasCMov() ||
9034 Cmp.getOpcode() != X86ISD::CMP ||
9035 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9036 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9037 return Cmp;
9038
9039 // The instruction selector will select an FUCOM instruction instead of
9040 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9041 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9042 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9043 DebugLoc dl = Cmp.getDebugLoc();
9044 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9045 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9046 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9047 DAG.getConstant(8, MVT::i8));
9048 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9049 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9050}
9051
Evan Cheng4e544802012-12-05 00:10:38 +00009052static bool isAllOnes(SDValue V) {
9053 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9054 return C && C->isAllOnesValue();
9055}
9056
Evan Chengd40d03e2010-01-06 19:38:29 +00009057/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9058/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009059SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9060 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009061 SDValue Op0 = And.getOperand(0);
9062 SDValue Op1 = And.getOperand(1);
9063 if (Op0.getOpcode() == ISD::TRUNCATE)
9064 Op0 = Op0.getOperand(0);
9065 if (Op1.getOpcode() == ISD::TRUNCATE)
9066 Op1 = Op1.getOperand(0);
9067
Evan Chengd40d03e2010-01-06 19:38:29 +00009068 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009069 if (Op1.getOpcode() == ISD::SHL)
9070 std::swap(Op0, Op1);
9071 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009072 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9073 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009074 // If we looked past a truncate, check that it's only truncating away
9075 // known zeros.
9076 unsigned BitWidth = Op0.getValueSizeInBits();
9077 unsigned AndBitWidth = And.getValueSizeInBits();
9078 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009079 APInt Zeros, Ones;
9080 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009081 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9082 return SDValue();
9083 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009084 LHS = Op1;
9085 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009086 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009087 } else if (Op1.getOpcode() == ISD::Constant) {
9088 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009089 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009090 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009091
9092 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009093 LHS = AndLHS.getOperand(0);
9094 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009095 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009096
9097 // Use BT if the immediate can't be encoded in a TEST instruction.
9098 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9099 LHS = AndLHS;
9100 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9101 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009102 }
Evan Cheng0488db92007-09-25 01:57:46 +00009103
Evan Chengd40d03e2010-01-06 19:38:29 +00009104 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009105 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9106 // the condition code later.
9107 bool Invert = false;
9108 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9109 Invert = true;
9110 LHS = LHS.getOperand(0);
9111 }
9112
Evan Chenge5b51ac2010-04-17 06:13:15 +00009113 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009114 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009115 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009116 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009117 // Also promote i16 to i32 for performance / code size reason.
9118 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009119 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009120 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009121
Evan Chengd40d03e2010-01-06 19:38:29 +00009122 // If the operand types disagree, extend the shift amount to match. Since
9123 // BT ignores high bits (like shifts) we can use anyextend.
9124 if (LHS.getValueType() != RHS.getValueType())
9125 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009126
Evan Chengd40d03e2010-01-06 19:38:29 +00009127 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009128 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9129 // Flip the condition if the LHS was a not instruction
9130 if (Invert)
9131 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009132 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9133 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009134 }
9135
Evan Cheng54de3ea2010-01-05 06:52:31 +00009136 return SDValue();
9137}
9138
Dan Gohmand858e902010-04-17 15:26:15 +00009139SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00009140
9141 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
9142
Evan Cheng54de3ea2010-01-05 06:52:31 +00009143 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
9144 SDValue Op0 = Op.getOperand(0);
9145 SDValue Op1 = Op.getOperand(1);
9146 DebugLoc dl = Op.getDebugLoc();
9147 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9148
9149 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00009150 // Lower (X & (1 << N)) == 0 to BT(X, N).
9151 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9152 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00009153 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009154 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00009155 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009156 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9157 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9158 if (NewSetCC.getNode())
9159 return NewSetCC;
9160 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00009161
Chris Lattner481eebc2010-12-19 21:23:48 +00009162 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9163 // these.
9164 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009165 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009166 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9167 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009168
Chris Lattner481eebc2010-12-19 21:23:48 +00009169 // If the input is a setcc, then reuse the input setcc or use a new one with
9170 // the inverted condition.
9171 if (Op0.getOpcode() == X86ISD::SETCC) {
9172 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9173 bool Invert = (CC == ISD::SETNE) ^
9174 cast<ConstantSDNode>(Op1)->isNullValue();
9175 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009176
Evan Cheng2c755ba2010-02-27 07:36:59 +00009177 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009178 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9179 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9180 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009181 }
9182
Evan Chenge5b51ac2010-04-17 06:13:15 +00009183 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009184 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009185 if (X86CC == X86::COND_INVALID)
9186 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009187
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009188 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009189 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009191 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009192}
9193
Craig Topper89af15e2011-09-18 08:03:58 +00009194// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009195// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009196static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009197 EVT VT = Op.getValueType();
9198
Craig Topper7a9a28b2012-08-12 02:23:29 +00009199 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009200 "Unsupported value type for operation");
9201
Craig Topper66ddd152012-04-27 22:54:43 +00009202 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009203 DebugLoc dl = Op.getDebugLoc();
9204 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009205
9206 // Extract the LHS vectors
9207 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009208 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9209 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009210
9211 // Extract the RHS vectors
9212 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009213 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9214 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009215
9216 // Issue the operation on the smaller types and concatenate the result back
9217 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9218 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9219 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9220 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9221 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9222}
9223
Dan Gohmand858e902010-04-17 15:26:15 +00009224SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009225 SDValue Cond;
9226 SDValue Op0 = Op.getOperand(0);
9227 SDValue Op1 = Op.getOperand(1);
9228 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009229 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009230 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9231 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009232 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009233
9234 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009235#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009236 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009237 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9238#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009239
Craig Topper523908d2012-08-13 02:34:03 +00009240 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009241 bool Swap = false;
9242
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009243 // SSE Condition code mapping:
9244 // 0 - EQ
9245 // 1 - LT
9246 // 2 - LE
9247 // 3 - UNORD
9248 // 4 - NEQ
9249 // 5 - NLT
9250 // 6 - NLE
9251 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009252 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009253 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009254 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009255 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009256 case ISD::SETOGT:
9257 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009258 case ISD::SETLT:
9259 case ISD::SETOLT: SSECC = 1; break;
9260 case ISD::SETOGE:
9261 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009262 case ISD::SETLE:
9263 case ISD::SETOLE: SSECC = 2; break;
9264 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009265 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009266 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009267 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009268 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009269 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009270 case ISD::SETUGT: SSECC = 6; break;
9271 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009272 case ISD::SETUEQ:
9273 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009274 }
9275 if (Swap)
9276 std::swap(Op0, Op1);
9277
Nate Begemanfb8ead02008-07-25 19:05:58 +00009278 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009279 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009280 unsigned CC0, CC1;
9281 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009282 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009283 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9284 } else {
9285 assert(SetCCOpcode == ISD::SETONE);
9286 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009287 }
Craig Topper523908d2012-08-13 02:34:03 +00009288
9289 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9290 DAG.getConstant(CC0, MVT::i8));
9291 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9292 DAG.getConstant(CC1, MVT::i8));
9293 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009294 }
9295 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009296 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9297 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009298 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009299
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009300 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009301 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009302 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009303
Nate Begeman30a0de92008-07-17 16:51:19 +00009304 // We are handling one of the integer comparisons here. Since SSE only has
9305 // GT and EQ comparisons for integer, swapping operands and multiple
9306 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009307 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009308 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009309
Nate Begeman30a0de92008-07-17 16:51:19 +00009310 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009311 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009312 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009313 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009314 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009315 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009316 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009317 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009318 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009319 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009320 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009321 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009322 }
9323 if (Swap)
9324 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009325
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009326 // Check that the operation in question is available (most are plain SSE2,
9327 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009328 if (VT == MVT::v2i64) {
9329 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9330 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009331 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9332 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009333 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009334 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9335
9336 // First cast everything to the right type,
9337 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9338 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9339
9340 // Do the compare.
9341 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9342
9343 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009344 const int Mask[] = { 1, 0, 3, 2 };
9345 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9346 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009347
9348 if (Invert)
9349 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9350
9351 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9352 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009353 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009354
Nate Begeman30a0de92008-07-17 16:51:19 +00009355 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9356 // bits of the inputs before performing those operations.
9357 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009358 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009359 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9360 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009361 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009362 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9363 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009364 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9365 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009366 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009367
Dale Johannesenace16102009-02-03 19:33:06 +00009368 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009369
9370 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009371 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009372 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009373
Nate Begeman30a0de92008-07-17 16:51:19 +00009374 return Result;
9375}
Evan Cheng0488db92007-09-25 01:57:46 +00009376
Evan Cheng370e5342008-12-03 08:38:43 +00009377// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009378static bool isX86LogicalCmp(SDValue Op) {
9379 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009380 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9381 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009382 return true;
9383 if (Op.getResNo() == 1 &&
9384 (Opc == X86ISD::ADD ||
9385 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009386 Opc == X86ISD::ADC ||
9387 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009388 Opc == X86ISD::SMUL ||
9389 Opc == X86ISD::UMUL ||
9390 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009391 Opc == X86ISD::DEC ||
9392 Opc == X86ISD::OR ||
9393 Opc == X86ISD::XOR ||
9394 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009395 return true;
9396
Chris Lattner9637d5b2010-12-05 07:49:54 +00009397 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9398 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009399
Dan Gohman076aee32009-03-04 19:44:21 +00009400 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009401}
9402
Chris Lattnera2b56002010-12-05 01:23:24 +00009403static bool isZero(SDValue V) {
9404 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9405 return C && C->isNullValue();
9406}
9407
Evan Chengb64dd5f2012-08-07 22:21:00 +00009408static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9409 if (V.getOpcode() != ISD::TRUNCATE)
9410 return false;
9411
9412 SDValue VOp0 = V.getOperand(0);
9413 unsigned InBits = VOp0.getValueSizeInBits();
9414 unsigned Bits = V.getValueSizeInBits();
9415 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9416}
9417
Dan Gohmand858e902010-04-17 15:26:15 +00009418SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009419 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009420 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009421 SDValue Op1 = Op.getOperand(1);
9422 SDValue Op2 = Op.getOperand(2);
9423 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009424 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009425
Dan Gohman1a492952009-10-20 16:22:37 +00009426 if (Cond.getOpcode() == ISD::SETCC) {
9427 SDValue NewCond = LowerSETCC(Cond, DAG);
9428 if (NewCond.getNode())
9429 Cond = NewCond;
9430 }
Evan Cheng734503b2006-09-11 02:19:56 +00009431
Chris Lattnera2b56002010-12-05 01:23:24 +00009432 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009433 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009434 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009435 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009436 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009437 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9438 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009439 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009440
Chris Lattnera2b56002010-12-05 01:23:24 +00009441 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009442
9443 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009444 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9445 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009446
9447 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009448 // Apply further optimizations for special cases
9449 // (select (x != 0), -1, 0) -> neg & sbb
9450 // (select (x == 0), 0, -1) -> neg & sbb
9451 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009452 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009453 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9454 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009455 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9456 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009457 CmpOp0);
9458 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9459 DAG.getConstant(X86::COND_B, MVT::i8),
9460 SDValue(Neg.getNode(), 1));
9461 return Res;
9462 }
9463
Chris Lattnera2b56002010-12-05 01:23:24 +00009464 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9465 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009466 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009467
Chris Lattner96908b12010-12-05 02:00:51 +00009468 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009469 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9470 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009471
Chris Lattner96908b12010-12-05 02:00:51 +00009472 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9473 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009474
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009475 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009476 if (N2C == 0 || !N2C->isNullValue())
9477 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9478 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009479 }
9480 }
9481
Chris Lattnera2b56002010-12-05 01:23:24 +00009482 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009483 if (Cond.getOpcode() == ISD::AND &&
9484 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9485 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009486 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009487 Cond = Cond.getOperand(0);
9488 }
9489
Evan Cheng3f41d662007-10-08 22:16:29 +00009490 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9491 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009492 unsigned CondOpcode = Cond.getOpcode();
9493 if (CondOpcode == X86ISD::SETCC ||
9494 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009495 CC = Cond.getOperand(0);
9496
Dan Gohman475871a2008-07-27 21:46:04 +00009497 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009498 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009499 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009500
Evan Cheng3f41d662007-10-08 22:16:29 +00009501 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009502 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009503 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009504 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009505
Chris Lattnerd1980a52009-03-12 06:52:53 +00009506 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9507 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009508 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009509 addTest = false;
9510 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009511 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9512 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9513 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9514 Cond.getOperand(0).getValueType() != MVT::i8)) {
9515 SDValue LHS = Cond.getOperand(0);
9516 SDValue RHS = Cond.getOperand(1);
9517 unsigned X86Opcode;
9518 unsigned X86Cond;
9519 SDVTList VTs;
9520 switch (CondOpcode) {
9521 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9522 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9523 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9524 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9525 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9526 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9527 default: llvm_unreachable("unexpected overflowing operator");
9528 }
9529 if (CondOpcode == ISD::UMULO)
9530 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9531 MVT::i32);
9532 else
9533 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9534
9535 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9536
9537 if (CondOpcode == ISD::UMULO)
9538 Cond = X86Op.getValue(2);
9539 else
9540 Cond = X86Op.getValue(1);
9541
9542 CC = DAG.getConstant(X86Cond, MVT::i8);
9543 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009544 }
9545
9546 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009547 // Look pass the truncate if the high bits are known zero.
9548 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9549 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009550
9551 // We know the result of AND is compared against zero. Try to match
9552 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009553 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009554 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009555 if (NewSetCC.getNode()) {
9556 CC = NewSetCC.getOperand(0);
9557 Cond = NewSetCC.getOperand(1);
9558 addTest = false;
9559 }
9560 }
9561 }
9562
9563 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009564 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009565 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009566 }
9567
Benjamin Kramere915ff32010-12-22 23:09:28 +00009568 // a < b ? -1 : 0 -> RES = ~setcc_carry
9569 // a < b ? 0 : -1 -> RES = setcc_carry
9570 // a >= b ? -1 : 0 -> RES = setcc_carry
9571 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009572 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009573 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009574 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9575
9576 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9577 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9578 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9579 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9580 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9581 return DAG.getNOT(DL, Res, Res.getValueType());
9582 return Res;
9583 }
9584 }
9585
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009586 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9587 // widen the cmov and push the truncate through. This avoids introducing a new
9588 // branch during isel and doesn't add any extensions.
9589 if (Op.getValueType() == MVT::i8 &&
9590 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9591 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9592 if (T1.getValueType() == T2.getValueType() &&
9593 // Blacklist CopyFromReg to avoid partial register stalls.
9594 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9595 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009596 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009597 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9598 }
9599 }
9600
Evan Cheng0488db92007-09-25 01:57:46 +00009601 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9602 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009603 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009604 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009605 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009606}
9607
Nadav Rotem1a330af2012-12-27 22:47:16 +00009608SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9609 SelectionDAG &DAG) const {
9610 EVT VT = Op->getValueType(0);
9611 SDValue In = Op->getOperand(0);
9612 EVT InVT = In.getValueType();
9613 DebugLoc dl = Op->getDebugLoc();
9614
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009615 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9616 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9617 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009618
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009619 if (Subtarget->hasInt256())
9620 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009621
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009622 // Optimize vectors in AVX mode
9623 // Sign extend v8i16 to v8i32 and
9624 // v4i32 to v4i64
9625 //
9626 // Divide input vector into two parts
9627 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9628 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9629 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009630
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009631 unsigned NumElems = InVT.getVectorNumElements();
9632 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009633
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009634 SmallVector<int,8> ShufMask1(NumElems, -1);
9635 for (unsigned i = 0; i != NumElems/2; ++i)
9636 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009637
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009638 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009639
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009640 SmallVector<int,8> ShufMask2(NumElems, -1);
9641 for (unsigned i = 0; i != NumElems/2; ++i)
9642 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009643
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009644 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009645
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009646 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
9647 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009648
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009649 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9650 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009651
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009652 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009653}
9654
Evan Cheng370e5342008-12-03 08:38:43 +00009655// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9656// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9657// from the AND / OR.
9658static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9659 Opc = Op.getOpcode();
9660 if (Opc != ISD::OR && Opc != ISD::AND)
9661 return false;
9662 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9663 Op.getOperand(0).hasOneUse() &&
9664 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9665 Op.getOperand(1).hasOneUse());
9666}
9667
Evan Cheng961d6d42009-02-02 08:19:07 +00009668// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9669// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009670static bool isXor1OfSetCC(SDValue Op) {
9671 if (Op.getOpcode() != ISD::XOR)
9672 return false;
9673 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9674 if (N1C && N1C->getAPIntValue() == 1) {
9675 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9676 Op.getOperand(0).hasOneUse();
9677 }
9678 return false;
9679}
9680
Dan Gohmand858e902010-04-17 15:26:15 +00009681SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009682 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009683 SDValue Chain = Op.getOperand(0);
9684 SDValue Cond = Op.getOperand(1);
9685 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009686 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009687 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009688 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009689
Dan Gohman1a492952009-10-20 16:22:37 +00009690 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009691 // Check for setcc([su]{add,sub,mul}o == 0).
9692 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9693 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9694 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9695 Cond.getOperand(0).getResNo() == 1 &&
9696 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9697 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9698 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9699 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9700 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9701 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9702 Inverted = true;
9703 Cond = Cond.getOperand(0);
9704 } else {
9705 SDValue NewCond = LowerSETCC(Cond, DAG);
9706 if (NewCond.getNode())
9707 Cond = NewCond;
9708 }
Dan Gohman1a492952009-10-20 16:22:37 +00009709 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009710#if 0
9711 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009712 else if (Cond.getOpcode() == X86ISD::ADD ||
9713 Cond.getOpcode() == X86ISD::SUB ||
9714 Cond.getOpcode() == X86ISD::SMUL ||
9715 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009716 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009717#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009718
Evan Chengad9c0a32009-12-15 00:53:42 +00009719 // Look pass (and (setcc_carry (cmp ...)), 1).
9720 if (Cond.getOpcode() == ISD::AND &&
9721 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9722 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009723 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009724 Cond = Cond.getOperand(0);
9725 }
9726
Evan Cheng3f41d662007-10-08 22:16:29 +00009727 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9728 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009729 unsigned CondOpcode = Cond.getOpcode();
9730 if (CondOpcode == X86ISD::SETCC ||
9731 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009732 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009733
Dan Gohman475871a2008-07-27 21:46:04 +00009734 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009735 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009736 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009737 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009738 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009739 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009740 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009741 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009742 default: break;
9743 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009744 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009745 // These can only come from an arithmetic instruction with overflow,
9746 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009747 Cond = Cond.getNode()->getOperand(1);
9748 addTest = false;
9749 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009750 }
Evan Cheng0488db92007-09-25 01:57:46 +00009751 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009752 }
9753 CondOpcode = Cond.getOpcode();
9754 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9755 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9756 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9757 Cond.getOperand(0).getValueType() != MVT::i8)) {
9758 SDValue LHS = Cond.getOperand(0);
9759 SDValue RHS = Cond.getOperand(1);
9760 unsigned X86Opcode;
9761 unsigned X86Cond;
9762 SDVTList VTs;
9763 switch (CondOpcode) {
9764 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9765 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9766 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9767 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9768 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9769 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9770 default: llvm_unreachable("unexpected overflowing operator");
9771 }
9772 if (Inverted)
9773 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9774 if (CondOpcode == ISD::UMULO)
9775 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9776 MVT::i32);
9777 else
9778 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9779
9780 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9781
9782 if (CondOpcode == ISD::UMULO)
9783 Cond = X86Op.getValue(2);
9784 else
9785 Cond = X86Op.getValue(1);
9786
9787 CC = DAG.getConstant(X86Cond, MVT::i8);
9788 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009789 } else {
9790 unsigned CondOpc;
9791 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9792 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009793 if (CondOpc == ISD::OR) {
9794 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9795 // two branches instead of an explicit OR instruction with a
9796 // separate test.
9797 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009798 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009799 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009800 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009801 Chain, Dest, CC, Cmp);
9802 CC = Cond.getOperand(1).getOperand(0);
9803 Cond = Cmp;
9804 addTest = false;
9805 }
9806 } else { // ISD::AND
9807 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9808 // two branches instead of an explicit AND instruction with a
9809 // separate test. However, we only do this if this block doesn't
9810 // have a fall-through edge, because this requires an explicit
9811 // jmp when the condition is false.
9812 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009813 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009814 Op.getNode()->hasOneUse()) {
9815 X86::CondCode CCode =
9816 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9817 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009818 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009819 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009820 // Look for an unconditional branch following this conditional branch.
9821 // We need this because we need to reverse the successors in order
9822 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009823 if (User->getOpcode() == ISD::BR) {
9824 SDValue FalseBB = User->getOperand(1);
9825 SDNode *NewBR =
9826 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009827 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009828 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009829 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009830
Dale Johannesene4d209d2009-02-03 20:21:25 +00009831 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009832 Chain, Dest, CC, Cmp);
9833 X86::CondCode CCode =
9834 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9835 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009836 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009837 Cond = Cmp;
9838 addTest = false;
9839 }
9840 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009841 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009842 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9843 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9844 // It should be transformed during dag combiner except when the condition
9845 // is set by a arithmetics with overflow node.
9846 X86::CondCode CCode =
9847 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9848 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009849 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009850 Cond = Cond.getOperand(0).getOperand(1);
9851 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009852 } else if (Cond.getOpcode() == ISD::SETCC &&
9853 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9854 // For FCMP_OEQ, we can emit
9855 // two branches instead of an explicit AND instruction with a
9856 // separate test. However, we only do this if this block doesn't
9857 // have a fall-through edge, because this requires an explicit
9858 // jmp when the condition is false.
9859 if (Op.getNode()->hasOneUse()) {
9860 SDNode *User = *Op.getNode()->use_begin();
9861 // Look for an unconditional branch following this conditional branch.
9862 // We need this because we need to reverse the successors in order
9863 // to implement FCMP_OEQ.
9864 if (User->getOpcode() == ISD::BR) {
9865 SDValue FalseBB = User->getOperand(1);
9866 SDNode *NewBR =
9867 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9868 assert(NewBR == User);
9869 (void)NewBR;
9870 Dest = FalseBB;
9871
9872 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9873 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009874 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009875 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9876 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9877 Chain, Dest, CC, Cmp);
9878 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9879 Cond = Cmp;
9880 addTest = false;
9881 }
9882 }
9883 } else if (Cond.getOpcode() == ISD::SETCC &&
9884 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9885 // For FCMP_UNE, we can emit
9886 // two branches instead of an explicit AND instruction with a
9887 // separate test. However, we only do this if this block doesn't
9888 // have a fall-through edge, because this requires an explicit
9889 // jmp when the condition is false.
9890 if (Op.getNode()->hasOneUse()) {
9891 SDNode *User = *Op.getNode()->use_begin();
9892 // Look for an unconditional branch following this conditional branch.
9893 // We need this because we need to reverse the successors in order
9894 // to implement FCMP_UNE.
9895 if (User->getOpcode() == ISD::BR) {
9896 SDValue FalseBB = User->getOperand(1);
9897 SDNode *NewBR =
9898 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9899 assert(NewBR == User);
9900 (void)NewBR;
9901
9902 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9903 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009904 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009905 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9906 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9907 Chain, Dest, CC, Cmp);
9908 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9909 Cond = Cmp;
9910 addTest = false;
9911 Dest = FalseBB;
9912 }
9913 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009914 }
Evan Cheng0488db92007-09-25 01:57:46 +00009915 }
9916
9917 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009918 // Look pass the truncate if the high bits are known zero.
9919 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9920 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009921
9922 // We know the result of AND is compared against zero. Try to match
9923 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009924 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009925 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9926 if (NewSetCC.getNode()) {
9927 CC = NewSetCC.getOperand(0);
9928 Cond = NewSetCC.getOperand(1);
9929 addTest = false;
9930 }
9931 }
9932 }
9933
9934 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009935 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009936 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009937 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009938 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009939 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009940 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009941}
9942
Anton Korobeynikove060b532007-04-17 19:34:00 +00009943// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9944// Calls to _alloca is needed to probe the stack when allocating more than 4k
9945// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9946// that the guard pages used by the OS virtual memory manager are allocated in
9947// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009948SDValue
9949X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009950 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009951 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009952 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009953 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009954 "are being used");
9955 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009956 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009957
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009958 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009959 SDValue Chain = Op.getOperand(0);
9960 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009961 // FIXME: Ensure alignment here
9962
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009963 bool Is64Bit = Subtarget->is64Bit();
9964 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009965
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009966 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009967 MachineFunction &MF = DAG.getMachineFunction();
9968 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009969
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009970 if (Is64Bit) {
9971 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009972 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009973 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009974
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009975 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009976 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009977 if (I->hasNestAttr())
9978 report_fatal_error("Cannot use segmented stacks with functions that "
9979 "have nested arguments.");
9980 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009981
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009982 const TargetRegisterClass *AddrRegClass =
9983 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9984 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9985 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9986 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9987 DAG.getRegister(Vreg, SPTy));
9988 SDValue Ops1[2] = { Value, Chain };
9989 return DAG.getMergeValues(Ops1, 2, dl);
9990 } else {
9991 SDValue Flag;
9992 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009993
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009994 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9995 Flag = Chain.getValue(1);
9996 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009997
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009998 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9999 Flag = Chain.getValue(1);
10000
Michael Liaoc5c970e2012-10-31 04:14:09 +000010001 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10002 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010003
10004 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10005 return DAG.getMergeValues(Ops1, 2, dl);
10006 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010007}
10008
Dan Gohmand858e902010-04-17 15:26:15 +000010009SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010010 MachineFunction &MF = DAG.getMachineFunction();
10011 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10012
Dan Gohman69de1932008-02-06 22:27:42 +000010013 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010014 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010015
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010016 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010017 // vastart just stores the address of the VarArgsFrameIndex slot into the
10018 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010019 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10020 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010021 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10022 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010023 }
10024
10025 // __va_list_tag:
10026 // gp_offset (0 - 6 * 8)
10027 // fp_offset (48 - 48 + 8 * 16)
10028 // overflow_arg_area (point to parameters coming in memory).
10029 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010030 SmallVector<SDValue, 8> MemOps;
10031 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010032 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010033 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010034 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10035 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010036 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010037 MemOps.push_back(Store);
10038
10039 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010040 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010041 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010042 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010043 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10044 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010045 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010046 MemOps.push_back(Store);
10047
10048 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010049 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010050 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010051 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10052 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010053 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10054 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010055 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010056 MemOps.push_back(Store);
10057
10058 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010059 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010060 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010061 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10062 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010063 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10064 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010065 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010066 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010067 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010068}
10069
Dan Gohmand858e902010-04-17 15:26:15 +000010070SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010071 assert(Subtarget->is64Bit() &&
10072 "LowerVAARG only handles 64-bit va_arg!");
10073 assert((Subtarget->isTargetLinux() ||
10074 Subtarget->isTargetDarwin()) &&
10075 "Unhandled target in LowerVAARG");
10076 assert(Op.getNode()->getNumOperands() == 4);
10077 SDValue Chain = Op.getOperand(0);
10078 SDValue SrcPtr = Op.getOperand(1);
10079 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10080 unsigned Align = Op.getConstantOperandVal(3);
10081 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010082
Dan Gohman320afb82010-10-12 18:00:49 +000010083 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010084 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010085 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010086 uint8_t ArgMode;
10087
10088 // Decide which area this value should be read from.
10089 // TODO: Implement the AMD64 ABI in its entirety. This simple
10090 // selection mechanism works only for the basic types.
10091 if (ArgVT == MVT::f80) {
10092 llvm_unreachable("va_arg for f80 not yet implemented");
10093 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10094 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10095 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10096 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10097 } else {
10098 llvm_unreachable("Unhandled argument type in LowerVAARG");
10099 }
10100
10101 if (ArgMode == 2) {
10102 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010103 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010104 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010105 .getFunction()->getAttributes()
10106 .hasAttribute(AttributeSet::FunctionIndex,
10107 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010108 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010109 }
10110
10111 // Insert VAARG_64 node into the DAG
10112 // VAARG_64 returns two values: Variable Argument Address, Chain
10113 SmallVector<SDValue, 11> InstOps;
10114 InstOps.push_back(Chain);
10115 InstOps.push_back(SrcPtr);
10116 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10117 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10118 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10119 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10120 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10121 VTs, &InstOps[0], InstOps.size(),
10122 MVT::i64,
10123 MachinePointerInfo(SV),
10124 /*Align=*/0,
10125 /*Volatile=*/false,
10126 /*ReadMem=*/true,
10127 /*WriteMem=*/true);
10128 Chain = VAARG.getValue(1);
10129
10130 // Load the next argument and return it
10131 return DAG.getLoad(ArgVT, dl,
10132 Chain,
10133 VAARG,
10134 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010135 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010136}
10137
Craig Topper55b24052012-09-11 06:15:32 +000010138static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10139 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010140 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010141 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010142 SDValue Chain = Op.getOperand(0);
10143 SDValue DstPtr = Op.getOperand(1);
10144 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010145 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10146 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010147 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010148
Chris Lattnere72f2022010-09-21 05:40:29 +000010149 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010150 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010151 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010152 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010153}
10154
Craig Topper80e46362012-01-23 06:16:53 +000010155// getTargetVShiftNOde - Handle vector element shifts where the shift amount
10156// may or may not be a constant. Takes immediate version of shift as input.
10157static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10158 SDValue SrcOp, SDValue ShAmt,
10159 SelectionDAG &DAG) {
10160 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10161
10162 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010163 // Constant may be a TargetConstant. Use a regular constant.
10164 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010165 switch (Opc) {
10166 default: llvm_unreachable("Unknown target vector shift node");
10167 case X86ISD::VSHLI:
10168 case X86ISD::VSRLI:
10169 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010170 return DAG.getNode(Opc, dl, VT, SrcOp,
10171 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010172 }
10173 }
10174
10175 // Change opcode to non-immediate version
10176 switch (Opc) {
10177 default: llvm_unreachable("Unknown target vector shift node");
10178 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10179 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10180 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10181 }
10182
10183 // Need to build a vector containing shift amount
10184 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10185 SDValue ShOps[4];
10186 ShOps[0] = ShAmt;
10187 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010188 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010189 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010190
10191 // The return type has to be a 128-bit type with the same element
10192 // type as the input type.
10193 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10194 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10195
10196 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010197 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10198}
10199
Craig Topper55b24052012-09-11 06:15:32 +000010200static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010201 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010202 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010203 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010204 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010205 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010206 case Intrinsic::x86_sse_comieq_ss:
10207 case Intrinsic::x86_sse_comilt_ss:
10208 case Intrinsic::x86_sse_comile_ss:
10209 case Intrinsic::x86_sse_comigt_ss:
10210 case Intrinsic::x86_sse_comige_ss:
10211 case Intrinsic::x86_sse_comineq_ss:
10212 case Intrinsic::x86_sse_ucomieq_ss:
10213 case Intrinsic::x86_sse_ucomilt_ss:
10214 case Intrinsic::x86_sse_ucomile_ss:
10215 case Intrinsic::x86_sse_ucomigt_ss:
10216 case Intrinsic::x86_sse_ucomige_ss:
10217 case Intrinsic::x86_sse_ucomineq_ss:
10218 case Intrinsic::x86_sse2_comieq_sd:
10219 case Intrinsic::x86_sse2_comilt_sd:
10220 case Intrinsic::x86_sse2_comile_sd:
10221 case Intrinsic::x86_sse2_comigt_sd:
10222 case Intrinsic::x86_sse2_comige_sd:
10223 case Intrinsic::x86_sse2_comineq_sd:
10224 case Intrinsic::x86_sse2_ucomieq_sd:
10225 case Intrinsic::x86_sse2_ucomilt_sd:
10226 case Intrinsic::x86_sse2_ucomile_sd:
10227 case Intrinsic::x86_sse2_ucomigt_sd:
10228 case Intrinsic::x86_sse2_ucomige_sd:
10229 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010230 unsigned Opc;
10231 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010232 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010233 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010234 case Intrinsic::x86_sse_comieq_ss:
10235 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010236 Opc = X86ISD::COMI;
10237 CC = ISD::SETEQ;
10238 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010239 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010240 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010241 Opc = X86ISD::COMI;
10242 CC = ISD::SETLT;
10243 break;
10244 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010245 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010246 Opc = X86ISD::COMI;
10247 CC = ISD::SETLE;
10248 break;
10249 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010250 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010251 Opc = X86ISD::COMI;
10252 CC = ISD::SETGT;
10253 break;
10254 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010255 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010256 Opc = X86ISD::COMI;
10257 CC = ISD::SETGE;
10258 break;
10259 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010260 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010261 Opc = X86ISD::COMI;
10262 CC = ISD::SETNE;
10263 break;
10264 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010265 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010266 Opc = X86ISD::UCOMI;
10267 CC = ISD::SETEQ;
10268 break;
10269 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010270 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010271 Opc = X86ISD::UCOMI;
10272 CC = ISD::SETLT;
10273 break;
10274 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010275 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010276 Opc = X86ISD::UCOMI;
10277 CC = ISD::SETLE;
10278 break;
10279 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010280 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010281 Opc = X86ISD::UCOMI;
10282 CC = ISD::SETGT;
10283 break;
10284 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010285 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010286 Opc = X86ISD::UCOMI;
10287 CC = ISD::SETGE;
10288 break;
10289 case Intrinsic::x86_sse_ucomineq_ss:
10290 case Intrinsic::x86_sse2_ucomineq_sd:
10291 Opc = X86ISD::UCOMI;
10292 CC = ISD::SETNE;
10293 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010294 }
Evan Cheng734503b2006-09-11 02:19:56 +000010295
Dan Gohman475871a2008-07-27 21:46:04 +000010296 SDValue LHS = Op.getOperand(1);
10297 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010298 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010299 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010300 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10301 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10302 DAG.getConstant(X86CC, MVT::i8), Cond);
10303 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010304 }
Craig Topper6d688152012-08-14 07:43:25 +000010305
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010306 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010307 case Intrinsic::x86_sse2_pmulu_dq:
10308 case Intrinsic::x86_avx2_pmulu_dq:
10309 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10310 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010311
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010312 // SSE2/AVX2 sub with unsigned saturation intrinsics
10313 case Intrinsic::x86_sse2_psubus_b:
10314 case Intrinsic::x86_sse2_psubus_w:
10315 case Intrinsic::x86_avx2_psubus_b:
10316 case Intrinsic::x86_avx2_psubus_w:
10317 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10318 Op.getOperand(1), Op.getOperand(2));
10319
Craig Topper6d688152012-08-14 07:43:25 +000010320 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010321 case Intrinsic::x86_sse3_hadd_ps:
10322 case Intrinsic::x86_sse3_hadd_pd:
10323 case Intrinsic::x86_avx_hadd_ps_256:
10324 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010325 case Intrinsic::x86_sse3_hsub_ps:
10326 case Intrinsic::x86_sse3_hsub_pd:
10327 case Intrinsic::x86_avx_hsub_ps_256:
10328 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010329 case Intrinsic::x86_ssse3_phadd_w_128:
10330 case Intrinsic::x86_ssse3_phadd_d_128:
10331 case Intrinsic::x86_avx2_phadd_w:
10332 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010333 case Intrinsic::x86_ssse3_phsub_w_128:
10334 case Intrinsic::x86_ssse3_phsub_d_128:
10335 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010336 case Intrinsic::x86_avx2_phsub_d: {
10337 unsigned Opcode;
10338 switch (IntNo) {
10339 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10340 case Intrinsic::x86_sse3_hadd_ps:
10341 case Intrinsic::x86_sse3_hadd_pd:
10342 case Intrinsic::x86_avx_hadd_ps_256:
10343 case Intrinsic::x86_avx_hadd_pd_256:
10344 Opcode = X86ISD::FHADD;
10345 break;
10346 case Intrinsic::x86_sse3_hsub_ps:
10347 case Intrinsic::x86_sse3_hsub_pd:
10348 case Intrinsic::x86_avx_hsub_ps_256:
10349 case Intrinsic::x86_avx_hsub_pd_256:
10350 Opcode = X86ISD::FHSUB;
10351 break;
10352 case Intrinsic::x86_ssse3_phadd_w_128:
10353 case Intrinsic::x86_ssse3_phadd_d_128:
10354 case Intrinsic::x86_avx2_phadd_w:
10355 case Intrinsic::x86_avx2_phadd_d:
10356 Opcode = X86ISD::HADD;
10357 break;
10358 case Intrinsic::x86_ssse3_phsub_w_128:
10359 case Intrinsic::x86_ssse3_phsub_d_128:
10360 case Intrinsic::x86_avx2_phsub_w:
10361 case Intrinsic::x86_avx2_phsub_d:
10362 Opcode = X86ISD::HSUB;
10363 break;
10364 }
10365 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010366 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010367 }
10368
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010369 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10370 case Intrinsic::x86_sse2_pmaxu_b:
10371 case Intrinsic::x86_sse41_pmaxuw:
10372 case Intrinsic::x86_sse41_pmaxud:
10373 case Intrinsic::x86_avx2_pmaxu_b:
10374 case Intrinsic::x86_avx2_pmaxu_w:
10375 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010376 case Intrinsic::x86_sse2_pminu_b:
10377 case Intrinsic::x86_sse41_pminuw:
10378 case Intrinsic::x86_sse41_pminud:
10379 case Intrinsic::x86_avx2_pminu_b:
10380 case Intrinsic::x86_avx2_pminu_w:
10381 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010382 case Intrinsic::x86_sse41_pmaxsb:
10383 case Intrinsic::x86_sse2_pmaxs_w:
10384 case Intrinsic::x86_sse41_pmaxsd:
10385 case Intrinsic::x86_avx2_pmaxs_b:
10386 case Intrinsic::x86_avx2_pmaxs_w:
10387 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010388 case Intrinsic::x86_sse41_pminsb:
10389 case Intrinsic::x86_sse2_pmins_w:
10390 case Intrinsic::x86_sse41_pminsd:
10391 case Intrinsic::x86_avx2_pmins_b:
10392 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010393 case Intrinsic::x86_avx2_pmins_d: {
10394 unsigned Opcode;
10395 switch (IntNo) {
10396 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10397 case Intrinsic::x86_sse2_pmaxu_b:
10398 case Intrinsic::x86_sse41_pmaxuw:
10399 case Intrinsic::x86_sse41_pmaxud:
10400 case Intrinsic::x86_avx2_pmaxu_b:
10401 case Intrinsic::x86_avx2_pmaxu_w:
10402 case Intrinsic::x86_avx2_pmaxu_d:
10403 Opcode = X86ISD::UMAX;
10404 break;
10405 case Intrinsic::x86_sse2_pminu_b:
10406 case Intrinsic::x86_sse41_pminuw:
10407 case Intrinsic::x86_sse41_pminud:
10408 case Intrinsic::x86_avx2_pminu_b:
10409 case Intrinsic::x86_avx2_pminu_w:
10410 case Intrinsic::x86_avx2_pminu_d:
10411 Opcode = X86ISD::UMIN;
10412 break;
10413 case Intrinsic::x86_sse41_pmaxsb:
10414 case Intrinsic::x86_sse2_pmaxs_w:
10415 case Intrinsic::x86_sse41_pmaxsd:
10416 case Intrinsic::x86_avx2_pmaxs_b:
10417 case Intrinsic::x86_avx2_pmaxs_w:
10418 case Intrinsic::x86_avx2_pmaxs_d:
10419 Opcode = X86ISD::SMAX;
10420 break;
10421 case Intrinsic::x86_sse41_pminsb:
10422 case Intrinsic::x86_sse2_pmins_w:
10423 case Intrinsic::x86_sse41_pminsd:
10424 case Intrinsic::x86_avx2_pmins_b:
10425 case Intrinsic::x86_avx2_pmins_w:
10426 case Intrinsic::x86_avx2_pmins_d:
10427 Opcode = X86ISD::SMIN;
10428 break;
10429 }
10430 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010431 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010432 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010433
Craig Topper6d183e42012-12-29 16:44:25 +000010434 // SSE/SSE2/AVX floating point max/min intrinsics.
10435 case Intrinsic::x86_sse_max_ps:
10436 case Intrinsic::x86_sse2_max_pd:
10437 case Intrinsic::x86_avx_max_ps_256:
10438 case Intrinsic::x86_avx_max_pd_256:
10439 case Intrinsic::x86_sse_min_ps:
10440 case Intrinsic::x86_sse2_min_pd:
10441 case Intrinsic::x86_avx_min_ps_256:
10442 case Intrinsic::x86_avx_min_pd_256: {
10443 unsigned Opcode;
10444 switch (IntNo) {
10445 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10446 case Intrinsic::x86_sse_max_ps:
10447 case Intrinsic::x86_sse2_max_pd:
10448 case Intrinsic::x86_avx_max_ps_256:
10449 case Intrinsic::x86_avx_max_pd_256:
10450 Opcode = X86ISD::FMAX;
10451 break;
10452 case Intrinsic::x86_sse_min_ps:
10453 case Intrinsic::x86_sse2_min_pd:
10454 case Intrinsic::x86_avx_min_ps_256:
10455 case Intrinsic::x86_avx_min_pd_256:
10456 Opcode = X86ISD::FMIN;
10457 break;
10458 }
10459 return DAG.getNode(Opcode, dl, Op.getValueType(),
10460 Op.getOperand(1), Op.getOperand(2));
10461 }
10462
Craig Topper6d688152012-08-14 07:43:25 +000010463 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010464 case Intrinsic::x86_avx2_psllv_d:
10465 case Intrinsic::x86_avx2_psllv_q:
10466 case Intrinsic::x86_avx2_psllv_d_256:
10467 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010468 case Intrinsic::x86_avx2_psrlv_d:
10469 case Intrinsic::x86_avx2_psrlv_q:
10470 case Intrinsic::x86_avx2_psrlv_d_256:
10471 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010472 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010473 case Intrinsic::x86_avx2_psrav_d_256: {
10474 unsigned Opcode;
10475 switch (IntNo) {
10476 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10477 case Intrinsic::x86_avx2_psllv_d:
10478 case Intrinsic::x86_avx2_psllv_q:
10479 case Intrinsic::x86_avx2_psllv_d_256:
10480 case Intrinsic::x86_avx2_psllv_q_256:
10481 Opcode = ISD::SHL;
10482 break;
10483 case Intrinsic::x86_avx2_psrlv_d:
10484 case Intrinsic::x86_avx2_psrlv_q:
10485 case Intrinsic::x86_avx2_psrlv_d_256:
10486 case Intrinsic::x86_avx2_psrlv_q_256:
10487 Opcode = ISD::SRL;
10488 break;
10489 case Intrinsic::x86_avx2_psrav_d:
10490 case Intrinsic::x86_avx2_psrav_d_256:
10491 Opcode = ISD::SRA;
10492 break;
10493 }
10494 return DAG.getNode(Opcode, dl, Op.getValueType(),
10495 Op.getOperand(1), Op.getOperand(2));
10496 }
10497
Craig Topper969ba282012-01-25 06:43:11 +000010498 case Intrinsic::x86_ssse3_pshuf_b_128:
10499 case Intrinsic::x86_avx2_pshuf_b:
10500 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10501 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010502
Craig Topper969ba282012-01-25 06:43:11 +000010503 case Intrinsic::x86_ssse3_psign_b_128:
10504 case Intrinsic::x86_ssse3_psign_w_128:
10505 case Intrinsic::x86_ssse3_psign_d_128:
10506 case Intrinsic::x86_avx2_psign_b:
10507 case Intrinsic::x86_avx2_psign_w:
10508 case Intrinsic::x86_avx2_psign_d:
10509 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10510 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010511
Craig Toppere566cd02012-01-26 07:18:03 +000010512 case Intrinsic::x86_sse41_insertps:
10513 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10514 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010515
Craig Toppere566cd02012-01-26 07:18:03 +000010516 case Intrinsic::x86_avx_vperm2f128_ps_256:
10517 case Intrinsic::x86_avx_vperm2f128_pd_256:
10518 case Intrinsic::x86_avx_vperm2f128_si_256:
10519 case Intrinsic::x86_avx2_vperm2i128:
10520 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10521 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010522
Craig Topperffa6c402012-04-16 07:13:00 +000010523 case Intrinsic::x86_avx2_permd:
10524 case Intrinsic::x86_avx2_permps:
10525 // Operands intentionally swapped. Mask is last operand to intrinsic,
10526 // but second operand for node/intruction.
10527 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10528 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010529
Craig Topper22d8f0d2012-12-29 18:18:20 +000010530 case Intrinsic::x86_sse_sqrt_ps:
10531 case Intrinsic::x86_sse2_sqrt_pd:
10532 case Intrinsic::x86_avx_sqrt_ps_256:
10533 case Intrinsic::x86_avx_sqrt_pd_256:
10534 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10535
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010536 // ptest and testp intrinsics. The intrinsic these come from are designed to
10537 // return an integer value, not just an instruction so lower it to the ptest
10538 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010539 case Intrinsic::x86_sse41_ptestz:
10540 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010541 case Intrinsic::x86_sse41_ptestnzc:
10542 case Intrinsic::x86_avx_ptestz_256:
10543 case Intrinsic::x86_avx_ptestc_256:
10544 case Intrinsic::x86_avx_ptestnzc_256:
10545 case Intrinsic::x86_avx_vtestz_ps:
10546 case Intrinsic::x86_avx_vtestc_ps:
10547 case Intrinsic::x86_avx_vtestnzc_ps:
10548 case Intrinsic::x86_avx_vtestz_pd:
10549 case Intrinsic::x86_avx_vtestc_pd:
10550 case Intrinsic::x86_avx_vtestnzc_pd:
10551 case Intrinsic::x86_avx_vtestz_ps_256:
10552 case Intrinsic::x86_avx_vtestc_ps_256:
10553 case Intrinsic::x86_avx_vtestnzc_ps_256:
10554 case Intrinsic::x86_avx_vtestz_pd_256:
10555 case Intrinsic::x86_avx_vtestc_pd_256:
10556 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10557 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010558 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010559 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010560 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010561 case Intrinsic::x86_avx_vtestz_ps:
10562 case Intrinsic::x86_avx_vtestz_pd:
10563 case Intrinsic::x86_avx_vtestz_ps_256:
10564 case Intrinsic::x86_avx_vtestz_pd_256:
10565 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010566 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010567 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010568 // ZF = 1
10569 X86CC = X86::COND_E;
10570 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010571 case Intrinsic::x86_avx_vtestc_ps:
10572 case Intrinsic::x86_avx_vtestc_pd:
10573 case Intrinsic::x86_avx_vtestc_ps_256:
10574 case Intrinsic::x86_avx_vtestc_pd_256:
10575 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010576 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010577 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010578 // CF = 1
10579 X86CC = X86::COND_B;
10580 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010581 case Intrinsic::x86_avx_vtestnzc_ps:
10582 case Intrinsic::x86_avx_vtestnzc_pd:
10583 case Intrinsic::x86_avx_vtestnzc_ps_256:
10584 case Intrinsic::x86_avx_vtestnzc_pd_256:
10585 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010586 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010587 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010588 // ZF and CF = 0
10589 X86CC = X86::COND_A;
10590 break;
10591 }
Eric Christopherfd179292009-08-27 18:07:15 +000010592
Eric Christopher71c67532009-07-29 00:28:05 +000010593 SDValue LHS = Op.getOperand(1);
10594 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010595 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10596 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010597 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10598 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10599 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010600 }
Evan Cheng5759f972008-05-04 09:15:50 +000010601
Craig Topper80e46362012-01-23 06:16:53 +000010602 // SSE/AVX shift intrinsics
10603 case Intrinsic::x86_sse2_psll_w:
10604 case Intrinsic::x86_sse2_psll_d:
10605 case Intrinsic::x86_sse2_psll_q:
10606 case Intrinsic::x86_avx2_psll_w:
10607 case Intrinsic::x86_avx2_psll_d:
10608 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010609 case Intrinsic::x86_sse2_psrl_w:
10610 case Intrinsic::x86_sse2_psrl_d:
10611 case Intrinsic::x86_sse2_psrl_q:
10612 case Intrinsic::x86_avx2_psrl_w:
10613 case Intrinsic::x86_avx2_psrl_d:
10614 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010615 case Intrinsic::x86_sse2_psra_w:
10616 case Intrinsic::x86_sse2_psra_d:
10617 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010618 case Intrinsic::x86_avx2_psra_d: {
10619 unsigned Opcode;
10620 switch (IntNo) {
10621 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10622 case Intrinsic::x86_sse2_psll_w:
10623 case Intrinsic::x86_sse2_psll_d:
10624 case Intrinsic::x86_sse2_psll_q:
10625 case Intrinsic::x86_avx2_psll_w:
10626 case Intrinsic::x86_avx2_psll_d:
10627 case Intrinsic::x86_avx2_psll_q:
10628 Opcode = X86ISD::VSHL;
10629 break;
10630 case Intrinsic::x86_sse2_psrl_w:
10631 case Intrinsic::x86_sse2_psrl_d:
10632 case Intrinsic::x86_sse2_psrl_q:
10633 case Intrinsic::x86_avx2_psrl_w:
10634 case Intrinsic::x86_avx2_psrl_d:
10635 case Intrinsic::x86_avx2_psrl_q:
10636 Opcode = X86ISD::VSRL;
10637 break;
10638 case Intrinsic::x86_sse2_psra_w:
10639 case Intrinsic::x86_sse2_psra_d:
10640 case Intrinsic::x86_avx2_psra_w:
10641 case Intrinsic::x86_avx2_psra_d:
10642 Opcode = X86ISD::VSRA;
10643 break;
10644 }
10645 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010646 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010647 }
10648
10649 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010650 case Intrinsic::x86_sse2_pslli_w:
10651 case Intrinsic::x86_sse2_pslli_d:
10652 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010653 case Intrinsic::x86_avx2_pslli_w:
10654 case Intrinsic::x86_avx2_pslli_d:
10655 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010656 case Intrinsic::x86_sse2_psrli_w:
10657 case Intrinsic::x86_sse2_psrli_d:
10658 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010659 case Intrinsic::x86_avx2_psrli_w:
10660 case Intrinsic::x86_avx2_psrli_d:
10661 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010662 case Intrinsic::x86_sse2_psrai_w:
10663 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010664 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010665 case Intrinsic::x86_avx2_psrai_d: {
10666 unsigned Opcode;
10667 switch (IntNo) {
10668 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10669 case Intrinsic::x86_sse2_pslli_w:
10670 case Intrinsic::x86_sse2_pslli_d:
10671 case Intrinsic::x86_sse2_pslli_q:
10672 case Intrinsic::x86_avx2_pslli_w:
10673 case Intrinsic::x86_avx2_pslli_d:
10674 case Intrinsic::x86_avx2_pslli_q:
10675 Opcode = X86ISD::VSHLI;
10676 break;
10677 case Intrinsic::x86_sse2_psrli_w:
10678 case Intrinsic::x86_sse2_psrli_d:
10679 case Intrinsic::x86_sse2_psrli_q:
10680 case Intrinsic::x86_avx2_psrli_w:
10681 case Intrinsic::x86_avx2_psrli_d:
10682 case Intrinsic::x86_avx2_psrli_q:
10683 Opcode = X86ISD::VSRLI;
10684 break;
10685 case Intrinsic::x86_sse2_psrai_w:
10686 case Intrinsic::x86_sse2_psrai_d:
10687 case Intrinsic::x86_avx2_psrai_w:
10688 case Intrinsic::x86_avx2_psrai_d:
10689 Opcode = X86ISD::VSRAI;
10690 break;
10691 }
10692 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010693 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010694 }
10695
Craig Topper4feb6472012-08-06 06:22:36 +000010696 case Intrinsic::x86_sse42_pcmpistria128:
10697 case Intrinsic::x86_sse42_pcmpestria128:
10698 case Intrinsic::x86_sse42_pcmpistric128:
10699 case Intrinsic::x86_sse42_pcmpestric128:
10700 case Intrinsic::x86_sse42_pcmpistrio128:
10701 case Intrinsic::x86_sse42_pcmpestrio128:
10702 case Intrinsic::x86_sse42_pcmpistris128:
10703 case Intrinsic::x86_sse42_pcmpestris128:
10704 case Intrinsic::x86_sse42_pcmpistriz128:
10705 case Intrinsic::x86_sse42_pcmpestriz128: {
10706 unsigned Opcode;
10707 unsigned X86CC;
10708 switch (IntNo) {
10709 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10710 case Intrinsic::x86_sse42_pcmpistria128:
10711 Opcode = X86ISD::PCMPISTRI;
10712 X86CC = X86::COND_A;
10713 break;
10714 case Intrinsic::x86_sse42_pcmpestria128:
10715 Opcode = X86ISD::PCMPESTRI;
10716 X86CC = X86::COND_A;
10717 break;
10718 case Intrinsic::x86_sse42_pcmpistric128:
10719 Opcode = X86ISD::PCMPISTRI;
10720 X86CC = X86::COND_B;
10721 break;
10722 case Intrinsic::x86_sse42_pcmpestric128:
10723 Opcode = X86ISD::PCMPESTRI;
10724 X86CC = X86::COND_B;
10725 break;
10726 case Intrinsic::x86_sse42_pcmpistrio128:
10727 Opcode = X86ISD::PCMPISTRI;
10728 X86CC = X86::COND_O;
10729 break;
10730 case Intrinsic::x86_sse42_pcmpestrio128:
10731 Opcode = X86ISD::PCMPESTRI;
10732 X86CC = X86::COND_O;
10733 break;
10734 case Intrinsic::x86_sse42_pcmpistris128:
10735 Opcode = X86ISD::PCMPISTRI;
10736 X86CC = X86::COND_S;
10737 break;
10738 case Intrinsic::x86_sse42_pcmpestris128:
10739 Opcode = X86ISD::PCMPESTRI;
10740 X86CC = X86::COND_S;
10741 break;
10742 case Intrinsic::x86_sse42_pcmpistriz128:
10743 Opcode = X86ISD::PCMPISTRI;
10744 X86CC = X86::COND_E;
10745 break;
10746 case Intrinsic::x86_sse42_pcmpestriz128:
10747 Opcode = X86ISD::PCMPESTRI;
10748 X86CC = X86::COND_E;
10749 break;
10750 }
10751 SmallVector<SDValue, 5> NewOps;
10752 NewOps.append(Op->op_begin()+1, Op->op_end());
10753 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10754 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10755 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10756 DAG.getConstant(X86CC, MVT::i8),
10757 SDValue(PCMP.getNode(), 1));
10758 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10759 }
Craig Topper6d688152012-08-14 07:43:25 +000010760
Craig Topper4feb6472012-08-06 06:22:36 +000010761 case Intrinsic::x86_sse42_pcmpistri128:
10762 case Intrinsic::x86_sse42_pcmpestri128: {
10763 unsigned Opcode;
10764 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10765 Opcode = X86ISD::PCMPISTRI;
10766 else
10767 Opcode = X86ISD::PCMPESTRI;
10768
10769 SmallVector<SDValue, 5> NewOps;
10770 NewOps.append(Op->op_begin()+1, Op->op_end());
10771 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10772 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10773 }
Craig Topper0e292372012-08-24 04:03:22 +000010774 case Intrinsic::x86_fma_vfmadd_ps:
10775 case Intrinsic::x86_fma_vfmadd_pd:
10776 case Intrinsic::x86_fma_vfmsub_ps:
10777 case Intrinsic::x86_fma_vfmsub_pd:
10778 case Intrinsic::x86_fma_vfnmadd_ps:
10779 case Intrinsic::x86_fma_vfnmadd_pd:
10780 case Intrinsic::x86_fma_vfnmsub_ps:
10781 case Intrinsic::x86_fma_vfnmsub_pd:
10782 case Intrinsic::x86_fma_vfmaddsub_ps:
10783 case Intrinsic::x86_fma_vfmaddsub_pd:
10784 case Intrinsic::x86_fma_vfmsubadd_ps:
10785 case Intrinsic::x86_fma_vfmsubadd_pd:
10786 case Intrinsic::x86_fma_vfmadd_ps_256:
10787 case Intrinsic::x86_fma_vfmadd_pd_256:
10788 case Intrinsic::x86_fma_vfmsub_ps_256:
10789 case Intrinsic::x86_fma_vfmsub_pd_256:
10790 case Intrinsic::x86_fma_vfnmadd_ps_256:
10791 case Intrinsic::x86_fma_vfnmadd_pd_256:
10792 case Intrinsic::x86_fma_vfnmsub_ps_256:
10793 case Intrinsic::x86_fma_vfnmsub_pd_256:
10794 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10795 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10796 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10797 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010798 unsigned Opc;
10799 switch (IntNo) {
10800 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10801 case Intrinsic::x86_fma_vfmadd_ps:
10802 case Intrinsic::x86_fma_vfmadd_pd:
10803 case Intrinsic::x86_fma_vfmadd_ps_256:
10804 case Intrinsic::x86_fma_vfmadd_pd_256:
10805 Opc = X86ISD::FMADD;
10806 break;
10807 case Intrinsic::x86_fma_vfmsub_ps:
10808 case Intrinsic::x86_fma_vfmsub_pd:
10809 case Intrinsic::x86_fma_vfmsub_ps_256:
10810 case Intrinsic::x86_fma_vfmsub_pd_256:
10811 Opc = X86ISD::FMSUB;
10812 break;
10813 case Intrinsic::x86_fma_vfnmadd_ps:
10814 case Intrinsic::x86_fma_vfnmadd_pd:
10815 case Intrinsic::x86_fma_vfnmadd_ps_256:
10816 case Intrinsic::x86_fma_vfnmadd_pd_256:
10817 Opc = X86ISD::FNMADD;
10818 break;
10819 case Intrinsic::x86_fma_vfnmsub_ps:
10820 case Intrinsic::x86_fma_vfnmsub_pd:
10821 case Intrinsic::x86_fma_vfnmsub_ps_256:
10822 case Intrinsic::x86_fma_vfnmsub_pd_256:
10823 Opc = X86ISD::FNMSUB;
10824 break;
10825 case Intrinsic::x86_fma_vfmaddsub_ps:
10826 case Intrinsic::x86_fma_vfmaddsub_pd:
10827 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10828 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10829 Opc = X86ISD::FMADDSUB;
10830 break;
10831 case Intrinsic::x86_fma_vfmsubadd_ps:
10832 case Intrinsic::x86_fma_vfmsubadd_pd:
10833 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10834 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10835 Opc = X86ISD::FMSUBADD;
10836 break;
10837 }
10838
10839 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10840 Op.getOperand(2), Op.getOperand(3));
10841 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010842 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010843}
Evan Cheng72261582005-12-20 06:22:03 +000010844
Craig Topper55b24052012-09-11 06:15:32 +000010845static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010846 DebugLoc dl = Op.getDebugLoc();
10847 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10848 switch (IntNo) {
10849 default: return SDValue(); // Don't custom lower most intrinsics.
10850
10851 // RDRAND intrinsics.
10852 case Intrinsic::x86_rdrand_16:
10853 case Intrinsic::x86_rdrand_32:
10854 case Intrinsic::x86_rdrand_64: {
10855 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010856 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10857 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010858
10859 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10860 // return the value from Rand, which is always 0, casted to i32.
10861 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10862 DAG.getConstant(1, Op->getValueType(1)),
10863 DAG.getConstant(X86::COND_B, MVT::i32),
10864 SDValue(Result.getNode(), 1) };
10865 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10866 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10867 Ops, 4);
10868
10869 // Return { result, isValid, chain }.
10870 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010871 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010872 }
10873 }
10874}
10875
Dan Gohmand858e902010-04-17 15:26:15 +000010876SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10877 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010878 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10879 MFI->setReturnAddressIsTaken(true);
10880
Bill Wendling64e87322009-01-16 19:25:27 +000010881 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010882 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010883 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010884
10885 if (Depth > 0) {
10886 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10887 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010888 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10889 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10890 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010891 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010892 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010893 }
10894
10895 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010896 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010897 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010898 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010899}
10900
Dan Gohmand858e902010-04-17 15:26:15 +000010901SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010902 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10903 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010904
Owen Andersone50ed302009-08-10 22:56:29 +000010905 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010906 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010907 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10908 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010909 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010910 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010911 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10912 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010913 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010914 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010915}
10916
Dan Gohman475871a2008-07-27 21:46:04 +000010917SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010918 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010919 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010920}
10921
Dan Gohmand858e902010-04-17 15:26:15 +000010922SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010923 SDValue Chain = Op.getOperand(0);
10924 SDValue Offset = Op.getOperand(1);
10925 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010926 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010927
Dan Gohmand8816272010-08-11 18:14:00 +000010928 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10929 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10930 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010931 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010932
Dan Gohmand8816272010-08-11 18:14:00 +000010933 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010934 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010935 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010936 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10937 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010938 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010939
Dale Johannesene4d209d2009-02-03 20:21:25 +000010940 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010941 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010942 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010943}
10944
Michael Liao6c0e04c2012-10-15 22:39:43 +000010945SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10946 SelectionDAG &DAG) const {
10947 DebugLoc DL = Op.getDebugLoc();
10948 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10949 DAG.getVTList(MVT::i32, MVT::Other),
10950 Op.getOperand(0), Op.getOperand(1));
10951}
10952
10953SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10954 SelectionDAG &DAG) const {
10955 DebugLoc DL = Op.getDebugLoc();
10956 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10957 Op.getOperand(0), Op.getOperand(1));
10958}
10959
Craig Topper55b24052012-09-11 06:15:32 +000010960static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010961 return Op.getOperand(0);
10962}
10963
10964SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10965 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010966 SDValue Root = Op.getOperand(0);
10967 SDValue Trmp = Op.getOperand(1); // trampoline
10968 SDValue FPtr = Op.getOperand(2); // nested function
10969 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010970 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010971
Dan Gohman69de1932008-02-06 22:27:42 +000010972 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010973 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010974
10975 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010976 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010977
10978 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010979 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10980 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010981
Michael Liao7abf67a2012-10-04 19:50:43 +000010982 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10983 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010984
10985 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10986
10987 // Load the pointer to the nested function into R11.
10988 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010989 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010990 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010991 Addr, MachinePointerInfo(TrmpAddr),
10992 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010993
Owen Anderson825b72b2009-08-11 20:47:22 +000010994 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10995 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010996 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10997 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010998 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010999
11000 // Load the 'nest' parameter value into R10.
11001 // R10 is specified in X86CallingConv.td
11002 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011003 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11004 DAG.getConstant(10, MVT::i64));
11005 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011006 Addr, MachinePointerInfo(TrmpAddr, 10),
11007 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011008
Owen Anderson825b72b2009-08-11 20:47:22 +000011009 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11010 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011011 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11012 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011013 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011014
11015 // Jump to the nested function.
11016 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011017 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11018 DAG.getConstant(20, MVT::i64));
11019 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011020 Addr, MachinePointerInfo(TrmpAddr, 20),
11021 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011022
11023 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011024 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11025 DAG.getConstant(22, MVT::i64));
11026 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011027 MachinePointerInfo(TrmpAddr, 22),
11028 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011029
Duncan Sands4a544a72011-09-06 13:37:06 +000011030 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011031 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011032 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011033 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011034 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011035 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011036
11037 switch (CC) {
11038 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011039 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011040 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011041 case CallingConv::X86_StdCall: {
11042 // Pass 'nest' parameter in ECX.
11043 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011044 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011045
11046 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011047 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011048 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011049
Chris Lattner58d74912008-03-12 17:45:29 +000011050 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011051 unsigned InRegCount = 0;
11052 unsigned Idx = 1;
11053
11054 for (FunctionType::param_iterator I = FTy->param_begin(),
11055 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011056 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011057 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011058 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011059
11060 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011061 report_fatal_error("Nest register in use - reduce number of inreg"
11062 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011063 }
11064 }
11065 break;
11066 }
11067 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011068 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011069 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011070 // Pass 'nest' parameter in EAX.
11071 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011072 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011073 break;
11074 }
11075
Dan Gohman475871a2008-07-27 21:46:04 +000011076 SDValue OutChains[4];
11077 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011078
Owen Anderson825b72b2009-08-11 20:47:22 +000011079 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11080 DAG.getConstant(10, MVT::i32));
11081 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011082
Chris Lattnera62fe662010-02-05 19:20:30 +000011083 // This is storing the opcode for MOV32ri.
11084 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011085 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011086 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011087 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011088 Trmp, MachinePointerInfo(TrmpAddr),
11089 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011090
Owen Anderson825b72b2009-08-11 20:47:22 +000011091 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11092 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011093 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11094 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011095 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011096
Chris Lattnera62fe662010-02-05 19:20:30 +000011097 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011098 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11099 DAG.getConstant(5, MVT::i32));
11100 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011101 MachinePointerInfo(TrmpAddr, 5),
11102 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011103
Owen Anderson825b72b2009-08-11 20:47:22 +000011104 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11105 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011106 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11107 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011108 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011109
Duncan Sands4a544a72011-09-06 13:37:06 +000011110 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011111 }
11112}
11113
Dan Gohmand858e902010-04-17 15:26:15 +000011114SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11115 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011116 /*
11117 The rounding mode is in bits 11:10 of FPSR, and has the following
11118 settings:
11119 00 Round to nearest
11120 01 Round to -inf
11121 10 Round to +inf
11122 11 Round to 0
11123
11124 FLT_ROUNDS, on the other hand, expects the following:
11125 -1 Undefined
11126 0 Round to 0
11127 1 Round to nearest
11128 2 Round to +inf
11129 3 Round to -inf
11130
11131 To perform the conversion, we do:
11132 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11133 */
11134
11135 MachineFunction &MF = DAG.getMachineFunction();
11136 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011137 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011138 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011139 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011140 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011141
11142 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011143 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011144 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011145
Chris Lattner2156b792010-09-22 01:11:26 +000011146 MachineMemOperand *MMO =
11147 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11148 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011149
Chris Lattner2156b792010-09-22 01:11:26 +000011150 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11151 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11152 DAG.getVTList(MVT::Other),
11153 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011154
11155 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011156 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011157 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011158
11159 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011160 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011161 DAG.getNode(ISD::SRL, DL, MVT::i16,
11162 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011163 CWD, DAG.getConstant(0x800, MVT::i16)),
11164 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011165 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011166 DAG.getNode(ISD::SRL, DL, MVT::i16,
11167 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011168 CWD, DAG.getConstant(0x400, MVT::i16)),
11169 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011170
Dan Gohman475871a2008-07-27 21:46:04 +000011171 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011172 DAG.getNode(ISD::AND, DL, MVT::i16,
11173 DAG.getNode(ISD::ADD, DL, MVT::i16,
11174 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011175 DAG.getConstant(1, MVT::i16)),
11176 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011177
Duncan Sands83ec4b62008-06-06 12:08:01 +000011178 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011179 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011180}
11181
Craig Topper55b24052012-09-11 06:15:32 +000011182static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011183 EVT VT = Op.getValueType();
11184 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011185 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011186 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011187
11188 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011189 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011190 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011191 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011192 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011193 }
Evan Cheng18efe262007-12-14 02:13:44 +000011194
Evan Cheng152804e2007-12-14 08:30:15 +000011195 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011196 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011197 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011198
11199 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011200 SDValue Ops[] = {
11201 Op,
11202 DAG.getConstant(NumBits+NumBits-1, OpVT),
11203 DAG.getConstant(X86::COND_E, MVT::i8),
11204 Op.getValue(1)
11205 };
11206 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011207
11208 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011209 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011210
Owen Anderson825b72b2009-08-11 20:47:22 +000011211 if (VT == MVT::i8)
11212 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011213 return Op;
11214}
11215
Craig Topper55b24052012-09-11 06:15:32 +000011216static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011217 EVT VT = Op.getValueType();
11218 EVT OpVT = VT;
11219 unsigned NumBits = VT.getSizeInBits();
11220 DebugLoc dl = Op.getDebugLoc();
11221
11222 Op = Op.getOperand(0);
11223 if (VT == MVT::i8) {
11224 // Zero extend to i32 since there is not an i8 bsr.
11225 OpVT = MVT::i32;
11226 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11227 }
11228
11229 // Issue a bsr (scan bits in reverse).
11230 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11231 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11232
11233 // And xor with NumBits-1.
11234 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11235
11236 if (VT == MVT::i8)
11237 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11238 return Op;
11239}
11240
Craig Topper55b24052012-09-11 06:15:32 +000011241static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011242 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011243 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011244 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011245 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011246
11247 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011248 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011249 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011250
11251 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011252 SDValue Ops[] = {
11253 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011254 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011255 DAG.getConstant(X86::COND_E, MVT::i8),
11256 Op.getValue(1)
11257 };
Chandler Carruth77821022011-12-24 12:12:34 +000011258 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011259}
11260
Craig Topper13894fa2011-08-24 06:14:18 +000011261// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11262// ones, and then concatenate the result back.
11263static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011264 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011265
Craig Topper7a9a28b2012-08-12 02:23:29 +000011266 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011267 "Unsupported value type for operation");
11268
Craig Topper66ddd152012-04-27 22:54:43 +000011269 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011270 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011271
11272 // Extract the LHS vectors
11273 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011274 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11275 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011276
11277 // Extract the RHS vectors
11278 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011279 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11280 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011281
11282 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11283 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11284
11285 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11286 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11287 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11288}
11289
Craig Topper55b24052012-09-11 06:15:32 +000011290static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011291 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011292 Op.getValueType().isInteger() &&
11293 "Only handle AVX 256-bit vector integer operation");
11294 return Lower256IntArith(Op, DAG);
11295}
11296
Craig Topper55b24052012-09-11 06:15:32 +000011297static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011298 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011299 Op.getValueType().isInteger() &&
11300 "Only handle AVX 256-bit vector integer operation");
11301 return Lower256IntArith(Op, DAG);
11302}
11303
Craig Topper55b24052012-09-11 06:15:32 +000011304static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11305 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011306 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011307 EVT VT = Op.getValueType();
11308
11309 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011310 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011311 return Lower256IntArith(Op, DAG);
11312
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011313 SDValue A = Op.getOperand(0);
11314 SDValue B = Op.getOperand(1);
11315
11316 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11317 if (VT == MVT::v4i32) {
11318 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11319 "Should not custom lower when pmuldq is available!");
11320
11321 // Extract the odd parts.
11322 const int UnpackMask[] = { 1, -1, 3, -1 };
11323 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11324 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11325
11326 // Multiply the even parts.
11327 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11328 // Now multiply odd parts.
11329 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11330
11331 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11332 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11333
11334 // Merge the two vectors back together with a shuffle. This expands into 2
11335 // shuffles.
11336 const int ShufMask[] = { 0, 4, 2, 6 };
11337 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11338 }
11339
Craig Topper5b209e82012-02-05 03:14:49 +000011340 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11341 "Only know how to lower V2I64/V4I64 multiply");
11342
Craig Topper5b209e82012-02-05 03:14:49 +000011343 // Ahi = psrlqi(a, 32);
11344 // Bhi = psrlqi(b, 32);
11345 //
11346 // AloBlo = pmuludq(a, b);
11347 // AloBhi = pmuludq(a, Bhi);
11348 // AhiBlo = pmuludq(Ahi, b);
11349
11350 // AloBhi = psllqi(AloBhi, 32);
11351 // AhiBlo = psllqi(AhiBlo, 32);
11352 // return AloBlo + AloBhi + AhiBlo;
11353
Craig Topper5b209e82012-02-05 03:14:49 +000011354 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011355
Craig Topper5b209e82012-02-05 03:14:49 +000011356 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11357 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011358
Craig Topper5b209e82012-02-05 03:14:49 +000011359 // Bit cast to 32-bit vectors for MULUDQ
11360 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11361 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11362 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11363 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11364 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011365
Craig Topper5b209e82012-02-05 03:14:49 +000011366 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11367 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11368 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011369
Craig Topper5b209e82012-02-05 03:14:49 +000011370 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11371 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011372
Dale Johannesene4d209d2009-02-03 20:21:25 +000011373 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011374 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011375}
11376
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011377SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11378 EVT VT = Op.getValueType();
11379 EVT EltTy = VT.getVectorElementType();
11380 unsigned NumElts = VT.getVectorNumElements();
11381 SDValue N0 = Op.getOperand(0);
11382 DebugLoc dl = Op.getDebugLoc();
11383
11384 // Lower sdiv X, pow2-const.
11385 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11386 if (!C)
11387 return SDValue();
11388
11389 APInt SplatValue, SplatUndef;
11390 unsigned MinSplatBits;
11391 bool HasAnyUndefs;
11392 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11393 return SDValue();
11394
11395 if ((SplatValue != 0) &&
11396 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11397 unsigned lg2 = SplatValue.countTrailingZeros();
11398 // Splat the sign bit.
11399 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11400 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11401 // Add (N0 < 0) ? abs2 - 1 : 0;
11402 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11403 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11404 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11405 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11406 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11407
11408 // If we're dividing by a positive value, we're done. Otherwise, we must
11409 // negate the result.
11410 if (SplatValue.isNonNegative())
11411 return SRA;
11412
11413 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11414 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11415 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11416 }
11417 return SDValue();
11418}
11419
Nadav Rotem43012222011-05-11 08:12:09 +000011420SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11421
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011422 EVT VT = Op.getValueType();
11423 DebugLoc dl = Op.getDebugLoc();
11424 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011425 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011426 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011427
Craig Topper1accb7e2012-01-10 06:54:16 +000011428 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011429 return SDValue();
11430
Nadav Rotem43012222011-05-11 08:12:09 +000011431 // Optimize shl/srl/sra with constant shift amount.
11432 if (isSplatVector(Amt.getNode())) {
11433 SDValue SclrAmt = Amt->getOperand(0);
11434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11435 uint64_t ShiftAmt = C->getZExtValue();
11436
Craig Toppered2e13d2012-01-22 19:15:14 +000011437 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011438 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011439 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11440 if (Op.getOpcode() == ISD::SHL)
11441 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11442 DAG.getConstant(ShiftAmt, MVT::i32));
11443 if (Op.getOpcode() == ISD::SRL)
11444 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11445 DAG.getConstant(ShiftAmt, MVT::i32));
11446 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11447 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11448 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011449 }
11450
Craig Toppered2e13d2012-01-22 19:15:14 +000011451 if (VT == MVT::v16i8) {
11452 if (Op.getOpcode() == ISD::SHL) {
11453 // Make a large shift.
11454 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11455 DAG.getConstant(ShiftAmt, MVT::i32));
11456 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11457 // Zero out the rightmost bits.
11458 SmallVector<SDValue, 16> V(16,
11459 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11460 MVT::i8));
11461 return DAG.getNode(ISD::AND, dl, VT, SHL,
11462 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011463 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011464 if (Op.getOpcode() == ISD::SRL) {
11465 // Make a large shift.
11466 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11467 DAG.getConstant(ShiftAmt, MVT::i32));
11468 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11469 // Zero out the leftmost bits.
11470 SmallVector<SDValue, 16> V(16,
11471 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11472 MVT::i8));
11473 return DAG.getNode(ISD::AND, dl, VT, SRL,
11474 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11475 }
11476 if (Op.getOpcode() == ISD::SRA) {
11477 if (ShiftAmt == 7) {
11478 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011479 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011480 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011481 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011482
Craig Toppered2e13d2012-01-22 19:15:14 +000011483 // R s>> a === ((R u>> a) ^ m) - m
11484 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11485 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11486 MVT::i8));
11487 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11488 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11489 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11490 return Res;
11491 }
Craig Topper731dfd02012-04-23 03:42:40 +000011492 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011493 }
Craig Topper46154eb2011-11-11 07:39:23 +000011494
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011495 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011496 if (Op.getOpcode() == ISD::SHL) {
11497 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011498 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11499 DAG.getConstant(ShiftAmt, MVT::i32));
11500 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011501 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011502 SmallVector<SDValue, 32> V(32,
11503 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11504 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011505 return DAG.getNode(ISD::AND, dl, VT, SHL,
11506 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011507 }
Craig Topper0d86d462011-11-20 00:12:05 +000011508 if (Op.getOpcode() == ISD::SRL) {
11509 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011510 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11511 DAG.getConstant(ShiftAmt, MVT::i32));
11512 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011513 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011514 SmallVector<SDValue, 32> V(32,
11515 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11516 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011517 return DAG.getNode(ISD::AND, dl, VT, SRL,
11518 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11519 }
11520 if (Op.getOpcode() == ISD::SRA) {
11521 if (ShiftAmt == 7) {
11522 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011523 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011524 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011525 }
11526
11527 // R s>> a === ((R u>> a) ^ m) - m
11528 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11529 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11530 MVT::i8));
11531 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11532 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11533 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11534 return Res;
11535 }
Craig Topper731dfd02012-04-23 03:42:40 +000011536 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011537 }
Nadav Rotem43012222011-05-11 08:12:09 +000011538 }
11539 }
11540
11541 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011542 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011543 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11544 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011545
Chris Lattner7302d802012-02-06 21:56:39 +000011546 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11547 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011548 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11549 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011550 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011551 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011552
11553 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011554 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011555 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11556 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11557 }
Nadav Rotem43012222011-05-11 08:12:09 +000011558 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011559 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011560
Nate Begeman51409212010-07-28 00:21:48 +000011561 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011562 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11563 DAG.getConstant(5, MVT::i32));
11564 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011565
Lang Hames8b99c1e2011-12-17 01:08:46 +000011566 // Turn 'a' into a mask suitable for VSELECT
11567 SDValue VSelM = DAG.getConstant(0x80, VT);
11568 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011569 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011570
Lang Hames8b99c1e2011-12-17 01:08:46 +000011571 SDValue CM1 = DAG.getConstant(0x0f, VT);
11572 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011573
Lang Hames8b99c1e2011-12-17 01:08:46 +000011574 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11575 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011576 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11577 DAG.getConstant(4, MVT::i32), DAG);
11578 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011579 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11580
Nate Begeman51409212010-07-28 00:21:48 +000011581 // a += a
11582 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011583 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011584 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011585
Lang Hames8b99c1e2011-12-17 01:08:46 +000011586 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11587 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011588 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11589 DAG.getConstant(2, MVT::i32), DAG);
11590 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011591 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11592
Nate Begeman51409212010-07-28 00:21:48 +000011593 // a += a
11594 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011595 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011596 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011597
Lang Hames8b99c1e2011-12-17 01:08:46 +000011598 // return VSELECT(r, r+r, a);
11599 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011600 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011601 return R;
11602 }
Craig Topper46154eb2011-11-11 07:39:23 +000011603
11604 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011605 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011606 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011607 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11608 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11609
11610 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011611 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11612 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011613
11614 // Recreate the shift amount vectors
11615 SDValue Amt1, Amt2;
11616 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11617 // Constant shift amount
11618 SmallVector<SDValue, 4> Amt1Csts;
11619 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011620 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011621 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011622 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011623 Amt2Csts.push_back(Amt->getOperand(i));
11624
11625 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11626 &Amt1Csts[0], NumElems/2);
11627 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11628 &Amt2Csts[0], NumElems/2);
11629 } else {
11630 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011631 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11632 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011633 }
11634
11635 // Issue new vector shifts for the smaller types
11636 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11637 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11638
11639 // Concatenate the result back
11640 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11641 }
11642
Nate Begeman51409212010-07-28 00:21:48 +000011643 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011644}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011645
Craig Topper55b24052012-09-11 06:15:32 +000011646static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011647 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11648 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011649 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11650 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011651 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011652 SDValue LHS = N->getOperand(0);
11653 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011654 unsigned BaseOp = 0;
11655 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011656 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011657 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011658 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011659 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011660 // A subtract of one will be selected as a INC. Note that INC doesn't
11661 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011662 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11663 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011664 BaseOp = X86ISD::INC;
11665 Cond = X86::COND_O;
11666 break;
11667 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011668 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011669 Cond = X86::COND_O;
11670 break;
11671 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011672 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011673 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011674 break;
11675 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011676 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11677 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011678 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11679 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011680 BaseOp = X86ISD::DEC;
11681 Cond = X86::COND_O;
11682 break;
11683 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011684 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011685 Cond = X86::COND_O;
11686 break;
11687 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011688 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011689 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011690 break;
11691 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011692 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011693 Cond = X86::COND_O;
11694 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011695 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11696 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11697 MVT::i32);
11698 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011699
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011700 SDValue SetCC =
11701 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11702 DAG.getConstant(X86::COND_O, MVT::i32),
11703 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011704
Dan Gohman6e5fda22011-07-22 18:45:15 +000011705 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011706 }
Bill Wendling74c37652008-12-09 22:08:41 +000011707 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011708
Bill Wendling61edeb52008-12-02 01:06:39 +000011709 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011710 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011711 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011712
Bill Wendling61edeb52008-12-02 01:06:39 +000011713 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011714 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11715 DAG.getConstant(Cond, MVT::i32),
11716 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011717
Dan Gohman6e5fda22011-07-22 18:45:15 +000011718 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011719}
11720
Chad Rosier30450e82011-12-22 22:35:21 +000011721SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11722 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011723 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011724 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11725 EVT VT = Op.getValueType();
11726
Craig Toppered2e13d2012-01-22 19:15:14 +000011727 if (!Subtarget->hasSSE2() || !VT.isVector())
11728 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011729
Craig Toppered2e13d2012-01-22 19:15:14 +000011730 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11731 ExtraVT.getScalarType().getSizeInBits();
11732 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11733
11734 switch (VT.getSimpleVT().SimpleTy) {
11735 default: return SDValue();
11736 case MVT::v8i32:
11737 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011738 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011739 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011740 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011741 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011742 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011743
Craig Toppered2e13d2012-01-22 19:15:14 +000011744 // Extract the LHS vectors
11745 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011746 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11747 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011748
Craig Toppered2e13d2012-01-22 19:15:14 +000011749 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11750 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011751
Craig Toppered2e13d2012-01-22 19:15:14 +000011752 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011753 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011754 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11755 ExtraNumElems/2);
11756 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011757
Craig Toppered2e13d2012-01-22 19:15:14 +000011758 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11759 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011760
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011761 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011762 }
11763 // fall through
11764 case MVT::v4i32:
11765 case MVT::v8i16: {
11766 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11767 Op.getOperand(0), ShAmt, DAG);
11768 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011769 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011770 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011771}
11772
Craig Topper55b24052012-09-11 06:15:32 +000011773static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11774 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011775 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011776
Eric Christopher77ed1352011-07-08 00:04:56 +000011777 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11778 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011779 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011780 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011781 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011782 SDValue Ops[] = {
11783 DAG.getRegister(X86::ESP, MVT::i32), // Base
11784 DAG.getTargetConstant(1, MVT::i8), // Scale
11785 DAG.getRegister(0, MVT::i32), // Index
11786 DAG.getTargetConstant(0, MVT::i32), // Disp
11787 DAG.getRegister(0, MVT::i32), // Segment.
11788 Zero,
11789 Chain
11790 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011791 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011792 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11793 array_lengthof(Ops));
11794 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011795 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011796
Eric Christopher9a9d2752010-07-22 02:48:34 +000011797 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011798 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011799 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011800
Chris Lattner132929a2010-08-14 17:26:09 +000011801 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11802 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11803 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11804 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011805
Chris Lattner132929a2010-08-14 17:26:09 +000011806 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11807 if (!Op1 && !Op2 && !Op3 && Op4)
11808 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011809
Chris Lattner132929a2010-08-14 17:26:09 +000011810 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11811 if (Op1 && !Op2 && !Op3 && !Op4)
11812 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011813
11814 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011815 // (MFENCE)>;
11816 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011817}
11818
Craig Topper55b24052012-09-11 06:15:32 +000011819static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11820 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011821 DebugLoc dl = Op.getDebugLoc();
11822 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11823 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11824 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11825 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11826
11827 // The only fence that needs an instruction is a sequentially-consistent
11828 // cross-thread fence.
11829 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11830 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11831 // no-sse2). There isn't any reason to disable it if the target processor
11832 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011833 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011834 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11835
11836 SDValue Chain = Op.getOperand(0);
11837 SDValue Zero = DAG.getConstant(0, MVT::i32);
11838 SDValue Ops[] = {
11839 DAG.getRegister(X86::ESP, MVT::i32), // Base
11840 DAG.getTargetConstant(1, MVT::i8), // Scale
11841 DAG.getRegister(0, MVT::i32), // Index
11842 DAG.getTargetConstant(0, MVT::i32), // Disp
11843 DAG.getRegister(0, MVT::i32), // Segment.
11844 Zero,
11845 Chain
11846 };
11847 SDNode *Res =
11848 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11849 array_lengthof(Ops));
11850 return SDValue(Res, 0);
11851 }
11852
11853 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11854 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11855}
11856
Craig Topper55b24052012-09-11 06:15:32 +000011857static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11858 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011859 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011860 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011861 unsigned Reg = 0;
11862 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011863 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011864 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011865 case MVT::i8: Reg = X86::AL; size = 1; break;
11866 case MVT::i16: Reg = X86::AX; size = 2; break;
11867 case MVT::i32: Reg = X86::EAX; size = 4; break;
11868 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011869 assert(Subtarget->is64Bit() && "Node not type legal!");
11870 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011871 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011872 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011873 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011874 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011875 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011876 Op.getOperand(1),
11877 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011878 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011879 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011880 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011881 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11882 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11883 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011884 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011885 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011886 return cpOut;
11887}
11888
Craig Topper55b24052012-09-11 06:15:32 +000011889static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11890 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011891 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011892 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011893 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011894 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011895 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011896 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11897 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011898 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011899 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11900 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011901 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011902 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011903 rdx.getValue(1)
11904 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011905 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011906}
11907
Craig Topper55b24052012-09-11 06:15:32 +000011908SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011909 EVT SrcVT = Op.getOperand(0).getValueType();
11910 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011911 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011912 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011913 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011914 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011915 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011916 // i64 <=> MMX conversions are Legal.
11917 if (SrcVT==MVT::i64 && DstVT.isVector())
11918 return Op;
11919 if (DstVT==MVT::i64 && SrcVT.isVector())
11920 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011921 // MMX <=> MMX conversions are Legal.
11922 if (SrcVT.isVector() && DstVT.isVector())
11923 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011924 // All other conversions need to be expanded.
11925 return SDValue();
11926}
Chris Lattner5b856542010-12-20 00:59:46 +000011927
Craig Topper55b24052012-09-11 06:15:32 +000011928static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011929 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011930 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011931 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011932 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011933 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011934 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011935 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011936 Node->getOperand(0),
11937 Node->getOperand(1), negOp,
11938 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011939 cast<AtomicSDNode>(Node)->getAlignment(),
11940 cast<AtomicSDNode>(Node)->getOrdering(),
11941 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011942}
11943
Eli Friedman327236c2011-08-24 20:50:09 +000011944static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11945 SDNode *Node = Op.getNode();
11946 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011947 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011948
11949 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011950 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11951 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11952 // (The only way to get a 16-byte store is cmpxchg16b)
11953 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11954 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11955 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011956 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11957 cast<AtomicSDNode>(Node)->getMemoryVT(),
11958 Node->getOperand(0),
11959 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011960 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011961 cast<AtomicSDNode>(Node)->getOrdering(),
11962 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011963 return Swap.getValue(1);
11964 }
11965 // Other atomic stores have a simple pattern.
11966 return Op;
11967}
11968
Chris Lattner5b856542010-12-20 00:59:46 +000011969static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11970 EVT VT = Op.getNode()->getValueType(0);
11971
11972 // Let legalize expand this if it isn't a legal type yet.
11973 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11974 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011975
Chris Lattner5b856542010-12-20 00:59:46 +000011976 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011977
Chris Lattner5b856542010-12-20 00:59:46 +000011978 unsigned Opc;
11979 bool ExtraOp = false;
11980 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011981 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011982 case ISD::ADDC: Opc = X86ISD::ADD; break;
11983 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11984 case ISD::SUBC: Opc = X86ISD::SUB; break;
11985 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11986 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011987
Chris Lattner5b856542010-12-20 00:59:46 +000011988 if (!ExtraOp)
11989 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11990 Op.getOperand(1));
11991 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11992 Op.getOperand(1), Op.getOperand(2));
11993}
11994
Evan Cheng0db9fe62006-04-25 20:13:52 +000011995/// LowerOperation - Provide custom lowering hooks for some operations.
11996///
Dan Gohmand858e902010-04-17 15:26:15 +000011997SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011998 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011999 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012000 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012001 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12002 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12003 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012004 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012005 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012006 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012007 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012008 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12009 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12010 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012011 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12012 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012013 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12014 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12015 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012016 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012017 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012018 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012019 case ISD::SHL_PARTS:
12020 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012021 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012022 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012023 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000012024 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012025 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12026 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12027 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012028 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012029 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000012030 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012031 case ISD::FABS: return LowerFABS(Op, DAG);
12032 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012033 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012034 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012035 case ISD::SETCC: return LowerSETCC(Op, DAG);
12036 case ISD::SELECT: return LowerSELECT(Op, DAG);
12037 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012038 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012039 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012040 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012041 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012042 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012043 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012044 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12045 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012046 case ISD::FRAME_TO_ARGS_OFFSET:
12047 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012048 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012049 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012050 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12051 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012052 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12053 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012054 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012055 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012056 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012057 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012058 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012059 case ISD::SRA:
12060 case ISD::SRL:
12061 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012062 case ISD::SADDO:
12063 case ISD::UADDO:
12064 case ISD::SSUBO:
12065 case ISD::USUBO:
12066 case ISD::SMULO:
12067 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012068 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012069 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012070 case ISD::ADDC:
12071 case ISD::ADDE:
12072 case ISD::SUBC:
12073 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012074 case ISD::ADD: return LowerADD(Op, DAG);
12075 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012076 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012077 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012078}
12079
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012080static void ReplaceATOMIC_LOAD(SDNode *Node,
12081 SmallVectorImpl<SDValue> &Results,
12082 SelectionDAG &DAG) {
12083 DebugLoc dl = Node->getDebugLoc();
12084 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12085
12086 // Convert wide load -> cmpxchg8b/cmpxchg16b
12087 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12088 // (The only way to get a 16-byte load is cmpxchg16b)
12089 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012090 SDValue Zero = DAG.getConstant(0, VT);
12091 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012092 Node->getOperand(0),
12093 Node->getOperand(1), Zero, Zero,
12094 cast<AtomicSDNode>(Node)->getMemOperand(),
12095 cast<AtomicSDNode>(Node)->getOrdering(),
12096 cast<AtomicSDNode>(Node)->getSynchScope());
12097 Results.push_back(Swap.getValue(0));
12098 Results.push_back(Swap.getValue(1));
12099}
12100
Craig Topperc0878702012-08-17 06:55:11 +000012101static void
Duncan Sands1607f052008-12-01 11:39:25 +000012102ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012103 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012104 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012105 assert (Node->getValueType(0) == MVT::i64 &&
12106 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012107
12108 SDValue Chain = Node->getOperand(0);
12109 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012110 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012111 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012112 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012113 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012114 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012115 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012116 SDValue Result =
12117 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12118 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012119 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012120 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012121 Results.push_back(Result.getValue(2));
12122}
12123
Duncan Sands126d9072008-07-04 11:47:58 +000012124/// ReplaceNodeResults - Replace a node with an illegal result type
12125/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012126void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12127 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012128 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012129 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012130 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012131 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012132 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012133 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012134 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012135 case ISD::ADDC:
12136 case ISD::ADDE:
12137 case ISD::SUBC:
12138 case ISD::SUBE:
12139 // We don't want to expand or promote these.
12140 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012141 case ISD::FP_TO_SINT:
12142 case ISD::FP_TO_UINT: {
12143 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12144
12145 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12146 return;
12147
Eli Friedman948e95a2009-05-23 09:59:16 +000012148 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012149 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012150 SDValue FIST = Vals.first, StackSlot = Vals.second;
12151 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012152 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012153 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012154 if (StackSlot.getNode() != 0)
12155 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12156 MachinePointerInfo(),
12157 false, false, false, 0));
12158 else
12159 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012160 }
12161 return;
12162 }
Michael Liao991b6a22012-10-24 04:09:32 +000012163 case ISD::UINT_TO_FP: {
12164 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
12165 N->getValueType(0) != MVT::v2f32)
12166 return;
12167 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12168 N->getOperand(0));
12169 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12170 MVT::f64);
12171 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12172 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12173 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12174 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12175 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12176 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12177 return;
12178 }
Michael Liao44c2d612012-10-10 16:53:28 +000012179 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012180 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12181 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012182 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12183 Results.push_back(V);
12184 return;
12185 }
Duncan Sands1607f052008-12-01 11:39:25 +000012186 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012187 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012188 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012189 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012190 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012191 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012192 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012193 eax.getValue(2));
12194 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12195 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012196 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012197 Results.push_back(edx.getValue(1));
12198 return;
12199 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012200 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012201 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012202 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012203 bool Regs64bit = T == MVT::i128;
12204 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012205 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012206 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12207 DAG.getConstant(0, HalfT));
12208 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12209 DAG.getConstant(1, HalfT));
12210 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12211 Regs64bit ? X86::RAX : X86::EAX,
12212 cpInL, SDValue());
12213 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12214 Regs64bit ? X86::RDX : X86::EDX,
12215 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012216 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012217 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12218 DAG.getConstant(0, HalfT));
12219 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12220 DAG.getConstant(1, HalfT));
12221 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12222 Regs64bit ? X86::RBX : X86::EBX,
12223 swapInL, cpInH.getValue(1));
12224 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012225 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012226 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012227 SDValue Ops[] = { swapInH.getValue(0),
12228 N->getOperand(1),
12229 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012230 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012231 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012232 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12233 X86ISD::LCMPXCHG8_DAG;
12234 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012235 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012236 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12237 Regs64bit ? X86::RAX : X86::EAX,
12238 HalfT, Result.getValue(1));
12239 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12240 Regs64bit ? X86::RDX : X86::EDX,
12241 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012242 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012243 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012244 Results.push_back(cpOutH.getValue(1));
12245 return;
12246 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012247 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012248 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012249 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012250 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012251 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012252 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012253 case ISD::ATOMIC_LOAD_MAX:
12254 case ISD::ATOMIC_LOAD_MIN:
12255 case ISD::ATOMIC_LOAD_UMAX:
12256 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012257 case ISD::ATOMIC_SWAP: {
12258 unsigned Opc;
12259 switch (N->getOpcode()) {
12260 default: llvm_unreachable("Unexpected opcode");
12261 case ISD::ATOMIC_LOAD_ADD:
12262 Opc = X86ISD::ATOMADD64_DAG;
12263 break;
12264 case ISD::ATOMIC_LOAD_AND:
12265 Opc = X86ISD::ATOMAND64_DAG;
12266 break;
12267 case ISD::ATOMIC_LOAD_NAND:
12268 Opc = X86ISD::ATOMNAND64_DAG;
12269 break;
12270 case ISD::ATOMIC_LOAD_OR:
12271 Opc = X86ISD::ATOMOR64_DAG;
12272 break;
12273 case ISD::ATOMIC_LOAD_SUB:
12274 Opc = X86ISD::ATOMSUB64_DAG;
12275 break;
12276 case ISD::ATOMIC_LOAD_XOR:
12277 Opc = X86ISD::ATOMXOR64_DAG;
12278 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012279 case ISD::ATOMIC_LOAD_MAX:
12280 Opc = X86ISD::ATOMMAX64_DAG;
12281 break;
12282 case ISD::ATOMIC_LOAD_MIN:
12283 Opc = X86ISD::ATOMMIN64_DAG;
12284 break;
12285 case ISD::ATOMIC_LOAD_UMAX:
12286 Opc = X86ISD::ATOMUMAX64_DAG;
12287 break;
12288 case ISD::ATOMIC_LOAD_UMIN:
12289 Opc = X86ISD::ATOMUMIN64_DAG;
12290 break;
Craig Topperc0878702012-08-17 06:55:11 +000012291 case ISD::ATOMIC_SWAP:
12292 Opc = X86ISD::ATOMSWAP64_DAG;
12293 break;
12294 }
12295 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012296 return;
Craig Topperc0878702012-08-17 06:55:11 +000012297 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012298 case ISD::ATOMIC_LOAD:
12299 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012300 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012301}
12302
Evan Cheng72261582005-12-20 06:22:03 +000012303const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12304 switch (Opcode) {
12305 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012306 case X86ISD::BSF: return "X86ISD::BSF";
12307 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012308 case X86ISD::SHLD: return "X86ISD::SHLD";
12309 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012310 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012311 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012312 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012313 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012314 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012315 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012316 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12317 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12318 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012319 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012320 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012321 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012322 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012323 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012324 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012325 case X86ISD::COMI: return "X86ISD::COMI";
12326 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012327 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012328 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012329 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12330 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012331 case X86ISD::CMOV: return "X86ISD::CMOV";
12332 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012333 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012334 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12335 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012336 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012337 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012338 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012339 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012340 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012341 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12342 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012343 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012344 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012345 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012346 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012347 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012348 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012349 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012350 case X86ISD::HADD: return "X86ISD::HADD";
12351 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012352 case X86ISD::FHADD: return "X86ISD::FHADD";
12353 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012354 case X86ISD::UMAX: return "X86ISD::UMAX";
12355 case X86ISD::UMIN: return "X86ISD::UMIN";
12356 case X86ISD::SMAX: return "X86ISD::SMAX";
12357 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012358 case X86ISD::FMAX: return "X86ISD::FMAX";
12359 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012360 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12361 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012362 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12363 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012364 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012365 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012366 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012367 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12368 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012369 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012370 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012371 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012372 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012373 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12374 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012375 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12376 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12377 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12378 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12379 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12380 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012381 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012382 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012383 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012384 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12385 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012386 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012387 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012388 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12389 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012390 case X86ISD::VSHL: return "X86ISD::VSHL";
12391 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012392 case X86ISD::VSRA: return "X86ISD::VSRA";
12393 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12394 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12395 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012396 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012397 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12398 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012399 case X86ISD::ADD: return "X86ISD::ADD";
12400 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012401 case X86ISD::ADC: return "X86ISD::ADC";
12402 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012403 case X86ISD::SMUL: return "X86ISD::SMUL";
12404 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012405 case X86ISD::INC: return "X86ISD::INC";
12406 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012407 case X86ISD::OR: return "X86ISD::OR";
12408 case X86ISD::XOR: return "X86ISD::XOR";
12409 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012410 case X86ISD::BLSI: return "X86ISD::BLSI";
12411 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12412 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012413 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012414 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012415 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012416 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12417 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12418 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012419 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012420 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012421 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012422 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012423 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012424 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12425 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012426 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12427 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12428 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012429 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12430 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012431 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12432 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012433 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012434 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012435 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012436 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12437 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012438 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012439 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012440 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012441 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012442 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012443 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012444 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012445 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012446 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012447 case X86ISD::FMADD: return "X86ISD::FMADD";
12448 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12449 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12450 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12451 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12452 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012453 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12454 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012455 }
12456}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012457
Chris Lattnerc9addb72007-03-30 23:15:24 +000012458// isLegalAddressingMode - Return true if the addressing mode represented
12459// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012460bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012461 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012462 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012463 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012464 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012465
Chris Lattnerc9addb72007-03-30 23:15:24 +000012466 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012467 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012468 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012469
Chris Lattnerc9addb72007-03-30 23:15:24 +000012470 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012471 unsigned GVFlags =
12472 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012473
Chris Lattnerdfed4132009-07-10 07:38:24 +000012474 // If a reference to this global requires an extra load, we can't fold it.
12475 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012476 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012477
Chris Lattnerdfed4132009-07-10 07:38:24 +000012478 // If BaseGV requires a register for the PIC base, we cannot also have a
12479 // BaseReg specified.
12480 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012481 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012482
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012483 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012484 if ((M != CodeModel::Small || R != Reloc::Static) &&
12485 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012486 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012487 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012488
Chris Lattnerc9addb72007-03-30 23:15:24 +000012489 switch (AM.Scale) {
12490 case 0:
12491 case 1:
12492 case 2:
12493 case 4:
12494 case 8:
12495 // These scales always work.
12496 break;
12497 case 3:
12498 case 5:
12499 case 9:
12500 // These scales are formed with basereg+scalereg. Only accept if there is
12501 // no basereg yet.
12502 if (AM.HasBaseReg)
12503 return false;
12504 break;
12505 default: // Other stuff never works.
12506 return false;
12507 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012508
Chris Lattnerc9addb72007-03-30 23:15:24 +000012509 return true;
12510}
12511
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012512bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012513 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012514 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012515 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12516 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012517 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012518}
12519
Evan Cheng70e10d32012-07-17 06:53:39 +000012520bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012521 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012522}
12523
12524bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012525 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012526 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012527}
12528
Owen Andersone50ed302009-08-10 22:56:29 +000012529bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012530 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012531 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012532 unsigned NumBits1 = VT1.getSizeInBits();
12533 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012534 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012535}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012536
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012537bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012538 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012539 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012540}
12541
Owen Andersone50ed302009-08-10 22:56:29 +000012542bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012543 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012544 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012545}
12546
Evan Cheng2766a472012-12-06 19:13:27 +000012547bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12548 EVT VT1 = Val.getValueType();
12549 if (isZExtFree(VT1, VT2))
12550 return true;
12551
12552 if (Val.getOpcode() != ISD::LOAD)
12553 return false;
12554
12555 if (!VT1.isSimple() || !VT1.isInteger() ||
12556 !VT2.isSimple() || !VT2.isInteger())
12557 return false;
12558
12559 switch (VT1.getSimpleVT().SimpleTy) {
12560 default: break;
12561 case MVT::i8:
12562 case MVT::i16:
12563 case MVT::i32:
12564 // X86 has 8, 16, and 32-bit zero-extending loads.
12565 return true;
12566 }
12567
12568 return false;
12569}
12570
Owen Andersone50ed302009-08-10 22:56:29 +000012571bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012572 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012573 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012574}
12575
Evan Cheng60c07e12006-07-05 22:17:51 +000012576/// isShuffleMaskLegal - Targets can use this to indicate that they only
12577/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12578/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12579/// are assumed to be legal.
12580bool
Eric Christopherfd179292009-08-27 18:07:15 +000012581X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012582 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012583 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012584 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012585 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012586
Nate Begemana09008b2009-10-19 02:17:23 +000012587 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012588 return (VT.getVectorNumElements() == 2 ||
12589 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12590 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012591 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012592 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012593 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12594 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012595 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012596 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12597 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12598 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12599 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012600}
12601
Dan Gohman7d8143f2008-04-09 20:09:42 +000012602bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012603X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012604 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012605 unsigned NumElts = VT.getVectorNumElements();
12606 // FIXME: This collection of masks seems suspect.
12607 if (NumElts == 2)
12608 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012609 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012610 return (isMOVLMask(Mask, VT) ||
12611 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012612 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12613 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012614 }
12615 return false;
12616}
12617
12618//===----------------------------------------------------------------------===//
12619// X86 Scheduler Hooks
12620//===----------------------------------------------------------------------===//
12621
Michael Liaobe02a902012-11-08 07:28:54 +000012622/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012623static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12624 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012625 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012626
12627 const BasicBlock *BB = MBB->getBasicBlock();
12628 MachineFunction::iterator I = MBB;
12629 ++I;
12630
12631 // For the v = xbegin(), we generate
12632 //
12633 // thisMBB:
12634 // xbegin sinkMBB
12635 //
12636 // mainMBB:
12637 // eax = -1
12638 //
12639 // sinkMBB:
12640 // v = eax
12641
12642 MachineBasicBlock *thisMBB = MBB;
12643 MachineFunction *MF = MBB->getParent();
12644 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12645 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12646 MF->insert(I, mainMBB);
12647 MF->insert(I, sinkMBB);
12648
12649 // Transfer the remainder of BB and its successor edges to sinkMBB.
12650 sinkMBB->splice(sinkMBB->begin(), MBB,
12651 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12652 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12653
12654 // thisMBB:
12655 // xbegin sinkMBB
12656 // # fallthrough to mainMBB
12657 // # abortion to sinkMBB
12658 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12659 thisMBB->addSuccessor(mainMBB);
12660 thisMBB->addSuccessor(sinkMBB);
12661
12662 // mainMBB:
12663 // EAX = -1
12664 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12665 mainMBB->addSuccessor(sinkMBB);
12666
12667 // sinkMBB:
12668 // EAX is live into the sinkMBB
12669 sinkMBB->addLiveIn(X86::EAX);
12670 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12671 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12672 .addReg(X86::EAX);
12673
12674 MI->eraseFromParent();
12675 return sinkMBB;
12676}
12677
Michael Liaob118a072012-09-20 03:06:15 +000012678// Get CMPXCHG opcode for the specified data type.
12679static unsigned getCmpXChgOpcode(EVT VT) {
12680 switch (VT.getSimpleVT().SimpleTy) {
12681 case MVT::i8: return X86::LCMPXCHG8;
12682 case MVT::i16: return X86::LCMPXCHG16;
12683 case MVT::i32: return X86::LCMPXCHG32;
12684 case MVT::i64: return X86::LCMPXCHG64;
12685 default:
12686 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012687 }
Michael Liaob118a072012-09-20 03:06:15 +000012688 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012689}
12690
Michael Liaob118a072012-09-20 03:06:15 +000012691// Get LOAD opcode for the specified data type.
12692static unsigned getLoadOpcode(EVT VT) {
12693 switch (VT.getSimpleVT().SimpleTy) {
12694 case MVT::i8: return X86::MOV8rm;
12695 case MVT::i16: return X86::MOV16rm;
12696 case MVT::i32: return X86::MOV32rm;
12697 case MVT::i64: return X86::MOV64rm;
12698 default:
12699 break;
12700 }
12701 llvm_unreachable("Invalid operand size!");
12702}
12703
12704// Get opcode of the non-atomic one from the specified atomic instruction.
12705static unsigned getNonAtomicOpcode(unsigned Opc) {
12706 switch (Opc) {
12707 case X86::ATOMAND8: return X86::AND8rr;
12708 case X86::ATOMAND16: return X86::AND16rr;
12709 case X86::ATOMAND32: return X86::AND32rr;
12710 case X86::ATOMAND64: return X86::AND64rr;
12711 case X86::ATOMOR8: return X86::OR8rr;
12712 case X86::ATOMOR16: return X86::OR16rr;
12713 case X86::ATOMOR32: return X86::OR32rr;
12714 case X86::ATOMOR64: return X86::OR64rr;
12715 case X86::ATOMXOR8: return X86::XOR8rr;
12716 case X86::ATOMXOR16: return X86::XOR16rr;
12717 case X86::ATOMXOR32: return X86::XOR32rr;
12718 case X86::ATOMXOR64: return X86::XOR64rr;
12719 }
12720 llvm_unreachable("Unhandled atomic-load-op opcode!");
12721}
12722
12723// Get opcode of the non-atomic one from the specified atomic instruction with
12724// extra opcode.
12725static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12726 unsigned &ExtraOpc) {
12727 switch (Opc) {
12728 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12729 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12730 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12731 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012732 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012733 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12734 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12735 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012736 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012737 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12738 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12739 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012740 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012741 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12742 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12743 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012744 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012745 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12746 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12747 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12748 }
12749 llvm_unreachable("Unhandled atomic-load-op opcode!");
12750}
12751
12752// Get opcode of the non-atomic one from the specified atomic instruction for
12753// 64-bit data type on 32-bit target.
12754static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12755 switch (Opc) {
12756 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12757 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12758 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12759 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12760 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12761 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012762 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12763 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12764 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12765 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012766 }
12767 llvm_unreachable("Unhandled atomic-load-op opcode!");
12768}
12769
12770// Get opcode of the non-atomic one from the specified atomic instruction for
12771// 64-bit data type on 32-bit target with extra opcode.
12772static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12773 unsigned &HiOpc,
12774 unsigned &ExtraOpc) {
12775 switch (Opc) {
12776 case X86::ATOMNAND6432:
12777 ExtraOpc = X86::NOT32r;
12778 HiOpc = X86::AND32rr;
12779 return X86::AND32rr;
12780 }
12781 llvm_unreachable("Unhandled atomic-load-op opcode!");
12782}
12783
12784// Get pseudo CMOV opcode from the specified data type.
12785static unsigned getPseudoCMOVOpc(EVT VT) {
12786 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012787 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012788 case MVT::i16: return X86::CMOV_GR16;
12789 case MVT::i32: return X86::CMOV_GR32;
12790 default:
12791 break;
12792 }
12793 llvm_unreachable("Unknown CMOV opcode!");
12794}
12795
12796// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12797// They will be translated into a spin-loop or compare-exchange loop from
12798//
12799// ...
12800// dst = atomic-fetch-op MI.addr, MI.val
12801// ...
12802//
12803// to
12804//
12805// ...
12806// EAX = LOAD MI.addr
12807// loop:
12808// t1 = OP MI.val, EAX
12809// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12810// JNE loop
12811// sink:
12812// dst = EAX
12813// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012814MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012815X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12816 MachineBasicBlock *MBB) const {
12817 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12818 DebugLoc DL = MI->getDebugLoc();
12819
12820 MachineFunction *MF = MBB->getParent();
12821 MachineRegisterInfo &MRI = MF->getRegInfo();
12822
12823 const BasicBlock *BB = MBB->getBasicBlock();
12824 MachineFunction::iterator I = MBB;
12825 ++I;
12826
12827 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12828 "Unexpected number of operands");
12829
12830 assert(MI->hasOneMemOperand() &&
12831 "Expected atomic-load-op to have one memoperand");
12832
12833 // Memory Reference
12834 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12835 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12836
12837 unsigned DstReg, SrcReg;
12838 unsigned MemOpndSlot;
12839
12840 unsigned CurOp = 0;
12841
12842 DstReg = MI->getOperand(CurOp++).getReg();
12843 MemOpndSlot = CurOp;
12844 CurOp += X86::AddrNumOperands;
12845 SrcReg = MI->getOperand(CurOp++).getReg();
12846
12847 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012848 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012849 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12850
12851 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12852 unsigned LOADOpc = getLoadOpcode(VT);
12853
12854 // For the atomic load-arith operator, we generate
12855 //
12856 // thisMBB:
12857 // EAX = LOAD [MI.addr]
12858 // mainMBB:
12859 // t1 = OP MI.val, EAX
12860 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12861 // JNE mainMBB
12862 // sinkMBB:
12863
12864 MachineBasicBlock *thisMBB = MBB;
12865 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12866 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12867 MF->insert(I, mainMBB);
12868 MF->insert(I, sinkMBB);
12869
12870 MachineInstrBuilder MIB;
12871
12872 // Transfer the remainder of BB and its successor edges to sinkMBB.
12873 sinkMBB->splice(sinkMBB->begin(), MBB,
12874 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12875 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12876
12877 // thisMBB:
12878 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12879 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12880 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12881 MIB.setMemRefs(MMOBegin, MMOEnd);
12882
12883 thisMBB->addSuccessor(mainMBB);
12884
12885 // mainMBB:
12886 MachineBasicBlock *origMainMBB = mainMBB;
12887 mainMBB->addLiveIn(AccPhyReg);
12888
12889 // Copy AccPhyReg as it is used more than once.
12890 unsigned AccReg = MRI.createVirtualRegister(RC);
12891 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12892 .addReg(AccPhyReg);
12893
12894 unsigned t1 = MRI.createVirtualRegister(RC);
12895 unsigned Opc = MI->getOpcode();
12896 switch (Opc) {
12897 default:
12898 llvm_unreachable("Unhandled atomic-load-op opcode!");
12899 case X86::ATOMAND8:
12900 case X86::ATOMAND16:
12901 case X86::ATOMAND32:
12902 case X86::ATOMAND64:
12903 case X86::ATOMOR8:
12904 case X86::ATOMOR16:
12905 case X86::ATOMOR32:
12906 case X86::ATOMOR64:
12907 case X86::ATOMXOR8:
12908 case X86::ATOMXOR16:
12909 case X86::ATOMXOR32:
12910 case X86::ATOMXOR64: {
12911 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12912 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12913 .addReg(AccReg);
12914 break;
12915 }
12916 case X86::ATOMNAND8:
12917 case X86::ATOMNAND16:
12918 case X86::ATOMNAND32:
12919 case X86::ATOMNAND64: {
12920 unsigned t2 = MRI.createVirtualRegister(RC);
12921 unsigned NOTOpc;
12922 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12923 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12924 .addReg(AccReg);
12925 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12926 break;
12927 }
Michael Liao08382492012-09-21 03:00:17 +000012928 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012929 case X86::ATOMMAX16:
12930 case X86::ATOMMAX32:
12931 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012932 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012933 case X86::ATOMMIN16:
12934 case X86::ATOMMIN32:
12935 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012936 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012937 case X86::ATOMUMAX16:
12938 case X86::ATOMUMAX32:
12939 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012940 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012941 case X86::ATOMUMIN16:
12942 case X86::ATOMUMIN32:
12943 case X86::ATOMUMIN64: {
12944 unsigned CMPOpc;
12945 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12946
12947 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12948 .addReg(SrcReg)
12949 .addReg(AccReg);
12950
12951 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012952 if (VT != MVT::i8) {
12953 // Native support
12954 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12955 .addReg(SrcReg)
12956 .addReg(AccReg);
12957 } else {
12958 // Promote i8 to i32 to use CMOV32
12959 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12960 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12961 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12962 unsigned t2 = MRI.createVirtualRegister(RC32);
12963
12964 unsigned Undef = MRI.createVirtualRegister(RC32);
12965 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12966
12967 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12968 .addReg(Undef)
12969 .addReg(SrcReg)
12970 .addImm(X86::sub_8bit);
12971 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12972 .addReg(Undef)
12973 .addReg(AccReg)
12974 .addImm(X86::sub_8bit);
12975
12976 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12977 .addReg(SrcReg32)
12978 .addReg(AccReg32);
12979
12980 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12981 .addReg(t2, 0, X86::sub_8bit);
12982 }
Michael Liaob118a072012-09-20 03:06:15 +000012983 } else {
12984 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012985 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012986 "Invalid atomic-load-op transformation!");
12987 unsigned SelOpc = getPseudoCMOVOpc(VT);
12988 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12989 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12990 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12991 .addReg(SrcReg).addReg(AccReg)
12992 .addImm(CC);
12993 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12994 }
12995 break;
12996 }
12997 }
12998
12999 // Copy AccPhyReg back from virtual register.
13000 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
13001 .addReg(AccReg);
13002
13003 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13004 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13005 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13006 MIB.addReg(t1);
13007 MIB.setMemRefs(MMOBegin, MMOEnd);
13008
13009 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13010
13011 mainMBB->addSuccessor(origMainMBB);
13012 mainMBB->addSuccessor(sinkMBB);
13013
13014 // sinkMBB:
13015 sinkMBB->addLiveIn(AccPhyReg);
13016
13017 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13018 TII->get(TargetOpcode::COPY), DstReg)
13019 .addReg(AccPhyReg);
13020
13021 MI->eraseFromParent();
13022 return sinkMBB;
13023}
13024
13025// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13026// instructions. They will be translated into a spin-loop or compare-exchange
13027// loop from
13028//
13029// ...
13030// dst = atomic-fetch-op MI.addr, MI.val
13031// ...
13032//
13033// to
13034//
13035// ...
13036// EAX = LOAD [MI.addr + 0]
13037// EDX = LOAD [MI.addr + 4]
13038// loop:
13039// EBX = OP MI.val.lo, EAX
13040// ECX = OP MI.val.hi, EDX
13041// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13042// JNE loop
13043// sink:
13044// dst = EDX:EAX
13045// ...
13046MachineBasicBlock *
13047X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13048 MachineBasicBlock *MBB) const {
13049 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13050 DebugLoc DL = MI->getDebugLoc();
13051
13052 MachineFunction *MF = MBB->getParent();
13053 MachineRegisterInfo &MRI = MF->getRegInfo();
13054
13055 const BasicBlock *BB = MBB->getBasicBlock();
13056 MachineFunction::iterator I = MBB;
13057 ++I;
13058
13059 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
13060 "Unexpected number of operands");
13061
13062 assert(MI->hasOneMemOperand() &&
13063 "Expected atomic-load-op32 to have one memoperand");
13064
13065 // Memory Reference
13066 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13067 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13068
13069 unsigned DstLoReg, DstHiReg;
13070 unsigned SrcLoReg, SrcHiReg;
13071 unsigned MemOpndSlot;
13072
13073 unsigned CurOp = 0;
13074
13075 DstLoReg = MI->getOperand(CurOp++).getReg();
13076 DstHiReg = MI->getOperand(CurOp++).getReg();
13077 MemOpndSlot = CurOp;
13078 CurOp += X86::AddrNumOperands;
13079 SrcLoReg = MI->getOperand(CurOp++).getReg();
13080 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013081
Craig Topperc9099502012-04-20 06:31:50 +000013082 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013083 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013084
Michael Liaob118a072012-09-20 03:06:15 +000013085 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13086 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013087
Michael Liaob118a072012-09-20 03:06:15 +000013088 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013089 //
Michael Liaob118a072012-09-20 03:06:15 +000013090 // thisMBB:
13091 // EAX = LOAD [MI.addr + 0]
13092 // EDX = LOAD [MI.addr + 4]
13093 // mainMBB:
13094 // EBX = OP MI.vallo, EAX
13095 // ECX = OP MI.valhi, EDX
13096 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13097 // JNE mainMBB
13098 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000013099
Mon P Wang63307c32008-05-05 19:05:59 +000013100 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013101 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13102 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13103 MF->insert(I, mainMBB);
13104 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013105
Michael Liaob118a072012-09-20 03:06:15 +000013106 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013107
Michael Liaob118a072012-09-20 03:06:15 +000013108 // Transfer the remainder of BB and its successor edges to sinkMBB.
13109 sinkMBB->splice(sinkMBB->begin(), MBB,
13110 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13111 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013112
Michael Liaob118a072012-09-20 03:06:15 +000013113 // thisMBB:
13114 // Lo
13115 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
13116 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13117 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13118 MIB.setMemRefs(MMOBegin, MMOEnd);
13119 // Hi
13120 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
13121 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000013122 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000013123 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000013124 else
Michael Liaob118a072012-09-20 03:06:15 +000013125 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13126 }
13127 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000013128
Michael Liaob118a072012-09-20 03:06:15 +000013129 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013130
Michael Liaob118a072012-09-20 03:06:15 +000013131 // mainMBB:
13132 MachineBasicBlock *origMainMBB = mainMBB;
13133 mainMBB->addLiveIn(X86::EAX);
13134 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013135
Michael Liaob118a072012-09-20 03:06:15 +000013136 // Copy EDX:EAX as they are used more than once.
13137 unsigned LoReg = MRI.createVirtualRegister(RC);
13138 unsigned HiReg = MRI.createVirtualRegister(RC);
13139 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
13140 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000013141
Michael Liaob118a072012-09-20 03:06:15 +000013142 unsigned t1L = MRI.createVirtualRegister(RC);
13143 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000013144
Michael Liaob118a072012-09-20 03:06:15 +000013145 unsigned Opc = MI->getOpcode();
13146 switch (Opc) {
13147 default:
13148 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13149 case X86::ATOMAND6432:
13150 case X86::ATOMOR6432:
13151 case X86::ATOMXOR6432:
13152 case X86::ATOMADD6432:
13153 case X86::ATOMSUB6432: {
13154 unsigned HiOpc;
13155 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000013156 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
13157 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013158 break;
13159 }
13160 case X86::ATOMNAND6432: {
13161 unsigned HiOpc, NOTOpc;
13162 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
13163 unsigned t2L = MRI.createVirtualRegister(RC);
13164 unsigned t2H = MRI.createVirtualRegister(RC);
13165 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
13166 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
13167 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
13168 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
13169 break;
13170 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013171 case X86::ATOMMAX6432:
13172 case X86::ATOMMIN6432:
13173 case X86::ATOMUMAX6432:
13174 case X86::ATOMUMIN6432: {
13175 unsigned HiOpc;
13176 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13177 unsigned cL = MRI.createVirtualRegister(RC8);
13178 unsigned cH = MRI.createVirtualRegister(RC8);
13179 unsigned cL32 = MRI.createVirtualRegister(RC);
13180 unsigned cH32 = MRI.createVirtualRegister(RC);
13181 unsigned cc = MRI.createVirtualRegister(RC);
13182 // cl := cmp src_lo, lo
13183 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13184 .addReg(SrcLoReg).addReg(LoReg);
13185 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13186 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13187 // ch := cmp src_hi, hi
13188 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13189 .addReg(SrcHiReg).addReg(HiReg);
13190 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13191 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13192 // cc := if (src_hi == hi) ? cl : ch;
13193 if (Subtarget->hasCMov()) {
13194 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13195 .addReg(cH32).addReg(cL32);
13196 } else {
13197 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13198 .addReg(cH32).addReg(cL32)
13199 .addImm(X86::COND_E);
13200 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13201 }
13202 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13203 if (Subtarget->hasCMov()) {
13204 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
13205 .addReg(SrcLoReg).addReg(LoReg);
13206 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
13207 .addReg(SrcHiReg).addReg(HiReg);
13208 } else {
13209 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
13210 .addReg(SrcLoReg).addReg(LoReg)
13211 .addImm(X86::COND_NE);
13212 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13213 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
13214 .addReg(SrcHiReg).addReg(HiReg)
13215 .addImm(X86::COND_NE);
13216 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13217 }
13218 break;
13219 }
Michael Liaob118a072012-09-20 03:06:15 +000013220 case X86::ATOMSWAP6432: {
13221 unsigned HiOpc;
13222 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13223 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
13224 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
13225 break;
13226 }
13227 }
Mon P Wang63307c32008-05-05 19:05:59 +000013228
Michael Liaob118a072012-09-20 03:06:15 +000013229 // Copy EDX:EAX back from HiReg:LoReg
13230 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
13231 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
13232 // Copy ECX:EBX from t1H:t1L
13233 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
13234 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013235
Michael Liaob118a072012-09-20 03:06:15 +000013236 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13237 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13238 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13239 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013240
Michael Liaob118a072012-09-20 03:06:15 +000013241 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013242
Michael Liaob118a072012-09-20 03:06:15 +000013243 mainMBB->addSuccessor(origMainMBB);
13244 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013245
Michael Liaob118a072012-09-20 03:06:15 +000013246 // sinkMBB:
13247 sinkMBB->addLiveIn(X86::EAX);
13248 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013249
Michael Liaob118a072012-09-20 03:06:15 +000013250 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13251 TII->get(TargetOpcode::COPY), DstLoReg)
13252 .addReg(X86::EAX);
13253 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13254 TII->get(TargetOpcode::COPY), DstHiReg)
13255 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000013256
Michael Liaob118a072012-09-20 03:06:15 +000013257 MI->eraseFromParent();
13258 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013259}
13260
Eric Christopherf83a5de2009-08-27 18:08:16 +000013261// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013262// or XMM0_V32I8 in AVX all of this code can be replaced with that
13263// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013264static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13265 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013266 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013267 switch (MI->getOpcode()) {
13268 default: llvm_unreachable("illegal opcode!");
13269 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13270 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13271 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13272 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13273 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13274 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13275 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13276 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013277 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013278
Craig Topper8aae8dd2012-11-10 08:57:41 +000013279 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013280 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013281
Craig Topper52ea2452012-11-10 09:25:36 +000013282 unsigned NumArgs = MI->getNumOperands();
13283 for (unsigned i = 1; i < NumArgs; ++i) {
13284 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013285 if (!(Op.isReg() && Op.isImplicit()))
13286 MIB.addOperand(Op);
13287 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013288 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013289 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13290
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013291 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013292 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013293 .addReg(X86::XMM0);
13294
Dan Gohman14152b42010-07-06 20:24:04 +000013295 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013296 return BB;
13297}
13298
Craig Topper9c7ae012012-11-10 01:23:36 +000013299// FIXME: Custom handling because TableGen doesn't support multiple implicit
13300// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013301static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13302 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013303 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013304 switch (MI->getOpcode()) {
13305 default: llvm_unreachable("illegal opcode!");
13306 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13307 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13308 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13309 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13310 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13311 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13312 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13313 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013314 }
13315
Craig Topper8aae8dd2012-11-10 08:57:41 +000013316 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013317 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013318
Craig Topper52ea2452012-11-10 09:25:36 +000013319 unsigned NumArgs = MI->getNumOperands(); // remove the results
13320 for (unsigned i = 1; i < NumArgs; ++i) {
13321 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013322 if (!(Op.isReg() && Op.isImplicit()))
13323 MIB.addOperand(Op);
13324 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013325 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013326 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13327
13328 BuildMI(*BB, MI, dl,
13329 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13330 .addReg(X86::ECX);
13331
13332 MI->eraseFromParent();
13333 return BB;
13334}
13335
Craig Topper2da36912012-11-11 22:45:02 +000013336static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13337 const TargetInstrInfo *TII,
13338 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013339 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013340
Eric Christopher228232b2010-11-30 07:20:12 +000013341 // Address into RAX/EAX, other two args into ECX, EDX.
13342 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13343 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13344 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13345 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013346 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013347
Eric Christopher228232b2010-11-30 07:20:12 +000013348 unsigned ValOps = X86::AddrNumOperands;
13349 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13350 .addReg(MI->getOperand(ValOps).getReg());
13351 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13352 .addReg(MI->getOperand(ValOps+1).getReg());
13353
13354 // The instruction doesn't actually take any operands though.
13355 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013356
Eric Christopher228232b2010-11-30 07:20:12 +000013357 MI->eraseFromParent(); // The pseudo is gone now.
13358 return BB;
13359}
13360
13361MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013362X86TargetLowering::EmitVAARG64WithCustomInserter(
13363 MachineInstr *MI,
13364 MachineBasicBlock *MBB) const {
13365 // Emit va_arg instruction on X86-64.
13366
13367 // Operands to this pseudo-instruction:
13368 // 0 ) Output : destination address (reg)
13369 // 1-5) Input : va_list address (addr, i64mem)
13370 // 6 ) ArgSize : Size (in bytes) of vararg type
13371 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13372 // 8 ) Align : Alignment of type
13373 // 9 ) EFLAGS (implicit-def)
13374
13375 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13376 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13377
13378 unsigned DestReg = MI->getOperand(0).getReg();
13379 MachineOperand &Base = MI->getOperand(1);
13380 MachineOperand &Scale = MI->getOperand(2);
13381 MachineOperand &Index = MI->getOperand(3);
13382 MachineOperand &Disp = MI->getOperand(4);
13383 MachineOperand &Segment = MI->getOperand(5);
13384 unsigned ArgSize = MI->getOperand(6).getImm();
13385 unsigned ArgMode = MI->getOperand(7).getImm();
13386 unsigned Align = MI->getOperand(8).getImm();
13387
13388 // Memory Reference
13389 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13390 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13391 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13392
13393 // Machine Information
13394 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13395 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13396 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13397 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13398 DebugLoc DL = MI->getDebugLoc();
13399
13400 // struct va_list {
13401 // i32 gp_offset
13402 // i32 fp_offset
13403 // i64 overflow_area (address)
13404 // i64 reg_save_area (address)
13405 // }
13406 // sizeof(va_list) = 24
13407 // alignment(va_list) = 8
13408
13409 unsigned TotalNumIntRegs = 6;
13410 unsigned TotalNumXMMRegs = 8;
13411 bool UseGPOffset = (ArgMode == 1);
13412 bool UseFPOffset = (ArgMode == 2);
13413 unsigned MaxOffset = TotalNumIntRegs * 8 +
13414 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13415
13416 /* Align ArgSize to a multiple of 8 */
13417 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13418 bool NeedsAlign = (Align > 8);
13419
13420 MachineBasicBlock *thisMBB = MBB;
13421 MachineBasicBlock *overflowMBB;
13422 MachineBasicBlock *offsetMBB;
13423 MachineBasicBlock *endMBB;
13424
13425 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13426 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13427 unsigned OffsetReg = 0;
13428
13429 if (!UseGPOffset && !UseFPOffset) {
13430 // If we only pull from the overflow region, we don't create a branch.
13431 // We don't need to alter control flow.
13432 OffsetDestReg = 0; // unused
13433 OverflowDestReg = DestReg;
13434
13435 offsetMBB = NULL;
13436 overflowMBB = thisMBB;
13437 endMBB = thisMBB;
13438 } else {
13439 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13440 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13441 // If not, pull from overflow_area. (branch to overflowMBB)
13442 //
13443 // thisMBB
13444 // | .
13445 // | .
13446 // offsetMBB overflowMBB
13447 // | .
13448 // | .
13449 // endMBB
13450
13451 // Registers for the PHI in endMBB
13452 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13453 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13454
13455 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13456 MachineFunction *MF = MBB->getParent();
13457 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13458 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13459 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13460
13461 MachineFunction::iterator MBBIter = MBB;
13462 ++MBBIter;
13463
13464 // Insert the new basic blocks
13465 MF->insert(MBBIter, offsetMBB);
13466 MF->insert(MBBIter, overflowMBB);
13467 MF->insert(MBBIter, endMBB);
13468
13469 // Transfer the remainder of MBB and its successor edges to endMBB.
13470 endMBB->splice(endMBB->begin(), thisMBB,
13471 llvm::next(MachineBasicBlock::iterator(MI)),
13472 thisMBB->end());
13473 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13474
13475 // Make offsetMBB and overflowMBB successors of thisMBB
13476 thisMBB->addSuccessor(offsetMBB);
13477 thisMBB->addSuccessor(overflowMBB);
13478
13479 // endMBB is a successor of both offsetMBB and overflowMBB
13480 offsetMBB->addSuccessor(endMBB);
13481 overflowMBB->addSuccessor(endMBB);
13482
13483 // Load the offset value into a register
13484 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13485 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13486 .addOperand(Base)
13487 .addOperand(Scale)
13488 .addOperand(Index)
13489 .addDisp(Disp, UseFPOffset ? 4 : 0)
13490 .addOperand(Segment)
13491 .setMemRefs(MMOBegin, MMOEnd);
13492
13493 // Check if there is enough room left to pull this argument.
13494 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13495 .addReg(OffsetReg)
13496 .addImm(MaxOffset + 8 - ArgSizeA8);
13497
13498 // Branch to "overflowMBB" if offset >= max
13499 // Fall through to "offsetMBB" otherwise
13500 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13501 .addMBB(overflowMBB);
13502 }
13503
13504 // In offsetMBB, emit code to use the reg_save_area.
13505 if (offsetMBB) {
13506 assert(OffsetReg != 0);
13507
13508 // Read the reg_save_area address.
13509 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13510 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13511 .addOperand(Base)
13512 .addOperand(Scale)
13513 .addOperand(Index)
13514 .addDisp(Disp, 16)
13515 .addOperand(Segment)
13516 .setMemRefs(MMOBegin, MMOEnd);
13517
13518 // Zero-extend the offset
13519 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13520 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13521 .addImm(0)
13522 .addReg(OffsetReg)
13523 .addImm(X86::sub_32bit);
13524
13525 // Add the offset to the reg_save_area to get the final address.
13526 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13527 .addReg(OffsetReg64)
13528 .addReg(RegSaveReg);
13529
13530 // Compute the offset for the next argument
13531 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13532 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13533 .addReg(OffsetReg)
13534 .addImm(UseFPOffset ? 16 : 8);
13535
13536 // Store it back into the va_list.
13537 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13538 .addOperand(Base)
13539 .addOperand(Scale)
13540 .addOperand(Index)
13541 .addDisp(Disp, UseFPOffset ? 4 : 0)
13542 .addOperand(Segment)
13543 .addReg(NextOffsetReg)
13544 .setMemRefs(MMOBegin, MMOEnd);
13545
13546 // Jump to endMBB
13547 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13548 .addMBB(endMBB);
13549 }
13550
13551 //
13552 // Emit code to use overflow area
13553 //
13554
13555 // Load the overflow_area address into a register.
13556 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13557 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13558 .addOperand(Base)
13559 .addOperand(Scale)
13560 .addOperand(Index)
13561 .addDisp(Disp, 8)
13562 .addOperand(Segment)
13563 .setMemRefs(MMOBegin, MMOEnd);
13564
13565 // If we need to align it, do so. Otherwise, just copy the address
13566 // to OverflowDestReg.
13567 if (NeedsAlign) {
13568 // Align the overflow address
13569 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13570 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13571
13572 // aligned_addr = (addr + (align-1)) & ~(align-1)
13573 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13574 .addReg(OverflowAddrReg)
13575 .addImm(Align-1);
13576
13577 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13578 .addReg(TmpReg)
13579 .addImm(~(uint64_t)(Align-1));
13580 } else {
13581 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13582 .addReg(OverflowAddrReg);
13583 }
13584
13585 // Compute the next overflow address after this argument.
13586 // (the overflow address should be kept 8-byte aligned)
13587 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13588 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13589 .addReg(OverflowDestReg)
13590 .addImm(ArgSizeA8);
13591
13592 // Store the new overflow address.
13593 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13594 .addOperand(Base)
13595 .addOperand(Scale)
13596 .addOperand(Index)
13597 .addDisp(Disp, 8)
13598 .addOperand(Segment)
13599 .addReg(NextAddrReg)
13600 .setMemRefs(MMOBegin, MMOEnd);
13601
13602 // If we branched, emit the PHI to the front of endMBB.
13603 if (offsetMBB) {
13604 BuildMI(*endMBB, endMBB->begin(), DL,
13605 TII->get(X86::PHI), DestReg)
13606 .addReg(OffsetDestReg).addMBB(offsetMBB)
13607 .addReg(OverflowDestReg).addMBB(overflowMBB);
13608 }
13609
13610 // Erase the pseudo instruction
13611 MI->eraseFromParent();
13612
13613 return endMBB;
13614}
13615
13616MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013617X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13618 MachineInstr *MI,
13619 MachineBasicBlock *MBB) const {
13620 // Emit code to save XMM registers to the stack. The ABI says that the
13621 // number of registers to save is given in %al, so it's theoretically
13622 // possible to do an indirect jump trick to avoid saving all of them,
13623 // however this code takes a simpler approach and just executes all
13624 // of the stores if %al is non-zero. It's less code, and it's probably
13625 // easier on the hardware branch predictor, and stores aren't all that
13626 // expensive anyway.
13627
13628 // Create the new basic blocks. One block contains all the XMM stores,
13629 // and one block is the final destination regardless of whether any
13630 // stores were performed.
13631 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13632 MachineFunction *F = MBB->getParent();
13633 MachineFunction::iterator MBBIter = MBB;
13634 ++MBBIter;
13635 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13636 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13637 F->insert(MBBIter, XMMSaveMBB);
13638 F->insert(MBBIter, EndMBB);
13639
Dan Gohman14152b42010-07-06 20:24:04 +000013640 // Transfer the remainder of MBB and its successor edges to EndMBB.
13641 EndMBB->splice(EndMBB->begin(), MBB,
13642 llvm::next(MachineBasicBlock::iterator(MI)),
13643 MBB->end());
13644 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13645
Dan Gohmand6708ea2009-08-15 01:38:56 +000013646 // The original block will now fall through to the XMM save block.
13647 MBB->addSuccessor(XMMSaveMBB);
13648 // The XMMSaveMBB will fall through to the end block.
13649 XMMSaveMBB->addSuccessor(EndMBB);
13650
13651 // Now add the instructions.
13652 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13653 DebugLoc DL = MI->getDebugLoc();
13654
13655 unsigned CountReg = MI->getOperand(0).getReg();
13656 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13657 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13658
13659 if (!Subtarget->isTargetWin64()) {
13660 // If %al is 0, branch around the XMM save block.
13661 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013662 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013663 MBB->addSuccessor(EndMBB);
13664 }
13665
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013666 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013667 // In the XMM save block, save all the XMM argument registers.
13668 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13669 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013670 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013671 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013672 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013673 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013674 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013675 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013676 .addFrameIndex(RegSaveFrameIndex)
13677 .addImm(/*Scale=*/1)
13678 .addReg(/*IndexReg=*/0)
13679 .addImm(/*Disp=*/Offset)
13680 .addReg(/*Segment=*/0)
13681 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013682 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013683 }
13684
Dan Gohman14152b42010-07-06 20:24:04 +000013685 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013686
13687 return EndMBB;
13688}
Mon P Wang63307c32008-05-05 19:05:59 +000013689
Lang Hames6e3f7e42012-02-03 01:13:49 +000013690// The EFLAGS operand of SelectItr might be missing a kill marker
13691// because there were multiple uses of EFLAGS, and ISel didn't know
13692// which to mark. Figure out whether SelectItr should have had a
13693// kill marker, and set it if it should. Returns the correct kill
13694// marker value.
13695static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13696 MachineBasicBlock* BB,
13697 const TargetRegisterInfo* TRI) {
13698 // Scan forward through BB for a use/def of EFLAGS.
13699 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13700 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013701 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013702 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013703 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013704 if (mi.definesRegister(X86::EFLAGS))
13705 break; // Should have kill-flag - update below.
13706 }
13707
13708 // If we hit the end of the block, check whether EFLAGS is live into a
13709 // successor.
13710 if (miI == BB->end()) {
13711 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13712 sEnd = BB->succ_end();
13713 sItr != sEnd; ++sItr) {
13714 MachineBasicBlock* succ = *sItr;
13715 if (succ->isLiveIn(X86::EFLAGS))
13716 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013717 }
13718 }
13719
Lang Hames6e3f7e42012-02-03 01:13:49 +000013720 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13721 // out. SelectMI should have a kill flag on EFLAGS.
13722 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013723 return true;
13724}
13725
Evan Cheng60c07e12006-07-05 22:17:51 +000013726MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013727X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013728 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013729 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13730 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013731
Chris Lattner52600972009-09-02 05:57:00 +000013732 // To "insert" a SELECT_CC instruction, we actually have to insert the
13733 // diamond control-flow pattern. The incoming instruction knows the
13734 // destination vreg to set, the condition code register to branch on, the
13735 // true/false values to select between, and a branch opcode to use.
13736 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13737 MachineFunction::iterator It = BB;
13738 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013739
Chris Lattner52600972009-09-02 05:57:00 +000013740 // thisMBB:
13741 // ...
13742 // TrueVal = ...
13743 // cmpTY ccX, r1, r2
13744 // bCC copy1MBB
13745 // fallthrough --> copy0MBB
13746 MachineBasicBlock *thisMBB = BB;
13747 MachineFunction *F = BB->getParent();
13748 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13749 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013750 F->insert(It, copy0MBB);
13751 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013752
Bill Wendling730c07e2010-06-25 20:48:10 +000013753 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13754 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013755 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13756 if (!MI->killsRegister(X86::EFLAGS) &&
13757 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13758 copy0MBB->addLiveIn(X86::EFLAGS);
13759 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013760 }
13761
Dan Gohman14152b42010-07-06 20:24:04 +000013762 // Transfer the remainder of BB and its successor edges to sinkMBB.
13763 sinkMBB->splice(sinkMBB->begin(), BB,
13764 llvm::next(MachineBasicBlock::iterator(MI)),
13765 BB->end());
13766 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13767
13768 // Add the true and fallthrough blocks as its successors.
13769 BB->addSuccessor(copy0MBB);
13770 BB->addSuccessor(sinkMBB);
13771
13772 // Create the conditional branch instruction.
13773 unsigned Opc =
13774 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13775 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13776
Chris Lattner52600972009-09-02 05:57:00 +000013777 // copy0MBB:
13778 // %FalseValue = ...
13779 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013780 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013781
Chris Lattner52600972009-09-02 05:57:00 +000013782 // sinkMBB:
13783 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13784 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013785 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13786 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013787 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13788 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13789
Dan Gohman14152b42010-07-06 20:24:04 +000013790 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013791 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013792}
13793
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013794MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013795X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13796 bool Is64Bit) const {
13797 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13798 DebugLoc DL = MI->getDebugLoc();
13799 MachineFunction *MF = BB->getParent();
13800 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13801
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013802 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013803
13804 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13805 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13806
13807 // BB:
13808 // ... [Till the alloca]
13809 // If stacklet is not large enough, jump to mallocMBB
13810 //
13811 // bumpMBB:
13812 // Allocate by subtracting from RSP
13813 // Jump to continueMBB
13814 //
13815 // mallocMBB:
13816 // Allocate by call to runtime
13817 //
13818 // continueMBB:
13819 // ...
13820 // [rest of original BB]
13821 //
13822
13823 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13824 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13825 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13826
13827 MachineRegisterInfo &MRI = MF->getRegInfo();
13828 const TargetRegisterClass *AddrRegClass =
13829 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13830
13831 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13832 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13833 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013834 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013835 sizeVReg = MI->getOperand(1).getReg(),
13836 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13837
13838 MachineFunction::iterator MBBIter = BB;
13839 ++MBBIter;
13840
13841 MF->insert(MBBIter, bumpMBB);
13842 MF->insert(MBBIter, mallocMBB);
13843 MF->insert(MBBIter, continueMBB);
13844
13845 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13846 (MachineBasicBlock::iterator(MI)), BB->end());
13847 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13848
13849 // Add code to the main basic block to check if the stack limit has been hit,
13850 // and if so, jump to mallocMBB otherwise to bumpMBB.
13851 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013852 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013853 .addReg(tmpSPVReg).addReg(sizeVReg);
13854 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013855 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013856 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013857 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13858
13859 // bumpMBB simply decreases the stack pointer, since we know the current
13860 // stacklet has enough space.
13861 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013862 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013863 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013864 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013865 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13866
13867 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013868 const uint32_t *RegMask =
13869 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013870 if (Is64Bit) {
13871 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13872 .addReg(sizeVReg);
13873 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013874 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013875 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013876 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013877 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013878 } else {
13879 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13880 .addImm(12);
13881 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13882 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013883 .addExternalSymbol("__morestack_allocate_stack_space")
13884 .addRegMask(RegMask)
13885 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013886 }
13887
13888 if (!Is64Bit)
13889 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13890 .addImm(16);
13891
13892 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13893 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13894 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13895
13896 // Set up the CFG correctly.
13897 BB->addSuccessor(bumpMBB);
13898 BB->addSuccessor(mallocMBB);
13899 mallocMBB->addSuccessor(continueMBB);
13900 bumpMBB->addSuccessor(continueMBB);
13901
13902 // Take care of the PHI nodes.
13903 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13904 MI->getOperand(0).getReg())
13905 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13906 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13907
13908 // Delete the original pseudo instruction.
13909 MI->eraseFromParent();
13910
13911 // And we're done.
13912 return continueMBB;
13913}
13914
13915MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013916X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013917 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013918 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13919 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013920
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013921 assert(!Subtarget->isTargetEnvMacho());
13922
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013923 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13924 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013925
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013926 if (Subtarget->isTargetWin64()) {
13927 if (Subtarget->isTargetCygMing()) {
13928 // ___chkstk(Mingw64):
13929 // Clobbers R10, R11, RAX and EFLAGS.
13930 // Updates RSP.
13931 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13932 .addExternalSymbol("___chkstk")
13933 .addReg(X86::RAX, RegState::Implicit)
13934 .addReg(X86::RSP, RegState::Implicit)
13935 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13936 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13937 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13938 } else {
13939 // __chkstk(MSVCRT): does not update stack pointer.
13940 // Clobbers R10, R11 and EFLAGS.
13941 // FIXME: RAX(allocated size) might be reused and not killed.
13942 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13943 .addExternalSymbol("__chkstk")
13944 .addReg(X86::RAX, RegState::Implicit)
13945 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13946 // RAX has the offset to subtracted from RSP.
13947 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13948 .addReg(X86::RSP)
13949 .addReg(X86::RAX);
13950 }
13951 } else {
13952 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013953 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13954
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013955 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13956 .addExternalSymbol(StackProbeSymbol)
13957 .addReg(X86::EAX, RegState::Implicit)
13958 .addReg(X86::ESP, RegState::Implicit)
13959 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13960 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13961 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13962 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013963
Dan Gohman14152b42010-07-06 20:24:04 +000013964 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013965 return BB;
13966}
Chris Lattner52600972009-09-02 05:57:00 +000013967
13968MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013969X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13970 MachineBasicBlock *BB) const {
13971 // This is pretty easy. We're taking the value that we received from
13972 // our load from the relocation, sticking it in either RDI (x86-64)
13973 // or EAX and doing an indirect call. The return value will then
13974 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013975 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013976 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013977 DebugLoc DL = MI->getDebugLoc();
13978 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013979
13980 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013981 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013982
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013983 // Get a register mask for the lowered call.
13984 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13985 // proper register mask.
13986 const uint32_t *RegMask =
13987 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013988 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013989 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13990 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013991 .addReg(X86::RIP)
13992 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013993 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013994 MI->getOperand(3).getTargetFlags())
13995 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013996 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013997 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013998 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013999 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014000 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14001 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014002 .addReg(0)
14003 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014004 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014005 MI->getOperand(3).getTargetFlags())
14006 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014007 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014008 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014009 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014010 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014011 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14012 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014013 .addReg(TII->getGlobalBaseReg(F))
14014 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014015 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014016 MI->getOperand(3).getTargetFlags())
14017 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014018 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014019 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014020 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014021 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014022
Dan Gohman14152b42010-07-06 20:24:04 +000014023 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014024 return BB;
14025}
14026
14027MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014028X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14029 MachineBasicBlock *MBB) const {
14030 DebugLoc DL = MI->getDebugLoc();
14031 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14032
14033 MachineFunction *MF = MBB->getParent();
14034 MachineRegisterInfo &MRI = MF->getRegInfo();
14035
14036 const BasicBlock *BB = MBB->getBasicBlock();
14037 MachineFunction::iterator I = MBB;
14038 ++I;
14039
14040 // Memory Reference
14041 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14042 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14043
14044 unsigned DstReg;
14045 unsigned MemOpndSlot = 0;
14046
14047 unsigned CurOp = 0;
14048
14049 DstReg = MI->getOperand(CurOp++).getReg();
14050 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14051 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14052 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14053 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14054
14055 MemOpndSlot = CurOp;
14056
14057 MVT PVT = getPointerTy();
14058 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14059 "Invalid Pointer Size!");
14060
14061 // For v = setjmp(buf), we generate
14062 //
14063 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014064 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014065 // SjLjSetup restoreMBB
14066 //
14067 // mainMBB:
14068 // v_main = 0
14069 //
14070 // sinkMBB:
14071 // v = phi(main, restore)
14072 //
14073 // restoreMBB:
14074 // v_restore = 1
14075
14076 MachineBasicBlock *thisMBB = MBB;
14077 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14078 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14079 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14080 MF->insert(I, mainMBB);
14081 MF->insert(I, sinkMBB);
14082 MF->push_back(restoreMBB);
14083
14084 MachineInstrBuilder MIB;
14085
14086 // Transfer the remainder of BB and its successor edges to sinkMBB.
14087 sinkMBB->splice(sinkMBB->begin(), MBB,
14088 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14089 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14090
14091 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014092 unsigned PtrStoreOpc = 0;
14093 unsigned LabelReg = 0;
14094 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14095 Reloc::Model RM = getTargetMachine().getRelocationModel();
14096 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14097 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014098
Michael Liao281ae5a2012-10-17 02:22:27 +000014099 // Prepare IP either in reg or imm.
14100 if (!UseImmLabel) {
14101 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14102 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14103 LabelReg = MRI.createVirtualRegister(PtrRC);
14104 if (Subtarget->is64Bit()) {
14105 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14106 .addReg(X86::RIP)
14107 .addImm(0)
14108 .addReg(0)
14109 .addMBB(restoreMBB)
14110 .addReg(0);
14111 } else {
14112 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14113 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14114 .addReg(XII->getGlobalBaseReg(MF))
14115 .addImm(0)
14116 .addReg(0)
14117 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14118 .addReg(0);
14119 }
14120 } else
14121 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014122 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014123 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014124 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14125 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014126 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014127 else
14128 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14129 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014130 if (!UseImmLabel)
14131 MIB.addReg(LabelReg);
14132 else
14133 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014134 MIB.setMemRefs(MMOBegin, MMOEnd);
14135 // Setup
14136 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14137 .addMBB(restoreMBB);
14138 MIB.addRegMask(RegInfo->getNoPreservedMask());
14139 thisMBB->addSuccessor(mainMBB);
14140 thisMBB->addSuccessor(restoreMBB);
14141
14142 // mainMBB:
14143 // EAX = 0
14144 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14145 mainMBB->addSuccessor(sinkMBB);
14146
14147 // sinkMBB:
14148 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14149 TII->get(X86::PHI), DstReg)
14150 .addReg(mainDstReg).addMBB(mainMBB)
14151 .addReg(restoreDstReg).addMBB(restoreMBB);
14152
14153 // restoreMBB:
14154 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14155 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14156 restoreMBB->addSuccessor(sinkMBB);
14157
14158 MI->eraseFromParent();
14159 return sinkMBB;
14160}
14161
14162MachineBasicBlock *
14163X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14164 MachineBasicBlock *MBB) const {
14165 DebugLoc DL = MI->getDebugLoc();
14166 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14167
14168 MachineFunction *MF = MBB->getParent();
14169 MachineRegisterInfo &MRI = MF->getRegInfo();
14170
14171 // Memory Reference
14172 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14173 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14174
14175 MVT PVT = getPointerTy();
14176 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14177 "Invalid Pointer Size!");
14178
14179 const TargetRegisterClass *RC =
14180 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14181 unsigned Tmp = MRI.createVirtualRegister(RC);
14182 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14183 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14184 unsigned SP = RegInfo->getStackRegister();
14185
14186 MachineInstrBuilder MIB;
14187
Michael Liao281ae5a2012-10-17 02:22:27 +000014188 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14189 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014190
14191 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14192 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14193
14194 // Reload FP
14195 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14196 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14197 MIB.addOperand(MI->getOperand(i));
14198 MIB.setMemRefs(MMOBegin, MMOEnd);
14199 // Reload IP
14200 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14201 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14202 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014203 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014204 else
14205 MIB.addOperand(MI->getOperand(i));
14206 }
14207 MIB.setMemRefs(MMOBegin, MMOEnd);
14208 // Reload SP
14209 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14210 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14211 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014212 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014213 else
14214 MIB.addOperand(MI->getOperand(i));
14215 }
14216 MIB.setMemRefs(MMOBegin, MMOEnd);
14217 // Jump
14218 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14219
14220 MI->eraseFromParent();
14221 return MBB;
14222}
14223
14224MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014225X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014226 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014227 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014228 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014229 case X86::TAILJMPd64:
14230 case X86::TAILJMPr64:
14231 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014232 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014233 case X86::TCRETURNdi64:
14234 case X86::TCRETURNri64:
14235 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014236 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014237 case X86::WIN_ALLOCA:
14238 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014239 case X86::SEG_ALLOCA_32:
14240 return EmitLoweredSegAlloca(MI, BB, false);
14241 case X86::SEG_ALLOCA_64:
14242 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014243 case X86::TLSCall_32:
14244 case X86::TLSCall_64:
14245 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014246 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014247 case X86::CMOV_FR32:
14248 case X86::CMOV_FR64:
14249 case X86::CMOV_V4F32:
14250 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014251 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014252 case X86::CMOV_V8F32:
14253 case X86::CMOV_V4F64:
14254 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014255 case X86::CMOV_GR16:
14256 case X86::CMOV_GR32:
14257 case X86::CMOV_RFP32:
14258 case X86::CMOV_RFP64:
14259 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014260 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014261
Dale Johannesen849f2142007-07-03 00:53:03 +000014262 case X86::FP32_TO_INT16_IN_MEM:
14263 case X86::FP32_TO_INT32_IN_MEM:
14264 case X86::FP32_TO_INT64_IN_MEM:
14265 case X86::FP64_TO_INT16_IN_MEM:
14266 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014267 case X86::FP64_TO_INT64_IN_MEM:
14268 case X86::FP80_TO_INT16_IN_MEM:
14269 case X86::FP80_TO_INT32_IN_MEM:
14270 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014271 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14272 DebugLoc DL = MI->getDebugLoc();
14273
Evan Cheng60c07e12006-07-05 22:17:51 +000014274 // Change the floating point control register to use "round towards zero"
14275 // mode when truncating to an integer value.
14276 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014277 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014278 addFrameReference(BuildMI(*BB, MI, DL,
14279 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014280
14281 // Load the old value of the high byte of the control word...
14282 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014283 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014284 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014285 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014286
14287 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014288 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014289 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014290
14291 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014292 addFrameReference(BuildMI(*BB, MI, DL,
14293 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014294
14295 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014296 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014297 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014298
14299 // Get the X86 opcode to use.
14300 unsigned Opc;
14301 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014302 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014303 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14304 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14305 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14306 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14307 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14308 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014309 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14310 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14311 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014312 }
14313
14314 X86AddressMode AM;
14315 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014316 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014317 AM.BaseType = X86AddressMode::RegBase;
14318 AM.Base.Reg = Op.getReg();
14319 } else {
14320 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014321 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014322 }
14323 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014324 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014325 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014326 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014327 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014328 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014329 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014330 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014331 AM.GV = Op.getGlobal();
14332 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014333 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014334 }
Dan Gohman14152b42010-07-06 20:24:04 +000014335 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014336 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014337
14338 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014339 addFrameReference(BuildMI(*BB, MI, DL,
14340 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014341
Dan Gohman14152b42010-07-06 20:24:04 +000014342 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014343 return BB;
14344 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014345 // String/text processing lowering.
14346 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014347 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014348 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014349 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014350 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014351 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014352 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014353 case X86::VPCMPESTRM128MEM:
14354 assert(Subtarget->hasSSE42() &&
14355 "Target must have SSE4.2 or AVX features enabled");
14356 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014357
14358 // String/text processing lowering.
14359 case X86::PCMPISTRIREG:
14360 case X86::VPCMPISTRIREG:
14361 case X86::PCMPISTRIMEM:
14362 case X86::VPCMPISTRIMEM:
14363 case X86::PCMPESTRIREG:
14364 case X86::VPCMPESTRIREG:
14365 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014366 case X86::VPCMPESTRIMEM:
14367 assert(Subtarget->hasSSE42() &&
14368 "Target must have SSE4.2 or AVX features enabled");
14369 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014370
Craig Topper8aae8dd2012-11-10 08:57:41 +000014371 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014372 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014373 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014374
Michael Liaobe02a902012-11-08 07:28:54 +000014375 // xbegin
14376 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014377 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014378
Craig Topper8aae8dd2012-11-10 08:57:41 +000014379 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014380 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014381 case X86::ATOMAND16:
14382 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014383 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014384 // Fall through
14385 case X86::ATOMOR8:
14386 case X86::ATOMOR16:
14387 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014388 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014389 // Fall through
14390 case X86::ATOMXOR16:
14391 case X86::ATOMXOR8:
14392 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014393 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014394 // Fall through
14395 case X86::ATOMNAND8:
14396 case X86::ATOMNAND16:
14397 case X86::ATOMNAND32:
14398 case X86::ATOMNAND64:
14399 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014400 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014401 case X86::ATOMMAX16:
14402 case X86::ATOMMAX32:
14403 case X86::ATOMMAX64:
14404 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014405 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014406 case X86::ATOMMIN16:
14407 case X86::ATOMMIN32:
14408 case X86::ATOMMIN64:
14409 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014410 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014411 case X86::ATOMUMAX16:
14412 case X86::ATOMUMAX32:
14413 case X86::ATOMUMAX64:
14414 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014415 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014416 case X86::ATOMUMIN16:
14417 case X86::ATOMUMIN32:
14418 case X86::ATOMUMIN64:
14419 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014420
14421 // This group does 64-bit operations on a 32-bit host.
14422 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014423 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014424 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014425 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014426 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014427 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014428 case X86::ATOMMAX6432:
14429 case X86::ATOMMIN6432:
14430 case X86::ATOMUMAX6432:
14431 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014432 case X86::ATOMSWAP6432:
14433 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014434
Dan Gohmand6708ea2009-08-15 01:38:56 +000014435 case X86::VASTART_SAVE_XMM_REGS:
14436 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014437
14438 case X86::VAARG_64:
14439 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014440
14441 case X86::EH_SjLj_SetJmp32:
14442 case X86::EH_SjLj_SetJmp64:
14443 return emitEHSjLjSetJmp(MI, BB);
14444
14445 case X86::EH_SjLj_LongJmp32:
14446 case X86::EH_SjLj_LongJmp64:
14447 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014448 }
14449}
14450
14451//===----------------------------------------------------------------------===//
14452// X86 Optimization Hooks
14453//===----------------------------------------------------------------------===//
14454
Dan Gohman475871a2008-07-27 21:46:04 +000014455void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014456 APInt &KnownZero,
14457 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014458 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014459 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014460 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014461 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014462 assert((Opc >= ISD::BUILTIN_OP_END ||
14463 Opc == ISD::INTRINSIC_WO_CHAIN ||
14464 Opc == ISD::INTRINSIC_W_CHAIN ||
14465 Opc == ISD::INTRINSIC_VOID) &&
14466 "Should use MaskedValueIsZero if you don't know whether Op"
14467 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014468
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014469 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014470 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014471 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014472 case X86ISD::ADD:
14473 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014474 case X86ISD::ADC:
14475 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014476 case X86ISD::SMUL:
14477 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014478 case X86ISD::INC:
14479 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014480 case X86ISD::OR:
14481 case X86ISD::XOR:
14482 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014483 // These nodes' second result is a boolean.
14484 if (Op.getResNo() == 0)
14485 break;
14486 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014487 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014488 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014489 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014490 case ISD::INTRINSIC_WO_CHAIN: {
14491 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14492 unsigned NumLoBits = 0;
14493 switch (IntId) {
14494 default: break;
14495 case Intrinsic::x86_sse_movmsk_ps:
14496 case Intrinsic::x86_avx_movmsk_ps_256:
14497 case Intrinsic::x86_sse2_movmsk_pd:
14498 case Intrinsic::x86_avx_movmsk_pd_256:
14499 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014500 case Intrinsic::x86_sse2_pmovmskb_128:
14501 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014502 // High bits of movmskp{s|d}, pmovmskb are known zero.
14503 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014504 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014505 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14506 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14507 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14508 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14509 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14510 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014511 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014512 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014513 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014514 break;
14515 }
14516 }
14517 break;
14518 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014519 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014520}
Chris Lattner259e97c2006-01-31 19:43:35 +000014521
Owen Andersonbc146b02010-09-21 20:42:50 +000014522unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14523 unsigned Depth) const {
14524 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14525 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14526 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014527
Owen Andersonbc146b02010-09-21 20:42:50 +000014528 // Fallback case.
14529 return 1;
14530}
14531
Evan Cheng206ee9d2006-07-07 08:33:52 +000014532/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014533/// node is a GlobalAddress + offset.
14534bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014535 const GlobalValue* &GA,
14536 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014537 if (N->getOpcode() == X86ISD::Wrapper) {
14538 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014539 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014540 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014541 return true;
14542 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014543 }
Evan Chengad4196b2008-05-12 19:56:52 +000014544 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014545}
14546
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014547/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14548/// same as extracting the high 128-bit part of 256-bit vector and then
14549/// inserting the result into the low part of a new 256-bit vector
14550static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14551 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014552 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014553
14554 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014555 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014556 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14557 SVOp->getMaskElt(j) >= 0)
14558 return false;
14559
14560 return true;
14561}
14562
14563/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14564/// same as extracting the low 128-bit part of 256-bit vector and then
14565/// inserting the result into the high part of a new 256-bit vector
14566static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14567 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014568 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014569
14570 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014571 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014572 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14573 SVOp->getMaskElt(j) >= 0)
14574 return false;
14575
14576 return true;
14577}
14578
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014579/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14580static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014581 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014582 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014583 DebugLoc dl = N->getDebugLoc();
14584 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14585 SDValue V1 = SVOp->getOperand(0);
14586 SDValue V2 = SVOp->getOperand(1);
14587 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014588 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014589
14590 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14591 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14592 //
14593 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014594 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014595 // V UNDEF BUILD_VECTOR UNDEF
14596 // \ / \ /
14597 // CONCAT_VECTOR CONCAT_VECTOR
14598 // \ /
14599 // \ /
14600 // RESULT: V + zero extended
14601 //
14602 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14603 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14604 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14605 return SDValue();
14606
14607 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14608 return SDValue();
14609
14610 // To match the shuffle mask, the first half of the mask should
14611 // be exactly the first vector, and all the rest a splat with the
14612 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014613 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014614 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14615 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14616 return SDValue();
14617
Chad Rosier3d1161e2012-01-03 21:05:52 +000014618 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14619 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014620 if (Ld->hasNUsesOfValue(1, 0)) {
14621 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14622 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14623 SDValue ResNode =
14624 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14625 Ld->getMemoryVT(),
14626 Ld->getPointerInfo(),
14627 Ld->getAlignment(),
14628 false/*isVolatile*/, true/*ReadMem*/,
14629 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014630
14631 // Make sure the newly-created LOAD is in the same position as Ld in
14632 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14633 // and update uses of Ld's output chain to use the TokenFactor.
14634 if (Ld->hasAnyUseOfValue(1)) {
14635 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14636 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14637 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14638 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14639 SDValue(ResNode.getNode(), 1));
14640 }
14641
Chad Rosier42726832012-05-07 18:47:44 +000014642 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14643 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014644 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014645
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014646 // Emit a zeroed vector and insert the desired subvector on its
14647 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014648 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014649 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014650 return DCI.CombineTo(N, InsV);
14651 }
14652
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014653 //===--------------------------------------------------------------------===//
14654 // Combine some shuffles into subvector extracts and inserts:
14655 //
14656
14657 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14658 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014659 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14660 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014661 return DCI.CombineTo(N, InsV);
14662 }
14663
14664 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14665 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014666 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14667 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014668 return DCI.CombineTo(N, InsV);
14669 }
14670
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014671 return SDValue();
14672}
14673
14674/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014675static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014676 TargetLowering::DAGCombinerInfo &DCI,
14677 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014678 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014679 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014680
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014681 // Don't create instructions with illegal types after legalize types has run.
14682 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14683 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14684 return SDValue();
14685
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014686 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014687 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014688 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014689 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014690
14691 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014692 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014693 return SDValue();
14694
14695 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14696 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14697 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014698 SmallVector<SDValue, 16> Elts;
14699 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014700 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014701
Nate Begemanfdea31a2010-03-24 20:49:50 +000014702 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014703}
Evan Chengd880b972008-05-09 21:53:03 +000014704
Nadav Roteme12bf182013-01-04 17:35:21 +000014705/// PerformTruncateCombine - Converts truncate operation to
14706/// a sequence of vector shuffle operations.
14707/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014708static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14709 TargetLowering::DAGCombinerInfo &DCI,
14710 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014711 return SDValue();
14712}
14713
Craig Topper89f4e662012-03-20 07:17:59 +000014714/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14715/// specific shuffle of a load can be folded into a single element load.
14716/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14717/// shuffles have been customed lowered so we need to handle those here.
14718static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14719 TargetLowering::DAGCombinerInfo &DCI) {
14720 if (DCI.isBeforeLegalizeOps())
14721 return SDValue();
14722
14723 SDValue InVec = N->getOperand(0);
14724 SDValue EltNo = N->getOperand(1);
14725
14726 if (!isa<ConstantSDNode>(EltNo))
14727 return SDValue();
14728
14729 EVT VT = InVec.getValueType();
14730
14731 bool HasShuffleIntoBitcast = false;
14732 if (InVec.getOpcode() == ISD::BITCAST) {
14733 // Don't duplicate a load with other uses.
14734 if (!InVec.hasOneUse())
14735 return SDValue();
14736 EVT BCVT = InVec.getOperand(0).getValueType();
14737 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14738 return SDValue();
14739 InVec = InVec.getOperand(0);
14740 HasShuffleIntoBitcast = true;
14741 }
14742
14743 if (!isTargetShuffle(InVec.getOpcode()))
14744 return SDValue();
14745
14746 // Don't duplicate a load with other uses.
14747 if (!InVec.hasOneUse())
14748 return SDValue();
14749
14750 SmallVector<int, 16> ShuffleMask;
14751 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014752 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14753 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014754 return SDValue();
14755
14756 // Select the input vector, guarding against out of range extract vector.
14757 unsigned NumElems = VT.getVectorNumElements();
14758 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14759 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14760 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14761 : InVec.getOperand(1);
14762
14763 // If inputs to shuffle are the same for both ops, then allow 2 uses
14764 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14765
14766 if (LdNode.getOpcode() == ISD::BITCAST) {
14767 // Don't duplicate a load with other uses.
14768 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14769 return SDValue();
14770
14771 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14772 LdNode = LdNode.getOperand(0);
14773 }
14774
14775 if (!ISD::isNormalLoad(LdNode.getNode()))
14776 return SDValue();
14777
14778 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14779
14780 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14781 return SDValue();
14782
14783 if (HasShuffleIntoBitcast) {
14784 // If there's a bitcast before the shuffle, check if the load type and
14785 // alignment is valid.
14786 unsigned Align = LN0->getAlignment();
14787 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014788 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014789 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14790
14791 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14792 return SDValue();
14793 }
14794
14795 // All checks match so transform back to vector_shuffle so that DAG combiner
14796 // can finish the job
14797 DebugLoc dl = N->getDebugLoc();
14798
14799 // Create shuffle node taking into account the case that its a unary shuffle
14800 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14801 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14802 InVec.getOperand(0), Shuffle,
14803 &ShuffleMask[0]);
14804 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14805 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14806 EltNo);
14807}
14808
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014809/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14810/// generation and convert it from being a bunch of shuffles and extracts
14811/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014812static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014813 TargetLowering::DAGCombinerInfo &DCI) {
14814 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14815 if (NewOp.getNode())
14816 return NewOp;
14817
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014818 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014819 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14820 // from mmx to v2i32 has a single usage.
14821 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14822 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14823 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14824 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14825 N->getValueType(0),
14826 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014827
14828 // Only operate on vectors of 4 elements, where the alternative shuffling
14829 // gets to be more expensive.
14830 if (InputVector.getValueType() != MVT::v4i32)
14831 return SDValue();
14832
14833 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14834 // single use which is a sign-extend or zero-extend, and all elements are
14835 // used.
14836 SmallVector<SDNode *, 4> Uses;
14837 unsigned ExtractedElements = 0;
14838 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14839 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14840 if (UI.getUse().getResNo() != InputVector.getResNo())
14841 return SDValue();
14842
14843 SDNode *Extract = *UI;
14844 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14845 return SDValue();
14846
14847 if (Extract->getValueType(0) != MVT::i32)
14848 return SDValue();
14849 if (!Extract->hasOneUse())
14850 return SDValue();
14851 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14852 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14853 return SDValue();
14854 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14855 return SDValue();
14856
14857 // Record which element was extracted.
14858 ExtractedElements |=
14859 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14860
14861 Uses.push_back(Extract);
14862 }
14863
14864 // If not all the elements were used, this may not be worthwhile.
14865 if (ExtractedElements != 15)
14866 return SDValue();
14867
14868 // Ok, we've now decided to do the transformation.
14869 DebugLoc dl = InputVector.getDebugLoc();
14870
14871 // Store the value to a temporary stack slot.
14872 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014873 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14874 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014875
14876 // Replace each use (extract) with a load of the appropriate element.
14877 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14878 UE = Uses.end(); UI != UE; ++UI) {
14879 SDNode *Extract = *UI;
14880
Nadav Rotem86694292011-05-17 08:31:57 +000014881 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014882 SDValue Idx = Extract->getOperand(1);
14883 unsigned EltSize =
14884 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14885 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014886 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014887 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14888
Nadav Rotem86694292011-05-17 08:31:57 +000014889 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014890 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014891
14892 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014893 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014894 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014895 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014896
14897 // Replace the exact with the load.
14898 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14899 }
14900
14901 // The replacement was made in place; don't return anything.
14902 return SDValue();
14903}
14904
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014905/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14906static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14907 SDValue RHS, SelectionDAG &DAG,
14908 const X86Subtarget *Subtarget) {
14909 if (!VT.isVector())
14910 return 0;
14911
14912 switch (VT.getSimpleVT().SimpleTy) {
14913 default: return 0;
14914 case MVT::v32i8:
14915 case MVT::v16i16:
14916 case MVT::v8i32:
14917 if (!Subtarget->hasAVX2())
14918 return 0;
14919 case MVT::v16i8:
14920 case MVT::v8i16:
14921 case MVT::v4i32:
14922 if (!Subtarget->hasSSE2())
14923 return 0;
14924 }
14925
14926 // SSE2 has only a small subset of the operations.
14927 bool hasUnsigned = Subtarget->hasSSE41() ||
14928 (Subtarget->hasSSE2() && VT == MVT::v16i8);
14929 bool hasSigned = Subtarget->hasSSE41() ||
14930 (Subtarget->hasSSE2() && VT == MVT::v8i16);
14931
14932 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14933
14934 // Check for x CC y ? x : y.
14935 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14936 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14937 switch (CC) {
14938 default: break;
14939 case ISD::SETULT:
14940 case ISD::SETULE:
14941 return hasUnsigned ? X86ISD::UMIN : 0;
14942 case ISD::SETUGT:
14943 case ISD::SETUGE:
14944 return hasUnsigned ? X86ISD::UMAX : 0;
14945 case ISD::SETLT:
14946 case ISD::SETLE:
14947 return hasSigned ? X86ISD::SMIN : 0;
14948 case ISD::SETGT:
14949 case ISD::SETGE:
14950 return hasSigned ? X86ISD::SMAX : 0;
14951 }
14952 // Check for x CC y ? y : x -- a min/max with reversed arms.
14953 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14954 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
14955 switch (CC) {
14956 default: break;
14957 case ISD::SETULT:
14958 case ISD::SETULE:
14959 return hasUnsigned ? X86ISD::UMAX : 0;
14960 case ISD::SETUGT:
14961 case ISD::SETUGE:
14962 return hasUnsigned ? X86ISD::UMIN : 0;
14963 case ISD::SETLT:
14964 case ISD::SETLE:
14965 return hasSigned ? X86ISD::SMAX : 0;
14966 case ISD::SETGT:
14967 case ISD::SETGE:
14968 return hasSigned ? X86ISD::SMIN : 0;
14969 }
14970 }
14971
14972 return 0;
14973}
14974
Duncan Sands6bcd2192011-09-17 16:49:39 +000014975/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14976/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014977static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014978 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014979 const X86Subtarget *Subtarget) {
14980 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014981 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014982 // Get the LHS/RHS of the select.
14983 SDValue LHS = N->getOperand(1);
14984 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014985 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014986
Dan Gohman670e5392009-09-21 18:03:22 +000014987 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014988 // instructions match the semantics of the common C idiom x<y?x:y but not
14989 // x<=y?x:y, because of how they handle negative zero (which can be
14990 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014991 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14992 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014993 (Subtarget->hasSSE2() ||
14994 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014995 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014996
Chris Lattner47b4ce82009-03-11 05:48:52 +000014997 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014998 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014999 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15000 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015001 switch (CC) {
15002 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015003 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015004 // Converting this to a min would handle NaNs incorrectly, and swapping
15005 // the operands would cause it to handle comparisons between positive
15006 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015007 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015008 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015009 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15010 break;
15011 std::swap(LHS, RHS);
15012 }
Dan Gohman670e5392009-09-21 18:03:22 +000015013 Opcode = X86ISD::FMIN;
15014 break;
15015 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015016 // Converting this to a min would handle comparisons between positive
15017 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015018 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015019 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15020 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015021 Opcode = X86ISD::FMIN;
15022 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015023 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015024 // Converting this to a min would handle both negative zeros and NaNs
15025 // incorrectly, but we can swap the operands to fix both.
15026 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015027 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015028 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015029 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015030 Opcode = X86ISD::FMIN;
15031 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015032
Dan Gohman670e5392009-09-21 18:03:22 +000015033 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015034 // Converting this to a max would handle comparisons between positive
15035 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015036 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015037 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015038 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015039 Opcode = X86ISD::FMAX;
15040 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015041 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015042 // Converting this to a max would handle NaNs incorrectly, and swapping
15043 // the operands would cause it to handle comparisons between positive
15044 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015045 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015046 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015047 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15048 break;
15049 std::swap(LHS, RHS);
15050 }
Dan Gohman670e5392009-09-21 18:03:22 +000015051 Opcode = X86ISD::FMAX;
15052 break;
15053 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015054 // Converting this to a max would handle both negative zeros and NaNs
15055 // incorrectly, but we can swap the operands to fix both.
15056 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015057 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015058 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015059 case ISD::SETGE:
15060 Opcode = X86ISD::FMAX;
15061 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015062 }
Dan Gohman670e5392009-09-21 18:03:22 +000015063 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015064 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15065 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015066 switch (CC) {
15067 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015068 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015069 // Converting this to a min would handle comparisons between positive
15070 // and negative zero incorrectly, and swapping the operands would
15071 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015072 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015073 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015074 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015075 break;
15076 std::swap(LHS, RHS);
15077 }
Dan Gohman670e5392009-09-21 18:03:22 +000015078 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015079 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015080 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015081 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015082 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015083 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15084 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015085 Opcode = X86ISD::FMIN;
15086 break;
15087 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015088 // Converting this to a min would handle both negative zeros and NaNs
15089 // incorrectly, but we can swap the operands to fix both.
15090 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015091 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015092 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015093 case ISD::SETGE:
15094 Opcode = X86ISD::FMIN;
15095 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015096
Dan Gohman670e5392009-09-21 18:03:22 +000015097 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015098 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015099 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015100 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015101 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015102 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015103 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015104 // Converting this to a max would handle comparisons between positive
15105 // and negative zero incorrectly, and swapping the operands would
15106 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015107 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015108 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015109 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015110 break;
15111 std::swap(LHS, RHS);
15112 }
Dan Gohman670e5392009-09-21 18:03:22 +000015113 Opcode = X86ISD::FMAX;
15114 break;
15115 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015116 // Converting this to a max would handle both negative zeros and NaNs
15117 // incorrectly, but we can swap the operands to fix both.
15118 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015119 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015120 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015121 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015122 Opcode = X86ISD::FMAX;
15123 break;
15124 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015125 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015126
Chris Lattner47b4ce82009-03-11 05:48:52 +000015127 if (Opcode)
15128 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015129 }
Eric Christopherfd179292009-08-27 18:07:15 +000015130
Chris Lattnerd1980a52009-03-12 06:52:53 +000015131 // If this is a select between two integer constants, try to do some
15132 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015133 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15134 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015135 // Don't do this for crazy integer types.
15136 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15137 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015138 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015139 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015140
Chris Lattnercee56e72009-03-13 05:53:31 +000015141 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015142 // Efficiently invertible.
15143 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15144 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15145 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15146 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015147 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015148 }
Eric Christopherfd179292009-08-27 18:07:15 +000015149
Chris Lattnerd1980a52009-03-12 06:52:53 +000015150 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015151 if (FalseC->getAPIntValue() == 0 &&
15152 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015153 if (NeedsCondInvert) // Invert the condition if needed.
15154 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15155 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015156
Chris Lattnerd1980a52009-03-12 06:52:53 +000015157 // Zero extend the condition if needed.
15158 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015159
Chris Lattnercee56e72009-03-13 05:53:31 +000015160 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015161 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015162 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015163 }
Eric Christopherfd179292009-08-27 18:07:15 +000015164
Chris Lattner97a29a52009-03-13 05:22:11 +000015165 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015166 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015167 if (NeedsCondInvert) // Invert the condition if needed.
15168 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15169 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015170
Chris Lattner97a29a52009-03-13 05:22:11 +000015171 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015172 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15173 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015174 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015175 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015176 }
Eric Christopherfd179292009-08-27 18:07:15 +000015177
Chris Lattnercee56e72009-03-13 05:53:31 +000015178 // Optimize cases that will turn into an LEA instruction. This requires
15179 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015180 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015181 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015182 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015183
Chris Lattnercee56e72009-03-13 05:53:31 +000015184 bool isFastMultiplier = false;
15185 if (Diff < 10) {
15186 switch ((unsigned char)Diff) {
15187 default: break;
15188 case 1: // result = add base, cond
15189 case 2: // result = lea base( , cond*2)
15190 case 3: // result = lea base(cond, cond*2)
15191 case 4: // result = lea base( , cond*4)
15192 case 5: // result = lea base(cond, cond*4)
15193 case 8: // result = lea base( , cond*8)
15194 case 9: // result = lea base(cond, cond*8)
15195 isFastMultiplier = true;
15196 break;
15197 }
15198 }
Eric Christopherfd179292009-08-27 18:07:15 +000015199
Chris Lattnercee56e72009-03-13 05:53:31 +000015200 if (isFastMultiplier) {
15201 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15202 if (NeedsCondInvert) // Invert the condition if needed.
15203 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15204 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015205
Chris Lattnercee56e72009-03-13 05:53:31 +000015206 // Zero extend the condition if needed.
15207 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15208 Cond);
15209 // Scale the condition by the difference.
15210 if (Diff != 1)
15211 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15212 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015213
Chris Lattnercee56e72009-03-13 05:53:31 +000015214 // Add the base if non-zero.
15215 if (FalseC->getAPIntValue() != 0)
15216 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15217 SDValue(FalseC, 0));
15218 return Cond;
15219 }
Eric Christopherfd179292009-08-27 18:07:15 +000015220 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015221 }
15222 }
Eric Christopherfd179292009-08-27 18:07:15 +000015223
Evan Cheng56f582d2012-01-04 01:41:39 +000015224 // Canonicalize max and min:
15225 // (x > y) ? x : y -> (x >= y) ? x : y
15226 // (x < y) ? x : y -> (x <= y) ? x : y
15227 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15228 // the need for an extra compare
15229 // against zero. e.g.
15230 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15231 // subl %esi, %edi
15232 // testl %edi, %edi
15233 // movl $0, %eax
15234 // cmovgl %edi, %eax
15235 // =>
15236 // xorl %eax, %eax
15237 // subl %esi, $edi
15238 // cmovsl %eax, %edi
15239 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15240 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15241 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15242 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15243 switch (CC) {
15244 default: break;
15245 case ISD::SETLT:
15246 case ISD::SETGT: {
15247 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15248 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15249 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15250 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15251 }
15252 }
15253 }
15254
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015255 // Match VSELECTs into subs with unsigned saturation.
15256 if (!DCI.isBeforeLegalize() &&
15257 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15258 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15259 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15260 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15261 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15262
15263 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15264 // left side invert the predicate to simplify logic below.
15265 SDValue Other;
15266 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15267 Other = RHS;
15268 CC = ISD::getSetCCInverse(CC, true);
15269 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15270 Other = LHS;
15271 }
15272
15273 if (Other.getNode() && Other->getNumOperands() == 2 &&
15274 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15275 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15276 SDValue CondRHS = Cond->getOperand(1);
15277
15278 // Look for a general sub with unsigned saturation first.
15279 // x >= y ? x-y : 0 --> subus x, y
15280 // x > y ? x-y : 0 --> subus x, y
15281 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15282 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15283 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15284
15285 // If the RHS is a constant we have to reverse the const canonicalization.
15286 // x > C-1 ? x+-C : 0 --> subus x, C
15287 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15288 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15289 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15290 if (CondRHS.getConstantOperandVal(0) == -A-1) {
15291 SmallVector<SDValue, 32> V(VT.getVectorNumElements(),
15292 DAG.getConstant(-A, VT.getScalarType()));
15293 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
15294 DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
15295 V.data(), V.size()));
15296 }
15297 }
15298
15299 // Another special case: If C was a sign bit, the sub has been
15300 // canonicalized into a xor.
15301 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15302 // it's safe to decanonicalize the xor?
15303 // x s< 0 ? x^C : 0 --> subus x, C
15304 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15305 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15306 isSplatVector(OpRHS.getNode())) {
15307 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15308 if (A.isSignBit())
15309 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15310 }
15311 }
15312 }
15313
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015314 // Try to match a min/max vector operation.
15315 if (!DCI.isBeforeLegalize() &&
15316 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15317 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15318 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15319
Nadav Rotemcc616562012-01-15 19:27:55 +000015320 // If we know that this node is legal then we know that it is going to be
15321 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15322 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15323 // to simplify previous instructions.
15324 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15325 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015326 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015327 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015328
15329 // Don't optimize vector selects that map to mask-registers.
15330 if (BitWidth == 1)
15331 return SDValue();
15332
Nadav Rotemcc616562012-01-15 19:27:55 +000015333 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15334 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15335
15336 APInt KnownZero, KnownOne;
15337 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15338 DCI.isBeforeLegalizeOps());
15339 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15340 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15341 DCI.CommitTargetLoweringOpt(TLO);
15342 }
15343
Dan Gohman475871a2008-07-27 21:46:04 +000015344 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015345}
15346
Michael Liao2a33cec2012-08-10 19:58:13 +000015347// Check whether a boolean test is testing a boolean value generated by
15348// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15349// code.
15350//
15351// Simplify the following patterns:
15352// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15353// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15354// to (Op EFLAGS Cond)
15355//
15356// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15357// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15358// to (Op EFLAGS !Cond)
15359//
15360// where Op could be BRCOND or CMOV.
15361//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015362static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015363 // Quit if not CMP and SUB with its value result used.
15364 if (Cmp.getOpcode() != X86ISD::CMP &&
15365 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15366 return SDValue();
15367
15368 // Quit if not used as a boolean value.
15369 if (CC != X86::COND_E && CC != X86::COND_NE)
15370 return SDValue();
15371
15372 // Check CMP operands. One of them should be 0 or 1 and the other should be
15373 // an SetCC or extended from it.
15374 SDValue Op1 = Cmp.getOperand(0);
15375 SDValue Op2 = Cmp.getOperand(1);
15376
15377 SDValue SetCC;
15378 const ConstantSDNode* C = 0;
15379 bool needOppositeCond = (CC == X86::COND_E);
15380
15381 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15382 SetCC = Op2;
15383 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15384 SetCC = Op1;
15385 else // Quit if all operands are not constants.
15386 return SDValue();
15387
15388 if (C->getZExtValue() == 1)
15389 needOppositeCond = !needOppositeCond;
15390 else if (C->getZExtValue() != 0)
15391 // Quit if the constant is neither 0 or 1.
15392 return SDValue();
15393
15394 // Skip 'zext' node.
15395 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15396 SetCC = SetCC.getOperand(0);
15397
Michael Liao7fdc66b2012-09-10 16:36:16 +000015398 switch (SetCC.getOpcode()) {
15399 case X86ISD::SETCC:
15400 // Set the condition code or opposite one if necessary.
15401 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15402 if (needOppositeCond)
15403 CC = X86::GetOppositeBranchCondition(CC);
15404 return SetCC.getOperand(1);
15405 case X86ISD::CMOV: {
15406 // Check whether false/true value has canonical one, i.e. 0 or 1.
15407 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15408 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15409 // Quit if true value is not a constant.
15410 if (!TVal)
15411 return SDValue();
15412 // Quit if false value is not a constant.
15413 if (!FVal) {
15414 // A special case for rdrand, where 0 is set if false cond is found.
15415 SDValue Op = SetCC.getOperand(0);
15416 if (Op.getOpcode() != X86ISD::RDRAND)
15417 return SDValue();
15418 }
15419 // Quit if false value is not the constant 0 or 1.
15420 bool FValIsFalse = true;
15421 if (FVal && FVal->getZExtValue() != 0) {
15422 if (FVal->getZExtValue() != 1)
15423 return SDValue();
15424 // If FVal is 1, opposite cond is needed.
15425 needOppositeCond = !needOppositeCond;
15426 FValIsFalse = false;
15427 }
15428 // Quit if TVal is not the constant opposite of FVal.
15429 if (FValIsFalse && TVal->getZExtValue() != 1)
15430 return SDValue();
15431 if (!FValIsFalse && TVal->getZExtValue() != 0)
15432 return SDValue();
15433 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15434 if (needOppositeCond)
15435 CC = X86::GetOppositeBranchCondition(CC);
15436 return SetCC.getOperand(3);
15437 }
15438 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015439
Michael Liao7fdc66b2012-09-10 16:36:16 +000015440 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015441}
15442
Chris Lattnerd1980a52009-03-12 06:52:53 +000015443/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15444static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015445 TargetLowering::DAGCombinerInfo &DCI,
15446 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015447 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015448
Chris Lattnerd1980a52009-03-12 06:52:53 +000015449 // If the flag operand isn't dead, don't touch this CMOV.
15450 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15451 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015452
Evan Chengb5a55d92011-05-24 01:48:22 +000015453 SDValue FalseOp = N->getOperand(0);
15454 SDValue TrueOp = N->getOperand(1);
15455 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15456 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015457
Evan Chengb5a55d92011-05-24 01:48:22 +000015458 if (CC == X86::COND_E || CC == X86::COND_NE) {
15459 switch (Cond.getOpcode()) {
15460 default: break;
15461 case X86ISD::BSR:
15462 case X86ISD::BSF:
15463 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15464 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15465 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15466 }
15467 }
15468
Michael Liao2a33cec2012-08-10 19:58:13 +000015469 SDValue Flags;
15470
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015471 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015472 if (Flags.getNode() &&
15473 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015474 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015475 SDValue Ops[] = { FalseOp, TrueOp,
15476 DAG.getConstant(CC, MVT::i8), Flags };
15477 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15478 Ops, array_lengthof(Ops));
15479 }
15480
Chris Lattnerd1980a52009-03-12 06:52:53 +000015481 // If this is a select between two integer constants, try to do some
15482 // optimizations. Note that the operands are ordered the opposite of SELECT
15483 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015484 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15485 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015486 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15487 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015488 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15489 CC = X86::GetOppositeBranchCondition(CC);
15490 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015491 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015492 }
Eric Christopherfd179292009-08-27 18:07:15 +000015493
Chris Lattnerd1980a52009-03-12 06:52:53 +000015494 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015495 // This is efficient for any integer data type (including i8/i16) and
15496 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015497 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015498 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15499 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015500
Chris Lattnerd1980a52009-03-12 06:52:53 +000015501 // Zero extend the condition if needed.
15502 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015503
Chris Lattnerd1980a52009-03-12 06:52:53 +000015504 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15505 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015506 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015507 if (N->getNumValues() == 2) // Dead flag value?
15508 return DCI.CombineTo(N, Cond, SDValue());
15509 return Cond;
15510 }
Eric Christopherfd179292009-08-27 18:07:15 +000015511
Chris Lattnercee56e72009-03-13 05:53:31 +000015512 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15513 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015514 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015515 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15516 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015517
Chris Lattner97a29a52009-03-13 05:22:11 +000015518 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015519 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15520 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015521 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15522 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015523
Chris Lattner97a29a52009-03-13 05:22:11 +000015524 if (N->getNumValues() == 2) // Dead flag value?
15525 return DCI.CombineTo(N, Cond, SDValue());
15526 return Cond;
15527 }
Eric Christopherfd179292009-08-27 18:07:15 +000015528
Chris Lattnercee56e72009-03-13 05:53:31 +000015529 // Optimize cases that will turn into an LEA instruction. This requires
15530 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015531 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015532 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015533 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015534
Chris Lattnercee56e72009-03-13 05:53:31 +000015535 bool isFastMultiplier = false;
15536 if (Diff < 10) {
15537 switch ((unsigned char)Diff) {
15538 default: break;
15539 case 1: // result = add base, cond
15540 case 2: // result = lea base( , cond*2)
15541 case 3: // result = lea base(cond, cond*2)
15542 case 4: // result = lea base( , cond*4)
15543 case 5: // result = lea base(cond, cond*4)
15544 case 8: // result = lea base( , cond*8)
15545 case 9: // result = lea base(cond, cond*8)
15546 isFastMultiplier = true;
15547 break;
15548 }
15549 }
Eric Christopherfd179292009-08-27 18:07:15 +000015550
Chris Lattnercee56e72009-03-13 05:53:31 +000015551 if (isFastMultiplier) {
15552 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015553 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15554 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015555 // Zero extend the condition if needed.
15556 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15557 Cond);
15558 // Scale the condition by the difference.
15559 if (Diff != 1)
15560 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15561 DAG.getConstant(Diff, Cond.getValueType()));
15562
15563 // Add the base if non-zero.
15564 if (FalseC->getAPIntValue() != 0)
15565 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15566 SDValue(FalseC, 0));
15567 if (N->getNumValues() == 2) // Dead flag value?
15568 return DCI.CombineTo(N, Cond, SDValue());
15569 return Cond;
15570 }
Eric Christopherfd179292009-08-27 18:07:15 +000015571 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015572 }
15573 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015574
15575 // Handle these cases:
15576 // (select (x != c), e, c) -> select (x != c), e, x),
15577 // (select (x == c), c, e) -> select (x == c), x, e)
15578 // where the c is an integer constant, and the "select" is the combination
15579 // of CMOV and CMP.
15580 //
15581 // The rationale for this change is that the conditional-move from a constant
15582 // needs two instructions, however, conditional-move from a register needs
15583 // only one instruction.
15584 //
15585 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15586 // some instruction-combining opportunities. This opt needs to be
15587 // postponed as late as possible.
15588 //
15589 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15590 // the DCI.xxxx conditions are provided to postpone the optimization as
15591 // late as possible.
15592
15593 ConstantSDNode *CmpAgainst = 0;
15594 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15595 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15596 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15597
15598 if (CC == X86::COND_NE &&
15599 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15600 CC = X86::GetOppositeBranchCondition(CC);
15601 std::swap(TrueOp, FalseOp);
15602 }
15603
15604 if (CC == X86::COND_E &&
15605 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15606 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15607 DAG.getConstant(CC, MVT::i8), Cond };
15608 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15609 array_lengthof(Ops));
15610 }
15611 }
15612 }
15613
Chris Lattnerd1980a52009-03-12 06:52:53 +000015614 return SDValue();
15615}
15616
Evan Cheng0b0cd912009-03-28 05:57:29 +000015617/// PerformMulCombine - Optimize a single multiply with constant into two
15618/// in order to implement it with two cheaper instructions, e.g.
15619/// LEA + SHL, LEA + LEA.
15620static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15621 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015622 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15623 return SDValue();
15624
Owen Andersone50ed302009-08-10 22:56:29 +000015625 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015626 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015627 return SDValue();
15628
15629 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15630 if (!C)
15631 return SDValue();
15632 uint64_t MulAmt = C->getZExtValue();
15633 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15634 return SDValue();
15635
15636 uint64_t MulAmt1 = 0;
15637 uint64_t MulAmt2 = 0;
15638 if ((MulAmt % 9) == 0) {
15639 MulAmt1 = 9;
15640 MulAmt2 = MulAmt / 9;
15641 } else if ((MulAmt % 5) == 0) {
15642 MulAmt1 = 5;
15643 MulAmt2 = MulAmt / 5;
15644 } else if ((MulAmt % 3) == 0) {
15645 MulAmt1 = 3;
15646 MulAmt2 = MulAmt / 3;
15647 }
15648 if (MulAmt2 &&
15649 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15650 DebugLoc DL = N->getDebugLoc();
15651
15652 if (isPowerOf2_64(MulAmt2) &&
15653 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15654 // If second multiplifer is pow2, issue it first. We want the multiply by
15655 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15656 // is an add.
15657 std::swap(MulAmt1, MulAmt2);
15658
15659 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015660 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015661 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015662 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015663 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015664 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015665 DAG.getConstant(MulAmt1, VT));
15666
Eric Christopherfd179292009-08-27 18:07:15 +000015667 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015668 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015669 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015670 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015671 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015672 DAG.getConstant(MulAmt2, VT));
15673
15674 // Do not add new nodes to DAG combiner worklist.
15675 DCI.CombineTo(N, NewMul, false);
15676 }
15677 return SDValue();
15678}
15679
Evan Chengad9c0a32009-12-15 00:53:42 +000015680static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15681 SDValue N0 = N->getOperand(0);
15682 SDValue N1 = N->getOperand(1);
15683 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15684 EVT VT = N0.getValueType();
15685
15686 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15687 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015688 if (VT.isInteger() && !VT.isVector() &&
15689 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015690 N0.getOperand(1).getOpcode() == ISD::Constant) {
15691 SDValue N00 = N0.getOperand(0);
15692 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15693 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15694 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15695 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15696 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15697 APInt ShAmt = N1C->getAPIntValue();
15698 Mask = Mask.shl(ShAmt);
15699 if (Mask != 0)
15700 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15701 N00, DAG.getConstant(Mask, VT));
15702 }
15703 }
15704
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015705 // Hardware support for vector shifts is sparse which makes us scalarize the
15706 // vector operations in many cases. Also, on sandybridge ADD is faster than
15707 // shl.
15708 // (shl V, 1) -> add V,V
15709 if (isSplatVector(N1.getNode())) {
15710 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15711 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15712 // We shift all of the values by one. In many cases we do not have
15713 // hardware support for this operation. This is better expressed as an ADD
15714 // of two values.
15715 if (N1C && (1 == N1C->getZExtValue())) {
15716 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15717 }
15718 }
15719
Evan Chengad9c0a32009-12-15 00:53:42 +000015720 return SDValue();
15721}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015722
Nate Begeman740ab032009-01-26 00:52:55 +000015723/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15724/// when possible.
15725static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015726 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015727 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015728 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015729 if (N->getOpcode() == ISD::SHL) {
15730 SDValue V = PerformSHLCombine(N, DAG);
15731 if (V.getNode()) return V;
15732 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015733
Nate Begeman740ab032009-01-26 00:52:55 +000015734 // On X86 with SSE2 support, we can transform this to a vector shift if
15735 // all elements are shifted by the same amount. We can't do this in legalize
15736 // because the a constant vector is typically transformed to a constant pool
15737 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015738 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015739 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015740
Craig Topper7be5dfd2011-11-12 09:58:49 +000015741 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015742 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015743 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015744 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015745
Mon P Wang3becd092009-01-28 08:12:05 +000015746 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015747 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015748 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015749 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015750 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15751 unsigned NumElts = VT.getVectorNumElements();
15752 unsigned i = 0;
15753 for (; i != NumElts; ++i) {
15754 SDValue Arg = ShAmtOp.getOperand(i);
15755 if (Arg.getOpcode() == ISD::UNDEF) continue;
15756 BaseShAmt = Arg;
15757 break;
15758 }
Craig Topper37c26772012-01-17 04:44:50 +000015759 // Handle the case where the build_vector is all undef
15760 // FIXME: Should DAG allow this?
15761 if (i == NumElts)
15762 return SDValue();
15763
Mon P Wang3becd092009-01-28 08:12:05 +000015764 for (; i != NumElts; ++i) {
15765 SDValue Arg = ShAmtOp.getOperand(i);
15766 if (Arg.getOpcode() == ISD::UNDEF) continue;
15767 if (Arg != BaseShAmt) {
15768 return SDValue();
15769 }
15770 }
15771 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015772 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015773 SDValue InVec = ShAmtOp.getOperand(0);
15774 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15775 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15776 unsigned i = 0;
15777 for (; i != NumElts; ++i) {
15778 SDValue Arg = InVec.getOperand(i);
15779 if (Arg.getOpcode() == ISD::UNDEF) continue;
15780 BaseShAmt = Arg;
15781 break;
15782 }
15783 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15784 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015785 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015786 if (C->getZExtValue() == SplatIdx)
15787 BaseShAmt = InVec.getOperand(1);
15788 }
15789 }
Mon P Wang845b1892012-02-01 22:15:20 +000015790 if (BaseShAmt.getNode() == 0) {
15791 // Don't create instructions with illegal types after legalize
15792 // types has run.
15793 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15794 !DCI.isBeforeLegalize())
15795 return SDValue();
15796
Mon P Wangefa42202009-09-03 19:56:25 +000015797 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15798 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015799 }
Mon P Wang3becd092009-01-28 08:12:05 +000015800 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015801 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015802
Mon P Wangefa42202009-09-03 19:56:25 +000015803 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015804 if (EltVT.bitsGT(MVT::i32))
15805 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15806 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015807 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015808
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015809 // The shift amount is identical so we can do a vector shift.
15810 SDValue ValOp = N->getOperand(0);
15811 switch (N->getOpcode()) {
15812 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015813 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015814 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015815 switch (VT.getSimpleVT().SimpleTy) {
15816 default: return SDValue();
15817 case MVT::v2i64:
15818 case MVT::v4i32:
15819 case MVT::v8i16:
15820 case MVT::v4i64:
15821 case MVT::v8i32:
15822 case MVT::v16i16:
15823 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15824 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015825 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015826 switch (VT.getSimpleVT().SimpleTy) {
15827 default: return SDValue();
15828 case MVT::v4i32:
15829 case MVT::v8i16:
15830 case MVT::v8i32:
15831 case MVT::v16i16:
15832 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15833 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015834 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015835 switch (VT.getSimpleVT().SimpleTy) {
15836 default: return SDValue();
15837 case MVT::v2i64:
15838 case MVT::v4i32:
15839 case MVT::v8i16:
15840 case MVT::v4i64:
15841 case MVT::v8i32:
15842 case MVT::v16i16:
15843 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15844 }
Nate Begeman740ab032009-01-26 00:52:55 +000015845 }
Nate Begeman740ab032009-01-26 00:52:55 +000015846}
15847
Stuart Hastings865f0932011-06-03 23:53:54 +000015848// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15849// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15850// and friends. Likewise for OR -> CMPNEQSS.
15851static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15852 TargetLowering::DAGCombinerInfo &DCI,
15853 const X86Subtarget *Subtarget) {
15854 unsigned opcode;
15855
15856 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15857 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015858 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015859 SDValue N0 = N->getOperand(0);
15860 SDValue N1 = N->getOperand(1);
15861 SDValue CMP0 = N0->getOperand(1);
15862 SDValue CMP1 = N1->getOperand(1);
15863 DebugLoc DL = N->getDebugLoc();
15864
15865 // The SETCCs should both refer to the same CMP.
15866 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15867 return SDValue();
15868
15869 SDValue CMP00 = CMP0->getOperand(0);
15870 SDValue CMP01 = CMP0->getOperand(1);
15871 EVT VT = CMP00.getValueType();
15872
15873 if (VT == MVT::f32 || VT == MVT::f64) {
15874 bool ExpectingFlags = false;
15875 // Check for any users that want flags:
15876 for (SDNode::use_iterator UI = N->use_begin(),
15877 UE = N->use_end();
15878 !ExpectingFlags && UI != UE; ++UI)
15879 switch (UI->getOpcode()) {
15880 default:
15881 case ISD::BR_CC:
15882 case ISD::BRCOND:
15883 case ISD::SELECT:
15884 ExpectingFlags = true;
15885 break;
15886 case ISD::CopyToReg:
15887 case ISD::SIGN_EXTEND:
15888 case ISD::ZERO_EXTEND:
15889 case ISD::ANY_EXTEND:
15890 break;
15891 }
15892
15893 if (!ExpectingFlags) {
15894 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15895 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15896
15897 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15898 X86::CondCode tmp = cc0;
15899 cc0 = cc1;
15900 cc1 = tmp;
15901 }
15902
15903 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15904 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15905 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15906 X86ISD::NodeType NTOperator = is64BitFP ?
15907 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15908 // FIXME: need symbolic constants for these magic numbers.
15909 // See X86ATTInstPrinter.cpp:printSSECC().
15910 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15911 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15912 DAG.getConstant(x86cc, MVT::i8));
15913 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15914 OnesOrZeroesF);
15915 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15916 DAG.getConstant(1, MVT::i32));
15917 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15918 return OneBitOfTruth;
15919 }
15920 }
15921 }
15922 }
15923 return SDValue();
15924}
15925
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015926/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15927/// so it can be folded inside ANDNP.
15928static bool CanFoldXORWithAllOnes(const SDNode *N) {
15929 EVT VT = N->getValueType(0);
15930
15931 // Match direct AllOnes for 128 and 256-bit vectors
15932 if (ISD::isBuildVectorAllOnes(N))
15933 return true;
15934
15935 // Look through a bit convert.
15936 if (N->getOpcode() == ISD::BITCAST)
15937 N = N->getOperand(0).getNode();
15938
15939 // Sometimes the operand may come from a insert_subvector building a 256-bit
15940 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015941 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015942 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15943 SDValue V1 = N->getOperand(0);
15944 SDValue V2 = N->getOperand(1);
15945
15946 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15947 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15948 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15949 ISD::isBuildVectorAllOnes(V2.getNode()))
15950 return true;
15951 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015952
15953 return false;
15954}
15955
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015956// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
15957// register. In most cases we actually compare or select YMM-sized registers
15958// and mixing the two types creates horrible code. This method optimizes
15959// some of the transition sequences.
15960static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
15961 TargetLowering::DAGCombinerInfo &DCI,
15962 const X86Subtarget *Subtarget) {
15963 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000015964 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015965 return SDValue();
15966
15967 assert((N->getOpcode() == ISD::ANY_EXTEND ||
15968 N->getOpcode() == ISD::ZERO_EXTEND ||
15969 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
15970
15971 SDValue Narrow = N->getOperand(0);
15972 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000015973 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015974 return SDValue();
15975
15976 if (Narrow->getOpcode() != ISD::XOR &&
15977 Narrow->getOpcode() != ISD::AND &&
15978 Narrow->getOpcode() != ISD::OR)
15979 return SDValue();
15980
15981 SDValue N0 = Narrow->getOperand(0);
15982 SDValue N1 = Narrow->getOperand(1);
15983 DebugLoc DL = Narrow->getDebugLoc();
15984
15985 // The Left side has to be a trunc.
15986 if (N0.getOpcode() != ISD::TRUNCATE)
15987 return SDValue();
15988
15989 // The type of the truncated inputs.
15990 EVT WideVT = N0->getOperand(0)->getValueType(0);
15991 if (WideVT != VT)
15992 return SDValue();
15993
15994 // The right side has to be a 'trunc' or a constant vector.
15995 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
15996 bool RHSConst = (isSplatVector(N1.getNode()) &&
15997 isa<ConstantSDNode>(N1->getOperand(0)));
15998 if (!RHSTrunc && !RHSConst)
15999 return SDValue();
16000
16001 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16002
16003 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16004 return SDValue();
16005
16006 // Set N0 and N1 to hold the inputs to the new wide operation.
16007 N0 = N0->getOperand(0);
16008 if (RHSConst) {
16009 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16010 N1->getOperand(0));
16011 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16012 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16013 } else if (RHSTrunc) {
16014 N1 = N1->getOperand(0);
16015 }
16016
16017 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016018 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016019 unsigned Opcode = N->getOpcode();
16020 switch (Opcode) {
16021 case ISD::ANY_EXTEND:
16022 return Op;
16023 case ISD::ZERO_EXTEND: {
16024 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16025 APInt Mask = APInt::getAllOnesValue(InBits);
16026 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16027 return DAG.getNode(ISD::AND, DL, VT,
16028 Op, DAG.getConstant(Mask, VT));
16029 }
16030 case ISD::SIGN_EXTEND:
16031 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16032 Op, DAG.getValueType(NarrowVT));
16033 default:
16034 llvm_unreachable("Unexpected opcode");
16035 }
16036}
16037
Nate Begemanb65c1752010-12-17 22:55:37 +000016038static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16039 TargetLowering::DAGCombinerInfo &DCI,
16040 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016041 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016042 if (DCI.isBeforeLegalizeOps())
16043 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016044
Stuart Hastings865f0932011-06-03 23:53:54 +000016045 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16046 if (R.getNode())
16047 return R;
16048
Craig Topperb926afc2012-12-17 05:12:30 +000016049 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016050 // BLSI is X & (-X)
16051 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016052 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16053 SDValue N0 = N->getOperand(0);
16054 SDValue N1 = N->getOperand(1);
16055 DebugLoc DL = N->getDebugLoc();
16056
Craig Topperb4c94572011-10-21 06:55:01 +000016057 // Check LHS for neg
16058 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16059 isZero(N0.getOperand(0)))
16060 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16061
16062 // Check RHS for neg
16063 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16064 isZero(N1.getOperand(0)))
16065 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16066
16067 // Check LHS for X-1
16068 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16069 isAllOnes(N0.getOperand(1)))
16070 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16071
16072 // Check RHS for X-1
16073 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16074 isAllOnes(N1.getOperand(1)))
16075 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16076
Craig Topper54a11172011-10-14 07:06:56 +000016077 return SDValue();
16078 }
16079
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016080 // Want to form ANDNP nodes:
16081 // 1) In the hopes of then easily combining them with OR and AND nodes
16082 // to form PBLEND/PSIGN.
16083 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016084 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016085 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016086
Nate Begemanb65c1752010-12-17 22:55:37 +000016087 SDValue N0 = N->getOperand(0);
16088 SDValue N1 = N->getOperand(1);
16089 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016090
Nate Begemanb65c1752010-12-17 22:55:37 +000016091 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016092 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016093 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16094 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016095 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016096
16097 // Check RHS for vnot
16098 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016099 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16100 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016101 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016102
Nate Begemanb65c1752010-12-17 22:55:37 +000016103 return SDValue();
16104}
16105
Evan Cheng760d1942010-01-04 21:22:48 +000016106static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016107 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016108 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016109 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016110 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016111 return SDValue();
16112
Stuart Hastings865f0932011-06-03 23:53:54 +000016113 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16114 if (R.getNode())
16115 return R;
16116
Evan Cheng760d1942010-01-04 21:22:48 +000016117 SDValue N0 = N->getOperand(0);
16118 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016119
Nate Begemanb65c1752010-12-17 22:55:37 +000016120 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016121 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016122 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016123 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016124 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016125
Craig Topper1666cb62011-11-19 07:07:26 +000016126 // Canonicalize pandn to RHS
16127 if (N0.getOpcode() == X86ISD::ANDNP)
16128 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016129 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016130 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16131 SDValue Mask = N1.getOperand(0);
16132 SDValue X = N1.getOperand(1);
16133 SDValue Y;
16134 if (N0.getOperand(0) == Mask)
16135 Y = N0.getOperand(1);
16136 if (N0.getOperand(1) == Mask)
16137 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016138
Craig Topper1666cb62011-11-19 07:07:26 +000016139 // Check to see if the mask appeared in both the AND and ANDNP and
16140 if (!Y.getNode())
16141 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016142
Craig Topper1666cb62011-11-19 07:07:26 +000016143 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016144 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016145 if (Mask.getOpcode() == ISD::BITCAST)
16146 Mask = Mask.getOperand(0);
16147 if (X.getOpcode() == ISD::BITCAST)
16148 X = X.getOperand(0);
16149 if (Y.getOpcode() == ISD::BITCAST)
16150 Y = Y.getOperand(0);
16151
Craig Topper1666cb62011-11-19 07:07:26 +000016152 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016153
Craig Toppered2e13d2012-01-22 19:15:14 +000016154 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016155 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16156 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016157 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016158 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016159
16160 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016161 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016162 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16163 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16164 if ((SraAmt + 1) != EltBits)
16165 return SDValue();
16166
16167 DebugLoc DL = N->getDebugLoc();
16168
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016169 // We are going to replace the AND, OR, NAND with either BLEND
16170 // or PSIGN, which only look at the MSB. The VSRAI instruction
16171 // does not affect the highest bit, so we can get rid of it.
16172 Mask = Mask.getOperand(0);
16173
Craig Topper1666cb62011-11-19 07:07:26 +000016174 // Now we know we at least have a plendvb with the mask val. See if
16175 // we can form a psignb/w/d.
16176 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016177 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16178 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016179 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16180 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16181 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016182 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000016183 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016184 }
16185 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016186 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016187 return SDValue();
16188
16189 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16190
16191 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16192 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16193 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016194 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016195 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016196 }
16197 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016198
Craig Topper1666cb62011-11-19 07:07:26 +000016199 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16200 return SDValue();
16201
Nate Begemanb65c1752010-12-17 22:55:37 +000016202 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016203 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16204 std::swap(N0, N1);
16205 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16206 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016207 if (!N0.hasOneUse() || !N1.hasOneUse())
16208 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016209
16210 SDValue ShAmt0 = N0.getOperand(1);
16211 if (ShAmt0.getValueType() != MVT::i8)
16212 return SDValue();
16213 SDValue ShAmt1 = N1.getOperand(1);
16214 if (ShAmt1.getValueType() != MVT::i8)
16215 return SDValue();
16216 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16217 ShAmt0 = ShAmt0.getOperand(0);
16218 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16219 ShAmt1 = ShAmt1.getOperand(0);
16220
16221 DebugLoc DL = N->getDebugLoc();
16222 unsigned Opc = X86ISD::SHLD;
16223 SDValue Op0 = N0.getOperand(0);
16224 SDValue Op1 = N1.getOperand(0);
16225 if (ShAmt0.getOpcode() == ISD::SUB) {
16226 Opc = X86ISD::SHRD;
16227 std::swap(Op0, Op1);
16228 std::swap(ShAmt0, ShAmt1);
16229 }
16230
Evan Cheng8b1190a2010-04-28 01:18:01 +000016231 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016232 if (ShAmt1.getOpcode() == ISD::SUB) {
16233 SDValue Sum = ShAmt1.getOperand(0);
16234 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016235 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16236 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16237 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16238 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016239 return DAG.getNode(Opc, DL, VT,
16240 Op0, Op1,
16241 DAG.getNode(ISD::TRUNCATE, DL,
16242 MVT::i8, ShAmt0));
16243 }
16244 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16245 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16246 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016247 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016248 return DAG.getNode(Opc, DL, VT,
16249 N0.getOperand(0), N1.getOperand(0),
16250 DAG.getNode(ISD::TRUNCATE, DL,
16251 MVT::i8, ShAmt0));
16252 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016253
Evan Cheng760d1942010-01-04 21:22:48 +000016254 return SDValue();
16255}
16256
Manman Ren92363622012-06-07 22:39:10 +000016257// Generate NEG and CMOV for integer abs.
16258static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16259 EVT VT = N->getValueType(0);
16260
16261 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16262 // 8-bit integer abs to NEG and CMOV.
16263 if (VT.isInteger() && VT.getSizeInBits() == 8)
16264 return SDValue();
16265
16266 SDValue N0 = N->getOperand(0);
16267 SDValue N1 = N->getOperand(1);
16268 DebugLoc DL = N->getDebugLoc();
16269
16270 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16271 // and change it to SUB and CMOV.
16272 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16273 N0.getOpcode() == ISD::ADD &&
16274 N0.getOperand(1) == N1 &&
16275 N1.getOpcode() == ISD::SRA &&
16276 N1.getOperand(0) == N0.getOperand(0))
16277 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16278 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16279 // Generate SUB & CMOV.
16280 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16281 DAG.getConstant(0, VT), N0.getOperand(0));
16282
16283 SDValue Ops[] = { N0.getOperand(0), Neg,
16284 DAG.getConstant(X86::COND_GE, MVT::i8),
16285 SDValue(Neg.getNode(), 1) };
16286 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16287 Ops, array_lengthof(Ops));
16288 }
16289 return SDValue();
16290}
16291
Craig Topper3738ccd2011-12-27 06:27:23 +000016292// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016293static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16294 TargetLowering::DAGCombinerInfo &DCI,
16295 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016296 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016297 if (DCI.isBeforeLegalizeOps())
16298 return SDValue();
16299
Manman Ren45d53b82012-06-08 18:58:26 +000016300 if (Subtarget->hasCMov()) {
16301 SDValue RV = performIntegerAbsCombine(N, DAG);
16302 if (RV.getNode())
16303 return RV;
16304 }
Manman Ren92363622012-06-07 22:39:10 +000016305
16306 // Try forming BMI if it is available.
16307 if (!Subtarget->hasBMI())
16308 return SDValue();
16309
Craig Topperb4c94572011-10-21 06:55:01 +000016310 if (VT != MVT::i32 && VT != MVT::i64)
16311 return SDValue();
16312
Craig Topper3738ccd2011-12-27 06:27:23 +000016313 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16314
Craig Topperb4c94572011-10-21 06:55:01 +000016315 // Create BLSMSK instructions by finding X ^ (X-1)
16316 SDValue N0 = N->getOperand(0);
16317 SDValue N1 = N->getOperand(1);
16318 DebugLoc DL = N->getDebugLoc();
16319
16320 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16321 isAllOnes(N0.getOperand(1)))
16322 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16323
16324 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16325 isAllOnes(N1.getOperand(1)))
16326 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16327
16328 return SDValue();
16329}
16330
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016331/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16332static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016333 TargetLowering::DAGCombinerInfo &DCI,
16334 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016335 LoadSDNode *Ld = cast<LoadSDNode>(N);
16336 EVT RegVT = Ld->getValueType(0);
16337 EVT MemVT = Ld->getMemoryVT();
16338 DebugLoc dl = Ld->getDebugLoc();
16339 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016340 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016341
16342 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016343 unsigned Alignment = Ld->getAlignment();
Nadav Rotemba958652013-01-19 08:38:41 +000016344 bool IsAligned = Alignment == 0 || Alignment == MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016345
16346 // On Sandybridge unaligned 256bit loads are inefficient.
16347 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016348 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016349 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016350 if (NumElems < 2)
16351 return SDValue();
16352
Nadav Rotem48177ac2013-01-18 23:10:30 +000016353 SDValue Ptr = Ld->getBasePtr();
16354 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16355
16356 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16357 NumElems/2);
16358 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16359 Ld->getPointerInfo(), Ld->isVolatile(),
16360 Ld->isNonTemporal(), Ld->isInvariant(),
16361 Alignment);
16362 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16363 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16364 Ld->getPointerInfo(), Ld->isVolatile(),
16365 Ld->isNonTemporal(), Ld->isInvariant(),
Nadav Rotemba958652013-01-19 08:38:41 +000016366 std::max(Alignment/2U, 1U));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016367 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16368 Load1.getValue(1),
16369 Load2.getValue(1));
16370
16371 SDValue NewVec = DAG.getUNDEF(RegVT);
16372 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16373 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16374 return DCI.CombineTo(N, NewVec, TF, true);
16375 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016376
Nadav Rotemca6f2962011-09-18 19:00:23 +000016377 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016378 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16379 // expansion is still better than scalar code.
16380 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16381 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016382 // TODO: It is possible to support ZExt by zeroing the undef values
16383 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016384 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16385 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016386 assert(MemVT != RegVT && "Cannot extend to the same type");
16387 assert(MemVT.isVector() && "Must load a vector from memory");
16388
16389 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016390 unsigned MemSz = MemVT.getSizeInBits();
16391 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016392
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016393 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16394 return SDValue();
16395
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016396 // All sizes must be a power of two.
16397 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16398 return SDValue();
16399
16400 // Attempt to load the original value using scalar loads.
16401 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016402 MVT SclrLoadTy = MVT::i8;
16403 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16404 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16405 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016406 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016407 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016408 }
16409 }
16410
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016411 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16412 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16413 (64 <= MemSz))
16414 SclrLoadTy = MVT::f64;
16415
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016416 // Calculate the number of scalar loads that we need to perform
16417 // in order to load our vector from memory.
16418 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016419 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16420 return SDValue();
16421
16422 unsigned loadRegZize = RegSz;
16423 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16424 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016425
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016426 // Represent our vector as a sequence of elements which are the
16427 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016428 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016429 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016430
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016431 // Represent the data using the same element type that is stored in
16432 // memory. In practice, we ''widen'' MemVT.
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016433 EVT WideVecVT =
16434 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16435 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016436
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016437 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16438 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016439
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016440 // We can't shuffle using an illegal type.
16441 if (!TLI.isTypeLegal(WideVecVT))
16442 return SDValue();
16443
16444 SmallVector<SDValue, 8> Chains;
16445 SDValue Ptr = Ld->getBasePtr();
16446 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16447 TLI.getPointerTy());
16448 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16449
16450 for (unsigned i = 0; i < NumLoads; ++i) {
16451 // Perform a single load.
16452 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16453 Ptr, Ld->getPointerInfo(),
16454 Ld->isVolatile(), Ld->isNonTemporal(),
16455 Ld->isInvariant(), Ld->getAlignment());
16456 Chains.push_back(ScalarLoad.getValue(1));
16457 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16458 // another round of DAGCombining.
16459 if (i == 0)
16460 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16461 else
16462 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16463 ScalarLoad, DAG.getIntPtrConstant(i));
16464
16465 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16466 }
16467
16468 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16469 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016470
16471 // Bitcast the loaded value to a vector of the original element type, in
16472 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016473 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016474 unsigned SizeRatio = RegSz/MemSz;
16475
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016476 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016477 // If we have SSE4.1 we can directly emit a VSEXT node.
16478 if (Subtarget->hasSSE41()) {
16479 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16480 return DCI.CombineTo(N, Sext, TF, true);
16481 }
16482
16483 // Otherwise we'll shuffle the small elements in the high bits of the
16484 // larger type and perform an arithmetic shift. If the shift is not legal
16485 // it's better to scalarize.
16486 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16487 return SDValue();
16488
16489 // Redistribute the loaded elements into the different locations.
16490 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16491 for (unsigned i = 0; i != NumElems; ++i)
16492 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16493
16494 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16495 DAG.getUNDEF(WideVecVT),
16496 &ShuffleVec[0]);
16497
16498 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16499
16500 // Build the arithmetic shift.
16501 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16502 MemVT.getVectorElementType().getSizeInBits();
16503 SmallVector<SDValue, 8> C(NumElems,
16504 DAG.getConstant(Amt, RegVT.getScalarType()));
16505 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, RegVT, &C[0], C.size());
16506 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff, BV);
16507
16508 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016509 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016510
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016511 // Redistribute the loaded elements into the different locations.
16512 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016513 for (unsigned i = 0; i != NumElems; ++i)
16514 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016515
16516 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016517 DAG.getUNDEF(WideVecVT),
16518 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016519
16520 // Bitcast to the requested type.
16521 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16522 // Replace the original load with the new sequence
16523 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016524 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016525 }
16526
16527 return SDValue();
16528}
16529
Chris Lattner149a4e52008-02-22 02:09:43 +000016530/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016531static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016532 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016533 StoreSDNode *St = cast<StoreSDNode>(N);
16534 EVT VT = St->getValue().getValueType();
16535 EVT StVT = St->getMemoryVT();
16536 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016537 SDValue StoredVal = St->getOperand(1);
16538 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotemba958652013-01-19 08:38:41 +000016539 unsigned Alignment = St->getAlignment();
16540 bool IsAligned = Alignment == 0 || Alignment == VT.getSizeInBits()/8;
Nadav Rotem5e742a32011-08-11 16:41:21 +000016541
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016542 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016543 // On Sandy Bridge, 256-bit memory operations are executed by two
16544 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16545 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016546 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016547 StVT == VT && !IsAligned) {
16548 unsigned NumElems = VT.getVectorNumElements();
16549 if (NumElems < 2)
16550 return SDValue();
16551
16552 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16553 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016554
16555 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16556 SDValue Ptr0 = St->getBasePtr();
16557 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16558
16559 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16560 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016561 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016562 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16563 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016564 St->isNonTemporal(),
16565 std::max(Alignment/2U, 1U));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016566 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16567 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016568
16569 // Optimize trunc store (of multiple scalars) to shuffle and store.
16570 // First, pack all of the elements in one place. Next, store to memory
16571 // in fewer chunks.
16572 if (St->isTruncatingStore() && VT.isVector()) {
16573 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16574 unsigned NumElems = VT.getVectorNumElements();
16575 assert(StVT != VT && "Cannot truncate to the same type");
16576 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16577 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16578
16579 // From, To sizes and ElemCount must be pow of two
16580 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016581 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016582 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016583 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016584
Nadav Rotem614061b2011-08-10 19:30:14 +000016585 unsigned SizeRatio = FromSz / ToSz;
16586
16587 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16588
16589 // Create a type on which we perform the shuffle
16590 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16591 StVT.getScalarType(), NumElems*SizeRatio);
16592
16593 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16594
16595 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16596 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016597 for (unsigned i = 0; i != NumElems; ++i)
16598 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016599
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016600 // Can't shuffle using an illegal type.
16601 if (!TLI.isTypeLegal(WideVecVT))
16602 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016603
16604 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016605 DAG.getUNDEF(WideVecVT),
16606 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016607 // At this point all of the data is stored at the bottom of the
16608 // register. We now need to save it to mem.
16609
16610 // Find the largest store unit
16611 MVT StoreType = MVT::i8;
16612 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16613 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16614 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016615 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016616 StoreType = Tp;
16617 }
16618
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016619 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16620 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16621 (64 <= NumElems * ToSz))
16622 StoreType = MVT::f64;
16623
Nadav Rotem614061b2011-08-10 19:30:14 +000016624 // Bitcast the original vector into a vector of store-size units
16625 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016626 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016627 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16628 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16629 SmallVector<SDValue, 8> Chains;
16630 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16631 TLI.getPointerTy());
16632 SDValue Ptr = St->getBasePtr();
16633
16634 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016635 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016636 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16637 StoreType, ShuffWide,
16638 DAG.getIntPtrConstant(i));
16639 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16640 St->getPointerInfo(), St->isVolatile(),
16641 St->isNonTemporal(), St->getAlignment());
16642 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16643 Chains.push_back(Ch);
16644 }
16645
16646 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16647 Chains.size());
16648 }
16649
Chris Lattner149a4e52008-02-22 02:09:43 +000016650 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16651 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016652 // A preferable solution to the general problem is to figure out the right
16653 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016654
16655 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016656 if (VT.getSizeInBits() != 64)
16657 return SDValue();
16658
Devang Patel578efa92009-06-05 21:57:13 +000016659 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016660 bool NoImplicitFloatOps = F->getAttributes().
16661 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016662 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016663 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016664 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016665 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016666 isa<LoadSDNode>(St->getValue()) &&
16667 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16668 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016669 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016670 LoadSDNode *Ld = 0;
16671 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016672 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016673 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016674 // Must be a store of a load. We currently handle two cases: the load
16675 // is a direct child, and it's under an intervening TokenFactor. It is
16676 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016677 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016678 Ld = cast<LoadSDNode>(St->getChain());
16679 else if (St->getValue().hasOneUse() &&
16680 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016681 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016682 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016683 TokenFactorIndex = i;
16684 Ld = cast<LoadSDNode>(St->getValue());
16685 } else
16686 Ops.push_back(ChainVal->getOperand(i));
16687 }
16688 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016689
Evan Cheng536e6672009-03-12 05:59:15 +000016690 if (!Ld || !ISD::isNormalLoad(Ld))
16691 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016692
Evan Cheng536e6672009-03-12 05:59:15 +000016693 // If this is not the MMX case, i.e. we are just turning i64 load/store
16694 // into f64 load/store, avoid the transformation if there are multiple
16695 // uses of the loaded value.
16696 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16697 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016698
Evan Cheng536e6672009-03-12 05:59:15 +000016699 DebugLoc LdDL = Ld->getDebugLoc();
16700 DebugLoc StDL = N->getDebugLoc();
16701 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16702 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16703 // pair instead.
16704 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016705 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016706 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16707 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016708 Ld->isNonTemporal(), Ld->isInvariant(),
16709 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016710 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016711 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016712 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016713 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016714 Ops.size());
16715 }
Evan Cheng536e6672009-03-12 05:59:15 +000016716 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016717 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016718 St->isVolatile(), St->isNonTemporal(),
16719 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016720 }
Evan Cheng536e6672009-03-12 05:59:15 +000016721
16722 // Otherwise, lower to two pairs of 32-bit loads / stores.
16723 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016724 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16725 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016726
Owen Anderson825b72b2009-08-11 20:47:22 +000016727 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016728 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016729 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016730 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016731 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016732 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016733 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016734 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016735 MinAlign(Ld->getAlignment(), 4));
16736
16737 SDValue NewChain = LoLd.getValue(1);
16738 if (TokenFactorIndex != -1) {
16739 Ops.push_back(LoLd);
16740 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016741 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016742 Ops.size());
16743 }
16744
16745 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016746 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16747 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016748
16749 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016750 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016751 St->isVolatile(), St->isNonTemporal(),
16752 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016753 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016754 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016755 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016756 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016757 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016758 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016759 }
Dan Gohman475871a2008-07-27 21:46:04 +000016760 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016761}
16762
Duncan Sands17470be2011-09-22 20:15:48 +000016763/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16764/// and return the operands for the horizontal operation in LHS and RHS. A
16765/// horizontal operation performs the binary operation on successive elements
16766/// of its first operand, then on successive elements of its second operand,
16767/// returning the resulting values in a vector. For example, if
16768/// A = < float a0, float a1, float a2, float a3 >
16769/// and
16770/// B = < float b0, float b1, float b2, float b3 >
16771/// then the result of doing a horizontal operation on A and B is
16772/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16773/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16774/// A horizontal-op B, for some already available A and B, and if so then LHS is
16775/// set to A, RHS to B, and the routine returns 'true'.
16776/// Note that the binary operation should have the property that if one of the
16777/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016778static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016779 // Look for the following pattern: if
16780 // A = < float a0, float a1, float a2, float a3 >
16781 // B = < float b0, float b1, float b2, float b3 >
16782 // and
16783 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16784 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16785 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16786 // which is A horizontal-op B.
16787
16788 // At least one of the operands should be a vector shuffle.
16789 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16790 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16791 return false;
16792
16793 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016794
16795 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16796 "Unsupported vector type for horizontal add/sub");
16797
16798 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16799 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016800 unsigned NumElts = VT.getVectorNumElements();
16801 unsigned NumLanes = VT.getSizeInBits()/128;
16802 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016803 assert((NumLaneElts % 2 == 0) &&
16804 "Vector type should have an even number of elements in each lane");
16805 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016806
16807 // View LHS in the form
16808 // LHS = VECTOR_SHUFFLE A, B, LMask
16809 // If LHS is not a shuffle then pretend it is the shuffle
16810 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16811 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16812 // type VT.
16813 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016814 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016815 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16816 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16817 A = LHS.getOperand(0);
16818 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16819 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016820 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16821 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016822 } else {
16823 if (LHS.getOpcode() != ISD::UNDEF)
16824 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016825 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016826 LMask[i] = i;
16827 }
16828
16829 // Likewise, view RHS in the form
16830 // RHS = VECTOR_SHUFFLE C, D, RMask
16831 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016832 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016833 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16834 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16835 C = RHS.getOperand(0);
16836 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16837 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016838 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16839 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016840 } else {
16841 if (RHS.getOpcode() != ISD::UNDEF)
16842 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016843 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016844 RMask[i] = i;
16845 }
16846
16847 // Check that the shuffles are both shuffling the same vectors.
16848 if (!(A == C && B == D) && !(A == D && B == C))
16849 return false;
16850
16851 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16852 if (!A.getNode() && !B.getNode())
16853 return false;
16854
16855 // If A and B occur in reverse order in RHS, then "swap" them (which means
16856 // rewriting the mask).
16857 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016858 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016859
16860 // At this point LHS and RHS are equivalent to
16861 // LHS = VECTOR_SHUFFLE A, B, LMask
16862 // RHS = VECTOR_SHUFFLE A, B, RMask
16863 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016864 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016865 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016866
Craig Topperf8363302011-12-02 08:18:41 +000016867 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016868 if (LIdx < 0 || RIdx < 0 ||
16869 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16870 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016871 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016872
Craig Topperf8363302011-12-02 08:18:41 +000016873 // Check that successive elements are being operated on. If not, this is
16874 // not a horizontal operation.
16875 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16876 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016877 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016878 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016879 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016880 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016881 }
16882
16883 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16884 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16885 return true;
16886}
16887
16888/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16889static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16890 const X86Subtarget *Subtarget) {
16891 EVT VT = N->getValueType(0);
16892 SDValue LHS = N->getOperand(0);
16893 SDValue RHS = N->getOperand(1);
16894
16895 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016896 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016897 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016898 isHorizontalBinOp(LHS, RHS, true))
16899 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16900 return SDValue();
16901}
16902
16903/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16904static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16905 const X86Subtarget *Subtarget) {
16906 EVT VT = N->getValueType(0);
16907 SDValue LHS = N->getOperand(0);
16908 SDValue RHS = N->getOperand(1);
16909
16910 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016911 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016912 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016913 isHorizontalBinOp(LHS, RHS, false))
16914 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16915 return SDValue();
16916}
16917
Chris Lattner6cf73262008-01-25 06:14:17 +000016918/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16919/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016920static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016921 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16922 // F[X]OR(0.0, x) -> x
16923 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016924 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16925 if (C->getValueAPF().isPosZero())
16926 return N->getOperand(1);
16927 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16928 if (C->getValueAPF().isPosZero())
16929 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016930 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016931}
16932
Nadav Rotemd60cb112012-08-19 13:06:16 +000016933/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16934/// X86ISD::FMAX nodes.
16935static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16936 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16937
16938 // Only perform optimizations if UnsafeMath is used.
16939 if (!DAG.getTarget().Options.UnsafeFPMath)
16940 return SDValue();
16941
16942 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016943 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016944 unsigned NewOp = 0;
16945 switch (N->getOpcode()) {
16946 default: llvm_unreachable("unknown opcode");
16947 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16948 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16949 }
16950
16951 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16952 N->getOperand(0), N->getOperand(1));
16953}
16954
Chris Lattneraf723b92008-01-25 05:46:26 +000016955/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016956static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016957 // FAND(0.0, x) -> 0.0
16958 // FAND(x, 0.0) -> 0.0
16959 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16960 if (C->getValueAPF().isPosZero())
16961 return N->getOperand(0);
16962 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16963 if (C->getValueAPF().isPosZero())
16964 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016965 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016966}
16967
Dan Gohmane5af2d32009-01-29 01:59:02 +000016968static SDValue PerformBTCombine(SDNode *N,
16969 SelectionDAG &DAG,
16970 TargetLowering::DAGCombinerInfo &DCI) {
16971 // BT ignores high bits in the bit index operand.
16972 SDValue Op1 = N->getOperand(1);
16973 if (Op1.hasOneUse()) {
16974 unsigned BitWidth = Op1.getValueSizeInBits();
16975 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16976 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016977 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16978 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016979 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016980 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16981 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16982 DCI.CommitTargetLoweringOpt(TLO);
16983 }
16984 return SDValue();
16985}
Chris Lattner83e6c992006-10-04 06:57:07 +000016986
Eli Friedman7a5e5552009-06-07 06:52:44 +000016987static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16988 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016989 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016990 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016991 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016992 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016993 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016994 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016995 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016996 }
16997 return SDValue();
16998}
16999
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017000static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17001 TargetLowering::DAGCombinerInfo &DCI,
17002 const X86Subtarget *Subtarget) {
Elena Demikhovsky6c327f92013-01-17 09:59:53 +000017003 EVT VT = N->getValueType(0);
Craig Topper0a388612013-01-18 06:50:59 +000017004
Elena Demikhovsky6c327f92013-01-17 09:59:53 +000017005 if (!VT.isVector())
17006 return SDValue();
17007
17008 SDValue In = N->getOperand(0);
17009 EVT InVT = In.getValueType();
17010 DebugLoc dl = N->getDebugLoc();
Craig Topper0a388612013-01-18 06:50:59 +000017011 unsigned ExtendedEltSize = VT.getVectorElementType().getSizeInBits();
Elena Demikhovsky6c327f92013-01-17 09:59:53 +000017012
17013 // Split SIGN_EXTEND operation to use vmovsx instruction when possible
17014 if (InVT == MVT::v8i8) {
Craig Topper0a388612013-01-18 06:50:59 +000017015 if (ExtendedEltSize > 16 && !Subtarget->hasInt256())
Elena Demikhovsky6c327f92013-01-17 09:59:53 +000017016 In = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, In);
Craig Topper0a388612013-01-18 06:50:59 +000017017 if (ExtendedEltSize > 32)
Elena Demikhovsky6c327f92013-01-17 09:59:53 +000017018 In = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i32, In);
17019 return DAG.getNode(ISD::SIGN_EXTEND, dl, VT, In);
17020 }
17021
17022 if ((InVT == MVT::v4i8 || InVT == MVT::v4i16) &&
Craig Topper0a388612013-01-18 06:50:59 +000017023 ExtendedEltSize > 32 && !Subtarget->hasInt256()) {
Elena Demikhovsky6c327f92013-01-17 09:59:53 +000017024 In = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, In);
17025 return DAG.getNode(ISD::SIGN_EXTEND, dl, VT, In);
17026 }
Craig Topper0a388612013-01-18 06:50:59 +000017027
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017028 if (!DCI.isBeforeLegalizeOps())
17029 return SDValue();
17030
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017031 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017032 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017033
Elena Demikhovsky6c327f92013-01-17 09:59:53 +000017034 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017035 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17036 if (R.getNode())
17037 return R;
17038 }
17039
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017040 return SDValue();
17041}
17042
Michael Liaof6c24ee2012-08-10 14:39:24 +000017043static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017044 const X86Subtarget* Subtarget) {
17045 DebugLoc dl = N->getDebugLoc();
17046 EVT VT = N->getValueType(0);
17047
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017048 // Let legalize expand this if it isn't a legal type yet.
17049 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17050 return SDValue();
17051
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017052 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017053 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17054 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017055 return SDValue();
17056
17057 SDValue A = N->getOperand(0);
17058 SDValue B = N->getOperand(1);
17059 SDValue C = N->getOperand(2);
17060
17061 bool NegA = (A.getOpcode() == ISD::FNEG);
17062 bool NegB = (B.getOpcode() == ISD::FNEG);
17063 bool NegC = (C.getOpcode() == ISD::FNEG);
17064
Michael Liaof6c24ee2012-08-10 14:39:24 +000017065 // Negative multiplication when NegA xor NegB
17066 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017067 if (NegA)
17068 A = A.getOperand(0);
17069 if (NegB)
17070 B = B.getOperand(0);
17071 if (NegC)
17072 C = C.getOperand(0);
17073
17074 unsigned Opcode;
17075 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017076 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017077 else
Craig Topperbf404372012-08-31 15:40:30 +000017078 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17079
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017080 return DAG.getNode(Opcode, dl, VT, A, B, C);
17081}
17082
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017083static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017084 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017085 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017086 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17087 // (and (i32 x86isd::setcc_carry), 1)
17088 // This eliminates the zext. This transformation is necessary because
17089 // ISD::SETCC is always legalized to i8.
17090 DebugLoc dl = N->getDebugLoc();
17091 SDValue N0 = N->getOperand(0);
17092 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017093
Evan Cheng2e489c42009-12-16 00:53:11 +000017094 if (N0.getOpcode() == ISD::AND &&
17095 N0.hasOneUse() &&
17096 N0.getOperand(0).hasOneUse()) {
17097 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017098 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17099 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17100 if (!C || C->getZExtValue() != 1)
17101 return SDValue();
17102 return DAG.getNode(ISD::AND, dl, VT,
17103 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17104 N00.getOperand(0), N00.getOperand(1)),
17105 DAG.getConstant(1, VT));
17106 }
17107 }
17108
Craig Topper5a529e42013-01-18 06:44:29 +000017109 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017110 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17111 if (R.getNode())
17112 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017113 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017114
Evan Cheng2e489c42009-12-16 00:53:11 +000017115 return SDValue();
17116}
17117
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017118// Optimize x == -y --> x+y == 0
17119// x != -y --> x+y != 0
17120static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17121 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17122 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017123 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017124
17125 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17126 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17127 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17128 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17129 LHS.getValueType(), RHS, LHS.getOperand(1));
17130 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17131 addV, DAG.getConstant(0, addV.getValueType()), CC);
17132 }
17133 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17134 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17135 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17136 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17137 RHS.getValueType(), LHS, RHS.getOperand(1));
17138 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17139 addV, DAG.getConstant(0, addV.getValueType()), CC);
17140 }
17141 return SDValue();
17142}
17143
Shuxin Yanga5526a92012-10-31 23:11:48 +000017144// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17145// as "sbb reg,reg", since it can be extended without zext and produces
17146// an all-ones bit which is more useful than 0/1 in some cases.
17147static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17148 return DAG.getNode(ISD::AND, DL, MVT::i8,
17149 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17150 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17151 DAG.getConstant(1, MVT::i8));
17152}
17153
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017154// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017155static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17156 TargetLowering::DAGCombinerInfo &DCI,
17157 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017158 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017159 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17160 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017161
Shuxin Yanga5526a92012-10-31 23:11:48 +000017162 if (CC == X86::COND_A) {
17163 // Try to convert COND_A into COND_B in an attempt to facilitate
17164 // materializing "setb reg".
17165 //
17166 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17167 // cannot take an immediate as its first operand.
17168 //
17169 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
17170 EFLAGS.getValueType().isInteger() &&
17171 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17172 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17173 EFLAGS.getNode()->getVTList(),
17174 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17175 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17176 return MaterializeSETB(DL, NewEFLAGS, DAG);
17177 }
17178 }
17179
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017180 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17181 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17182 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017183 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017184 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017185
Michael Liao2a33cec2012-08-10 19:58:13 +000017186 SDValue Flags;
17187
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017188 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17189 if (Flags.getNode()) {
17190 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17191 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17192 }
17193
Michael Liao2a33cec2012-08-10 19:58:13 +000017194 return SDValue();
17195}
17196
17197// Optimize branch condition evaluation.
17198//
17199static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17200 TargetLowering::DAGCombinerInfo &DCI,
17201 const X86Subtarget *Subtarget) {
17202 DebugLoc DL = N->getDebugLoc();
17203 SDValue Chain = N->getOperand(0);
17204 SDValue Dest = N->getOperand(1);
17205 SDValue EFLAGS = N->getOperand(3);
17206 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17207
17208 SDValue Flags;
17209
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017210 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17211 if (Flags.getNode()) {
17212 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17213 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17214 Flags);
17215 }
17216
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017217 return SDValue();
17218}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017219
Benjamin Kramer1396c402011-06-18 11:09:41 +000017220static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17221 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017222 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017223 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017224
17225 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017226 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017227 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017228 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017229 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17230 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17231 }
17232
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017233 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17234 // a 32-bit target where SSE doesn't support i64->FP operations.
17235 if (Op0.getOpcode() == ISD::LOAD) {
17236 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17237 EVT VT = Ld->getValueType(0);
17238 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17239 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17240 !XTLI->getSubtarget()->is64Bit() &&
17241 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017242 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17243 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017244 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17245 return FILDChain;
17246 }
17247 }
17248 return SDValue();
17249}
17250
Chris Lattner23a01992010-12-20 01:37:09 +000017251// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17252static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17253 X86TargetLowering::DAGCombinerInfo &DCI) {
17254 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17255 // the result is either zero or one (depending on the input carry bit).
17256 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17257 if (X86::isZeroNode(N->getOperand(0)) &&
17258 X86::isZeroNode(N->getOperand(1)) &&
17259 // We don't have a good way to replace an EFLAGS use, so only do this when
17260 // dead right now.
17261 SDValue(N, 1).use_empty()) {
17262 DebugLoc DL = N->getDebugLoc();
17263 EVT VT = N->getValueType(0);
17264 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17265 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17266 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17267 DAG.getConstant(X86::COND_B,MVT::i8),
17268 N->getOperand(2)),
17269 DAG.getConstant(1, VT));
17270 return DCI.CombineTo(N, Res1, CarryOut);
17271 }
17272
17273 return SDValue();
17274}
17275
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017276// fold (add Y, (sete X, 0)) -> adc 0, Y
17277// (add Y, (setne X, 0)) -> sbb -1, Y
17278// (sub (sete X, 0), Y) -> sbb 0, Y
17279// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017280static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017281 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017282
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017283 // Look through ZExts.
17284 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17285 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17286 return SDValue();
17287
17288 SDValue SetCC = Ext.getOperand(0);
17289 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17290 return SDValue();
17291
17292 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17293 if (CC != X86::COND_E && CC != X86::COND_NE)
17294 return SDValue();
17295
17296 SDValue Cmp = SetCC.getOperand(1);
17297 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017298 !X86::isZeroNode(Cmp.getOperand(1)) ||
17299 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017300 return SDValue();
17301
17302 SDValue CmpOp0 = Cmp.getOperand(0);
17303 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17304 DAG.getConstant(1, CmpOp0.getValueType()));
17305
17306 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17307 if (CC == X86::COND_NE)
17308 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17309 DL, OtherVal.getValueType(), OtherVal,
17310 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17311 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17312 DL, OtherVal.getValueType(), OtherVal,
17313 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17314}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017315
Craig Topper54f952a2011-11-19 09:02:40 +000017316/// PerformADDCombine - Do target-specific dag combines on integer adds.
17317static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17318 const X86Subtarget *Subtarget) {
17319 EVT VT = N->getValueType(0);
17320 SDValue Op0 = N->getOperand(0);
17321 SDValue Op1 = N->getOperand(1);
17322
17323 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017324 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017325 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017326 isHorizontalBinOp(Op0, Op1, true))
17327 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17328
17329 return OptimizeConditionalInDecrement(N, DAG);
17330}
17331
17332static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17333 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017334 SDValue Op0 = N->getOperand(0);
17335 SDValue Op1 = N->getOperand(1);
17336
17337 // X86 can't encode an immediate LHS of a sub. See if we can push the
17338 // negation into a preceding instruction.
17339 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017340 // If the RHS of the sub is a XOR with one use and a constant, invert the
17341 // immediate. Then add one to the LHS of the sub so we can turn
17342 // X-Y -> X+~Y+1, saving one register.
17343 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17344 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017345 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017346 EVT VT = Op0.getValueType();
17347 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17348 Op1.getOperand(0),
17349 DAG.getConstant(~XorC, VT));
17350 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017351 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017352 }
17353 }
17354
Craig Topper54f952a2011-11-19 09:02:40 +000017355 // Try to synthesize horizontal adds from adds of shuffles.
17356 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017357 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017358 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017359 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017360 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17361
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017362 return OptimizeConditionalInDecrement(N, DAG);
17363}
17364
Michael Liaod9d09602012-10-23 17:34:00 +000017365/// performVZEXTCombine - Performs build vector combines
17366static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17367 TargetLowering::DAGCombinerInfo &DCI,
17368 const X86Subtarget *Subtarget) {
17369 // (vzext (bitcast (vzext (x)) -> (vzext x)
17370 SDValue In = N->getOperand(0);
17371 while (In.getOpcode() == ISD::BITCAST)
17372 In = In.getOperand(0);
17373
17374 if (In.getOpcode() != X86ISD::VZEXT)
17375 return SDValue();
17376
17377 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17378}
17379
Dan Gohman475871a2008-07-27 21:46:04 +000017380SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017381 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017382 SelectionDAG &DAG = DCI.DAG;
17383 switch (N->getOpcode()) {
17384 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017385 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017386 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017387 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017388 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017389 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017390 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17391 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017392 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017393 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017394 case ISD::SHL:
17395 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017396 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017397 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017398 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017399 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017400 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017401 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017402 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017403 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17404 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017405 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017406 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017407 case X86ISD::FMIN:
17408 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017409 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017410 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017411 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017412 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017413 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017414 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017415 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017416 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017417 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017418 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017419 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017420 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000017421 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000017422 case X86ISD::UNPCKH:
17423 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017424 case X86ISD::MOVHLPS:
17425 case X86ISD::MOVLHPS:
17426 case X86ISD::PSHUFD:
17427 case X86ISD::PSHUFHW:
17428 case X86ISD::PSHUFLW:
17429 case X86ISD::MOVSS:
17430 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017431 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017432 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017433 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017434 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017435 }
17436
Dan Gohman475871a2008-07-27 21:46:04 +000017437 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017438}
17439
Evan Chenge5b51ac2010-04-17 06:13:15 +000017440/// isTypeDesirableForOp - Return true if the target has native support for
17441/// the specified value type and it is 'desirable' to use the type for the
17442/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17443/// instruction encodings are longer and some i16 instructions are slow.
17444bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17445 if (!isTypeLegal(VT))
17446 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017447 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017448 return true;
17449
17450 switch (Opc) {
17451 default:
17452 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017453 case ISD::LOAD:
17454 case ISD::SIGN_EXTEND:
17455 case ISD::ZERO_EXTEND:
17456 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017457 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017458 case ISD::SRL:
17459 case ISD::SUB:
17460 case ISD::ADD:
17461 case ISD::MUL:
17462 case ISD::AND:
17463 case ISD::OR:
17464 case ISD::XOR:
17465 return false;
17466 }
17467}
17468
17469/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017470/// beneficial for dag combiner to promote the specified node. If true, it
17471/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017472bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017473 EVT VT = Op.getValueType();
17474 if (VT != MVT::i16)
17475 return false;
17476
Evan Cheng4c26e932010-04-19 19:29:22 +000017477 bool Promote = false;
17478 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017479 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017480 default: break;
17481 case ISD::LOAD: {
17482 LoadSDNode *LD = cast<LoadSDNode>(Op);
17483 // If the non-extending load has a single use and it's not live out, then it
17484 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017485 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17486 Op.hasOneUse()*/) {
17487 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17488 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17489 // The only case where we'd want to promote LOAD (rather then it being
17490 // promoted as an operand is when it's only use is liveout.
17491 if (UI->getOpcode() != ISD::CopyToReg)
17492 return false;
17493 }
17494 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017495 Promote = true;
17496 break;
17497 }
17498 case ISD::SIGN_EXTEND:
17499 case ISD::ZERO_EXTEND:
17500 case ISD::ANY_EXTEND:
17501 Promote = true;
17502 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017503 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017504 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017505 SDValue N0 = Op.getOperand(0);
17506 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017507 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017508 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017509 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017510 break;
17511 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017512 case ISD::ADD:
17513 case ISD::MUL:
17514 case ISD::AND:
17515 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017516 case ISD::XOR:
17517 Commute = true;
17518 // fallthrough
17519 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017520 SDValue N0 = Op.getOperand(0);
17521 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017522 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017523 return false;
17524 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017525 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017526 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017527 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017528 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017529 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017530 }
17531 }
17532
17533 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017534 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017535}
17536
Evan Cheng60c07e12006-07-05 22:17:51 +000017537//===----------------------------------------------------------------------===//
17538// X86 Inline Assembly Support
17539//===----------------------------------------------------------------------===//
17540
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017541namespace {
17542 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017543 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017544 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017545
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017546 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017547 StringRef piece(*args[i]);
17548 if (!s.startswith(piece)) // Check if the piece matches.
17549 return false;
17550
17551 s = s.substr(piece.size());
17552 StringRef::size_type pos = s.find_first_not_of(" \t");
17553 if (pos == 0) // We matched a prefix.
17554 return false;
17555
17556 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017557 }
17558
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017559 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017560 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017561 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017562}
17563
Chris Lattnerb8105652009-07-20 17:51:36 +000017564bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17565 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017566
17567 std::string AsmStr = IA->getAsmString();
17568
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017569 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17570 if (!Ty || Ty->getBitWidth() % 16 != 0)
17571 return false;
17572
Chris Lattnerb8105652009-07-20 17:51:36 +000017573 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017574 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017575 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017576
17577 switch (AsmPieces.size()) {
17578 default: return false;
17579 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017580 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017581 // we will turn this bswap into something that will be lowered to logical
17582 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17583 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017584 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017585 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17586 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17587 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17588 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17589 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17590 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017591 // No need to check constraints, nothing other than the equivalent of
17592 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017593 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017594 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017595
Chris Lattnerb8105652009-07-20 17:51:36 +000017596 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017597 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017598 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017599 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17600 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017601 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017602 const std::string &ConstraintsStr = IA->getConstraintString();
17603 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017604 std::sort(AsmPieces.begin(), AsmPieces.end());
17605 if (AsmPieces.size() == 4 &&
17606 AsmPieces[0] == "~{cc}" &&
17607 AsmPieces[1] == "~{dirflag}" &&
17608 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017609 AsmPieces[3] == "~{fpsr}")
17610 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017611 }
17612 break;
17613 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017614 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017615 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017616 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17617 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17618 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017619 AsmPieces.clear();
17620 const std::string &ConstraintsStr = IA->getConstraintString();
17621 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17622 std::sort(AsmPieces.begin(), AsmPieces.end());
17623 if (AsmPieces.size() == 4 &&
17624 AsmPieces[0] == "~{cc}" &&
17625 AsmPieces[1] == "~{dirflag}" &&
17626 AsmPieces[2] == "~{flags}" &&
17627 AsmPieces[3] == "~{fpsr}")
17628 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017629 }
Evan Cheng55d42002011-01-08 01:24:27 +000017630
17631 if (CI->getType()->isIntegerTy(64)) {
17632 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17633 if (Constraints.size() >= 2 &&
17634 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17635 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17636 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017637 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17638 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17639 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017640 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017641 }
17642 }
17643 break;
17644 }
17645 return false;
17646}
17647
Chris Lattnerf4dff842006-07-11 02:54:03 +000017648/// getConstraintType - Given a constraint letter, return the type of
17649/// constraint it is for this target.
17650X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017651X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17652 if (Constraint.size() == 1) {
17653 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017654 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017655 case 'q':
17656 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017657 case 'f':
17658 case 't':
17659 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017660 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017661 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017662 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017663 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017664 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017665 case 'a':
17666 case 'b':
17667 case 'c':
17668 case 'd':
17669 case 'S':
17670 case 'D':
17671 case 'A':
17672 return C_Register;
17673 case 'I':
17674 case 'J':
17675 case 'K':
17676 case 'L':
17677 case 'M':
17678 case 'N':
17679 case 'G':
17680 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017681 case 'e':
17682 case 'Z':
17683 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017684 default:
17685 break;
17686 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017687 }
Chris Lattner4234f572007-03-25 02:14:49 +000017688 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017689}
17690
John Thompson44ab89e2010-10-29 17:29:13 +000017691/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017692/// This object must already have been set up with the operand type
17693/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017694TargetLowering::ConstraintWeight
17695 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017696 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017697 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017698 Value *CallOperandVal = info.CallOperandVal;
17699 // If we don't have a value, we can't do a match,
17700 // but allow it at the lowest weight.
17701 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017702 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017703 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017704 // Look at the constraint type.
17705 switch (*constraint) {
17706 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017707 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17708 case 'R':
17709 case 'q':
17710 case 'Q':
17711 case 'a':
17712 case 'b':
17713 case 'c':
17714 case 'd':
17715 case 'S':
17716 case 'D':
17717 case 'A':
17718 if (CallOperandVal->getType()->isIntegerTy())
17719 weight = CW_SpecificReg;
17720 break;
17721 case 'f':
17722 case 't':
17723 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017724 if (type->isFloatingPointTy())
17725 weight = CW_SpecificReg;
17726 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017727 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017728 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17729 weight = CW_SpecificReg;
17730 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017731 case 'x':
17732 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017733 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017734 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017735 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017736 break;
17737 case 'I':
17738 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17739 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017740 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017741 }
17742 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017743 case 'J':
17744 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17745 if (C->getZExtValue() <= 63)
17746 weight = CW_Constant;
17747 }
17748 break;
17749 case 'K':
17750 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17751 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17752 weight = CW_Constant;
17753 }
17754 break;
17755 case 'L':
17756 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17757 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17758 weight = CW_Constant;
17759 }
17760 break;
17761 case 'M':
17762 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17763 if (C->getZExtValue() <= 3)
17764 weight = CW_Constant;
17765 }
17766 break;
17767 case 'N':
17768 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17769 if (C->getZExtValue() <= 0xff)
17770 weight = CW_Constant;
17771 }
17772 break;
17773 case 'G':
17774 case 'C':
17775 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17776 weight = CW_Constant;
17777 }
17778 break;
17779 case 'e':
17780 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17781 if ((C->getSExtValue() >= -0x80000000LL) &&
17782 (C->getSExtValue() <= 0x7fffffffLL))
17783 weight = CW_Constant;
17784 }
17785 break;
17786 case 'Z':
17787 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17788 if (C->getZExtValue() <= 0xffffffff)
17789 weight = CW_Constant;
17790 }
17791 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017792 }
17793 return weight;
17794}
17795
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017796/// LowerXConstraint - try to replace an X constraint, which matches anything,
17797/// with another that has more specific requirements based on the type of the
17798/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017799const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017800LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017801 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17802 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017803 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017804 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017805 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017806 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017807 return "x";
17808 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017809
Chris Lattner5e764232008-04-26 23:02:14 +000017810 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017811}
17812
Chris Lattner48884cd2007-08-25 00:47:38 +000017813/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17814/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017815void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017816 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017817 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017818 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017819 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017820
Eric Christopher100c8332011-06-02 23:16:42 +000017821 // Only support length 1 constraints for now.
17822 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017823
Eric Christopher100c8332011-06-02 23:16:42 +000017824 char ConstraintLetter = Constraint[0];
17825 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017826 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017827 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017828 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017829 if (C->getZExtValue() <= 31) {
17830 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017831 break;
17832 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017833 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017834 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017835 case 'J':
17836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017837 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017838 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17839 break;
17840 }
17841 }
17842 return;
17843 case 'K':
17844 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017845 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017846 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17847 break;
17848 }
17849 }
17850 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017851 case 'N':
17852 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017853 if (C->getZExtValue() <= 255) {
17854 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017855 break;
17856 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017857 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017858 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017859 case 'e': {
17860 // 32-bit signed value
17861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017862 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17863 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017864 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017865 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017866 break;
17867 }
17868 // FIXME gcc accepts some relocatable values here too, but only in certain
17869 // memory models; it's complicated.
17870 }
17871 return;
17872 }
17873 case 'Z': {
17874 // 32-bit unsigned value
17875 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017876 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17877 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017878 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17879 break;
17880 }
17881 }
17882 // FIXME gcc accepts some relocatable values here too, but only in certain
17883 // memory models; it's complicated.
17884 return;
17885 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017886 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017887 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017888 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017889 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017890 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017891 break;
17892 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017893
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017894 // In any sort of PIC mode addresses need to be computed at runtime by
17895 // adding in a register or some sort of table lookup. These can't
17896 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017897 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017898 return;
17899
Chris Lattnerdc43a882007-05-03 16:52:29 +000017900 // If we are in non-pic codegen mode, we allow the address of a global (with
17901 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017902 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017903 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017904
Chris Lattner49921962009-05-08 18:23:14 +000017905 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17906 while (1) {
17907 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17908 Offset += GA->getOffset();
17909 break;
17910 } else if (Op.getOpcode() == ISD::ADD) {
17911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17912 Offset += C->getZExtValue();
17913 Op = Op.getOperand(0);
17914 continue;
17915 }
17916 } else if (Op.getOpcode() == ISD::SUB) {
17917 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17918 Offset += -C->getZExtValue();
17919 Op = Op.getOperand(0);
17920 continue;
17921 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017922 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017923
Chris Lattner49921962009-05-08 18:23:14 +000017924 // Otherwise, this isn't something we can handle, reject it.
17925 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017926 }
Eric Christopherfd179292009-08-27 18:07:15 +000017927
Dan Gohman46510a72010-04-15 01:51:59 +000017928 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017929 // If we require an extra load to get this address, as in PIC mode, we
17930 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017931 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17932 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017933 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017934
Devang Patel0d881da2010-07-06 22:08:15 +000017935 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17936 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017937 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017938 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017939 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017940
Gabor Greifba36cb52008-08-28 21:40:38 +000017941 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017942 Ops.push_back(Result);
17943 return;
17944 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017945 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017946}
17947
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017948std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017949X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017950 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017951 // First, see if this is a constraint that directly corresponds to an LLVM
17952 // register class.
17953 if (Constraint.size() == 1) {
17954 // GCC Constraint Letters
17955 switch (Constraint[0]) {
17956 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017957 // TODO: Slight differences here in allocation order and leaving
17958 // RIP in the class. Do they matter any more here than they do
17959 // in the normal allocation?
17960 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17961 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017962 if (VT == MVT::i32 || VT == MVT::f32)
17963 return std::make_pair(0U, &X86::GR32RegClass);
17964 if (VT == MVT::i16)
17965 return std::make_pair(0U, &X86::GR16RegClass);
17966 if (VT == MVT::i8 || VT == MVT::i1)
17967 return std::make_pair(0U, &X86::GR8RegClass);
17968 if (VT == MVT::i64 || VT == MVT::f64)
17969 return std::make_pair(0U, &X86::GR64RegClass);
17970 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017971 }
17972 // 32-bit fallthrough
17973 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017974 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017975 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17976 if (VT == MVT::i16)
17977 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17978 if (VT == MVT::i8 || VT == MVT::i1)
17979 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17980 if (VT == MVT::i64)
17981 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017982 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017983 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017984 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017985 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017986 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017987 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017988 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017989 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017990 return std::make_pair(0U, &X86::GR32RegClass);
17991 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017992 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017993 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017994 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017995 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017996 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017997 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017998 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17999 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018000 case 'f': // FP Stack registers.
18001 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18002 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018003 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018004 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018005 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018006 return std::make_pair(0U, &X86::RFP64RegClass);
18007 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018008 case 'y': // MMX_REGS if MMX allowed.
18009 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018010 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018011 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018012 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018013 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018014 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018015 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018016
Owen Anderson825b72b2009-08-11 20:47:22 +000018017 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018018 default: break;
18019 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018020 case MVT::f32:
18021 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018022 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018023 case MVT::f64:
18024 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018025 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018026 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018027 case MVT::v16i8:
18028 case MVT::v8i16:
18029 case MVT::v4i32:
18030 case MVT::v2i64:
18031 case MVT::v4f32:
18032 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018033 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018034 // AVX types.
18035 case MVT::v32i8:
18036 case MVT::v16i16:
18037 case MVT::v8i32:
18038 case MVT::v4i64:
18039 case MVT::v8f32:
18040 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018041 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018042 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018043 break;
18044 }
18045 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018046
Chris Lattnerf76d1802006-07-31 23:26:50 +000018047 // Use the default implementation in TargetLowering to convert the register
18048 // constraint into a member of a register class.
18049 std::pair<unsigned, const TargetRegisterClass*> Res;
18050 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018051
18052 // Not found as a standard register?
18053 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018054 // Map st(0) -> st(7) -> ST0
18055 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18056 tolower(Constraint[1]) == 's' &&
18057 tolower(Constraint[2]) == 't' &&
18058 Constraint[3] == '(' &&
18059 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18060 Constraint[5] == ')' &&
18061 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018062
Chris Lattner56d77c72009-09-13 22:41:48 +000018063 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018064 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018065 return Res;
18066 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018067
Chris Lattner56d77c72009-09-13 22:41:48 +000018068 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018069 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018070 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018071 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018072 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018073 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018074
18075 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018076 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018077 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018078 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018079 return Res;
18080 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018081
Dale Johannesen330169f2008-11-13 21:52:36 +000018082 // 'A' means EAX + EDX.
18083 if (Constraint == "A") {
18084 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018085 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018086 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018087 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018088 return Res;
18089 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018090
Chris Lattnerf76d1802006-07-31 23:26:50 +000018091 // Otherwise, check to see if this is a register class of the wrong value
18092 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18093 // turn into {ax},{dx}.
18094 if (Res.second->hasType(VT))
18095 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018096
Chris Lattnerf76d1802006-07-31 23:26:50 +000018097 // All of the single-register GCC register classes map their values onto
18098 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18099 // really want an 8-bit or 32-bit register, map to the appropriate register
18100 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018101 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000018102 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018103 unsigned DestReg = 0;
18104 switch (Res.first) {
18105 default: break;
18106 case X86::AX: DestReg = X86::AL; break;
18107 case X86::DX: DestReg = X86::DL; break;
18108 case X86::CX: DestReg = X86::CL; break;
18109 case X86::BX: DestReg = X86::BL; break;
18110 }
18111 if (DestReg) {
18112 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018113 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018114 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018115 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018116 unsigned DestReg = 0;
18117 switch (Res.first) {
18118 default: break;
18119 case X86::AX: DestReg = X86::EAX; break;
18120 case X86::DX: DestReg = X86::EDX; break;
18121 case X86::CX: DestReg = X86::ECX; break;
18122 case X86::BX: DestReg = X86::EBX; break;
18123 case X86::SI: DestReg = X86::ESI; break;
18124 case X86::DI: DestReg = X86::EDI; break;
18125 case X86::BP: DestReg = X86::EBP; break;
18126 case X86::SP: DestReg = X86::ESP; break;
18127 }
18128 if (DestReg) {
18129 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018130 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018131 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018132 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018133 unsigned DestReg = 0;
18134 switch (Res.first) {
18135 default: break;
18136 case X86::AX: DestReg = X86::RAX; break;
18137 case X86::DX: DestReg = X86::RDX; break;
18138 case X86::CX: DestReg = X86::RCX; break;
18139 case X86::BX: DestReg = X86::RBX; break;
18140 case X86::SI: DestReg = X86::RSI; break;
18141 case X86::DI: DestReg = X86::RDI; break;
18142 case X86::BP: DestReg = X86::RBP; break;
18143 case X86::SP: DestReg = X86::RSP; break;
18144 }
18145 if (DestReg) {
18146 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018147 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018148 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018149 }
Craig Topperc9099502012-04-20 06:31:50 +000018150 } else if (Res.second == &X86::FR32RegClass ||
18151 Res.second == &X86::FR64RegClass ||
18152 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018153 // Handle references to XMM physical registers that got mapped into the
18154 // wrong class. This can happen with constraints like {xmm0} where the
18155 // target independent register mapper will just pick the first match it can
18156 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018157
18158 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018159 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018160 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018161 Res.second = &X86::FR64RegClass;
18162 else if (X86::VR128RegClass.hasType(VT))
18163 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018164 else if (X86::VR256RegClass.hasType(VT))
18165 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018166 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018167
Chris Lattnerf76d1802006-07-31 23:26:50 +000018168 return Res;
18169}